Wrapping Objective C in Objective c++/c++ - c++

I've got a c++ app written using Boost/WXWidgets, targeting Windows and Mac OSX. However, I've got one issue that I can't solve using these libraries. My solution requires me to wrap an Objective C class so that I can call it from one of my c++ modules. My research so far tells me that I need to use Objective C++ written into source files with a .mm extension, allowing XCode to treat the file as a mix of Objective C and c++. I've found lots of articles detailing how to wrap c++ so that it can be called from ObjectiveC, but nothing that gives any detail on the reverse. Any links to articles or, better still, a worked example, would be greatly appreciated.

If you want a reusable pure C++ wrapper around an Objective C class, the Pimpl idiom works pretty well. The Pimpl idiom will make it so that there is no Objective C / Cocoa stuff visible in the header file that will be included by pure C++ code.
// FooWrapper.hpp
// Absolutely no Cocoa includes or types here!
class FooWrapper
{
public:
int bar();
private:
struct Impl; // Forward declaration
Impl* impl;
};
// FooWrapper.mm
#import "FooWraper.hpp"
#import "Foundation/NSFoo.h"
struct FooWrapper::Impl
{
NSFoo* nsFoo;
};
FooWrapper::FooWrapper() : impl(new Impl)
{
impl->nsFoo = [[NSFoo alloc] init];
}
FooWrapper::~FooWrapper()
{
[impl->nsFoo release];
delete impl;
}
int FooWrapper::bar()
{
return [impl->nsFoo getInteger];
}

