Boost logo

Boost :

Subject: Re: [boost] boost::python: to-python conversion on polymorphic class
From: Dave Abrahams (dave_at_[hidden])
Date: 2010-12-15 15:18:38


At Mon, 13 Dec 2010 10:56:25 +1100,
Allan Johns wrote:
>
> Right you are.
>
> With regards to my original snippet:
>
>
> template<typename Attrib>
> struct typed_attrib_topython
> {
> static PyObject* convert(const Attrib& a)
> {
> return bp::incref(bp::object(a.value()).ptr()); // return POD
> type
> }
> };
>
> template<typename T>
> struct TypedAttribBind
> {
> typedef TypedAttribute<T> attrib_t;
>
> TypedAttribBind()
> {
> boost::python::to_python_converter<attrib_t,
> typed_attrib_topython<attrib_t> >();
> }
> };
>
>
> TypedAttribute<T> derives from Attribute, which derives from Object (the
> common polymorphic base class).
> TypedAttribute<T>.value() is a function which returns the POD type
> associated with the attribute.
> TypedAttribBind<T> is instantiated for a range of POD types T, in order to
> register the to-python converters.

> This doesn't behave as expected - if I call any bound function which
> returns a shared_ptr<Object> from python (where the object is
> actually a TypedAttribute<T>), I just get a wrapped Object back,
> rather than a POD type.

I *think* what you mean is that you don't get a Python built-in type
(since POD is a C++ concept and you're looking at the resulting object
from the Python side)?

I'm afraid shared_ptr<T> is treated specially by Boost.Python at an
extremely low level, so this is hard to do. However, although I can't
support this usage, you might take a look in
boost/python/converter/shared_ptr_to_python.hpp and consider creating
an overload (in namespace boost::python::converter) for
shared_ptr<Object>.

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk