Re: The newbie's question about wrapping c++ class with pyrex

Yong Sun <[email protected]>
Newsgroups gmane.comp.python.pyrex
Message-ID <[email protected]>
Hi, Robert,

Thanks for your reply. I think you article implicitly mentioned how to 
deal the reference parameter (I also figured it out by myself :)), e.g., 
if we have a new member function for Rectangle:

void getPosition (int &x, int &y)

ctypedef struct c_Rectangle "Rectangle":
int x0, y0, x1, y1
..
void getPosition (int x, int y)

This trick will cheat cython/pyrex to prepare the arguments properly. I 
think you may explicitly document this scenario.

Regards,

Robert Bradshaw 写道:
> On Apr 17, 2008, at 6:47 PM, Yong Sun wrote:
>
>> Hi, experts,
>>
>> I am following the tutorial at 
>> http://wiki.cython.org/WrappingCPlusPlus, and found a minor issue,
>>
>> cdef class Rectangle:
>> - c_Rectangle *thisptr # hold a C++ instance which we're wrapping
>> + cdef c_Rectangle *thisptr # hold a C++ instance which we're wrapping
>
> Thanks. I've made this change on the Cython wiki. I also corrected an 
> error about operator overloading.
>
> I'd like to add this example to the automated testing infrastructure, 
> but it seems unclear how to do so with the current runtests.py.
>
>>
>> I need the above change to make it successfully compiled by cython. 
>> While, when I tried to compile it with pyrexc, I met following errors,
>>
>> $ pyrexc --cplus rect.pyx
>> rect.pyx:4:21: Non-extern C function declared but not defined
>> rect.pyx:5:21: Non-extern C function declared but not defined
>> rect.pyx:6:19: Non-extern C function declared but not defined
>> rect.pyx:7:17: Non-extern C function declared but not defined
>> rect.pyx:18:27: Object of type 'c_Rectangle' has no attribute 
>> 'getLength'
>> rect.pyx:20:27: Object of type 'c_Rectangle' has no attribute 
>> 'getHeight'
>> rect.pyx:22:27: Object of type 'c_Rectangle' has no attribute 'getArea'
>> rect.pyx:24:20: Object of type 'c_Rectangle' has no attribute 'move'
>>
>> BTW, I am using the latest stable version,
>>
>> $ pyrexc --version
>> Pyrex version 0.9.6.4
>
> In Pyrex you need to declare functions as function pointers, i.e.
>
> cdef extern from "Rectangle.h":
> ctypedef struct c_Rectangle "Rectangle":
> int x0, y0, x1, y1
> (int *getLength)()
> ...
>
>>
>> Thank you very much!
>>
>> Regards,
>>
>>
>>
>> cdef extern from "Rectangle.h":
>> ctypedef struct c_Rectangle "Rectangle":
>> int x0, y0, x1, y1
>> int getLength()
>> int getHeight()
>> int getArea()
>> void move(int dx, int dy)
>> c_Rectangle *new_Rectangle "new Rectangle" (int x0, int y0, int x1, 
>> int y1)
>> void del_Rectangle "delete" (c_Rectangle *rect)
>>
>> cdef class Rectangle:
>> cdef c_Rectangle *thisptr # hold a C++ instance which we're wrapping
>> def __cinit__(self, int x0, int y0, int x1, int y1):
>> self.thisptr = new_Rectangle(x0, y0, x1, y1)
>> def __dealloc__(self):
>> del_Rectangle(self.thisptr)
>> def getLength(self):
>> return self.thisptr.getLength()
>> def getHeight(self):
>> return self.thisptr.getHeight()
>> def getArea(self):
>> return self.thisptr.getArea()
>> def move(self, dx, dy):
>> self.thisptr.move(dx, dy)
>> _______________________________________________
>> Pyrex mailing list
>> [email protected]
>> http://lists.copyleft.no/mailman/listinfo/pyrex
>


_______________________________________________
Pyrex mailing list
[email protected]
http://lists.copyleft.no/mailman/listinfo/pyrex
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.