summaryrefslogtreecommitdiff
path: root/Examples
diff options
context:
space:
mode:
authorWilliam S Fulton <wsf@fultondesigns.co.uk>2019-01-14 23:16:59 +0000
committerWilliam S Fulton <wsf@fultondesigns.co.uk>2019-01-14 23:16:59 +0000
commit36846e9342a402bab1cf49abdbda818a6045e32f (patch)
tree70eb3c8ec1fc82ce0483c518889bdcf6135b5175 /Examples
parentb82d3007f3754cf321838fe4e78115b2d8b3e85b (diff)
parenta851e0a9ac448e21754f16c4e0978535049682da (diff)
downloadswig-36846e9342a402bab1cf49abdbda818a6045e32f.tar.gz
Merge branch 'ZackerySpytz-OCaml-class-example'
* ZackerySpytz-OCaml-class-example: [OCaml] Re-enable the using_protected unit test [OCaml] Add a class example
Diffstat (limited to 'Examples')
-rw-r--r--Examples/ocaml/check.list9
-rw-r--r--Examples/ocaml/class/Makefile31
-rw-r--r--Examples/ocaml/class/example.c28
-rw-r--r--Examples/ocaml/class/example.h34
-rw-r--r--Examples/ocaml/class/example.i9
-rw-r--r--Examples/ocaml/class/runme.ml57
-rw-r--r--Examples/ocaml/stl/example.h2
-rw-r--r--Examples/test-suite/ocaml/Makefile.in1
-rw-r--r--Examples/test-suite/ocaml/using_protected_runme.ml2
9 files changed, 166 insertions, 7 deletions
diff --git a/Examples/ocaml/check.list b/Examples/ocaml/check.list
index bcc63fa7f..fb7317b3a 100644
--- a/Examples/ocaml/check.list
+++ b/Examples/ocaml/check.list
@@ -1,11 +1,12 @@
# see top-level Makefile.in
+argout_ref
+class
+contract
+scoped_enum
+shapes
simple
#std_string
std_vector
stl
-argout_ref
-shapes
-contract
-scoped_enum
string_from_ptr
strings_test
diff --git a/Examples/ocaml/class/Makefile b/Examples/ocaml/class/Makefile
new file mode 100644
index 000000000..4cb4ef3dd
--- /dev/null
+++ b/Examples/ocaml/class/Makefile
@@ -0,0 +1,31 @@
+TOP = ../..
+SWIGEXE = $(TOP)/../swig
+SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
+SWIGOPT =
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+PROGFILE = runme.ml
+OBJS = example.o
+
+check: build
+ $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_run
+
+build: static
+
+static:
+ $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
+ SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static_cpp
+
+static_top:
+ $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
+ SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static_cpp_toplevel
+
+clean:
+ $(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_clean
diff --git a/Examples/ocaml/class/example.c b/Examples/ocaml/class/example.c
new file mode 100644
index 000000000..3bac43244
--- /dev/null
+++ b/Examples/ocaml/class/example.c
@@ -0,0 +1,28 @@
+/* File : example.c */
+
+#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/ocaml/class/example.h b/Examples/ocaml/class/example.h
new file mode 100644
index 000000000..0dff185b2
--- /dev/null
+++ b/Examples/ocaml/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/ocaml/class/example.i b/Examples/ocaml/class/example.i
new file mode 100644
index 000000000..fbdf7249f
--- /dev/null
+++ b/Examples/ocaml/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/ocaml/class/runme.ml b/Examples/ocaml/class/runme.ml
new file mode 100644
index 000000000..5e7b1f26c
--- /dev/null
+++ b/Examples/ocaml/class/runme.ml
@@ -0,0 +1,57 @@
+(* file: runme.ml
+
+ This file illustrates the proxy class C++ interface generated
+ by SWIG. *)
+
+open Swig
+open Example
+
+let repr o =
+ Printf.sprintf "<%s at %#x>" (o -> ":classof" () as string) (o -> "&" () as int)
+
+(* ----- Object creation ----- *)
+
+let _ = print_endline "Creating some objects:"
+let c = new_Circle '(10)
+let _ = Printf.printf " Created circle %s\n" (repr c)
+let s = new_Square '(10)
+let _ = Printf.printf " Created square %s\n" (repr s)
+
+(* ----- Access a static member ----- *)
+
+let _ = Printf.printf "\nA total of %d shapes were created\n" (_Shape_nshapes '() as int)
+
+(* ----- Member data access ----- *)
+
+(* Set the location of the object *)
+
+let _ = c -> "[x]" (20)
+let _ = c -> "[y]" (30)
+
+(* Temp var to work around a swigp4 bug (it doesn't properly handle "-" in some cases). *)
+let arg = (-10. to float)
+let _ = s -> "[x]" (arg)
+let _ = s -> "[y]" (5)
+
+let _ = print_endline "\nHere is their current position:"
+let _ = Printf.printf " Circle = (%f, %f)\n" (c -> "[x]" () as float) (c -> "[y]" () as float)
+let _ = Printf.printf " Square = (%f, %f)\n" (s -> "[x]" () as float) (s -> "[y]" () as float)
+
+(* ----- Call some methods ----- *)
+
+let _ = print_endline "\nHere are some properties of the shapes:"
+
+let _ = List.iter (fun o ->
+ Printf.printf " %s\n" (repr o);
+ Printf.printf " area = %f\n" (o -> area () as float);
+ Printf.printf " perimeter = %f\n" (o -> perimeter () as float)
+ ) [c; s]
+
+let _ = print_endline "\nGuess I'll clean up now"
+
+(* Note: this invokes the virtual destructor *)
+let _ = c -> "~" ()
+let _ = s -> "~" ()
+
+let _ = Printf.printf "%d shapes remain\n" (_Shape_nshapes '() as int)
+let _ = print_endline "Goodbye"
diff --git a/Examples/ocaml/stl/example.h b/Examples/ocaml/stl/example.h
index df5762bfc..da1d50e22 100644
--- a/Examples/ocaml/stl/example.h
+++ b/Examples/ocaml/stl/example.h
@@ -8,7 +8,7 @@
using std::string;
-double vec_write(std::vector<string> v) {
+void vec_write(std::vector<string> v) {
int n = 0;
for( std::vector<string>::iterator i = v.begin();
i != v.end();
diff --git a/Examples/test-suite/ocaml/Makefile.in b/Examples/test-suite/ocaml/Makefile.in
index 9128b3720..23b05e477 100644
--- a/Examples/test-suite/ocaml/Makefile.in
+++ b/Examples/test-suite/ocaml/Makefile.in
@@ -40,7 +40,6 @@ nested_directors \
preproc_constants \
smart_pointer_inherit \
typedef_mptr \
-using_protected \
FAILING_C_TESTS = \
enums \
diff --git a/Examples/test-suite/ocaml/using_protected_runme.ml b/Examples/test-suite/ocaml/using_protected_runme.ml
index 4dc4fe1c9..29647c288 100644
--- a/Examples/test-suite/ocaml/using_protected_runme.ml
+++ b/Examples/test-suite/ocaml/using_protected_runme.ml
@@ -2,7 +2,7 @@ open Swig
open Using_protected
let f = new_FooBar C_void
-let _ = (invoke f) "x" (C_int 3)
+let _ = (invoke f) "[x]" (C_int 3)
let _ = if (invoke f) "blah" (C_int 4) <> (C_int 4) then
raise (Failure "blah(int)")