Just mix it (but don't forget setting up the pool). It works.
// obj-c code, in .mm file
void functionContainingObjC(){
NSAutoreleasePool*pool=[[NSAutoreleasePool alloc] init];
[lots of brackets!];
[pool release];
}
// c++ code, in .cc file
functionContainingObjC();

objc provides c interfaces for the basic interfaces and types (#include <objc/headers_you_need.h>. therefore, you can use these interfaces in pure c/c++ TUs. then include libs like Foundation or AppKit in the mm and use objc types and messaging in the implementation.
the following is a very basic interface which is not typesafe, but i encourage you to make it typesafe for the objc type you are wrapping. this should be enough to get you started in the right direction.
// .hpp
namespace MON {
// could be an auto pointer or a dumb pointer; depending on your needs
class t_MONSubclassWrapper {
public:
// usual stuff here...
// example wrapper usage:
void release();
private:
id d_objcInstance;
};
} /* << MON */
// .mm
...
#include <Foundation/Foundation.h>
MON::t_MONSubclassWrapper::t_MONSubclassWrapper() :
d_objcInstance([[MONSubclass alloc] init]) {
}
...
void MON::t_MONSubclassWrapper::release() {
[d_objcInstance release];
}

Related

I need help mixing C++ code and Objective-C code

I am writing a device driver for a Blackmagic Design AV device in XCode, and I'm having trouble including BMD's SyncController class from their abbreviated sample code (below) into my purely Objective-C project.
Their DecklinkAPI.h file is rich in C++ code, so when I try include this header file as-is in a an Objective-C class, the compiler chokes deep in the API include: Unknown type name 'class'; did you mean 'Class'?
I have tried to to bundle up the C++ bits into a Obj-C class extension as noted here, but without much success. I've never done any C++ programming (and have never used Obj-C class extensions), so this is new territory for me.
I'm not sure if I need to create an additional wrapper class for my SyncController object, or whether I can just do a class extension on this one and shuffle the C++ bits into the .mm file.
I would like to be able to do a #include "SyncController.h" (or its wrapper) in an Objective-C class without having the compiler choke.
Any assistance in doing so would be much appreciated.
First up, here is my current SyncController.h file:
#import <Cocoa/Cocoa.h>
#import "DeckLinkAPI.h" // this is rich in C++ code
class PlaybackDelegate;
#interface SyncController : NSObject {
PlaybackDelegate* playerDelegate;
IDeckLink* deckLink;
IDeckLinkOutput* deckLinkOutput;
}
- (void)scheduleNextFrame:(BOOL)prerolling;
- (void)writeNextAudioSamples;
#end
class PlaybackDelegate : public IDeckLinkVideoOutputCallback, public IDeckLinkAudioOutputCallback
{
SyncController* mController;
IDeckLinkOutput* mDeckLinkOutput;
public:
PlaybackDelegate (SyncController* owner, IDeckLinkOutput* deckLinkOutput);
// IUnknown needs only a dummy implementation
virtual HRESULT QueryInterface (REFIID iid, LPVOID *ppv) {return E_NOINTERFACE;}
virtual ULONG AddRef () {return 1;}
virtual ULONG Release () {return 1;}
virtual HRESULT ScheduledFrameCompleted (IDeckLinkVideoFrame* completedFrame, BMDOutputFrameCompletionResult result);
virtual HRESULT ScheduledPlaybackHasStopped ();
virtual HRESULT RenderAudioSamples (bool preroll);
};
void ScheduleNextVideoFrame (void);
Next up, here is my (simplified) SyncController.mm file:
#import <CoreFoundation/CFString.h>
#import "SyncController.h"
#implementation SyncController
- (instancetype)init
{
self = [super init];
return self;
}
- (void)dealloc
{
}
- (void)scheduleNextFrame:(BOOL)prerolling
{
}
- (void)writeNextAudioSamples
{
}
#end
PlaybackDelegate::PlaybackDelegate (SyncController* owner, IDeckLinkOutput* deckLinkOutput)
{
mController = owner;
mDeckLinkOutput = deckLinkOutput;
}
HRESULT PlaybackDelegate::ScheduledFrameCompleted (IDeckLinkVideoFrame* completedFrame, BMDOutputFrameCompletionResult result)
{
[mController scheduleNextFrame:NO];
return S_OK;
}
HRESULT PlaybackDelegate::ScheduledPlaybackHasStopped ()
{
return S_OK;
}
HRESULT PlaybackDelegate::RenderAudioSamples (bool preroll)
{
[mController writeNextAudioSamples];
if (preroll)
mDeckLinkOutput->StartScheduledPlayback(0, 100, 1.0);
return S_OK;
}
I have tried to to bundle up the C++ bits into a Obj-C class extension as noted here, but without much success.
If you're targeting 64-bit, the class extension method should be fairly simple.
The following is equivalent to the code you've post, but moves all of the C++ declarations to a separate header:
SyncController.h:
#import <Cocoa/Cocoa.h>
#interface SyncController : NSObject
- (void)scheduleNextFrame:(BOOL)prerolling;
- (void)writeNextAudioSamples;
#end
SyncController_CPP.h
#import "SyncController.h"
#include "DeckLinkAPI.h"
class PlaybackDelegate;
#interface SyncController() {
PlaybackDelegate* playerDelegate;
IDeckLink* deckLink;
IDeckLinkOutput* deckLinkOutput;
}
#end
class PlaybackDelegate ...
{
...
}
SyncController.mm
#import "SyncController_CPP.h"
#implementation SyncController
...
#end
PlaybackDelegate::PlaybackDelegate (SyncController* owner, IDeckLinkOutput* deckLinkOutput)
{
mController = owner;
mDeckLinkOutput = deckLinkOutput;
}
// etc..
Any other ObjC classes that need access to SyncController will import "SyncController.h". Any other ObjC++ classes can import either "SyncController.h" or "SyncController_CPP.h"
Not a complete answer, however errors like:
Unknown type name 'class'; did you mean 'Class'?
Is a classic issue with Objective-C++ where an Objective-C implementation file is seeing a C++ header file.
I can only provide advice about how to avoid it as you didn't post the complete build output.
Don't put C++ headers is the pre-compiled header.
Try to only include C++ headers within Objective-C++ implementation files and not in their counterpart header file which might, in turn, be included into an Objective-C file.
Hide the use of C++ from any header files, for example using private instance variables:
#import <vector>
#implementation MyObjCppClass {
std::vector<int> _stuff;
}
- (id)init {
...
}
#end
If you are mixing Objective-C and Objective-C++ then you might find you need to provide Objective-C wrappers to C++ classes (which look from the outside as Objective-C but are actually implemented in Objective-C++).
Rename your .m files (objective-c) to .mm (objective-c++). this should allow you to then mix objc and c++ by including c++ headers and referencing c++ code from your objc.
---EDIT---
Any header file you include from objective-c must contain only objective-c. Remove any c++ from the header in your wrapper class to get the other objc classes to build. In modern objc, you can split your ivars between the .h and .m files; keep all your methods in the .h for other objc classes to use, and declare your c++ ivars in the .mm. Stick your c++ delegate class in its own .h that is only included from the .mm wrapper.
Use #if __cplusplus.
For example,
#import <Cocoa/Cocoa.h>
#if __cplusplus
#import "DeckLinkAPI.h" // this is rich in C++ code
#endif // __cplusplus
#interface SyncController : NSObject {
void* playerDelegate; // should be cast as C++ PlaybackDelegate class.
...
}
#end
#if __cplusplus
class PlaybackDelegate : public IDeckLinkVideoOutputCallback, public IDeckLinkAudioOutputCallback
{
...
};
#endif // __cplusplus
The header file can be used with Objective-C and Objective-C++. But you can not use C++ class signature in SyncController Objective-C class declaration in the header. Use void * instead of PlaybackDelegate * with proper type cast.
Also using void * means that C++ stuff in the header is no longer needed.
#import <Cocoa/Cocoa.h>
#interface SyncController : NSObject {
void* playerDelegate; // should be cast as C++ PlaybackDelegate class.
...
}
#end
In Objective-C++ code,
// initialize
syncController.playerDelegate = new PlaybackDelegate();
// use the pointer
PlaybackDelegate *playbackDelegate = (PlaybackDelegate *)syncController.playerDelegate;
initialize
syncController.playerDelegate = new PlaybackDelegate();
// use the pointer
PlaybackDelegate *playbackDelegate = (PlaybackDelegate *)syncController.playerDelegate

