Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Pointers in Objective C

Quick question. Does anyone know how to get the function pointer of an objective c method? I can declare a C++ method as a function pointer, but this is a callback method so that C++ method would need to be part of the class SO THAT IT CAN ACCESS THE INSTANCE FIELDS. I don't know how to make a C++ method part of an objective c class. Any suggestions?

like image 349
Sam Stewart Avatar asked Nov 22 '09 01:11

Sam Stewart


People also ask

How do you pass a pointer to a function in Objective-C?

Objective-C programming language allows you to pass a pointer to a function. To do so, simply declare the function parameter as a pointer type. Following a simple example where we pass an unsigned long pointer to a function and change the value inside the function which reflects back in the calling function −.

What is the use of pointers in C?

Pointers give greatly possibilities to ‘C’ functions which we are limited to return one value. With pointer parameters, our functions now can process actual data rather than a copy of data. In order to modify the actual values of variables, the calling statement passes addresses to pointer parameters in a function.

How to declare a pointer variable in Objective-C?

Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The general form of a pointer variable declaration is −. type *var-name; Here, type is the pointer's base type; it must be a valid Objective-C data type and var-name is the name of the pointer variable.

How do you call a pointer function with 3 arguments?

Rather than the standard function calling by taping the function name with arguments, we call only the pointer function by passing the number 3 as arguments, and that's it! Keep in mind that the function name points to the beginning address of the executable code like an array name which points to its first element.


1 Answers

Typically, you need two pieces of information to call back into Objective-C; the method to be invoked and the object to invoke it upon. Neither just a selector or just the IMP -- the instanceMethodForSelector: result -- will be enough information.

Most callback APIs provide a context pointer that is treated as an opaque value that is passed through to the callback. This is the key to your conundrum.

I.e. if you have a callback function that is declared as:

typedef void (*CallBackFuncType)(int something, char *else, void *context);

And some API that consumes a pointer of said callback function type:

void APIThatWillCallBack(int f1, int f2, CallBackFuncType callback, void *context);

Then you would implement your callback something like this:

void MyCallbackDude(int a, char *b, void *context) {
    [((MyCallbackObjectClass*)context) myMethodThatTakesSomething: a else: b];
}

And then you would call the API something akin to this:

MyCallbackObjectClass *callbackContext = [MyCallbackObjectClass new];
APIThatWillCallBack(17, 42, MyCallbackDude, (void*)callbackContext);

If you need to switch between different selectors, I would recommend creating a little glue class that sits between the callback and the Objective-C API. The instance of the glue class could contain the configuration necessary or logic necessary to switch between selectors based on the incoming callback data.

like image 170
bbum Avatar answered Oct 12 '22 05:10

bbum