I have a c++ class which exposes collections by providing functions returning ranges, using boost::range.
In order to export this class to python with boost::python, I use the function boost::python::range, which can accept two parameters: the member functions of the class returning the begin and end iterators of the collection.
I want to avoid writing begin/end pairs manually for each collection, as I already provide the range. But I cannot manage to write a wrapper over boost::python::range accepting as argument a member function returning a range. Any ideas? (I have in fact more than one class, which are templatized, so a template function taking as template parameter the address of a member function of a template class won’t work, my compiler said)
I will accept a c++0x solution if compilable with g++-4.6.
edit: a sample code as asked:
say I have this class:
struct A
{
std::vector<int> c;
typedef boost::sub_range<std::vector<int> > c_range;
c_range getc() { return c; }
};
To produce a Python iterator from the getc method, I now add these two member functions to class A:
c_range::iterator c_begin() { return getc().begin(); }
c_range::iterator c_end() { return getc().end(); }
and then expose them like this:
boost::python::class_<A>("A")
.def("getc", boost::python::range(&A::c_begin, &A::c_end));
Is there a way to write directly something like:
.def("getc", pyrange(&A::getc));
and to avoid having to write c_begin and c_end?
The solution was to use the more general form a
rangewith four template parameters: create begin/end accessors asboost::bind‘ed objects, and then specify the Target template parameter ofrange. For const iterators, this code fulfills my needs:Edit: a slightly more compact solution, by using a local struct inside function definition: