[C++-sig] Re: will custom lvalue converters be supported?

Igor Lapshin igor at pcigeomatics.com
Thu Jul 22 23:10:03 CEST 2004


Hi Jonathan, 

It's an interesting idea to wrap the function into such template. I need
to think how I can overcome the problem that the types of the function
parameters of my functions may be in different sequences and thus I may
need too many templates. Meanwhile I'll probably use
vector_indexing_suite<> or scitbx/stl/vector_wrapper.h. 

Thanks,
Igor

$-> 
$-> template <typename T, void (T:: *T_fn)(std::vector<double>&)>
$-> void
$-> call_from_python_with_list( T* This, boost::python::list py_arg1)
$-> {
$->   int arg1_size = extract<int>(py_arg1.attr("__len__")());
$->   std::vector<double> cpp_arg1(arg1_size);
$-> 
$->   // Initialize cpp_arg1 from py_arg1
$->   for (int i = 0; i < arg1_size; ++i)
$->     cpp_arg1[i] = extract<double>(py_arg1[i]);
$-> 
$->   // Call the C++ mem fun that takes an lvalue.
$->   (This->*T_fn)( cpp_arg1);
$-> 
$->   // overwrite py_arg1 with data from cpp_arg1
$->   py_arg1.attr("__delslice__")(0, arg1_size);
$->   for (std::vector<double>::iterator i = cpp_arg1.begin();
$->       i < cpp_arg1.end(); ++i)
$->     py_arg1.append( object(*i));
$-> 
$->   return;
$-> }
$-> 
$-> struct call_me
$-> {
$->   void f( std::vector<double>& arg)
$->   {
$->     // do something to modify the vector.
$->     arg.push_back(2);
$->   }
$-> };
$-> 
$-> BOOST_PYTHON_MODULE(call_from_python_with_list)
$-> {
$->   class_<call_me>("call_me")
$->     .def( "f", &call_from_python_with_list< call_me, &call_me::f>)
$->     ;
$-> }
$-> 
$-> 
$-> _______________________________________________
$-> C++-sig mailing list
$-> C++-sig at python.org
$-> http://mail.python.org/mailman/listinfo/c++-sig




More information about the Cplusplus-sig mailing list