Boost.Python, callbacks and inheritance
Timo Savola
timo.savola at iki.fi
Sat Nov 17 14:05:31 EST 2001
On Fri, 2001-11-09 at 13:45, Michael Hudson wrote:
> Timo Savola <timo.savola at iki.fi> writes:
>
> > Now I'm doing an extension module, or rather a system that I want to
> > control via Python. I have a C++ class hierarchy that I want to be
> > accessible from Python.
>
> Have you looked at Boost::Python? I haven't, but I've heard it's good
> for this sort of thing.
I've now converted my code to use Boost.Python and quite happy with it.
But I do have one major problem.
I have a C++ class (Color) with only pure virtual methods. All other
classes with methods that have Color as the type of their argument
obviously always get an instance of a class derived from Color. One of
these other classes (ColorStream) also has only pure virtual methods,
and is meant to be implemented with Python.
class Color {
virtual float red() = 0;
virtual float green() = 0;
virtual float blue() = 0;
}
class_builder<Color> color(module, "Color");
color.def(&Color::red, "red");
color.def(&Color::green, "green");
color.def(&Color::blue, "blue");
class ColorStream {
virtual void put(Color &) = 0;
}
struct ColorStreamCallback : ColorStream {
ColorStreamCallback(PyObject *s)
{
self = s;
}
void put(Color &color)
{
callback<void>::call_method(self, "put", color);
}
private:
PyObject *self;
}
class_builder<ColorStream, ColorStreamCallback>
color_stream(module, "ColorStream");
color_stream.def(constructor<>());
The problem surfaces in the put function of ColorStreamCallback. The
call_method function of callback<void> doesn't accept the reference to
the non-concrete Color. I tried playing around with auto_ptr<Color>, but
just ended up with incomprehensible errors with template unrolling. I'm
more or less clueless.
Timo
More information about the Python-list
mailing list