Last active
March 27, 2017 08:35
-
-
Save hainm/7d225e03726c2a24c528 to your computer and use it in GitHub Desktop.
test.pyx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# distutils: language = c++ | |
cdef cppclass A: | |
void c_test(): | |
print ("hello from A") | |
cdef cppclass B(A): | |
void SetObject(A* ptr): | |
print ("dummy method, hello from B") | |
cdef class pyA: | |
cdef A* thisptr | |
def __cinit__(self): | |
self.thisptr = new A() | |
def __dealloc__(self): | |
if self.thisptr is not NULL: | |
del self.thisptr | |
cdef class pyB (pyA): | |
def __cinit__(self): | |
if self.thisptr: | |
del self.thisptr | |
self.thisptr = new B() | |
def __dealloc__(self): | |
if self.thisptr is not NULL: | |
del self.thisptr | |
def set_object(self, obj): | |
""" | |
>>> from test import pyA, pyB | |
>>> pya = pyA() | |
>>> pyb = pyB() | |
>>> pyb_2 = pyB() | |
>>> pyb_2.set_object(pya) | |
dummy method, hello from B | |
>>> pyb_2.set_object(pyb) | |
dummy method, hello from B | |
""" | |
cdef pyA tmpobj = <pyA> obj | |
# still need to cast to B class to use SetObject | |
(<B*> self.thisptr).SetObject(<A*> tmpobj.thisptr) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment