I'm writing a program in python which should be able to pass "callables" which are registered in a C++ class. So far I've written the following code:
C++:
class MyClass
{
...
public:
register_callback(boost::function<void (int)> fun);
};
Python/C API:
class_<MyClass, boost::shared_ptr<MyClass>, boost::noncopyable>("MyClass", no_init)
.def("register_callback", &MyClass::register_callback);
The code compiles but when I call the method from python, passing a callable (in my case, another instance method) I get the following runtime error:
Boost.Python.ArgumentError: Python argument types in
MyClass.register_callback(MyClass, instancemethod)
did not match C++ signature:
register_callback(MyClass {lvalue}, boost::function<void ()(int)>)
I think I need a way to tell boost he can safety pass a callable every time a boost::function is required. Everything works if I use an hand-made solution:
void my_caller(MyClass* obj, object callable)
{
obj->register_callback(callable); // Works!
}
class_<MyClass, boost::shared_ptr<MyClass>, boost::noncopyable>("MyClass", no_init)
.def("register_callback", &my_caller);
Since I only have a few register functions like this I could stick with the hand-made solution (with macros to help), but I wonder how can I tell to boost::python to make the conversion automatically. Looking at the documentation I found the to_python_converter directive which, quite ironically, does exactly the contrary of what I need...