boost python returning tuple containing custom types

Viewed 3377

I have a C++ myObject class that I expose via boost python using a wrapper structure:

struct myObjectWrapper{
   static tuple compute(myObject& o,const Container& x0, const double& t0, Container& x){
      double t;
      int stat = o.evaluate(x0,t0,x,t);
      return make_tuple(stat,t);
   }
}

BOOST_PYTHON_MODULE(myModule)
{
   // not shown here is code to expose Container class

   class_<myObject>("MyObject")
      .def("compute",&myObjectWrapper::compute)
   ;
}

Container is currently defined as:

typedef std::valarray<double> Container

and is exposed to python.

Now in python I can do.

x = Container()
(status,t) = obj.compute(Container([0.,0.,0.]),0.0,x)
print status, t, x[0]

This is not very pythonic. I would prefer to do:

(status,t,x) = obj.compute(Container([0.,0.,0.]),0.0)
print status, t, x[0]    

I could write an additional wrapper in python, but I would prefer to avoid adding more wrappers.

The following code does't compile:

struct myObjectWrapper{
   static tuple compute(myObject& o,const Container& x0, const double& t0){
      double t;
      Container x;
      int stat = o.evaluate(x0,t0,x,t);
      return make_tuple(stat,t,x);
   }
}

Also I would prefer to steal the content of the local variable x and have python manage it rather than copy it:

return make_tuple(stat,t,std::move(x));

How do I achieve this?

1 Answers
Related