[C++-sig] polymorphic class question

Roman Yakovenko roman.yakovenko at gmail.com
Sun Mar 11 07:49:57 CET 2007


On 3/11/07, Tim Blechmann <tim at 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/



More information about the Cplusplus-sig mailing list