Boost logo

Boost :

Subject: Re: [boost] boost::python: to-python conversion on polymorphic class
From: Dave Abrahams (dave_at_[hidden])
Date: 2010-12-10 13:40:56


At Fri, 10 Dec 2010 17:12:12 +1100,
Allan Johns wrote:
>
> Hi,
>
> Is it possible to register to-python converters for polymorphic types held
> in shared_ptr? This does not seem to work for me.
>
> I have a virtual base class Object, and derived classes TypedAttribute<T>
> (with method 'value()' that returns a simple type - float, int etc).
>
> My boost.python registration code looks like so:
>
>
> template<typename Attrib>
> struct typed_attrib_topython
> {
> static PyObject* convert(const Attrib& a)
> {
> return bp::incref(bp::object(a.value()).ptr());
> }
> };
>
>
> template<typename T>
> struct TypedAttribBind
> {
> typedef TypedAttribute<T> attrib_t;
>
> TypedAttribBind()
> {
> boost::python::to_python_converter<attrib_t,
> typed_attrib_topython<attrib_t> >();
> }
> };
>
>
> However this doesn't work.

Doesn't work how?

You shouldn't need to do anything special for shared_ptrs; just return
them. Don't register converters. See shared_ptr.cpp in
Boost.Python's libs/python/test/ directory.

-- 
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