how to write Objective C Wrapper for C++

I have a burning question on writing objective C wrapper for c++. That is an error in my code when i try to build it. I'm not sure what i have do it wrong. Would truly appreciate any help or guide. The following is the sample code that i have written:
///Print.h///
int test1();
///Print.cpp///
int test1()
{
printf ("hello man\n");
}
///cppWrapper.h///
struct Print;
typedef struct Print Print;
#interface cppWrapper : NSObject
{
Print *print;
}
#property (nonatomic, assign) Print *print;
-(id)init;
-(int)runTest;
///cppWrapper.mm///
#import "cppWrapper.h"
#implementation cppWrapper
#synthesize print = _print;
- (id)init
{
self = [super init];
if (self)
{
_print = new Print(); //error occurred here.
}
return self;
}
-(int)runTest
{
self.print->test1();
}
Objective-C wrapper for C++ is not implemented in terms of objective-C code.
To implement it, use pure C functions.
Under pure C functions, implement objective-C code. In C++ code, use C function to call objective-C code.
Objective-C does not understand C++.
Rather use Objective-C++ to use C++ code.
In objective-C++, you can use C++ code. Save your file as .mm rather than .m.
Edit:
The error in your code is because, the compiler is not able to find the definition of the structure. Compiler needs to know what is the size of the structure. Without its definition, it cannot create the object. This is also true even if you create object on stack.

How to include C++ headers in an Objective C++ header?

I have a .cpp/.hpp file combo -> the .hpp file has #include ..
I also have a .mm/.h file combo -> if I include the .hpp file within my .mm Objective C++ file, there are no issues. However, if I try to include the .hpp file within my .h (Objective C header) file, I get a preprocessor issue 'iostream not found'.
Is there any way around this other than doing funky stuff like having a void* in my Objective C .h file and then casting it as the type that is included in the .mm or wrapping every C++ type within an Objective C++ type?
My question is basically the same as Tony's question (but nobody answered his):
https://stackoverflow.com/questions/10163322/how-to-include-c-header-file-in-objective-c-header-file
The problem is that you have to avoid all C++ semantics in the header to allow normal Objective-C classes to include it. This can be accomplished using opaque pointers.
CPPClass.h
class CPPClass
{
public:
int getNumber()
{
return 10;
}
};
ObjCPP.h
//Forward declare struct
struct CPPMembers;
#interface ObjCPP : NSObject
{
//opaque pointer to store cpp members
struct CPPMembers *_cppMembers;
}
#end
ObjCPP.mm
#import "ObjCPP.h"
#import "CPPClass.h"
struct CPPMembers {
CPPClass member1;
};
#implementation ObjCPP
- (id)init
{
self = [super init];
if (self) {
//Allocate storage for members
_cppMembers = new CPPMembers;
//usage
NSLog(#"%d", _cppMembers->member1.getNumber());
}
return self;
}
- (void)dealloc
{
//Free members even if ARC.
delete _cppMembers;
//If not ARC uncomment the following line
//[super dealloc];
}
#end
To use C++ in an Objective-C++ header file make sure to:
Have a .h/.mm couple (Objective-C++)
In the identity of your file, have the type set to Objective-C++ Source (it should be automatic)
Include the considered file only from Objective-C++ source files. This point is obvious but can be very quickly forgotten and hard to track down
The #include directive simply includes text; the only time Objective C will complain is if there is something in the include file that is not valid Objective C.
In your case, it gives you the answer; the iostream.h header file is not found in Objective C. Find where this file is referenced and remove the #include to it.

Iphone, callback and objective c

I am using a c++ library using callback to inform about the progress of an operation.
Everything is working fine except this:
I need to have a function in my controller to be use as a c++ callback function.
I've tried a lot of things but none of them are working.
Do you know how we can manage this kind of thing?
Thanks :)
iPhone APIs like the Audio Queue Services use a void * parameter in their callbacks, into which you can stuff your Objective-C instance.
If your C++ library has a similar setup - your callback gives a void * "context" parameter - you could do this:
void interruptionListener(void *inClientData, UInt32 inInterruptionState) {
InterruptionMonitor *self = (InterruptionMonitor *)inClientData;
[self inInterruption: inInterruptionState == kAudioSessionBeginInterruption];
}
So you use the inClientData to store your instance, and can then call methods on that instance that do the actual processing.
You have to define a c++-class in your .h with your callback methods, implementing the c++-interface. This class also keeps a delegate of your objC Class.
in your .m File after #end you specify the c++ methods. You may then use the delegate to perform selectors of your objC class
in .h
#interface YourObjcClass {
#ifdef __cplusplus
class FooObserver : public YourNS::Interface {
public:
virtual ~FooObserver() {
}
YourObjcClass *delegate;
};
YourNS::YourCallbackClass *myCallbackClass;
#endif
in .m
#ifdef __cplusplus
void FooObserver::callback( args ) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[delegate performSelectorOnMainThread:#selector(performCallback)
withObject:nil
waitUntilDone:false];
[pool release];
}
#endif
There is a much better way of doing this. If you are already using boost in your iOS project you can use my objc_callback template: http://alex.tapmania.org/2012/04/c-to-obj-c-callbacks-part-2.html

