我正在尝试使用 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();
}
}
}
尽管我得到了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;
}
}
请有人告诉我我缺少什么来使用 SWIG 生成包装器MyType
and vector_MyType
代替SWIGTYPE_p_myNamespace__std__vectorT_myNamespace__MyType_p_t
and SWIGTYPE_p_p_myNamespace__MyType
?
这与模板 getVect() 函数返回有什么关系吗T&
,即我是否需要做一些不同的事情%template(myVectorInt) myVector<int>;
情况,其中int
不是指针吗?