Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event handling between C++ and Objective C

I recently started working on an iOS game and decided to write a distinct part of the project in C++. This approach seems to work fine as long as the Objective C classes simply access some members or call functions on the C++ objects. However i can't seem to find an elegant way to make my Objective C classes respond to 'events' in the C++ classes. Any event handling system that uses callbacks seems out of the question (since Objective C methods and C++ functions and probably not interchangeable). All i can think of is using the delegate pattern and writing wrapper classes around my C++ delegate classes so i can use them in Objective C code. So my question is: Is there a better way of doing this?

NB: I would like to prevent using Objective C directly in my C++ files, since these classes are supposed to be platform independent.

like image 421
Double Dan Avatar asked Jan 15 '13 00:01

Double Dan


People also ask

How different is Objective-C from C?

The main difference in C and Objective C is that C is a procedure programming language which doesn't support the concepts of objects and classes and Objective C is Object-oriented language which contains the concept of both procedural and object-oriented programming languages.

Is Objective-C as fast as C?

Objective-C uses the runtime code compilation Generally, this happens very fast but when the code compilation happens a significant number of times, it becomes measurable. Objective-C is a superset of C and all C functions that you will write in Objective-C will be just as fast.

Is Objective-C derived from C?

Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.

What is Objective-C based on?

The Objective-C model of object-oriented programming is based on message passing to object instances. In Objective-C one does not call a method; one sends a message. This is unlike the Simula-style programming model used by C++.


2 Answers

You may want to consider the mechanism that CoreVideo uses.

In their model, they have a mechanism which involves registering a C callback function( http://tinyurl.com/axtxajf ), and one of the parameters to this function is a void*, which can be typecast to the Objective-C class you need access to.

Here is an example of a C function you may implement in your C++ modules to register the callback function. The parameter are the callback function and the pointer to the class instance:

SetEventCallback(EventCallbackFunction, self);

When the event needs to be handled, the callback function is called, and you can typecast the void* to call Obj-C class and invoke the method:

void EventCallbackFunction(void* objCPtr)
{
    [(MyObjCClass*)objCPtr someMethod];
}
like image 59
AhiyaHiya Avatar answered Sep 30 '22 05:09

AhiyaHiya


You could use C++0x lambda functions, they are interchangeable (assignable to each other) with Objective-C blocks.

like image 34
Jonathan Cichon Avatar answered Sep 30 '22 05:09

Jonathan Cichon