Can I separate C++ main function and classes from Objective-C and/or C routines at compile and link?

I have a small C++ application which I imported Objective-C classes. It works as Objective-C++ files, .mm, but any C++ file that includes a header which may end up including some Objective-C header must be renamed to a .mm extension for the proper GCC drivers.
Is there a way to write either a purely C++ wrapper for Objective-C classes or can I separate the Objective-C objects out somehow and just link them separately? Maybe even if the Objective-C classes became a small library I could statically re-link at compile time?
The problem is that this code is cross-platform, and it is more difficult to compile on systems that normally do not use Objective-C (i.e. not Macs). Even though preprocessor commands restrict any implementation of Objective-C code on Windows or Linux, the original code still has .mm extensions and GCC still treats the code as Objective-C++.
Usually you simply wrap your Objective-C classes with C++ classes by e.g. using opaque pointers and forwarding calls to C++ methods to Objective-C methods.
That way your portable C++ sources never have to see any Objective-C includes and ideally you only have to swap out the implementation files for the wrappers on different platforms.
Example:
// c++ header:
class Wrapper {
struct Opaque;
Opaque* opaque;
// ...
public:
void f();
};
// Objective-C++ source on Mac:
struct Wrapper::Opaque {
id contained;
// ...
};
void Wrapper::f() {
[opaque->contained f];
}
// ...
Yes, that's easily possible, both ways, if you know a few tricks:
1) The "id" type is actually defined in a plain C header. So you can do the following:
In your header:
#include <objc/objc.h>
class MyWindow
{
public:
MyWindow();
~MyWindow();
protected:
id mCocoaWindow;
};
In your implementation (.mm):
#include "MyWindow.h"
#include <Cocoa/Cocoa.h>
MyWindow::MyWindow()
{
mCocoaWindow = [[NSWindow alloc] init];
}
MyWindow::~MyWindow()
{
[mCocoaWindow release];
mCocoaWindow = nil;
}
2) There are two preprocessor constants you can use to exclude C++/ObjC-specific code when a source file includes them that is one of the two, but not ObjC++:
#if __OBJC__
// ObjC code goes here.
#endif /* __OBJC__*/
#if __cplusplus
// C++ code goes here.
#endif
Just be careful, you can't just add/remove ivars or virtual methods with an #ifdef, that will create two classes with different memory layouts and make your app crash in very weird ways.
3) You can use a pointer to a struct without declaring its contents:
In your header:
#interface MyCppObjectWrapper : NSObject
{
struct MyCppObjectWrapperIVars *ivars; // This is straight ObjC, no ++.
}
#end
In your implementation file (.mm):
struct MyCppObjectWrapperIVars
{
std::string myCppString1;
std::string myCppString2;
std::string myCppString3;
};
#implementation MyCppObjectWrapper
-(id) init
{
if(( self = [super init] ))
{
ivars = new MyCppObjectWrapperIVars;
}
return self;
}
-(void) dealloc
{
delete ivars;
ivars = NULL;
[super dealloc];
}
#end
This will make your header simple standard C or ObjC, while your implementation file gets constructors/destructors of all ivars called without you having to create/delete each one as an object on the heap.
This is all only the Mac side of things, but this would mean that you could keep the ObjC stuff out of your headers, or at least make it compile when used from cross-platform client files of the Mac implementation of your C++ portability layer.