[C++-sig] Re: shared_ptr<Object>

David Abrahams dave at boost-consulting.com
Sun Oct 31 21:57:33 CET 2004


John Meinel <john at johnmeinel.com> writes:

> David Abrahams wrote:
>> John Meinel <john at johnmeinel.com> writes:
>> I have lots of questions about your questions, but see ********
>> below
>> before answering.
>> 
>
> I recognized I use the wrong terms in several places. Using "object"
> instead of "class", etc. However, your ***** was what I needed, as:
> http://www.boost.org/libs/python/doc/v2/register_ptr_to_python.html#register_ptr_to_python-spec
>
> Is pretty much exactly what I am doing, just with a little more hierarchy.
>
>
> [...]
>
>> Please show code that produces the error... otherwise I will assume
>> you just added it 
> If you change the line:
> 	class_<mine, mine_w, bases<parent>, boost::noncopyable>("mine")
> To:
> 	class_<mine, boost::shared_ptr<mine>, mine_w
> 		, bases<parent>, boost::noncopyable>("mine")
>
> It will not compile.

That's expected.  Does the documentation give you any reason to
believe that it should work?

>> What happens that indicates to you that it will "not allow you to
>> inherit the C++ object" in python?  BTW, you can't "inherit a C++
>> object."  I think I know what you mean, but using clear and correct
>> terminology will help me to help you.
>
> Sorry, again here I meant I cannot create a Python class that inherits
> properly from the C++ class. From my limited understanding, I could
> either use:
>
> class_<mine, shared_ptr<mine>, ...>
> or
> class_<mine, mine_w, ...>
>
> The second would let me inherit in Python, the first would let me
> return shared_ptr<mine> objects.

  class_<mine, shared_ptr<mine_w>, ...>

is supposed to work.

>> 
>>>    If I try to do shared_ptr<mine_w>, then I still cannot create a
>>>    shared_ptr<mine>.
>> What happens that indicates to you that "still cannot create a
>> shared_ptr<mine>."
>> 
>
> If I used:
> class_<mine, shared_ptr<mine_w>, ...>
> then if I called a function with:
>
> shared_ptr<mine> create() { return shared_ptr<mine>(new child); }
> I would get "no to-python converter registered for type ...".

Right.  Using shared_ptr<mine_w> as a template parameter here only
registers a to-python conversion for shared_ptr<mine_w>.  If you want
to convert a shared_ptr<mine> to python you need to explicitly
register the converter.

>> *************
>> Did you try using   implicitly_convertible<shared_ptr<mine_w>,
>> shared_ptr<mine> >()
>> ??
>> Did you try using   register_ptr_to_python<shared_ptr<mine> >()
>> ??
>> 
>
> Just to clarify, should I use both? 

No, actually the first one is totally unneeded.

> I think if I use:
> class_<mine, mine_w, bases<...>, boost::noncopyable> ...
> register_ptr_to_python<shared_ptr<mine> >();
>
> Then the above function "create" would work. But do I need the
> "implicitly_convertible" in the case where I have create a Python
> class, and then I want to pass that into a C++ function that wants a
> shared_ptr? Or do I need to define my base class as:
> class_<mine, shared_ptr<mine_w>, ...
> to make both cases work?

The latter.

> [...]
>
>>>
>>>Which has stuff like "get_override" and "public wrapper<>". Is this a
>>> better way to do the python wrapping. 
>> Yes, if you have the latest Boost CVS state.  If not, you can't use
>> it.
>> 
>>>It's not in the tutorial
>> It will be, in the upcoming release.
>> 
>>> and the documentation in general seems pretty weak. 
>> What's weak about it?
>> 
>
> The documentation seems decent if you already know what to look
> for. Like once you told me about "register_ptr_to_python" it has a
> good example, and it fits what I need.
>
> But there isn't a very good overview. The tutorial is very good for
> starting out, but doesn't cover the more advanced topics. I suppose
> one possibility is to just read *all* of the documentation, but when I
> first started out, it got so technical in places that it stopped
> making sense. It seemed like it was more for people who were trying to
> understand the implementation, rather than how to use it.

I think you're right.  The library needs an overview of how
conversions to and from python work.

>>>I also didn't
>>>find it in the documentation.
>>>http://www.boost.org/libs/python/doc/v2/reference.html
>> That's because it's not released yet.
>
> Thank you for your time. I'm interested in what 1.32 is going to
> look like. In general I'm very happy with boost, and
> boost::python. A lot of times the documentation feels like reading
> commented source code, rather than a document describing how things
> should be used.  I suppose that's what tutorials and possibly wikis
> are for.

Right; that's what the tutorial is for.  But some essays on select
topics (like conversions) would be very useful in addition to the
tutorial.  Unfortunately there are few resources available for
Boost.Python development (including further documentation) right now.
It would be nice if things were otherwise, but the project is quite
useful and stable, so I guess it's not too bad that it isn't moving
fast.

-- 
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com




More information about the Cplusplus-sig mailing list