I think what you will have to do is create a stub class whose
overridden methods call some C function pointers. You can then
initialize and use this class from Cython.
- Robert
Sure. Warning, I have not even tried to compile the code below, but it
should be enough to get you started.
--------- wrapper.cc -----------
class CallbackWrapper: public BaseClass {
int (*my_func_ptr)(double, void*);
public:
CallbackWrapper(int (*f)(double, void*)) {
this->my_func_ptr = f;
}
int callback_stub(double x, void* data) {
return this->my_func_ptr(x, data);
}
};
-------- module.pyx ----------
cdef extern from "wrapper.cpp":
cdef cpp class CallbackWrapper:
CallbackWrapper(int (*f)(double void*))
cdef int my_cython_callback(double x, void* data):
[do callback stuff]
def registerCallback():
cdef CallbackWrapper *callback_object = new
CallbackWrapper(&my_cython_callback);
[register callback_object]
> Thanks
>
> 2010/7/28 Robert Bradshaw <robe...@math.washington.edu>
>>
And one more word of warning, you may need to worry about capturing
the GIL if you do anything with Python objects, if your callback is
being invoked from another thread.
- Robert
What you're really writing is
> cdef class oFBaseApp:
> cdef ofBaseAppStub.c_ofBaseAppStub *thisptr
>
> cdef void setup(self):
> print "setup from python!!"
which makes the problem clearer. Member functions always take a "self"
argument. What you might need to do is pass self (you may have to
declare it as a PyObject* in the C++ side, a oFBaseApp on the Cython
side, or do some casting) to your C++ class, which would then store it
an invoke it passing the "self" back as the first parameter. Be sure
to incref that object in your C++ constructor, and decref it in its
destructor (or otherwise make sure that self doesn't go away too
soon).
- Robert