2011-04-04 77 views
2

編輯:解決,我的錯誤;在我的答案中解釋。在SWIG for Python中包裝std :: boost :: shared_ptr的向量

我有這樣的:

std::vector < boost::shared_ptr <Entity> > entities; 

我嘗試通過痛飲這樣的揭露它:

%include "boost_shared_ptr.i" 
%include "std_vector.i" 

%shared_ptr(Entity) 
%include <Entity.h> 

namespace std { 
    %template(EntityVector) vector<boost::shared_ptr<Entity> >; 
}; 

%include <TheFileWithEntities.h> 

然而,在Python實體結束是一個元組:

import MyModule 
print type(MyModule.cvar.entities) 
# Output: (type 'tuple') 

我已經谷歌搜索這個,但找不到任何具體的例子如何包裝這個。一頁給出了一個用於包裝C#的小例子,但對我來說並沒有幫助。

任何幫助,非常感謝。

回答

0

痛飲似乎型STD的全局變量::載體包裝成元組。解決方案是將實體移動到一個類中,並通過該類的一個實例訪問它。例如:

class Globals 
{ 
public: 
    std::vector < boost::shared_ptr <Entity> > entities; 
}; 

extern Globals globals; 
0
+0

感謝您的迴應,但正如您在我的問題中所看到的,我已經知道如何包裝std :: vector。問題是包裝一個boost :: shared_ptr的std :: vector。 SWIG似乎將它暴露爲一個元組,因爲它無法找出它的shared_ptr部分。 – morrog 2011-04-04 21:32:34

+0

您是否看到過這樣的內容:http://www.swig.org/Doc1.3/SWIGPlus.html#SWIGPlus_nn34 – sehe 2011-04-04 21:35:14

+0

是的,謝謝,我已閱讀過,但沒有看到任何相關內容。再次,我可以包裝boost :: shared_ptr,我可以包裝std :: vector,並按預期工作。這個問題具體是std :: vector >,它在Python中顯示爲一個元組。 Python可以訪問元組,查看其中的內容,操縱元組內的單個實體,但不能追加或修改它,因爲它是一個元組。 – morrog 2011-04-05 01:22:47

3

我遇到了一些困難得到指針對象的Python的序列自動轉換爲指針對象的std::vector。我目前(卡住)使用Swig 1.3;因人而異,如果你正在使用痛飲2.訣竅是在痛飲接口文件實例(與%template)不只是載體,而不僅僅是對象,但指針對象也:

%include "std_vector.i" 
%template(myObjectT) namespace::of::myObject<T>; 
%template(myObjectPtrT) boost::shared_ptr<namespace::of::myObject<T> >; 
%template(myObjectVectorT) std::vector<boost::shared_ptr<namespace::of::myObject<T> > >; 

沒有myObjectPtrT ,Swig似乎沒有足夠的知識將足夠的Python指針序列轉換爲myObjectTmyObjectVectorT

更新:出於某種原因,我還沒有弄清楚,這導致無法從調用myObjectT上的方法,即使我也使用了SWIG_SHARED_PTR(myObjectT, myObject<T>)

+0

謝謝你,這對我來說! – 2016-10-22 14:21:49

相關問題