Question

Disclaimer I'm a PHP programmer with experience in Java, and have dabbled with Obj-C/C++/C.

I'm trying to use the IB SDK inside an Obj-C application. I've got the hang of the compilation with .mm vs. .cpp vs .m files.

The IB API has a Send and Recieve pattern where a class EClientSocketBase sends data to the API and a second class EWrapper receives data from from the API in a separate thread.

I have an obj-c wrapper around a class, CppSend, inherited from EClientSocketBase class, ObjCSend to send data to the api. Call c++ classes from obj-c doesn't seem to be a problem. But, I run into a problem when I try to do the reverse. I don't know how to either inject a class, say ObjCRecieve, to handle call backs in EWrapper.

My objc sender, looks like this:

// This class sends messages to CppSend
@interface ObjCSend
@property (atomic) CppSend* client;
...
@end

and my cpp sender looks like this:

#import "CppReceive.h"
// This class sends messages to the IB Api.
class CppSend: public EClientSocketBase {
    public:
        CppReceive * incomingFromAPI;
        ...
}

and my cpp receiver looks like this:

#import "ObjCReceive.h"
// This class receives messages from the IB API, containing
// data relevant to the messages sent in CppSend
class CppReceive: public EWrapper {
    public:
        ObjCRecieve *outgoingToGUI;
        void setObjCReceive(ObjReceive * delegate);
        ...
}

Here's what I've got for my Obj-C receiver:

// This class should receive messages from CppReceive, containing
// data relevant to the messages sent in CppSend
@interface ObjCReceive
- (void)callbackHandler;
@end

My problem is in CppReceive->delegate. I get a compilation error, "Unknown type name 'ObjCReceive'". I assume this is because the c++ header can't appropriately reference the obj-c interface as a type.

How would I create an Obj-C class, which can have an appropriate type to be parsed and compiled in a C++ class?

Edit: The data flow would be something like this

------------
| ObjCSend |
------------
     |
     V
-----------
| CppSend |
-----------
     |
     V
  --------
 ( IB API )
  --------
     |
     V
--------------
| CppReceive |
--------------
     |
     V
--------------
| ObjReceive |
--------------
Was it helpful?

Solution

You cannot use an Objective-C object or constructs inside a C/C++ program.

To be able to use Objective-C object, you may use pimpl-idiom (Pointer to implementation).

For example, in your case, there will be a class CppSendImpl and CppSend will have its pointer.

This CppSendImpl can be implemented in Objective-C++ (not Objective-C) where it implements the code requiring Objective-C and can interface with Objective-C object which you want.

Here, you can use Objective-C construct. But if your CppSend is meant for C++ client also, you cannot expose or use any of the Objective-C construct outside implementation class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top