boost.Python 如何公开 boost::shared_ptr 的 typedef?
boost.Python How to expose typedef of boost::shared_ptr?
我有一个 C++ class 定义为:
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 将 class 公开给 Python,因为(此 class 从未在 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 公开更改为:
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
class_<MyFuture, boost::shared_ptr<MyFuture>, boost::noncopyable>("MyFuture")
根据文档
我有一个 C++ class 定义为:
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 将 class 公开给 Python,因为(此 class 从未在 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 公开更改为:
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
class_<MyFuture, boost::shared_ptr<MyFuture>, boost::noncopyable>("MyFuture")
根据文档