[C++-sig] shared_ptr converters

David Abrahams dave at boost-consulting.com
Wed Apr 16 15:35:34 CEST 2003


Nicodemus <nicodemus at globalite.com.br> writes:

> Is this a bug, or am I missing something? I tried changing the order
> of the template parameters of class_, but then the code either doesn't
> compile, or compiles but gives the same results (I don't think the
> error messages from the compiler are revelant in this case, but I will
> post if there's need).

Argh!

Well, it's not a bug exactly, but the library needs to change a bit
to accomodate this use-case.  If you want instances of A to be held
by shared_ptr, the appropriate way to wrap your class
is:

    class_<A, boost::noncopyable, boost::shared_ptr<A_Wrapper> >("A")
        ;

or

    class_<A, boost::shared_ptr<A_Wrapper>, boost::noncopyable>("A")
        ;

Of course, the problem with this is that you are also wrapping a
function which returns a shared_ptr<A> (New), and there's no
automatic to_python conversion for shared_ptr<X> for any X.


If you don't care that your instances of A are held by shared_ptr,
you can wrap them as follows:

    class_<A, boost::noncopyable, A_Wrapper>("A")
        ;

or

    class_<A, A_Wrapper, boost::noncopyable>("A")
        ;

but then the same problem holds (there's no to-python converter for
shared_ptr<A>).  The right answer is to change the library so that
shared_ptr<X> can be automatically converted to_python for any X (and
likewise for any smart pointer) as long as a class_ for X is
registered.  That change will require some work.

In the meantime, you can do this horrible thing:

    objects::class_value_wrapper<
        shared_ptr<A>
      , objects::make_ptr_instance<A, objects::pointer_holder<shared_ptr<A>,A> >
    >();
        
Which will register a to_python converter for shared_ptr<A>.



P.S. The number of issues like this one is piling up.  If there are any
companies out there who depend on Boost.Python, it might be a good
investment to fund some development so that we can keep the project
healthy (hint, hint)!

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list