使用 boost python 传递函数引用
我在 C++ 中有这样的函数:
typedef boost::function<boost::shared_ptr<Object> (CL_DomElement*, std::string& desc)> Parser;
void registerParser(std::string type, Parser p);
// Later: exporting into python-module:
BOOST_PYTHON_MODULE(TypesManager)
{
bp::def("RegisterParser", registerParser);
}
# Python code:
class TestObj(Object):
@staticmethod
def ParseTestObj(node, desc):
pass
RegisterParser("test_obj", TestObj.ParseTestObj)
python 代码中的对象是导出类,在 typedef 中使用(来自 C++ 代码)。
Boost.Python.ArgumentError: Python argument types in
RegisterParser(str, function)
did not match C++ signature:
RegisterParser(TypesManager {lvalue}, std::string, boost::function<boost::shared_ptr<Object> ()(CL_DomElement*, std::string&)>)
我做错了什么?
I have such function in C++:
typedef boost::function<boost::shared_ptr<Object> (CL_DomElement*, std::string& desc)> Parser;
void registerParser(std::string type, Parser p);
// Later: exporting into python-module:
BOOST_PYTHON_MODULE(TypesManager)
{
bp::def("RegisterParser", registerParser);
}
# Python code:
class TestObj(Object):
@staticmethod
def ParseTestObj(node, desc):
pass
RegisterParser("test_obj", TestObj.ParseTestObj)
Object in python-code is exported class which is used in typedef (from c++ code).
Boost.Python.ArgumentError: Python argument types in
RegisterParser(str, function)
did not match C++ signature:
RegisterParser(TypesManager {lvalue}, std::string, boost::function<boost::shared_ptr<Object> ()(CL_DomElement*, std::string&)>)
What I do wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不相信 Boost Python 理解如何将 python 函数转换为 boost::function 对象。我建议是使用代理来获取 python 可调用对象并模仿 C++ 接口。快速示例模型(当然未经测试):
I don't believe Boost Python understands how to convert a python function to a boost::function object. What I would suggest is to use a proxy to take the python callable object and mimic the C++ interface. Quick example mock-up (untested, of course):