[C++-sig] Custom smart pointer with same behaviour as shared_ptr

john haddon theboyhaddon at hotmail.com
Sat Jan 19 02:59:04 CET 2008


> > we had similar problems when wrapping a library where we used 
> > boost::intrusive_ptr as our
> > smart pointer of choice. we managed to work around most of the problems 
> > using the magic
> > incantations in this file :
> > 
> > http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/IntrusivePtrPatch.h
> > 
> 
> Could you please tell me what you meant by
> 
>  > "we managed to work around most of the problems"
> 
> since it looks like I will be following the same steps as you.

Our main problem was that when returning an intrusive_ptr<Base> from C++ to python,
it would create a Python object of type Base, when what we really wanted to do was create
a Python object of type Derived, by downcasting to the most derived type. The code I
posted deals with that successfully (and I think one other issue I don't recall). We still have
the problem that passing the same object to python twice results in two different objects :

# x always returns an intrusive pointer to the same c++ object
a = x()
b = x()
# assertion fails - two python objects refer to one c++ object and don't know it
assert( a is b )

We are working around this problem in two ways. Firstly we have an ugly isSame() method bound for object
to be used instead of "is". Secondly we have another to_python converter for use when we wrap classes. This
solves the object identity problem by keeping a mapping from C++ objects to python objects. You can see that
here :

http://cortex-vfx.googlecode.com/svn/trunk/include/IECore/bindings/WrapperToPython.h

I suspect the two solutions could be combined into one general solution, but currently it's working just
well enough so we've left it there...

Hope that helps...
Cheers...
John

_________________________________________________________________

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20080119/017c4fa8/attachment.htm>


More information about the Cplusplus-sig mailing list