diff --git a/Examples/perl5/funcptr/Makefile b/Examples/perl5/funcptr/Makefile new file mode 100644 index 000000000..e5944f9af --- /dev/null +++ b/Examples/perl5/funcptr/Makefile @@ -0,0 +1,18 @@ +TOP = ../.. +SWIG = $(TOP)/../swig +SRCS = example.c +TARGET = example +INTERFACE = example.i +SWIGOPT = +all:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' perl5 + +static:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='myperl' INTERFACE='$(INTERFACE)' perl5_static + +clean:: + rm -f *_wrap* *.o core *~ *.so *.pm myperl + +check: all diff --git a/Examples/perl5/funcptr/example.c b/Examples/perl5/funcptr/example.c new file mode 100644 index 000000000..99583b72e --- /dev/null +++ b/Examples/perl5/funcptr/example.c @@ -0,0 +1,17 @@ +/* File : example.c */ + +int do_op(int a, int b, int (*op)(int,int)) { + return (*op)(a,b); +} + +int add(int a, int b) { + return a+b; +} + +int sub(int a, int b) { + return a-b; +} + +int mul(int a, int b) { + return a*b; +} diff --git a/Examples/perl5/funcptr/example.h b/Examples/perl5/funcptr/example.h new file mode 100644 index 000000000..58989db79 --- /dev/null +++ b/Examples/perl5/funcptr/example.h @@ -0,0 +1,7 @@ +/* file: example.h */ + +extern int do_op(int,int, int (*op)(int,int)); +extern int add(int,int); +extern int sub(int,int); +extern int mul(int,int); + diff --git a/Examples/perl5/funcptr/example.i b/Examples/perl5/funcptr/example.i new file mode 100644 index 000000000..73cc6eb8c --- /dev/null +++ b/Examples/perl5/funcptr/example.i @@ -0,0 +1,15 @@ +/* File : example.i */ +%module example +%{ +#include "example.h" +%} + +/* Wrap a function taking a pointer to a function */ +extern int do_op(int a, int b, int (*op)(int, int)); + +/* Now install a bunch of "ops" as constants */ +%constant(int (*)(int,int)) ADD = add; +%constant(int (*)(int,int)) SUB = sub; +%constant(int (*)(int,int)) MUL = mul; + + diff --git a/Examples/perl5/funcptr/example.pl b/Examples/perl5/funcptr/example.pl new file mode 100644 index 000000000..ff80f42fc --- /dev/null +++ b/Examples/perl5/funcptr/example.pl @@ -0,0 +1,21 @@ +# file: example.pl + +use example; + +$a = 37; +$b = 42; + +# Now call our C function with a bunch of callbacks + +print "Trying some C callback functions\n"; +print " a = $a\n"; +print " b = $b\n"; +print " ADD(a,b) = ", example::do_op($a,$b,$example::ADD),"\n"; +print " SUB(a,b) = ", example::do_op($a,$b,$example::SUB),"\n"; +print " MUL(a,b) = ", example::do_op($a,$b,$example::MUL),"\n"; + +print "Here is what the C callback function objects look like in Perl\n"; +print " ADD = $example::ADD\n"; +print " SUB = $example::SUB\n"; +print " MUL = $example::MUL\n"; + diff --git a/Examples/perl5/funcptr/index.html b/Examples/perl5/funcptr/index.html new file mode 100644 index 000000000..b685d390a --- /dev/null +++ b/Examples/perl5/funcptr/index.html @@ -0,0 +1,92 @@ + + +SWIG:Examples:perl5:funcptr + + + + + +SWIG/Examples/perl5/funcptr/ +
+ +

Pointers to Functions

+ +$Header$
+ +

+Okay, just what in the heck does SWIG do with a declaration like this? + +

+
+int do_op(int a, int b, int (*op)(int, int));
+
+
+ +Well, it creates a wrapper as usual. Of course, that does raise some +questions about the third argument (the pointer to a function). + +

+In this case, SWIG will wrap the function pointer as it does for all other +pointers. However, in order to actually call this function from a script, +you will need to pass some kind of C function pointer object. In C, +this is easy, you just supply a function name as an argument like this: + +

+
+/* Some callback function */
+int add(int a, int b) {
+   return a+b;
+} 
+...
+int r = do_op(x,y,add);
+
+
+ +To make this work with SWIG, you will need to do a little extra work. Specifically, +you need to create some function pointer objects using the %constant directive like this: + +
+
+%constant(int (*)(int,int)) ADD = add;
+
+
+ +Now, in a script, you would do this: + +
+
+$r = do_op($x,$y, $ADD);
+
+
+ +

An Example

+ +Here are some files that illustrate this with a simple example: + + + +

Notes

+ + + +
+ + + + + + diff --git a/Examples/perl5/index.html b/Examples/perl5/index.html index e33bcb491..9f6556cd6 100644 --- a/Examples/perl5/index.html +++ b/Examples/perl5/index.html @@ -21,6 +21,7 @@ certain C declarations are turned into constants.
  • class. How to wrap a simple C++ class.
  • reference. C++ references.
  • pointer. Simple pointer handling. +
  • funcptr. Pointers to functions.

    Compilation Issues