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

Andrea mariofutire at googlemail.com
Sat Jan 19 13:03:17 CET 2008


john haddon wrote:
> 

It works... at least in my small proof of concept...

> 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 :

I am not sure I follow here.

My hierarchy is

base class A
B : A
C : B

then I call a function returning an intrusive_ptr<A>(new C()) into c

 >>> type(c)
<class 'bo.C'>

So Python can tell that the object is of type C even if it was returned as a pointer to the base 
class A.

Other point: please correct me if I am wrong.
I need to register every class I want to be able to cast to (i.e. every class I want to accept as 
argument).
So in that case I need to add both

INTRUSIVE_PTR_PATCH(B, B_class);
and
INTRUSIVE_PTR_PATCH(C, C_class);

so that my functions can take intrusive_ptr of type B and C.
No need to do it with A.

> 
> 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...

Here, first I need to understand the problem, then I pass to your solution.
Is it a general problem, or something relative to intrusive_ptr, that would not be there, had you 
used boost::shared_ptr?

Thanks very much.

Andrea




More information about the Cplusplus-sig mailing list