Swigging 作为模板函数的 class 成员函数
Swigging a class member function that is a template function
此问题基于以下问题:How to instantiate a template method of a template class with swig?。
然而,与那个问题相比,我试图包装的代码有点不同:
class MyClass {
public:
template <class T>
void f1(const string& firstArg, const T& value);
};
MyClass 是一个普通的 C++ class,有一个模板函数 f1.
尝试包装 MyClass::f1:,即 Swig .i 文件
%template(f1String) MyClass::f1<std::string>;
有了以上,一个Python客户端就可以做到
o = MyClass
str1 = "A String"
o.f1String("", str1)
此接口要求 Python 客户端了解所有不同的 f1 函数名称,每个名称因类型而异。不太干净。
可以通过在接口文件中重载、扩展来获得更清晰的接口,例如
%extend MyClass {
void f1(const string& s, const string& s1){
$self->f1(s, s1);
}
void f1(const string& s, const int& anInt){
$self->f1(s, anInt);
}
}
这允许这样的客户端代码:
o = MyClass
str1 = "A String"
anInt = 34
o.f1("", str1)
o.f1("", anInt)
问题是,有没有办法通过Swig获得上面的接口(通过扩展),不扩展,使用Swig?
幸运的是,Python 包装器支持重载,因此您可以简单地用相同的名称实例化这两个方法,SWIG 会在 运行 时施展魔法来解析重载。有关详细信息,请参阅文档“SWIG 和 C++”一章中的 6.18 Templates。
test.i
%module example
%{
#include<iostream>
class MyClass {
public:
template <class T>
void f1(const std::string& firstArg, const T& value) {
std::cout << firstArg << ',' << value << '\n';
}
};
%}
%include <std_string.i>
class MyClass {
public:
template <class T>
void f1(const std::string& firstArg, const T& value);
};
%extend MyClass {
%template(f1) f1<std::string>;
%template(f1) f1<int>;
}
test.py
from example import *
o = MyClass()
str1 = "A String"
anInt = 34
o.f1("X", str1)
o.f1("Y", anInt)
要编译和 运行 的示例工作流程:
$ swig -python -c++ test.i
$ g++ -Wall -Wextra -Wpedantic -I /usr/include/python2.7/ -fPIC -shared test_wrap.cxx -o _example.so -lpython2.7
$ python2.7 test.py
X,A String
Y,34
此问题基于以下问题:How to instantiate a template method of a template class with swig?。
然而,与那个问题相比,我试图包装的代码有点不同:
class MyClass {
public:
template <class T>
void f1(const string& firstArg, const T& value);
};
MyClass 是一个普通的 C++ class,有一个模板函数 f1.
尝试包装 MyClass::f1:,即 Swig .i 文件
%template(f1String) MyClass::f1<std::string>;
有了以上,一个Python客户端就可以做到
o = MyClass
str1 = "A String"
o.f1String("", str1)
此接口要求 Python 客户端了解所有不同的 f1 函数名称,每个名称因类型而异。不太干净。
可以通过在接口文件中重载、扩展来获得更清晰的接口,例如
%extend MyClass {
void f1(const string& s, const string& s1){
$self->f1(s, s1);
}
void f1(const string& s, const int& anInt){
$self->f1(s, anInt);
}
}
这允许这样的客户端代码:
o = MyClass
str1 = "A String"
anInt = 34
o.f1("", str1)
o.f1("", anInt)
问题是,有没有办法通过Swig获得上面的接口(通过扩展),不扩展,使用Swig?
幸运的是,Python 包装器支持重载,因此您可以简单地用相同的名称实例化这两个方法,SWIG 会在 运行 时施展魔法来解析重载。有关详细信息,请参阅文档“SWIG 和 C++”一章中的 6.18 Templates。
test.i
%module example
%{
#include<iostream>
class MyClass {
public:
template <class T>
void f1(const std::string& firstArg, const T& value) {
std::cout << firstArg << ',' << value << '\n';
}
};
%}
%include <std_string.i>
class MyClass {
public:
template <class T>
void f1(const std::string& firstArg, const T& value);
};
%extend MyClass {
%template(f1) f1<std::string>;
%template(f1) f1<int>;
}
test.py
from example import *
o = MyClass()
str1 = "A String"
anInt = 34
o.f1("X", str1)
o.f1("Y", anInt)
要编译和 运行 的示例工作流程:
$ swig -python -c++ test.i
$ g++ -Wall -Wextra -Wpedantic -I /usr/include/python2.7/ -fPIC -shared test_wrap.cxx -o _example.so -lpython2.7
$ python2.7 test.py
X,A String
Y,34