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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

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

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

To correctly handle the memory for the objects, I want my boost.python
class wrappers for a class T to be held using ref_ptr<T>.  I can do this
with:

class_<T, bases<...>, ref_ptr<T> >("T",...);

This all works fine.

I run into a problem though when I want to use wrapped method that
return ptr<>'s to objects instead of a full ref_ptr<>.

For example if I wrap a method like this:

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>.  I know that I can use
register_ptr_to_python<ptr<Base> > to register a to_python converter for
the type, but as I understand it this will cause the python wrapper to
store the C++ object using a ptr<> type internally.  This won't work
correctly because it will not do any memory reference counting.

What I need is a way to tell boost python that anytime it needs to
convert a ptr<Base> to python it should first convert it to a
ref_ptr<Base>  (using a standard copy constructor in ref_ptr<>) and then
use this ref_ptr as the held type like normal.

At first I thought that implicitly_convertible<> would help:

implicitly_convertible<ptr<Base>, ref_ptr<Base> >();

But this doesn't seem to do anything for me. (this really isn't too
surprising since this isn't what implicitly_convertible is for, but it
seemed worth a try).

Has anyone else ran into anything similar?  Any ideas?

Thanks,
Allen
_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig
Reply | Threaded
Open this post in threaded view
|

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

Ralf W. Grosse-Kunstleve
--- Allen Bierbaum <[hidden email]> 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 
_______________________________________________
C++-sig mailing list
[hidden email]
http://mail.python.org/mailman/listinfo/c++-sig