summaryrefslogtreecommitdiff
path: root/Examples/mzscheme
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2019-02-09 13:15:32 +0000
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2019-02-09 14:44:33 +0000
commit20344093efc8acc9da4fe77ce6b3601e10cc747d (patch)
treeacfcbea48cb415522ae49527387e2a4d810fc663 /Examples/mzscheme
parentb21a28f26ae178ee59f857ab4e0e498dff509ad7 (diff)
downloadswig-20344093efc8acc9da4fe77ce6b3601e10cc747d.tar.gz
Fix mzscheme static variable wrappers
$argnum needs to be expanded in the 'varin' typemap which shares code used by the 'in' typemap. Setting the static variable is also a function call (argnum=1). Added class examples - needs static variables to work Test newly working test cases with this fix
Diffstat (limited to 'Examples/mzscheme')
-rw-r--r--Examples/mzscheme/check.list1
-rw-r--r--Examples/mzscheme/class/Makefile18
-rw-r--r--Examples/mzscheme/class/example.cxx28
-rw-r--r--Examples/mzscheme/class/example.h34
-rw-r--r--Examples/mzscheme/class/example.i9
-rw-r--r--Examples/mzscheme/class/runme.scm60
6 files changed, 150 insertions, 0 deletions
diff --git a/Examples/mzscheme/check.list b/Examples/mzscheme/check.list
index f9e4f11c7..d2554c41c 100644
--- a/Examples/mzscheme/check.list
+++ b/Examples/mzscheme/check.list
@@ -1,4 +1,5 @@
# see top-level Makefile.in
+class
multimap
simple
std_vector
diff --git a/Examples/mzscheme/class/Makefile b/Examples/mzscheme/class/Makefile
new file mode 100644
index 000000000..ee92aa9f1
--- /dev/null
+++ b/Examples/mzscheme/class/Makefile
@@ -0,0 +1,18 @@
+TOP = ../..
+SWIGEXE = $(TOP)/../swig
+SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
+CXXSRCS = example.cxx
+TARGET = example
+INTERFACE = example.i
+SWIGOPT =
+
+check: build
+ $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' mzscheme_run
+
+build:
+ $(MAKE) -f $(TOP)/Makefile SRCDIR='$(CXXSRCDIR)' CXXSRCS='$(CXXSRCS)' \
+ SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' mzscheme_cpp
+
+clean:
+ $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' mzscheme_clean
diff --git a/Examples/mzscheme/class/example.cxx b/Examples/mzscheme/class/example.cxx
new file mode 100644
index 000000000..046304519
--- /dev/null
+++ b/Examples/mzscheme/class/example.cxx
@@ -0,0 +1,28 @@
+/* File : example.cxx */
+
+#include "example.h"
+#define M_PI 3.14159265358979323846
+
+/* Move the shape to a new location */
+void Shape::move(double dx, double dy) {
+ x += dx;
+ y += dy;
+}
+
+int Shape::nshapes = 0;
+
+double Circle::area() {
+ return M_PI*radius*radius;
+}
+
+double Circle::perimeter() {
+ return 2*M_PI*radius;
+}
+
+double Square::area() {
+ return width*width;
+}
+
+double Square::perimeter() {
+ return 4*width;
+}
diff --git a/Examples/mzscheme/class/example.h b/Examples/mzscheme/class/example.h
new file mode 100644
index 000000000..0dff185b2
--- /dev/null
+++ b/Examples/mzscheme/class/example.h
@@ -0,0 +1,34 @@
+/* File : example.h */
+
+class Shape {
+public:
+ Shape() {
+ nshapes++;
+ }
+ virtual ~Shape() {
+ nshapes--;
+ }
+ double x, y;
+ void move(double dx, double dy);
+ virtual double area() = 0;
+ virtual double perimeter() = 0;
+ static int nshapes;
+};
+
+class Circle : public Shape {
+private:
+ double radius;
+public:
+ Circle(double r) : radius(r) { }
+ virtual double area();
+ virtual double perimeter();
+};
+
+class Square : public Shape {
+private:
+ double width;
+public:
+ Square(double w) : width(w) { }
+ virtual double area();
+ virtual double perimeter();
+};
diff --git a/Examples/mzscheme/class/example.i b/Examples/mzscheme/class/example.i
new file mode 100644
index 000000000..fbdf7249f
--- /dev/null
+++ b/Examples/mzscheme/class/example.i
@@ -0,0 +1,9 @@
+/* File : example.i */
+%module example
+
+%{
+#include "example.h"
+%}
+
+/* Let's just grab the original header file here */
+%include "example.h"
diff --git a/Examples/mzscheme/class/runme.scm b/Examples/mzscheme/class/runme.scm
new file mode 100644
index 000000000..dea0b75bc
--- /dev/null
+++ b/Examples/mzscheme/class/runme.scm
@@ -0,0 +1,60 @@
+; file: runme.scm
+
+; This file illustrates the proxy class C++ interface generated
+; by SWIG.
+
+(load-extension "example.so")
+
+; Convenience wrapper around the display function
+; (which only accepts one argument at the time)
+
+(define (mdisplay-newline . args)
+ (for-each display args)
+ (newline))
+
+; ----- Object creation -----
+
+(mdisplay-newline "Creating some objects:")
+(define c (new-Circle 10))
+(mdisplay-newline " Created circle " c)
+(define s (new-Square 10))
+(mdisplay-newline " Created square " s)
+
+; ----- Access a static member -----
+
+(mdisplay-newline "\nA total of " (Shape-nshapes) " shapes were created")
+
+; ----- Member data access -----
+
+; Set the location of the object
+
+(Shape-x-set c 20)
+(Shape-y-set c 30)
+
+(Shape-x-set s -10)
+(Shape-y-set s 5)
+
+(mdisplay-newline "\nHere is their current position:")
+(mdisplay-newline " Circle = (" (Shape-x-get c) "," (Shape-y-get c) ")")
+(mdisplay-newline " Square = (" (Shape-x-get s) "," (Shape-y-get s) ")")
+
+; ----- Call some methods -----
+
+(mdisplay-newline "\nHere are some properties of the shapes:")
+(define (shape-props o)
+ (mdisplay-newline " " o)
+ (mdisplay-newline " area = " (Shape-area o))
+ (mdisplay-newline " perimeter = " (Shape-perimeter o)))
+(for-each shape-props (list c s))
+
+(mdisplay-newline "\nGuess I'll clean up now")
+
+; Note: this invokes the virtual destructor
+(delete-Shape c)
+(delete-Shape s)
+
+(define s 3)
+(mdisplay-newline (Shape-nshapes) " shapes remain")
+(mdisplay-newline "Goodbye")
+
+(exit 0)