NuclearAlchemist
NuclearAlchemist

Reputation: 361

Mixing Objective-C and boost/C++

I finally have a very basic version of my graph searcher working through a CLI, but I find myself in a position where I need to refactor all of the code. Since I am doing this programming on a mac, I was thinking of using Cocoa for the GUI, and then having my C++ functions underneath as workhorses. This would just be a temporary fix, as eventually I want to be able to run all of my code on a linux box/cluster. Here is my last question about the implementation, and I did get this working in boost, rolling my own graph functions (for now).

So here was my thought: Build the C++ classes for the graph itself, and either separate functions or methods for the quantities I want to compute. This is the latest description that I can find that deals with mixing Objective-C and C++. But I was wondering:

Will compiling my C++ code in Xcode be optimized? The whole point is that I want the C++ code to be fully optimized for fast memory access, multiple threads, and have access to the boost libraries. Then, I can encapsulate the C++ class in a wrapping ObjectiveC class. To do this, I would essentially have 2 classes, correct? The C++ class with a .h and .cpp file and included boost libraries, and then the ObjC class wrapper with a .h and .mm file, where the .h does not contain any reference to the C++ class, I use the include in the .mm file. Then I can use my normal MVC design and GUI implementation for Objective C. As long as I give the dealloc command the ability to nuke the C++ class, I don't have to worry about memory management? Does ARC work well with C++ as well, and does it give OSX the ability to memory manage and clean up my C++ code?

I suspect that if I want threading, I would go with the boost threading, since coding for the GCD would make it platform specific (although for now, this is all it will run on). I think that for now, I will stay away from the Core Data, since I create my graph by simply parsing a text file.

Upvotes: 3

Views: 2219

Answers (3)

Jasper Blues
Jasper Blues

Reputation: 28746

I agree with everything H2CO3 said (except a nit-pick about reference counting).

Also wanted to note that Daniel Sefton created a nice article on how to compile Boost for OSX and iOS:

Also, yes, -dealloc is a good place to delete C++ objects.

Threading:

  • If you only wish to target OSX and iOS, C++11 has native thread support, this is not yet supported by Android.

Other options for threading are:

  • Posix threads (aka pthreads)
  • Boost threads
  • Poco threads

I personally use Poco.

Upvotes: 1

justin
justin

Reputation: 104698

Will compiling my C++ code in Xcode be optimized?

Yes.

To do this, I would essentially have 2 classes, correct? The C++ class with a .h and .cpp file and included boost libraries, and then the ObjC class wrapper with a .h and .mm file, where the .h does not contain any reference to the C++ class, I use the include in the .mm file.

Correct.

As long as I give the dealloc command the ability to nuke the C++ class, I don't have to worry about memory management?

Yes, your C++ objects' default constructor and destructor will be called at appropriate points in the ObjC object's lifetime when you declare a C++ ivar.

Like normal C++, you just use values or smart pointers rather than raw pointers for your C++ ivars and do little additional intervention with the object's lifetime. The compiler will not apply ObjC reference counting to your C++ types if ARC is enbled (after all, C++ objects are not strictly reference counted).

Does ARC work well with C++ as well, and does it give OSX the ability to memory manage and clean up my C++ code?

ARC only applies to ObjC types. It does nothing for C++ or even C types/allocations. Just use smart pointers or some appropriate C++ substitute (i.e. std::vector) for your C++ and C allocations/types. If you find yourself needing to write delete, delete[], or free often in C++, you are not using smart pointers correctly -- they are almost as simple as ARC, just offer more flexibility.

You may also want to look into CLANG_WARN_OBJCPP_ARC_ABI.

ARC is also different in that when a program is compiled as ObjC++, local strong ObjC references will be -released during unwinding from a thrown exception (not the case when compiled as ObjC+ARC).

Upvotes: 1

user529758
user529758

Reputation:

Will compiling my C++ code in Xcode be optimized?

It's not Xcode that compiles your code, it's the compiler (GCC or clang), and it will optimize your code if you request it to do so using the appropriate compiler flags. I don't see how mixing C++ and Objective-C would make the compiler not optimize...

As long as I give the dealloc command the ability to nuke the C++ class, I don't have to worry about memory management?

Specifically? You still do have to manage memory for both C++ and Objective-C objects as normally.

Does ARC work well with C++ as well?

No, it doesn't, it's an Objective-C feature. C++ doesn't even have reference counting (I wish it had...).

Upvotes: 5

Related Questions