[C++-sig] extractor
David Abrahams
dave at boost-consulting.com
Fri Jan 24 15:48:44 CET 2003
"Mike Rovner" <mike at bindkey.com> writes:
> I'm trying to implement an extractor.
> Unfortunately I couldn't find an example of it.
> Please point me to a simple one.
>
> My code:
> #include <string>
> #include <vector>
> #include <boost/python.hpp>
> using namespace boost::python;
>
> template <class T>
> struct vec_extractor
> {
> static std::vector<T>& execute(PyObject* obj)
> {
> int size=PySequence_Size(obj);
> std::vector<T> *vec=new std::vector<T>(size);
> for(int i=0; i<size; ++i)
> (*vec)[i]=extract<T>(PySequence_GetItem(obj,i));
> return *vec;
> }
> };
>
> static std::string test_ext(std::vector<bool>& v)
> {
> std::string cout("[");
> for(int i=0; i<v.size(); ++i)
> cout += (v[i]? 'T': 'F') << ' ';
> cout += ']';
> //delete &v;
> return cout;
> }
>
> BOOST_PYTHON_MODULE(ext)
> {
> lvalue_from_pytype<vec_extractor<bool>,&PyTuple_Type>();
> lvalue_from_pytype<vec_extractor<bool>,&PyList_Type>();
>
> def("test", test_ext);
> }
Mike,
In this case your source Python object does not contain a vector, so
you should not expect to be able to extract a vector lvalue. You need
an rvalue from-python converter. Ralf' solutions in the FAQ are what
you want:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/*checkout*/boost/boost/libs/python/doc/v2/faq.html#question2
--
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