C++ 从专用模板类型继承的类的Swig包装将导致缺少方法
我使用SWI1.4.1来封装C++程序中的一些C++代码。 考虑下面的代码,它在一个文件<强> fo.h < /强>C++ 从专用模板类型继承的类的Swig包装将导致缺少方法,c++,swig,C++,Swig,我使用SWI1.4.1来封装C++程序中的一些C++代码。 考虑下面的代码,它在一个文件 fo.h < /强> #ifndef __Foo__ #define __Foo__ namespace Ogre { template<int foo, typename T> class FooBase { public: int getFooBase() const { return 1;
#ifndef __Foo__
#define __Foo__
namespace Ogre
{
template<int foo, typename T> class FooBase
{
public:
int getFooBase() const
{
return 1;
}
};
struct FooSpecialized : public FooBase<4, double>
{
int getFooSpecialized() const
{
return 2;
}
};
class FooImpl : public FooSpecialized
{
public:
int getFooImpl() const
{
return 3;
}
};
}
#endif
因此,我可以访问方法getFooSpecialized()
和getFooImpl()
。但是缺少getFooBase()
编译Swig接口文件时,我没有收到任何警告
%include "Foo.h"
通过查看已生成的C#文件可以很容易地看到这一点。有一个文件FooSpecialized.cs,其中包含
public class FooSpecialized : global::System.IDisposable {
...
public int getFooFooSpecialized() {
int ret = OgrePINVOKE.FooSpecialized_getFooFooSpecialized(swigCPtr);
if (OgrePINVOKE.SWIGPendingException.Pending) throw OgrePINVOKE.SWIGPendingException.Retrieve();
return ret;
}
}
public class FooImpl : FooSpecialized {
...
public int getFooImpl() {
int ret = OgrePINVOKE.FooImpl_getFooImpl(swigCPtr);
if (OgrePINVOKE.SWIGPendingException.Pending) throw OgrePINVOKE.SWIGPendingException.Retrieve();
return ret;
}
}
和FooImpl.cs,其中包含
public class FooSpecialized : global::System.IDisposable {
...
public int getFooFooSpecialized() {
int ret = OgrePINVOKE.FooSpecialized_getFooFooSpecialized(swigCPtr);
if (OgrePINVOKE.SWIGPendingException.Pending) throw OgrePINVOKE.SWIGPendingException.Retrieve();
return ret;
}
}
public class FooImpl : FooSpecialized {
...
public int getFooImpl() {
int ret = OgrePINVOKE.FooImpl_getFooImpl(swigCPtr);
if (OgrePINVOKE.SWIGPendingException.Pending) throw OgrePINVOKE.SWIGPendingException.Retrieve();
return ret;
}
}
到目前为止,我尝试了%rename
和%template
,但不起作用。
我也读过关于分割文件的内容,但我没有选择,因为这是我正在使用的第三方组件 关键是
%模板的使用和位置。按原样使用Foo.h
,以下test.i
文件有效:
%module test
%{
#include "Foo.h" // Adds Foo.h to the generated code.
%}
// Declare to SWIG the FooBase class interface.
namespace Ogre
{
template<int foo, typename T> class FooBase
{
public:
int getFooBase() const;
};
// Now that SWIG knows the template, declare an instantiation of
// the template you want to use.
%template(FooBase4dbl) FooBase<4,double>;
// Declare the class interfaces that uses the template instance
struct FooSpecialized : public FooBase<4, double>
{
int getFooSpecialized() const;
};
class FooImpl : public FooSpecialized
{
public:
int getFooImpl() const;
};
}
谢谢你的回答。方法2对我来说没有选项,因为它是第三方头文件。如果我没弄错的话,方法1就是代码复制。看来没有别的办法了。
%module test
%{
#include "Foo.h"
%}
%include "FooBase.h"
%template(FooSpec) Ogre::FooBase<4,double>;
%include "Foo.h"
>>> import test
>>> f=test.FooImpl()
>>> f.getFooBase()
1