简体   繁体   中英

boost.Python How to expose typedef of boost::shared_ptr?

I have a C++ class defined as:

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_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:

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:

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; ?

UPDATE

Changing the class expose using boost.Python to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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