Wednesday, August 27, 2014

How to handle pure virtuals with optional argumets in Boost::Python

Let's assume we have following C++ snippet:


class A
{
public:
  virtual void Method1(const char* par1, IInterface* p = 0) = 0;
};
How to handle default value for paremeter p? 
My first idea was using BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS as per boost doc.

class AWrap: public A, public wrapper<A>
{
public:
  void Method1(const char* par1, IInterface* p = 0){ this->get_override("Method1")();}
};

BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(method_overload, IInterface::onRequestCompleted, 1, 2)

class_<AWrap>("A")
  .def("Method1", &A::Method1, method_overload())
   ;

This would solve the optionality of the parameter, hovewer how to pass any default value? So I've ended up using args:

class_<AWrap>("A")
  .def("Method1", &A::Method1, (arg("par1"), arg("p") = 0))
   ;

No comments:

Post a Comment