Boost logo

Boost :

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


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.

On Mon, Dec 13, 2010 at 10:04 AM, Dave Abrahams <dave_at_[hidden]> wrote:

> At Mon, 13 Dec 2010 09:57:59 +1100,
> Allan Johns wrote:
> >
> > Sorry I don't think I was being clear enough.
> >
> > I want to have the derived classes to-python convert into basic types
> (float
> > ,string etc). In other words, I don't actually want TypedAttribute<T> to
> be
> > directly bound at all.
>
> Oh, I see.
>
> > Background: I have a 'table' class that can hold various types of
> attributes
> > (all derived from the same base class), a subset of which are just
> wrapper
> > classes for POD types. I want these POD-based attributes to automatically
> > to-python convert to the relevant types.
> >
> >
> > I actually have this working at the moment but not by way of boost.python
> > converters... I have a hand-rolled dispatcher going on (think std::map
> keyed
> > on std::type_info), that my table class's python binding uses directly.
> But
> > I'd rather not be duplicating boost.python functionality, if that's what
> I'm
> > doing (it would seem more elegant, and more flexible, to do this with
> > boost.python itself).
>
>
> You might start by answering the question of in which way what you did
> fails to work.
>
>
>
> --
> Dave Abrahams
> BoostPro Computing
> http://www.boostpro.com
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


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