你用[ANY]尝试的想法由于几个原因而不起作用。虽然ANY可以在typemaps中使用,允许同一个typemap处理不同大小的数组,但这并不是你所能得到的。在
C的语法在那里也不是必需的。你不能写:int[4] bar() {
static int data[4];
return data;
}
或者:
^{pr2}$
在标准C中,最接近的是:int (*bar())[4] {
static int data[4] = {1,2,3,4};
return &data;
}
但包装起来并不容易。在
但是,可以使用%array_class来实现简单的解决方案,例如:%module test
%inline %{
struct foo {
int member;
};
struct foo *bar() {
struct foo *arr = malloc(sizeof(struct foo) * 4);
for (int i = 0; i < 4; ++i)
arr[i].member = i;
return arr;
}
%}
%include
%array_class(struct foo, fooArray);
我可以这样做:Python 3.2.3 (default, May 3 2012, 15:54:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> arr = test.fooArray.frompointer(test.bar())
>>> arr
>
>>> arr[0]
>
>>> arr[1]
>
>>> arr[2]
>
>>> arr[3]
>
>>>
我们可以做得更好(可能),通过注入代码来自动隐藏指向数组类型的指针,在SWIG看到bar()之前添加以下内容:%pythonappend bar() %{
# Wrap it automatically
val = fooArray.frompointer(val)
%}
所以你现在可以像这样使用它:Python 3.2.3 (default, May 3 2012, 15:54:42)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.bar()[1].member
1
>>> arr = test.bar()
>>> arr[3].member
3
你需要注意内存的所有权。到目前为止,在这些例子中,内存是泄漏的。您可以使用%newobject告诉SWIG内存由Python一方拥有,但这样会太早释放(一旦原始返回值不再被引用),因此您需要安排将原始值保留更长时间。这是一个完整的例子,它将原始指针保存在array类的实例中,以便在数组包装器本身保持一个引用:%module test
%pythonappend bar() %{
# Wrap it automatically
newval = fooArray.frompointer(val)
newval.ptr_retain = val
val = newval
%}
%newobject bar();
%inline %{
struct foo {
int member;
};
struct foo *bar() {
struct foo *arr = malloc(sizeof(struct foo) * 4);
for (int i = 0; i < 4; ++i)
arr[i].member = i;
return arr;
}
%}
%include
%array_class(struct foo, fooArray);
{d{d>在这个类中,你可以生成一个unbounditerate},但在这个类中,你可以生成一个unbounditerate。如果大小是真的固定的,或者你有一种知道大小的方法,你可以用一种更好的方式包装它(在我看来),写一个类型映射,返回一个PyList。这需要编写更多的工作,但在Python端使接口看起来更好。在