Question

I am mixing Objective-C parts into a C++ project (please don't argue about that, its cross-platform).
I now want to invoke some C++ functions or methods on the correct thread (i.e. the main thread) in a cocoa enviroment.

My current approach is passing function pointers to an objective-c instance (derived from NSObject) and do performSelector/performSelectorOnMainThread on it.
But the performSelectors expect objects as their arguments, so how would one usually wrap this?

Example:

 typedef void(*FnPtr)(void*);
 FnPtr fn;
 [someInstance performSelector:@selector(test:) withObject:fn];

... where test is declared as:

- (void)test:(FnPtr)fn;

Have to add that i only started with objective-c this week, so if there is a better way i'd be also glad to hear about it.
Also note that i don't have any access to the main loop or any application objects because the project is an browser plug-in (currently only targetting Safari on the mac).

Was it helpful?

Solution

As answered by smorgan here, NSValue is designed as a container for scalar C & Objective-C types:

- (void)test:(NSValue*)nv
{
    FnPtr fn = [nv pointerValue];
    // ...
}

// usage:
NSValue* nv = [NSValue valueWithPointer:fn];
[someInstance performSelector:@selector(test:) withObject:nv];

OTHER TIPS

I am not sure if my solution is considered sane / correct -- I do however frequently pass pointers by simply typecasting them to (id)s. That is dirty but does work for me. The possibly cleaner way would be using NSValue.

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