Hi, I just started using boost.python, so please excuse if this is trivial. I have two questions, the first one is related to registering a converter for the std::pair<T1,T2> to a python tuple. I've learned that I cannot have bindings for c++ templates, so lets say that T1=T2=int. I'm a little bit lost on how to do this. I thought I had to do something like this: namespace boost { namespace python { template <class T1,class T2> struct to_python_value<std::pair<T1,T2> > { typedef typename add_reference< typename add_const<std::pair<T1,T2> >::type >::type argument_type; static bool convertible() {return true;} PyObject* operator()(argument_type x) const { PyObject* aux1 = to_python_value<T1>()(x.first); PyObject* aux2 = to_python_value<T2>()(x.second); return Py_BuildValue("(O,O)",aux1,aux2); } }; }; }; and then register this with the 'to_python_converter': to_python_converter<std::pair<int,int>, to_python_value<std::pair<int,int> > >(); but since the second parameter is supposed to have a convert() method, I guess this is not the way to do it. I could wrap the to_python_value struct in another struct with a convert method, but I have the feeling that I'm complicating things. How do I do this? ---- The next question is about std::set::iterator. I'm trying to come up with a front end in python to a library I've been working on focused on constraint logic programming. A central object in the library is derived from std::set and I would be very happy if my python users could also handle std::set::iterators, for efficiency reasons. I had no problems in converting std::set using pyste, but set::iterator seems more complicated. My first attempt was to try to export only the "std::_Rb_tree_const_iterator". I had to comment a ".def( *self )" line but it compiled and linked with no errors. However, when I import the module in python I get an undefined symbol. Next stop was to try to export all std::_Rb_tree_const_iterator class hierarchy (actually only the std::_Rb_tree_node base class), but still get the undefined symbol. I guess that the .def(*self) is a wrapper for the set::operator*, but this shouldn't be the cause for undefined symbol, or am I wrong? --- I've have read and re-read the tutorial and reference manual of boost.python, and searched the mailing list. I found the boost.python tutorial too basic for these questions and I'm having trouble in learning from the reference manual. Pointers to documentation somewhere between these two are very welcome. Thanks! ps: Please reply me directly since I'm not subscribed. -- Marco Correia <mvc@di.fct.unl.pt>
Hi, Thanks a lot for the replies. I was not subscribed (I have been following this thread on the mailing list archives), and therefore I'm not sure if this mail will be filed in the correct thread (I'm hoping that keeping the same subject line will do it). Ok, so here are my questions: - Will this solution work with a user defined type UserT, i.e. will it allow python users to handle pair<UserT,UserT> ? - If not, then why can't we just have a single 'convert_to_python' template which we would then specialize for user defined types, and use it in the convert function as I did in my previous post? - Where is the documentation for boost::python::incref ? I can't find it in the reference manual. thanks! -- Marco Correia <mvc@di.fct.unl.pt>
participants (1)
-
Marco Correia