[C++-sig] rvalue custom converters ( was Protected destructor compile error )

Ralf W. Grosse-Kunstleve rwgk at yahoo.com
Sat Jun 24 07:02:59 CEST 2006


--- Roman Yakovenko <roman.yakovenko at gmail.com> wrote:
> I will study the examples. I and Lakin Wecker work on Python bindings for
> Ogre.
> That library contains few classes like Matrix3 or Matrix4( Vector3, Vector4
> ).
> Those classes have constructors from regular C++ arrays. It could be nice if
> we can create Python bindings, where user could pass Python list or
> tuple instead
> of [Matrix|Vector][3|4] as argument to functions.
> 
> If I understand right, custom rvalue converters is the way to go, right?
> The only question I have is how it will work in presents of registration of
> [Matrix|Vector][3|4] classes?

Situation 1 a:

  class_<matrix>(...)
    .def(init<matrix const&>()) // wrap the copy constructor
  ;

  Also define custom from_python converters converting Python sequences (e.g.
list, tuple, iter) to matrix. This way any function expecting a matrix const&
will also accept a Python sequence, including the copy constructor above.

Situation 1 b:

  If you don't want all function to convert sequences on the fly (e.g. because
you are afraid your users don't understand the impact on runtime performance),
don't define the custom from_python converters. Instead of wrapping the copy
constructor, use make_constructor to inject a constructor converting Python
sequences.

Situation 2:

  Define both custom to_python and from_python conversions, don't use class_.

Examples:

http://cctbx.cvs.sourceforge.net/cctbx/scitbx/include/scitbx/boost_python/container_conversions.h?view=markup

Which enables, e.g.:
tuple_mapping_fixed_size<matrix>(); // situation 2 above

http://cctbx.cvs.sourceforge.net/cctbx/scitbx/include/scitbx/stl/vector_wrapper.h?revision=1.4&view=markup

This also uses container_conversions.h, but only to define from_python
conversions. The copy constructor is wrapped. This is situation 1 a above.

http://cctbx.cvs.sourceforge.net/cctbx/scitbx/array_family/boost_python/flex_double.cpp?revision=1.31&view=markup

Look for from_list_or_tuple_of_lists_or_tuples() which is used with
make_constructor. This is situation 1 b above.


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 



More information about the Cplusplus-sig mailing list