2015-04-17 113 views
15

我想使用SWIG來包裝(在C#中)一些C++代碼,其中包含一個模板類,它本身包裝std::vector<T>。我在互聯網上看到了各種關於如何爲矢量類型聲明模板的參考資料,但無法讓它與額外的抽象層一起工作。這是我在例如interface.i文件正在做的:如何在包裝包含向量的模板類時讓SWIG應用模板?

// interface.i file  
%module myNamespaceWrapper 
%{ 
#include "myVector.h" 
%} 

%include "myVector.h" 
%include "std_string.i" 
%include "std_vector.i" 

namespace std { 
%template(vector_MyType) vector<MyType*>; 
%template(vector_Int) vector<int>; 
} 

namespace myNamespace { 
%template(myVectorMyType) myVector<MyType*>; 
%template(myVectorInt) myVector<int>; 
} 

雖然這似乎是由自己正確地創建相應的C#類,沒有應用,我試圖定義的std ::矢量模板到其他內部使用它們的類,通過頭文件包含它們。爲了向你展示我的意思,有一個C++類,如:

// myVector.h 
namespace myNamespace 
{  
    template<class T> 
    class myVector 
    { 

    private : 
     std::vector<T> vect; 

    public : 
     myVector(){} 
     virtual ~myVector(){} 

     virtual const std::vector<T> & getVect() 
     { 
      return vect; 
     } 

     virtual T& front() 
     { 
      return vect.front(); 
     } 
    } 
} 

即使我得到痛飲,創造了vector_MyType類,當它圍繞我的模板類是不使用它,而是讓許多像這樣的例子:

public class myVectorMyType : global::System.IDisposable { 

    public virtual SWIGTYPE_p_p_myNamespace__MyType front() { 
     SWIGTYPE_p_p_myNamespace__MyType ret = new SWIGTYPE_p_p_myNamespace__MyType(myNamespaceWrapperPINVOKE.myVectorMyType_front__SWIG_0(swigCPtr), false); 
     return ret; 
    } 

    public virtual SWIGTYPE_p_myNamespace__std__vectorT_myNamespace__MyType_p_t getVect() { 
     SWIGTYPE_p_myNamespace__std__vectorT_myNamespace__MyType_p_t ret = new SWIGTYPE_p_myNamespace__std__vectorT_myNamespace__MyType_p_t(myNamespaceWrapperPINVOKE.myVectorMyType_getVect(swigCPtr), false); 
     return ret; 
    } 
} 

可以請別人指教一下我失蹤痛飲生成使用MyTypevector_MyType代替SWIGTYPE_p_myNamespace__std__vectorT_myNamespace__MyType_p_tSWIGTYPE_p_p_myNamespace__MyType包裝?

這是什麼與模板getVect()函數返回T&,即我需要爲%template(myVectorInt) myVector<int>;情況做不同的事情,其中​​int不是一個指針嗎?

+2

是否使用的是SWIG的版本?我試着用2.0.11和3.0.5的例子以及正確生成的C#源代碼,例如:'public virtual vector_MyType getVect(){vector_MyType ret = new vector_MyType(myNamespaceWrapperPINVOKE.myVectorMyType_getVect(swigCPtr),false); return ret; }' –

+0

謝謝你試用這個!我也使用3.0.5。既然你知道它的工作原理,我就能夠用我的代碼找到問題。這與我提出的問題並不完全相同,實際上我在'namespace myNamespace {}'部分有一個額外的'using namespace std;'引起了這個問題。刪除它,它按預期工作。 – SWilliams

+0

小心修復問題中的示例並將其寫爲答案? – Flexo

回答

0

最終我意識到自己的錯誤,並痛飲生成的類如預期。

// interface.i file  
%module myNamespaceWrapper 
%{ 
#include "myVector.h" 
%} 

%include "myVector.h" 
%include "std_string.i" 
%include "std_vector.i" 

namespace std { 
%template(vector_MyType) vector<MyType*>; 
%template(vector_Int) vector<int>; 
} 

namespace myNamespace { 
// using namespace std; // don't include this! 
%template(myVectorMyType) myVector<MyType*>; 
%template(myVectorInt) myVector<int>; 
} 

這產生類,如:

public class myVectorMyType : global::System.IDisposable { 
    private global::System.Runtime.InteropServices.HandleRef swigCPtr; 
    protected bool swigCMemOwn; 

    ... 

    public virtual vector_MyType getVect() { 
    vector_MyType ret = new vector_MyType(myNamespaceWrapperPINVOKE.myVectorMyType_getVect(swigCPtr), false); 
    return ret; 
    } 
} 

其中基礎vector_MyType是每std_vector.i模板:

public class vector_MyType: global::System.IDisposable, global::System.Collections.IEnumerable 
    , global::System.Collections.Generic.IList<MyType> 
{...} 
0

我會建議避免這個問題,只需創建包裝您需要的每個實例:

class wrap_int : public temp <int> 
{ 
};