Reputation: 14307
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
Upvotes: 2
Views: 347
Reputation: 69892
class_<MyFuture, boost::shared_ptr<MyFuture>, boost::noncopyable>("MyFuture")
as per the docs
Upvotes: 3