boost.Python如何公开boost :: shared_ptr的typedef?

时间:2016-12-13 09:54:32

标签: c++ boost boost-python

我将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

1 个答案:

答案 0 :(得分:3)