[C++-sig] Is there a way to automatically convert a smart_ptr to held type

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Sat Sep 3 03:07:51 CEST 2005


--- Allen Bierbaum <allenb at vrsource.org> wrote:

> I am wrapping a C++ library with boost.python that has multiple types of 
> smart ptrs for it's objects.  It has a ptr<> type that holds the object 
> and provides a ref counting interface (through and addRef()/subRef() 
> interface).  It then has a ref_ptr<> type that acts like a "standard" 
> smart pointer by holding a ptr<> internally and calling the ref handling 
> operations upon construction and destruction.

I am a bit confused by your ptr<> / ref_ptr<> interactions, but it seems
related to what I do routinely with my array types. The main trick is to use
custom to_python and from_python converters. E.g. here:

http://phenix-online.org/cctbx_sources/scitbx/include/scitbx/array_family/boost_python/ref_flex_conversions.h

http://phenix-online.org/cctbx_sources/scitbx/include/scitbx/array_family/boost_python/shared_flex_conversions.h

Read this first (the basic mechanism is exactly the same):

http://www.boost.org/libs/python/doc/v2/faq.html#custom_string

If I understood how you construct a ref_ptr<> from a ptr<> I could try to be
more specific. You wrote:

> ptr<Base> createObject()
>
> then when I call this from boost.python it needs to figure out that it 
> should convert the ptr<Base> to a ref_ptr<Base>.

If ptr<> owns the held Base object, but ptr<> is "a standard" smart pointer, I
don't understand under which conditions the Base object is constructed and
destroyed...

Cheers,
        Ralf


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Cplusplus-sig mailing list