Cython - implementing callbacks

后端 未结 2 1586
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-15 02:22

I have been working with Cython in an attempt to interface with a library written in c++. So far things are going pretty good, and I can effectively use MOST functions within th

2条回答
  •  忘了有多久
    2021-02-15 02:39

    If you can modify the library to define:

    typedef void (*Function1)(const uint16_t *data, 
                              unsigned width, unsigned height,
                              void *user_data);
    void SetCallBack(Function1, void*);
    

    instead, I fear you are out of luck. If you have the void*, than you define a function that calls a python callable object with correct arguments and SetCallBack with this function and the python callable.

    If you can't, but the callback is global (it seems to be), you can create a global variable to store the python object in. Than you'd again create a function to call the python object and pass it to SetCallBack and your PySetCallback would just set the global and ensure proper function is registered.

    If the callback is context-specific, but you have no way to pass it a "user data" pointer, I fear you are out of luck here.

    I know python and C++, but not cython, so I don't know whether you can create the function in cython, or whether you'd have to write in C++.

提交回复
热议问题