给定一个接口文件,如:
%module test
%{
#include <vector>
%}
%include "std_vector.i"
%template(DoubleVector) std::vector<double>;
更多的功能添加到DoubleVector
最简单的方法是使用%extend
把它写在C++中,SWIG接口文件:
%extend std::vector<double> {
void bar() {
// don't for get to #include <iostream> where you include vector:
std::cout << "Hello from bar" << std::endl;
}
}
这有它的优势在于它适用于您使用SWIG定位的任何语言,而不仅仅是Python。
你也可以做到这一点使用%pythoncode
和unbound function:
%pythoncode %{
def foo (self):
print "Hello from foo"
DoubleVector.foo = foo
%}
实例运行以下命令:
Python 2.6.7 (r267:88850, Aug 11 2011, 12:16:10)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> d = test.DoubleVector()
>>> d.foo()
Hello from foo
>>> d.bar()
Hello from bar
>>>
我假设你的意思是'%模板(DoubleVector)载体;'? –
Flexo
2012-01-12 16:41:15
雅,我很抱歉,我的意思%模板(DoubleVector)载体;只要。 谢谢:) –
Saurabh
2012-01-12 19:07:59