hi all, i'm using lots of polymorphic classes in a mixed c++/python environment. the biggest problem for me are noncopyable c++ classes, that are derived from python and stored (via pointers) in c++ data structures. my_class: boost::noncopyable {}; in order to keep the reference, i introduced the following helper class: template <class T> struct self_containing_wrapper: public T { self_containing_wrapper(PyObject * self): self(self) { } virtual ~self_containing_wrapper(void){} PyObject * self; }; which is used to wrap classes in the following way: class_<my_class, self_containing_wrapper<my_class>, boost::noncopyable>("my_class"); now i'm trying to write a c++-to-python converter in order to get the correct reference when wrapping functions that return pointers to instances of my_class: template <class T> struct self_containing_wrapper_converter { static PyObject* convert(T const & x) { self_containing_wrapper<T> const & wrapper = static_cast<self_containing_wrapper<T> const &>(x); return wrapper.self; } }; to be used like: to_python_converter<my_class, self_containing_wrapper_converter<my_class> >(); now i have two questions: - is this correct or do i need to take care in terms of reference counting? - when wrapped c++ functions return my_class*, will i have to specify the return_value_policy? if so, what policy should i use? thanks in advance ... tim -- tim@klingt.org ICQ: 96771783 http://tim.klingt.org Which is more musical, a truck passing by a factory or a truck passing by a music school? John Cage
On 3/11/07, Tim Blechmann <tim@klingt.org> wrote:
hi all,
i'm using lots of polymorphic classes in a mixed c++/python environment. the biggest problem for me are noncopyable c++ classes, that are derived from python and stored (via pointers) in c++ data structures.
my_class: boost::noncopyable {};
in order to keep the reference, i introduced the following helper class:
template <class T> struct self_containing_wrapper: public T { self_containing_wrapper(PyObject * self): self(self) { }
virtual ~self_containing_wrapper(void){}
PyObject * self; };
which is used to wrap classes in the following way:
class_<my_class, self_containing_wrapper<my_class>, boost::noncopyable>("my_class");
now i'm trying to write a c++-to-python converter in order to get the correct reference when wrapping functions that return pointers to instances of my_class:
template <class T> struct self_containing_wrapper_converter { static PyObject* convert(T const & x) { self_containing_wrapper<T> const & wrapper = static_cast<self_containing_wrapper<T> const &>(x); return wrapper.self; } };
to be used like:
to_python_converter<my_class, self_containing_wrapper_converter<my_class> >();
now i have two questions: - is this correct or do i need to take care in terms of reference counting?
- when wrapped c++ functions return my_class*, will i have to specify the return_value_policy? if so, what policy should i use?
What is wrong with Boost.Python wrapper class?( http://boost.org/libs/python/doc/v2/wrapper.html ) You also can get reference to the Python object: http://boost.org/libs/python/doc/v2/faq.html#xref -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/
On Sun, 2007-03-11 at 08:49 +0200, Roman Yakovenko wrote:
On 3/11/07, Tim Blechmann <tim@klingt.org> wrote:
hi all,
i'm using lots of polymorphic classes in a mixed c++/python environment. the biggest problem for me are noncopyable c++ classes, that are derived from python and stored (via pointers) in c++ data structures.
my_class: boost::noncopyable {};
in order to keep the reference, i introduced the following helper class:
template <class T> struct self_containing_wrapper: public T { self_containing_wrapper(PyObject * self): self(self) { }
virtual ~self_containing_wrapper(void){}
PyObject * self; };
which is used to wrap classes in the following way:
class_<my_class, self_containing_wrapper<my_class>, boost::noncopyable>("my_class");
now i'm trying to write a c++-to-python converter in order to get the correct reference when wrapping functions that return pointers to instances of my_class:
template <class T> struct self_containing_wrapper_converter { static PyObject* convert(T const & x) { self_containing_wrapper<T> const & wrapper = static_cast<self_containing_wrapper<T> const &>(x); return wrapper.self; } };
to be used like:
to_python_converter<my_class, self_containing_wrapper_converter<my_class> >();
now i have two questions: - is this correct or do i need to take care in terms of reference counting?
- when wrapped c++ functions return my_class*, will i have to specify the return_value_policy? if so, what policy should i use?
What is wrong with Boost.Python wrapper class?( http://boost.org/libs/python/doc/v2/wrapper.html )
isn't Boost.Python wrapper only useful, when implementing virtual functions from python? get_override is the only documented member
You also can get reference to the Python object: http://boost.org/libs/python/doc/v2/faq.html#xref
that's what i was trying to implement without the need to wrap function explicitly but by using an implicit converter ... tim -- tim@klingt.org ICQ: 96771783 http://tim.klingt.org Silence is only frightening to people who are compulsively verbalizing. William S. Burroughs
participants (2)
-
Roman Yakovenko -
Tim Blechmann