[Cython] Wrapping Virtual C++ Class Functions
Robert Bradshaw
robertwb at math.washington.edu
Thu Jul 24 06:42:51 CEST 2008
On Jul 23, 2008, at 8:48 PM, Greg Ewing wrote:
> Chris Swierczewski wrote:
>
>> void callEvent()
>> virtual void onEvent(object eventData)
>
>> Question: How do I wrap this intricate situation in Cython?
>
> You may need to create a C++ subclass of the class to
> be wrapped, with an onEvent() implementation that calls
> a C function, passing it "this" as a parameter.
>
> Then implement that C function in the .pyx file
> (declaring it "public" so that it can be called from
> the C++ code) so that it locates the Python wrapper
> object and calls a Python method of it.
>
> To map the C++ object back to the Python wrapper, you
> could cast a reference to the wrapper to void * and
> store it in a member of the C++ object (this effectively
> makes it a borrowed reference, so that it won't keep
> the wrapper object alive).
Yep, I agree. Another related option would be to have the C++
subclass take a function pointer and a void* (which, again, may hold
a pointer to the Python object) which would get filled in in Cython.
- Robert
More information about the Cython-dev
mailing list