【问题标题】:How to get SWIG to apply templates when wrapping a template class containing vectors?包装包含向量的模板类时如何让 SWIG 应用模板?
【发布时间】:2015-06-24 07:47:57
【问题描述】:

我正在尝试使用 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>;
}

虽然这似乎可以自行正确创建相应的 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();
        }
    }
}

即使我得到了一个由 SWIG 创建的 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;
    }
}

请有人建议我缺少什么让 SWIG 使用 MyTypevector_MyType 而不是 SWIGTYPE_p_myNamespace__std__vectorT_myNamespace__MyType_p_tSWIGTYPE_p_p_myNamespace__MyType 生成包装器?

这是否与返回 T&amp; 的模板 getVect() 函数有关,即对于 %template(myVectorInt) myVector&lt;int&gt;; 的情况,我是否需要做任何不同的事情,其中​​ int 不是指针?

【问题讨论】:

  • 您使用的是哪个版本的 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; }
  • 感谢您试用!我也在使用 3.0.5。既然你让它工作了,我就能找到我的代码的问题。这与我提出的问题并不完全一样,实际上我在 namespace myNamespace {} 部分中有一个额外的 using namespace std; 导致了问题。删除它,它按预期工作。
  • 想修正问题中的示例并将其写下来作为答案?
  • 您是否考虑过使用 C++/CLI 而不是 SWIG 来与 C# 代码进行本机集成?
  • 我确实考虑过这一点,但就我而言,C++ 可能会发生变化。更改接口文件并重新运行 SWIG 似乎比更改 C++/CLI 代码更容易,所以我同意了。

标签: c# c++ templates vector swig


【解决方案1】:

我建议避免这个问题,只为您需要的每个实例创建包装器:

class wrap_int : public temp <int>
{
};

【讨论】:

    【解决方案2】:

    最后我意识到了我的错误,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>;
    }
    

    这产生了如下类:

    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>
     {...}
    

    【讨论】:

      猜你喜欢
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多