diff options
author | David Nadlinger <code@klickverbot.at> | 2010-11-18 00:24:02 +0000 |
---|---|---|
committer | David Nadlinger <code@klickverbot.at> | 2010-11-18 00:24:02 +0000 |
commit | 03aefbc6e95d094a6de231e1f5264c0946e209a3 (patch) | |
tree | 94dff73a4aa3c27366f29f36712bde78317c5776 /Examples/d/funcptr | |
parent | a355d2d46af56c655816c37f24bb59fa6bade43f (diff) | |
download | swig-03aefbc6e95d094a6de231e1f5264c0946e209a3.tar.gz |
Added support for the D programming languge.
It is still a bit rough around some edges, particularly with regard to multi-threading and operator overloading, and there are some documentation bits missing, but it should be fine for basic use.
The test-suite should build and run fine with the current versions of DMD, LDC and Tango (at least) on Linux x86_64 and Mac OS X 10.6.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@12299 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'Examples/d/funcptr')
-rw-r--r-- | Examples/d/funcptr/Makefile | 28 | ||||
-rw-r--r-- | Examples/d/funcptr/d1/runme.d | 34 | ||||
-rw-r--r-- | Examples/d/funcptr/d2/runme.d | 34 | ||||
-rw-r--r-- | Examples/d/funcptr/example.c | 19 | ||||
-rw-r--r-- | Examples/d/funcptr/example.h | 9 | ||||
-rw-r--r-- | Examples/d/funcptr/example.i | 16 |
6 files changed, 140 insertions, 0 deletions
diff --git a/Examples/d/funcptr/Makefile b/Examples/d/funcptr/Makefile new file mode 100644 index 000000000..616102926 --- /dev/null +++ b/Examples/d/funcptr/Makefile @@ -0,0 +1,28 @@ +ifeq (2,$(D_VERSION)) + WORKING_DIR = d2/ +else + WORKING_DIR = d1/ +endif + +TOP = ../../.. +SWIG = $(TOP)/../preinst-swig +EXTRA_CFLAGS = -I../ ../example.c example_wrap.c +EXTRA_LDFLAGS = example.o example_wrap.o +TARGET = example_wrap +SWIGOPT = +DSRCS = *.d +DFLAGS = -ofrunme + + +all:: d + +d:: + cd $(WORKING_DIR); \ + $(MAKE) -f $(TOP)/Makefile EXTRA_CFLAGS='$(EXTRA_CFLAGS)' EXTRA_LDFLAGS='$(EXTRA_LDFLAGS)' EXTRA_LDFLAGS='$(EXTRA_LDFLAGS)' SWIG='$(SWIG)' SWIGOPT='$(SWIGOPT) -outcurrentdir ../example.i' TARGET='$(TARGET)' d; \ + $(MAKE) -f $(TOP)/Makefile DSRCS='$(DSRCS)' DFLAGS='$(DFLAGS)' d_compile + +clean:: + cd $(WORKING_DIR); \ + $(MAKE) -f $(TOP)/Makefile d_clean + +check: all diff --git a/Examples/d/funcptr/d1/runme.d b/Examples/d/funcptr/d1/runme.d new file mode 100644 index 000000000..2947602a5 --- /dev/null +++ b/Examples/d/funcptr/d1/runme.d @@ -0,0 +1,34 @@ +module runme; + +import tango.io.Stdout; +static import example; + +extern(C) int add(int a, int b) { + return a + b; +} + +extern(C) int sub(int a, int b) { + return a - b; +} + +extern(C) int mul(int a, int b) { + return a * b; +} + +void main() { + int a = 37; + int b = 42; + + Stdout( "a = " )( a ).newline; + Stdout( "b = " )( b ).newline; + + Stdout( "Trying some C callback functions:" ).newline; + Stdout( " ADD(a,b) = " )( example.do_op( a, b, example.ADD ) ).newline; + Stdout( " SUB(a,b) = " )( example.do_op( a, b, example.SUB ) ).newline; + Stdout( " MUL(a,b) = " )( example.do_op( a, b, example.MUL ) ).newline; + + Stdout( "Now the same with callback functions defined in D:" ).newline; + Stdout( " add(a,b) = " )( example.do_op( a, b, &add ) ).newline; + Stdout( " sub(a,b) = " )( example.do_op( a, b, &sub ) ).newline; + Stdout( " mul(a,b) = " )( example.do_op( a, b, &mul ) ).newline; +} diff --git a/Examples/d/funcptr/d2/runme.d b/Examples/d/funcptr/d2/runme.d new file mode 100644 index 000000000..929911d6c --- /dev/null +++ b/Examples/d/funcptr/d2/runme.d @@ -0,0 +1,34 @@ +module runme; + +import std.stdio; +static import example; + +extern(C) int add(int a, int b) { + return a + b; +} + +extern(C) int sub(int a, int b) { + return a - b; +} + +extern(C) int mul(int a, int b) { + return a * b; +} + +void main() { + int a = 37; + int b = 42; + + writefln( "a = %s", a ); + writefln( "b = %s", b ); + + writeln( "Trying some C callback functions:" ); + writefln( " ADD(a,b) = %s", example.do_op( a, b, example.ADD ) ); + writefln( " SUB(a,b) = %s", example.do_op( a, b, example.SUB ) ); + writefln( " MUL(a,b) = %s", example.do_op( a, b, example.MUL ) ); + + writeln( "Now the same with callback functions defined in D:" ); + writefln( " add(a,b) = %s", example.do_op( a, b, &add ) ); + writefln( " sub(a,b) = %s", example.do_op( a, b, &sub ) ); + writefln( " mul(a,b) = %s", example.do_op( a, b, &mul ) ); +} diff --git a/Examples/d/funcptr/example.c b/Examples/d/funcptr/example.c new file mode 100644 index 000000000..5c4a3dabf --- /dev/null +++ b/Examples/d/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/d/funcptr/example.h b/Examples/d/funcptr/example.h new file mode 100644 index 000000000..9936e24fc --- /dev/null +++ b/Examples/d/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/d/funcptr/example.i b/Examples/d/funcptr/example.i new file mode 100644 index 000000000..8b3bef678 --- /dev/null +++ b/Examples/d/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); + |