简体   繁体   English

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

[英]boost.Python How to expose typedef of boost::shared_ptr?

I have a C++ class defined as: 我有一个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;

I expose the class to Python using boost.python as (this class is never created in Python but returned from an API call and thus the noncopyable ): 我使用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)
    ;
}

From python I use it like: 从python,我像这样使用它:

import MySDK

def main():
    # more code here ...
    future = session.submit()
    response = future.get
    print response

if __name__ == "__main__":
    main()      

but this leads to the Python error: 但这导致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>     

How can I expose the typedef typedef boost::shared_ptr<MyFuture> MyFuturePtr; 我该如何公开typedef typedef boost::shared_ptr<MyFuture> MyFuturePtr; ?

UPDATE UPDATE

Changing the class expose using boost.Python to: 使用boost.Python将类公开更改为:

class_<MyFuture, boost::shared_ptr<MyFuture> >("MyFuture", no_init)

leads to the compiler error: 导致编译器错误:

boost\python\converter\as_to_python_function.hpp(21): 
    error C2259: 'MySDK::MyFuture' : cannot instantiate abstract class

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM