Re: [cython-users] Wrapping a factory method returning a shared_ptr

80 views
Skip to first unread message

Nikita Nemkin

unread,
May 26, 2013, 6:23:29 AM5/26/13
to cython...@googlegroups.com
On Fri, 24 May 2013 02:59:24 +0600, Wichert Akkerman <wic...@wiggy.net>
wrote:

> I have a fairly typical API where I have a public pure virtual class
> with a
> factory method that returns an implementation. It looks like this:
>
> class User {
> public:
> static boost::shared_ptr<User*> create() ;
> };
>
> I can't find any hints in the documentation how you would wrap this in
> Cython.

Is this
http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#static-member-method
the hint you are looking for?

Your code should look like:

cdef extern from "boost/shared_ptr.hpp" namespace "boost":

cppclass shared_ptr[T]:
T* get()
# Note: operator->, operator= are not supported

cdef extern from "YourHeader.h" namespace "yournamespace":

cppclass User:
... # non static User methods here

cdef extern from "YourHeader.h" namespace "yournamespace::User":
# static user methods here, note the renaming
shared_ptr[User*] User_create "create"()

cdef shared_ptr[User*] user = User_create()


BTW shouldn't your code be using shared_ptr<User> instead of
shared_ptr<User*> ?
Smart pointer to a pointer makes little sense.


Best regards,
Nikita Nemkin

Wichert Akkerman

unread,
May 28, 2013, 4:24:58 PM5/28/13
to cython...@googlegroups.com

On May 26, 2013, at 12:23, Nikita Nemkin <nik...@nemkin.ru> wrote:
> Is this http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html#static-member-method
> the hint you are looking for?
>
> Your code should look like:
>
> cdef extern from "boost/shared_ptr.hpp" namespace "boost":
>
> cppclass shared_ptr[T]:
> T* get()
> # Note: operator->, operator= are not supported

I needed an shared_ptr() and shared_ptr(T*) as well, but that was indeed the missing trick.

> BTW shouldn't your code be using shared_ptr<User> instead of shared_ptr<User*> ?
> Smart pointer to a pointer makes little sense.

It does, that was a typo in my email.

Wichert.


Reply all
Reply to author
Forward
0 new messages