Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get SWIG to automatically wrap an emulated "this" pointer to a C struct?

I've got a simple C "class" I have implemented, using function pointers in a struct to implement the member functions, and passing a pointer to the struct as the first argument to each function, similar to the implicit "this" pointer in 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);

Now my problem is, when the interface is created to whatever language I choose in the front end, I wind up having to explicitly pass the "this" pointer to the object, even though the language itself supports hiding this.

For instance, suppose I choose Python. I have to do something like this:

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

Where what I really want is to do it like this:

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

I know I could just write some wrapping code, but for my actual project I have a lot of functions in a lot of objects of existing C code, and multiplying this by every language that I want to support, this is just too much work! Is there some way to get SWIG to do this automatically?

like image 999
Michael Avatar asked Jun 13 '12 21:06

Michael


1 Answers

You can do this in a language neutral way in SWIG with just two typemaps provided you name the parameter something consistent in the SWIG interface as well as the definitions to allow the typemaps to be applied selectively. (Unless you wanted all pointers to mytest to become "this" pointers by default of course)

The typemaps you need are:

// 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;
}

The check typemap isn't really intended for use like this, but it's the easiest way to get the code to be injected after the arguments have been extracted from the target language and before the actual call is made.

You can also simplify the module with the help of a macro to avoid having to write and keep in sync the mapping between the function pointers and the members trick. I ended up with test.h as:

#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));
};

And the corresponding interface file (test.i):

%module test

%{
#include "test.h"

static int f1(mytest *me,int n) { return me->data + n; }
static void f2(mytest *me,int n) { me->data += n; }
%}

%extend mytest {
  mytest(int n) {
    $self->data = n;
    $self->func1 = f1;
    $self->func2 = f2;
  }
}

%typemap(in,numinputs=0) mytest *me "$1=NULL;"
%typemap(check) mytest *me {
  $1 = arg1;
}

%include "test.h"

(This interface file provides a constructor that "creates" the "object" exactly how a Java programmer would expect - you can call new and it sets the function pointers behind the scenes)

like image 50
Flexo Avatar answered Oct 31 '22 02:10

Flexo