views:

49

answers:

2
+3  A: 

Objective-C methods are C functions, but they have two hidden parameters at the front, so they won't have the int f(void *) signature.

What you probably want to do is use an libffi closure. That allows you to create a function with exactly the signature that you want, but that also has a pointer to your object passed along with it. See the example in the ffi_prep_closure man page. Your handleEvent function would probably then change to look something like this:

static void handleEventClosure(ffi_cif * cif, void * result, void ** args, void * userdata)
{
    // Arguments.
    void * Event = *args[0];

    // Closed-over data.
    id delegate = (id)userdata;

    // Execute the method.
    [delegate didFinishSearching];

    // Smaller than sizeof(long), so use ffi_arg or ffi_sarg (unsigned or signed).
    *(ffi_sarg *)result = (ffi_sarg)0;
}
John Calsbeek
+2  A: 

Most of the time C Libraries like the one you describe accept a "userinfo" parameter conveniently size to match a pointer. You can use this to your advantage by passing your object as this "userinfo" parameter.

Then in callbacks, you cast the pointer back to an object and make the calls you need.

tlindner