如何在包装包含向量的模板类时让SWIG应用模板?

SWi*_*ams 15 c# c++ swig templates vector

我正在尝试使用SWIG来包装(在C#中)一些包含模板类的c ++代码,该模板类本身包含了一个std::vector<T>.我在互联网上看到了关于如何为矢量类型声明模板的各种参考,但是无法使用额外的抽象层来实现.这就是我在interface.i文件中所做的事情,例如:

// 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>;
}
Run Code Online (Sandbox Code Playgroud)

虽然这似乎可以自己正确地创建相应的C#类型,但我尝试定义的std :: vector模板不会应用于内部使用它们的其他类,通过头文件包含.为了告诉你我的意思,有一个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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

即使我得到一个vector_MyType由SWIG创建的类,当它包装我的模板类时,它没有使用它,而是提供了很多这样的例子:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

请有人可以告诉SWIG我缺少什么来生成包装使用MyTypevector_MyType不是SWIGTYPE_p_myNamespace__std__vectorT_myNamespace__MyType_p_tSWIGTYPE_p_p_myNamespace__MyType

这与模板getVect()函数返回有什么关系T&,即我需要做什么不同的%template(myVectorInt) myVector<int>;情况,哪里int不是指针?

SWi*_*ams 0

最后我意识到我的错误,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 {
// using namespace std; // don't include this!
%template(myVectorMyType) myVector<MyType*>;
%template(myVectorInt) myVector<int>;
}
Run Code Online (Sandbox Code Playgroud)

这产生了这样的类:

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

其中底层vector_MyType是按照 std_vector.i 模板:

public class vector_MyType: global::System.IDisposable, global::System.Collections.IEnumerable
    , global::System.Collections.Generic.IList<MyType>
 {...}
Run Code Online (Sandbox Code Playgroud)