2016-12-13 83 views
2

我有一個C++類定義爲:boost.Python如何顯示boost :: shared_ptr的typedef?

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;

UPDATE

更改類暴露使用的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 

回答

3
+0

謝謝,但看到更新。 –

+0

@GiovanniAzua在關於抽象類包裝的文檔中有章節。你可能不得不把袖子捲起來並閱讀。我最初的懷疑是boost :: python目前假設它可以創建一個MyFuture,給定你創建的類定義。當然,它不能,因爲它是一個抽象類。必須有一種方法來註釋它,說沒有構造函數。 –

+0

@(理查德霍奇斯)夠公平的,我一直這樣做,但沒有明顯的這個案件的文件,一直在研究,因此OP。 –

相關問題