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 17:57:59


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.

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

Thanks!
A

On Sat, Dec 11, 2010 at 5:40 AM, Dave Abrahams <dave_at_[hidden]> wrote:

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