[C++-sig] polymorphic class question

Tim Blechmann tim at klingt.org
Sun Mar 11 01:36:40 CET 2007


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 at 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070311/ddfe01ad/attachment.pgp>


More information about the Cplusplus-sig mailing list