用Swig包装专用的c ++模板类 [英] wrapping specialised c++ template class with swig

查看:75
本文介绍了用Swig包装专用的c ++模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下类声明:

 命名空间X {
template< class T>
Foo类{
public:
Foo();
虚拟〜Foo();

T value()const;
};

template< class T>
类Foo< vector< T> {
public:
Foo();
虚拟〜Foo();

T值(const int)const;
};
}

对于他们,我在foo.i文件中具有以下声明

 %include stl.i 
%include std_string.i
%include std_vector.i

命名空间X {
using namespace std;

template< class T>
Foo类{
public:
Foo();
虚拟〜Foo();
T value()const;
};

template< class T>
类Foo< vector< T> > {
public:
Foo();
虚拟〜Foo();
T值(const int)const;
};

%template(FooInt)Foo< int> ;;
%template(FooString)Foo< string> ;;
%template(FooVectorInt)Foo< vector< int> > ;;

}

这两个类之间的区别是后者的专业化向量容器 value()方法的不同签名,其中第一个不带任何参数,而第二个带一个期望的整数。



swig打包的包装器代码包装了%template(FooVectorInt)错误,因为它调用了 value()方法,而不是专用矢量方法 value(const int)。给我以下编译错误消息:

  foo_wrap.cxx:in function»int _wrap_FooVectorInt_value(lua_State *)«:

/home/noobsaibot/foo/bindings/src/lua/foo_wrap.cxx:6706:81:错误:没有匹配的函数可以调用»X :: Foo< std :: vector< int> > :: value()const«
/home/noobsaibot/foo/src/foo.h:78:5:注意:候选对象是:T X :: Foo< std :: vector< _RealType> > :: value(int)const [with T = int,int = unsigned int]

任何关于可能使Swig理解哪个功能正确的建议?



欢呼声

解决方案

您可以通过执行以下操作来获得所需的结果:

 %include stl.i  
%include std_string.i
%include std_vector.i

命名空间X {
使用命名空间std;

%rename(FooVectorInt)Foo< std :: vector< int> > ;;

类Foo< std :: vector< int> > {
public:
virtual〜Foo();
int value(const int)const;
};

template< class T>
Foo类{
public:
Foo();
虚拟〜Foo();
T value()const;
};

%template(FooInt)Foo< int> ;;
%template(FooString)Foo< string> ;;
}

之所以有效,是因为您在接口文件中写的不是 C ++,最重要的是SWIG生成了正确的代码。如果您想重复很多,可以编写宏(无论如何,它都接近%template c)。



这仍然不是一个很干净的解决方案-我希望它可以与专业化结合使用,而且我也看不到任何更简单的解决方法。

consider the following class declarations:

namespace X {
template<class T>
class Foo {
public:
    Foo();
    virtual ~Foo();

    T value() const;
};

template<class T>
class Foo<vector<T>> {
public:
    Foo();
    virtual ~Foo();

    T value( const int ) const;
};
}

for them i have the following declaration in the foo.i file

%include "stl.i"
%include "std_string.i"
%include "std_vector.i"

namespace X {
using namespace std;

template<class T>
class Foo {
public:
    Foo();
    virtual ~Foo();
    T value() const;
};

template<class T>
class Foo<vector<T> > {
public:
    Foo();
    virtual ~Foo();
    T value( const int ) const;
};

%template(FooInt) Foo<int>;
%template(FooString) Foo<string>;
%template(FooVectorInt) Foo<vector<int> >;

}

the difference between the two classes is the specialisation of the later to vector container and the differrent signature of the value() method, where the first takes no arguments, while the second takes an expects an integer.

the wrapper code put together by swig wrapps the %template(FooVectorInt) wrong, in that it calls the value() method instead that of the specialised vector method value(const int). giving me the following compile error message:

foo_wrap.cxx: in function »int _wrap_FooVectorInt_value(lua_State*)«:

/home/noobsaibot/foo/bindings/src/lua/foo_wrap.cxx:6706:81: error: no matching function to call »X::Foo<std::vector<int> >::value() const«
/home/noobsaibot/foo/src/foo.h:78:5: note: candidate is: T X::Foo<std::vector<_RealType> >::value(int) const [with T = int, int = unsigned int]

any advices as to what i might be missing to make swig understand which function is the correct one?

cheers

解决方案

You can achieve the result you want by doing:

%include "stl.i"
%include "std_string.i"
%include "std_vector.i"

namespace X {
using namespace std;

%rename(FooVectorInt) Foo<std::vector<int> >;

class Foo<std::vector<int> > {
public:
    virtual ~Foo();
    int value( const int ) const;
};

template<class T>
class Foo {
public:
    Foo();
    virtual ~Foo();
    T value() const;
};

%template(FooInt) Foo<int>;
%template(FooString) Foo<string>;
}

This works because what you write in the interface file isn't C++ and all that matters is that the correct code is generated by SWIG. If you want to repeat this lots you can write macros (which is close to what %template is anyway).

Still this isn't a very clean solution - I expected this to "just work" with the specialisations and I can't see a simpler workaround either.

这篇关于用Swig包装专用的c ++模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