From 9f8a09ed743cedd9547bf0661d518647966ab114 Mon Sep 17 00:00:00 2001 From: Lorry Tar Creator Date: Tue, 18 Aug 2009 20:56:02 +0000 Subject: Imported from /srv/lorry/lorry-area/swig-tarball/swig-1.3.40.tar.gz. --- Examples/java/funcptr/Makefile | 18 ++++++++ Examples/java/funcptr/example.c | 19 +++++++++ Examples/java/funcptr/example.h | 9 ++++ Examples/java/funcptr/example.i | 16 +++++++ Examples/java/funcptr/index.html | 91 ++++++++++++++++++++++++++++++++++++++++ Examples/java/funcptr/runme.java | 33 +++++++++++++++ 6 files changed, 186 insertions(+) create mode 100644 Examples/java/funcptr/Makefile create mode 100644 Examples/java/funcptr/example.c create mode 100644 Examples/java/funcptr/example.h create mode 100644 Examples/java/funcptr/example.i create mode 100644 Examples/java/funcptr/index.html create mode 100644 Examples/java/funcptr/runme.java (limited to 'Examples/java/funcptr') diff --git a/Examples/java/funcptr/Makefile b/Examples/java/funcptr/Makefile new file mode 100644 index 0000000..968c92c --- /dev/null +++ b/Examples/java/funcptr/Makefile @@ -0,0 +1,18 @@ +TOP = ../.. +SWIG = $(TOP)/../preinst-swig +SRCS = example.c +TARGET = example +INTERFACE = example.i +SWIGOPT = + +all:: java + +java:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' java + javac *.java + +clean:: + $(MAKE) -f $(TOP)/Makefile java_clean + +check: all diff --git a/Examples/java/funcptr/example.c b/Examples/java/funcptr/example.c new file mode 100644 index 0000000..5c4a3da --- /dev/null +++ b/Examples/java/funcptr/example.c @@ -0,0 +1,19 @@ +/* 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; +} + +int (*funcvar)(int,int) = add; diff --git a/Examples/java/funcptr/example.h b/Examples/java/funcptr/example.h new file mode 100644 index 0000000..9936e24 --- /dev/null +++ b/Examples/java/funcptr/example.h @@ -0,0 +1,9 @@ +/* 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); + +extern int (*funcvar)(int,int); + diff --git a/Examples/java/funcptr/example.i b/Examples/java/funcptr/example.i new file mode 100644 index 0000000..8b3bef6 --- /dev/null +++ b/Examples/java/funcptr/example.i @@ -0,0 +1,16 @@ +/* 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 (*ADD)(int,int) = add; +%constant int (*SUB)(int,int) = sub; +%constant int (*MUL)(int,int) = mul; + +extern int (*funcvar)(int,int); + diff --git a/Examples/java/funcptr/index.html b/Examples/java/funcptr/index.html new file mode 100644 index 0000000..56d3baa --- /dev/null +++ b/Examples/java/funcptr/index.html @@ -0,0 +1,91 @@ + + +SWIG:Examples:java:funcptr + + + + + +SWIG/Examples/java/funcptr/ +
+ +

Pointers to Functions

+ +

+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 Java program, +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 Java program, you would do this: + +
+
+int r = do_op(x,y, example.ADD)
+
+
+where example is the module name. + +

An Example

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

Notes

+ + + +
+ + + + + + diff --git a/Examples/java/funcptr/runme.java b/Examples/java/funcptr/runme.java new file mode 100644 index 0000000..cd34c1b --- /dev/null +++ b/Examples/java/funcptr/runme.java @@ -0,0 +1,33 @@ + +public class runme { + + static { + try { + System.loadLibrary("example"); + } catch (UnsatisfiedLinkError e) { + System.err.println("Native code library failed to load. See the chapter on Dynamic Linking Problems in the SWIG Java documentation for help.\n" + e); + System.exit(1); + } + } + + public static void main(String argv[]) { + + + int a = 37; + int b = 42; + + // Now call our C function with a bunch of callbacks + + System.out.println( "Trying some C callback functions" ); + System.out.println( " a = " + a ); + System.out.println( " b = " + b ); + System.out.println( " ADD(a,b) = " + example.do_op(a,b,example.ADD) ); + System.out.println( " SUB(a,b) = " + example.do_op(a,b,example.SUB) ); + System.out.println( " MUL(a,b) = " + example.do_op(a,b,example.MUL) ); + + System.out.println( "Here is what the C callback function classes are called in Java" ); + System.out.println( " ADD = " + example.ADD.getClass().getName() ); + System.out.println( " SUB = " + example.SUB.getClass().getName() ); + System.out.println( " MUL = " + example.MUL.getClass().getName() ); + } +} -- cgit v1.2.1