[C++-sig] [python] python + phoenix

Ravi lists_ravi at lavabit.com
Sun Nov 1 21:40:26 CET 2009


On Saturday 31 October 2009 01:24:16 troy d. straszheim wrote:
> I take it that you have a use-case where it is difficult to specify
> 
>    as<R(A1,A2)>(thing)
> 
> and easy to specify
> 
>    as<mpl::vector<R, A1, A2> >(thing)
> 
> Could you elaborate?

I have some code that takes member function pointers of the form
  R Obj::*( A1, A2, A3, ..., An )
and converts them to a function object Q with the following signature:
  Rnew Q( Obj&, B1, B2, ..., Bn )
where
  Rnew = result_conversion_metafunction<R>::type
  Bi = arg_conversion_metafunction<Ai>::type
and Q is exposed via boost.python as a member of Obj. 

For example, Ai could be a fixed-point number, with Bi being a double so that 
the python side does not know anything about fixed-point numbers. An instance 
of Q would convert eh floating point numbers passed from the python side into 
fixed-point numbers, call the member function, and convert the returned value 
to a double for use on the python side.

For these "converters", metafunctions from boost.function_types are used to 
obtain the mpl::vector specifying result type and argument types. Extending 
the technique above to function objects which take Obj* as their first 
argument, I have a protocol which relies on the presence of a typedef called 
'components' in the function object so that I can use the converter when 
exposing via boost.python:

struct my_functor {
  typedef mpl::vector<R, Obj*, A1, A2, A3, A4> components;
  R operator()( Obj*, A1, A2, A3, A4 ) { ... }
};

// my_converter uses the components typedef
typedef typename my_converter<my_functor>::type Q;

Q q;
class_<Obj,...>( "Obj" )
  .def( "my_func", q )
  ;

Regards,
Ravi



More information about the Cplusplus-sig mailing list