Inherited instance variables
Hi, I just discovered Boost.Python and I am very impressed (as I am with most of Boost). I have embedded a Python interpreter in my project and would like to tie in some C++ classes but am having problems with inherited member variables. Here is my example: C++ --- struct foo { foo(void) {} virtual ~foo(void) {} virtual bool handle_evt(object event) { return false; } list children; } struct foowrap : foo { foowrap(PyObject* self) : self_(self) {} foowrap(PyObject* self, const foo& f) : foo(f), self_(self) {} bool handle_evt(object event) { return call_method<bool>(self_, "handle_evt", event); } static bool def_handle_evt(foo* f, object event) { return f->foo::handle_evt(event); } PyObject* const self_; }; BOOST_PYTHON_MODULE(foomod) { class<foo, foowrap>("foo") .def("handle_evt", &foowrap::def_handle_evt) .def_readwrite("children", &widget::children); } Python ------ import foomod class subfoo(foomod.foo): def __init__(self, parent): self.parent = parent self.parent.children.append(self) def __del__(self) self.parent.children.remove(self) def handle_evt(self, event) print event for child in children: if handle_evt(event) return true else return false f = foo() # works fine g = subfoo(f) # works fine h = subfoo(g) # breaks Error: self.parent.children.append(self) TypeError: bad argument type for built-in operation # no children list available in the subfoo class instance 'g' print g.children TypeError: bad argument type for built-in operation It appears that the children list object is not visible from any subclasses of the foo class. I have tried changing the line: .def_readwrite("children", &widget::children) with .add_property("children", make_getter(&foo::children, return_internal_reference<>()), make_setter(&foo::children)) but still no luck...any help would be appreciated, thanks. -- Matt Giger Lunar Software, Inc. mgiger@lunarsoft.com
Matt Giger <mgiger@lunarsoft.com> writes:
Hi, I just discovered Boost.Python and I am very impressed (as I am with most of Boost). I have embedded a Python interpreter in my project and would like to tie in some C++ classes but am having problems with inherited member variables.
Matt, are you using Boost 1.29.0 or the CVS? There have been some improvements in handling of mutable attributes.
Here is my example:
C++ ---
struct foo { foo(void) {} virtual ~foo(void) {} virtual bool handle_evt(object event) { return false; } list children; }
struct foowrap : foo { foowrap(PyObject* self) : self_(self) {} foowrap(PyObject* self, const foo& f) : foo(f), self_(self) {} bool handle_evt(object event) { return call_method<bool>(self_, "handle_evt", event); } static bool def_handle_evt(foo* f, object event) { return f->foo::handle_evt(event); } PyObject* const self_; };
BOOST_PYTHON_MODULE(foomod) { class<foo, foowrap>("foo") .def("handle_evt", &foowrap::def_handle_evt) .def_readwrite("children", &widget::children); }
This won't compile; where's widget? ... -- Dave Abrahams Boost Consulting www.boost-consulting.com
Matt Giger <mgiger@lunarsoft.com> writes:
Hi, I just discovered Boost.Python and I am very impressed (as I am with most of Boost). I have embedded a Python interpreter in my project and would like to tie in some C++ classes but am having problems with inherited member variables.
Matt, are you using Boost 1.29.0 or the CVS? There have been some improvements in handling of mutable attributes.
Here is my example:
C++ ---
struct foo { foo(void) {} virtual ~foo(void) {} virtual bool handle_evt(object event) { return false; } list children; }
struct foowrap : foo { foowrap(PyObject* self) : self_(self) {} foowrap(PyObject* self, const foo& f) : foo(f), self_(self) {} bool handle_evt(object event) { return call_method<bool>(self_, "handle_evt", event); } static bool def_handle_evt(foo* f, object event) { return f->foo::handle_evt(event); } PyObject* const self_; };
BOOST_PYTHON_MODULE(foomod) { class<foo, foowrap>("foo") .def("handle_evt", &foowrap::def_handle_evt) .def_readwrite("children", &widget::children); }
This won't compile; where's widget?
Yikes, sorry I did some simplification of the code and got caught by that old copy-paste bug creation technique, "widget::children" should read "foo::children". I'm using 1.29.0. BTW. Thank you for replying, your Boost.Python lib saves *a ton* of work and you seem tireless in your (free) help for people using it. I just wanted to thank you for your tremendous efforts. -- Matt Giger Lunar Software, Inc. mgiger@lunarsoft.com
participants (2)
-
David Abrahams -
Matt Giger