You can do this in a language-neutral language in SWIG with only two types, provided that you specify a parameter that will be consistent in the SWIG interface, as well as definitions that allow selective use of the selective application. (If you don't want all pointers to mytest
become the "this" pointers by default)
Required file types:
A sample validation card is not really intended to be used in this way, but it is the easiest way to get the input code after the arguments have been extracted from the target language and before the actual call.
You can also simplify the module with a macro to avoid having to write and synchronize the mapping between function pointers and member tricks. 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 as a Java programmer would expect - you can call new
, and it sets pointers to functions behind the scenes)
source share