[C++-sig] 1-D numeric array <-> std::vector<double>
David Abrahams
dave at boost-consulting.com
Sat Dec 21 00:39:06 CET 2002
John Hunter <jdhunter at ace.bsd.uchicago.edu> writes:
> I have been experimenting with boost::python v2, and just got tuple
> <-> std::vector<double> working in my extension code using the
> container_conversions in scitbx. Quite impressive!
>
> Now, I am wondering if someone has implemented a similar functionality
> for transparent passing of Numeric 1D arrays to and from C++ functions
> which expect std::vector<double>.
>
> If this can be done, could someone point me to some example code or
> documentation.
Look at slice_from_python in scitbx/boost_python/slice.cpp and
scitbx/include/scitbx/boost_python/slice.h. This is an rvalue
from_python converter for a class called "slice".
The protocol for an rvalue converter (which is what you want, since
there's no std::vector<double> in a Numeric array) is that:
1. The "convertible" function must return non-NULL if overload
resolution should succeed with that converter. In your case, if
the source object is a 1D Numeric array. That pointer can be
anything, but it should not be a resource that might need to be
deallocated.
2. The "construct" function should downcast the data to
boost::python::converter::rvalue_from_python_storage<std::vector<double>
>* and construct the result vector in its storage.bytes using
placement new.
3. As soon as construction succeeds, you must set data->convertible to
point at the storage.
This is all undocumented because it's so hairy. I plan to come up
with something easier in the next few months so it's fit for users ;-)
--
David Abrahams
dave at boost-consulting.com * http://www.boost-consulting.com
Boost support, enhancements, training, and commercial distribution
More information about the Cplusplus-sig
mailing list