Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2001-04-01 22:55:35


----- Original Message -----
From: "Rainer Deyke" <root_at_[hidden]>

> If the object is created by Python, its real class is usually
> 'boost::python::held_instance<C>'.

...but might well be boost::python::held_instance<B>, where C is derived
from B.

> 'boost::python::held_instance<C>' could
> be modified to remember the 'PyObject *' that it is the first argument to
> its constructors.

...at a cost of one extra pointer per object. Not too bad, considering
nobody noticed that all Python objects got 12 bytes bigger with Python 2.0.

> We can then defined 'boost::python::to_python<C *>' like
> this:
>
> template <class C> PyObject *to_python(C *c)
> {
> held_instance<C> *held = dynamic_cast<held_instance<C> *>(c);
> if (held) {
> Py_INCREF(held->boost_py_object);
> return held->boost_py_object;
> } else {
> return 0; // Either that or raise an exception.
> }
> }

This will only work when C has at least one virtual function :-(

> This can probably be generalized to include cases where
> 'boost::python::held_instance' is not used.

What do you have in mind?

-Dave


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