summaryrefslogtreecommitdiff
path: root/Examples/d/funcptr
diff options
context:
space:
mode:
authorDerrick <a11426@users.sourceforge.net>2011-02-14 20:11:58 +0000
committerDerrick <a11426@users.sourceforge.net>2011-02-14 20:11:58 +0000
commit5815f7ec289e067e765fb8e893a2f337d8b48303 (patch)
treeebe9e0534a089fe431cedc6fdbc1a53ac523d70c /Examples/d/funcptr
parent3e1af1f698d5d02d7905431bcb3549c0f7bc9aa7 (diff)
parent1fab53b2046b97702e1de4cfab06cb8fa8fc129d (diff)
downloadswig-a11426-fortran.tar.gz
update fortran branch. merge of 12160:12460a11426-fortran
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/a11426-fortran@12461 626c5289-ae23-0410-ae9c-e8d60b6d4f22
Diffstat (limited to 'Examples/d/funcptr')
-rw-r--r--Examples/d/funcptr/Makefile30
-rw-r--r--Examples/d/funcptr/d1/runme.d42
-rw-r--r--Examples/d/funcptr/d2/runme.d34
-rw-r--r--Examples/d/funcptr/example.c19
-rw-r--r--Examples/d/funcptr/example.h9
-rw-r--r--Examples/d/funcptr/example.i16
6 files changed, 150 insertions, 0 deletions
diff --git a/Examples/d/funcptr/Makefile b/Examples/d/funcptr/Makefile
new file mode 100644
index 000000000..09efa8d88
--- /dev/null
+++ b/Examples/d/funcptr/Makefile
@@ -0,0 +1,30 @@
+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
+ cd $(WORKING_DIR); \
+ $(MAKE) -f $(TOP)/Makefile d_run
diff --git a/Examples/d/funcptr/d1/runme.d b/Examples/d/funcptr/d1/runme.d
new file mode 100644
index 000000000..1461c1546
--- /dev/null
+++ b/Examples/d/funcptr/d1/runme.d
@@ -0,0 +1,42 @@
+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;
+
+ version (LDC) {
+ // Currently, there is no way to specify the calling convention for
+ // function pointer parameters in D, but LDC does strict typechecking for
+ // them (which is reasonable, but not covered by the language spec yet).
+ // As a result, there is no way to make the code below compile with LDC at
+ // the moment, so just skip it.
+ } else {
+ 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);
+