[C++-sig] overriding a C++ method
David Abrahams
dave at boost-consulting.com
Sun Jul 29 19:40:28 CEST 2007
on Wed Jul 25 2007, Francois Ostiguy <ostiguy-AT-fnal.gov> wrote:
> Hi-
>
> I need help with the following problem.
>
> Consider a c++ class
>
> class A {
> public:
> A();
> .....
> void do_something( std::vector<std::pair<double,int> >& arg);
> ...
> };
>
> wrapped as follows:
>
> class_< A, bases<Base>, boost::shared_ptr<A>( "A", init<>() )
> ...
> ;
>
> I want to wrap the method do_something, but I would like to avoid
> introducing a converter for std::vector<std::pair<double,int> >.
> Rather, I would like to be able to call do_something(..) in python
> using as an argument, a list of 2-tuples, which would be more natural.
void do_something(A& self, boost::python::object seq)
{
std::vector<std::pair<double,int> > arg;
for (
std::size_t i = 0
, len = boost::python::extract<std::size_t>(seq.attr("__len__"));
i < len;
++i)
{
arg.push_back(
std::make_pair(
boost::python::extract<double>(seq[i][0])
, boost::python::extract<int>(seq[i][1])
)
)
}
self.do_something(arg)
}
class_< A, bases<Base>, boost::shared_ptr<A>( "A", init<>() )
.def("do_something", do_something)
...
;
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
The Astoria Seminar ==> http://www.astoriaseminar.com
More information about the Cplusplus-sig
mailing list