python - How to expose a function which returns a variant vector? -
i'm looking tiny working example of c++
module has method returns variant
vector
, exposes python
(it seems ancient problem, dates 2004, not find clear answers onwards). i've read this , this , more, still can not find solution. documentation seems throw reader tons of information right away. want tiny working example 1 tiny method returns 1 tiny vector. have now:
#include <boost/variant.hpp> #include <boost/python.hpp> #include <vector> using namespace std; using namespace boost:python typedef boost::variant<int> variant; typedef vector<variant> vector; class some{ private: int idx; public: some(...){ ... } //not important vector mymethod(){ return vector{1}; } }; boost_python_module(some){ class_<vector>("vector").def(vector_indexing_suite<vector, true>()); class_<some>("some", init ... //not important .def("mymethod",&some::mymethod); }
i have no problems compilation, linkage , usage of resulting shared library, if comment out method. however, if there such method returning vector, whole stack of errors. guess, need make steps, playing drums , doing other magic stuff (or data types conversion etc), not know steps.
you should write to_python_converter
variant
, should work:
#include <boost/python.hpp> #include <boost/python/suite/indexing/vector_indexing_suite.hpp> #include <python.h> #include <vector> #include <boost/variant.hpp> typedef boost::variant<int> number; typedef std::vector<number> vector; vector function() { return vector{1}; } struct number_to_object : boost::static_visitor<pyobject*> { static result_type convert(number const& v) { return apply_visitor(number_to_object(), v); } template<typename t> result_type operator () (t const& v) const { return boost::python::incref(boost::python::object(v).ptr()); } }; void init_module() {} boost_python_module(pyexc_test) { using namespace boost::python; class_<vector>("vector").def(vector_indexing_suite<vector, true>()); to_python_converter<number, number_to_object>(); implicitly_convertible<int, number>(); def("function", function); def("init_module", init_module); }
Comments
Post a Comment