[C++-sig] polymorphic class question

Tim Blechmann tim at klingt.org
Sun Mar 11 10:17:04 CET 2007


On Sun, 2007-03-11 at 08:49 +0200, Roman Yakovenko wrote:
> 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 )

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 at klingt.org    ICQ: 96771783
http://tim.klingt.org

Silence is only frightening to people who are compulsively
verbalizing.
  William S. Burroughs
-------------- 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/63972259/attachment.pgp>


More information about the Cplusplus-sig mailing list