Boost logo

Boost :

Subject: [boost] boost::python: to-python conversion on polymorphic class
From: Allan Johns (allan.johns_at_[hidden])
Date: 2010-12-10 01:12:12


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. I have other bound functions that return a
boost::python::object(boost::shared_ptr<Object>), but these do not get
converted. Am I misunderstanding to-python conversion? Should I expect this
to work on polymorphic types?

thanks in advance,
Allan


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