如何让SWIG自动包装模拟的“文件”;这";指向C结构的指针? 我已经实现了一个简单的C类,使用Stult中的函数指针来实现成员函数,并将一个指针作为结构的第一个参数传递给每个函数,类似于C++中的隐式“这个”指针。 %module mytest %{ typedef struct mytest mytest; struct mytest { int data; int (*func1)(mytest *,int); void (*func2)(mytest *,int); }; int f1(mytest *me,int n) { return me->data + n; } void f2(mytest *me,int n) { me->data += n; } mytest *mytestNew(int n) { mytest *me = (mytest*) malloc(sizeof(mytest)); me->data = n; me->func1 = f1; me->func2 = f2; return me; } %} typedef struct mytest mytest; struct mytest { int data; int func1(mytest *,int); void func2(mytest *,int); }; extern mytest *mytestNew(int n);

如何让SWIG自动包装模拟的“文件”;这";指向C结构的指针? 我已经实现了一个简单的C类,使用Stult中的函数指针来实现成员函数,并将一个指针作为结构的第一个参数传递给每个函数,类似于C++中的隐式“这个”指针。 %module mytest %{ typedef struct mytest mytest; struct mytest { int data; int (*func1)(mytest *,int); void (*func2)(mytest *,int); }; int f1(mytest *me,int n) { return me->data + n; } void f2(mytest *me,int n) { me->data += n; } mytest *mytestNew(int n) { mytest *me = (mytest*) malloc(sizeof(mytest)); me->data = n; me->func1 = f1; me->func2 = f2; return me; } %} typedef struct mytest mytest; struct mytest { int data; int func1(mytest *,int); void func2(mytest *,int); }; extern mytest *mytestNew(int n);,c,pointers,struct,this,swig,C,Pointers,Struct,This,Swig,现在我的问题是,当接口被创建为我在前端选择的任何语言时,我最终不得不显式地将“this”指针传递给对象,即使该语言本身支持隐藏它 例如,假设我选择Python。我必须这样做: from mytest import * m = mytestNew(1) m.func1(m,0) from mytest import * m = mytestNew(1) m.func1(0) 我真正想要的是这样做: from mytest import * m = mytestNew(1) m.func1(m,

现在我的问题是,当接口被创建为我在前端选择的任何语言时,我最终不得不显式地将“this”指针传递给对象,即使该语言本身支持隐藏它

例如,假设我选择Python。我必须这样做:

from mytest import *
m = mytestNew(1)
m.func1(m,0)
from mytest import *
m = mytestNew(1)
m.func1(0)
我真正想要的是这样做:

from mytest import *
m = mytestNew(1)
m.func1(m,0)
from mytest import *
m = mytestNew(1)
m.func1(0)

我知道我可以写一些包装代码,但对于我的实际项目,我在现有C代码的许多对象中有很多函数,将这些函数乘以我想要支持的每种语言,这实在是太多的工作了!有没有办法让SWIG自动执行此操作?

您可以在SWIG中以语言中立的方式执行此操作,只需使用两个类型映射,前提是您在SWIG界面中为参数命名一致的名称,以及允许有选择地应用类型映射的定义。(当然,除非您希望所有指向
mytest
的指针默认成为“this”指针)

您需要的类型映射是:

// Make sure the wraqpped function doesn't expect an input for this:
%typemap(in,numinputs=0) mytest *me "$1=NULL;"
// Slightly abuse check typemap, but it needs to happen after the rest of the arguments have been set:
%typemap(check) mytest *me {
  $1 = arg1;
}
check-typemap实际上不适合这样使用,但它是在从目标语言提取参数之后,在实际调用之前,获取要注入的代码的最简单方法

还可以借助宏简化模块,以避免编写函数指针和成员之间的映射并保持同步。我以
test.h
作为结束语:

#ifdef SWIG
#define MEMBER(name, args) name args
#else
#define MEMBER(name, args) (*name) args
#endif

typedef struct mytest mytest;

struct mytest {
  int data;
  int  MEMBER(func1,(mytest *me,int));
  void MEMBER(func2,(mytest *me,int));
};
以及相应的接口文件(test.i):


(此接口文件提供了一个构造函数,它“创建”了Java程序员所期望的“对象”-您可以调用
new
,并在幕后设置函数指针)

顺便说一句,您的示例泄漏了-您希望使用
%newobject mytestNew
来避免这种情况。出于好奇,有没有理由不使用C++在第一位?谢谢,这就行了!我花了一点时间才弄明白,但是“mytest*me”必须与我使用的任何类的第一个参数匹配。此外,ifdef可以简化一点:我使用“(*name)”和“name”,然后使用类似“int-MEMBER(func1)(mytest*me,int);“您可以使它仅与类型匹配,而不是与参数的名称匹配,但我有点假设,在某个时候,您可能希望将一个参数传递到一个不是“this”的函数中。”指针,然后您就可以开始为每种情况编写特殊的类型映射,与使用一致的名称相比,这种情况并不理想。