【发布时间】:2016-12-13 09:54:31
【问题描述】:
我有一个 C++ 类定义为:
class MyFuture {
public:
virtual bool isDone() = 0;
virtual const std::string& get() = 0;
virtual void onDone(MyCallBack& callBack) = 0;
virtual ~MyFuture() { /* empty */ }
};
typedef boost::shared_ptr<MyFuture> MyFuturePtr;
我使用 boost.python 向 Python 公开该类(该类从未在 Python 中创建,而是从 API 调用返回,因此返回 noncopyable):
BOOST_PYTHON_MODULE(MySDK)
{
class_<MyFuture, noncopyable>("MyFuture", no_init)
.def("isDone", &MyFuture::isDone)
.def("get", &MyFuture::get, return_value_policy<copy_const_reference>())
.def("onDone", &MyFuture::onDone)
;
}
在 python 中我像这样使用它:
import MySDK
def main():
# more code here ...
future = session.submit()
response = future.get
print response
if __name__ == "__main__":
main()
但这会导致 Python 错误:
File "main.py", line 14, in main
future = session.submit()
TypeError: No to_python (by-value) converter found for C++ type: class boost::shared_ptr<class MySDK::MyFuture>
我怎样才能暴露 typedef typedef boost::shared_ptr<MyFuture> MyFuturePtr;?
更新
将使用 boost.Python 的类公开更改为:
class_<MyFuture, boost::shared_ptr<MyFuture> >("MyFuture", no_init)
导致编译错误:
boost\python\converter\as_to_python_function.hpp(21):
error C2259: 'MySDK::MyFuture' : cannot instantiate abstract class
【问题讨论】:
标签: c++ boost boost-python