Pass instance method as function pointer to C Library

后端 未结 1 648
余生分开走
余生分开走 2021-02-01 22:09

I am writing an Objective-C application that uses a C Library. The issue which i am currently facing is that the C Library has a structure where some field are function pointers

1条回答
  •  孤街浪徒
    2021-02-01 22:58

    You will need to provide the C callback function within the Objective-C class implementation file, and this will only work if the callback is able to use a context pointer of some sort.

    So imagine the C callback signature is like this:

    void myCallback(void *context, int someOtherInfo);
    

    Then within the Objective-C class implementation file you need to use that callback to trampoline back into your Objective-C class (using the context pointer as the instance of the class to invoke):

    // Forward declaration of C callback function
    static void theCallbackFunction(void *context, int someOtherInfo);
    
    // Private Methods
    @interface MyClass ()
    - (void)_callbackWithInfo:(int)someOtherInfo;
    @end
    
    @implementation MyClass
    
    - (void)methodToSetupCallback
    {
        // Call function to set the callback function, passing it a "context"
        setCallbackFunction(theCallbackFunction, self);
        ...
    }
    
    - (void)_callbackWithInfo:(int)someOtherInfo
    {
        NSLog(@"Some info: %d", someOtherInfo);
    }
    
    @end
    
    static void theCallbackFunction(void *context, int someOtherInfo)
    {
        MyClass *object = (MyClass *)context;
        [object _callbackWithInfo:someOtherInfo];
    }
    

    If your C callback function does not accept some sort of context info, then:

    1. It's broken and this should be fixed/reported as a bug.
    2. You will need to rely on storing a pointer-to-self at global, static, scope to be used by the C callback function. This will limit the number of instances of MyClass to one!

    0 讨论(0)
提交回复
热议问题