From 6f556a1b14fece5a15741ae21dfb0c97cf36c54e Mon Sep 17 00:00:00 2001 From: Dave Beazley Date: Mon, 4 Sep 2000 15:54:56 +0000 Subject: [PATCH] new example git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk/SWIG@844 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Examples/tcl/funcptr/Makefile | 19 +++++++ Examples/tcl/funcptr/example.c | 17 ++++++ Examples/tcl/funcptr/example.h | 7 +++ Examples/tcl/funcptr/example.i | 15 ++++++ Examples/tcl/funcptr/example.tcl | 22 ++++++++ Examples/tcl/funcptr/index.html | 92 ++++++++++++++++++++++++++++++++ Examples/tcl/index.html | 1 + 7 files changed, 173 insertions(+) create mode 100644 Examples/tcl/funcptr/Makefile create mode 100644 Examples/tcl/funcptr/example.c create mode 100644 Examples/tcl/funcptr/example.h create mode 100644 Examples/tcl/funcptr/example.i create mode 100644 Examples/tcl/funcptr/example.tcl create mode 100644 Examples/tcl/funcptr/index.html diff --git a/Examples/tcl/funcptr/Makefile b/Examples/tcl/funcptr/Makefile new file mode 100644 index 000000000..fcb994d09 --- /dev/null +++ b/Examples/tcl/funcptr/Makefile @@ -0,0 +1,19 @@ +TOP = ../.. +SWIG = $(TOP)/../swig +SRCS = example.c +TARGET = my_tclsh +DLTARGET = example +INTERFACE = example.i + +all:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='$(DLTARGET)' INTERFACE='$(INTERFACE)' tcl + +static:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' tclsh + +clean:: + rm -f *_wrap* *.o my_tclsh *~ .~* core *.so *.sl + +check: all diff --git a/Examples/tcl/funcptr/example.c b/Examples/tcl/funcptr/example.c new file mode 100644 index 000000000..99583b72e --- /dev/null +++ b/Examples/tcl/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/tcl/funcptr/example.h b/Examples/tcl/funcptr/example.h new file mode 100644 index 000000000..58989db79 --- /dev/null +++ b/Examples/tcl/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/tcl/funcptr/example.i b/Examples/tcl/funcptr/example.i new file mode 100644 index 000000000..73cc6eb8c --- /dev/null +++ b/Examples/tcl/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/tcl/funcptr/example.tcl b/Examples/tcl/funcptr/example.tcl new file mode 100644 index 000000000..0190ca565 --- /dev/null +++ b/Examples/tcl/funcptr/example.tcl @@ -0,0 +1,22 @@ +# file: example.tcl + +catch { load ./example.so example} +catch { load ./example.dll example} ;# Windows + +set a 37 +set b 42 + +# Now call our C function with a bunch of callbacks + +puts "Trying some C callback functions" +puts " a = $a" +puts " b = $b" +puts " ADD(a,b) = [do_op $a $b $ADD]" +puts " SUB(a,b) = [do_op $a $b $SUB]" +puts " MUL(a,b) = [do_op $a $b $MUL]" + +puts "Here is what the C callback function objects look like in Tcl" +puts " ADD = $ADD" +puts " SUB = $SUB" +puts " MUL = $MUL" + diff --git a/Examples/tcl/funcptr/index.html b/Examples/tcl/funcptr/index.html new file mode 100644 index 000000000..014df8a2b --- /dev/null +++ b/Examples/tcl/funcptr/index.html @@ -0,0 +1,92 @@ + + +SWIG:Examples:tcl:funcptr + + + + + +SWIG/Examples/tcl/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: + +
+
+set r [do_op $x $y $ADD]
+
+
+ +

An Example

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

Notes

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

    Compilation Issues