summaryrefslogtreecommitdiff
path: root/Examples/ocaml
diff options
context:
space:
mode:
Diffstat (limited to 'Examples/ocaml')
-rw-r--r--Examples/ocaml/argout_ref/Makefile27
-rw-r--r--Examples/ocaml/argout_ref/example.c19
-rw-r--r--Examples/ocaml/argout_ref/example.i4
-rw-r--r--Examples/ocaml/argout_ref/example_prog.ml26
-rw-r--r--Examples/ocaml/check.list10
-rw-r--r--Examples/ocaml/contract/Makefile33
-rw-r--r--Examples/ocaml/contract/example.i18
-rw-r--r--Examples/ocaml/contract/example_prog.ml7
-rw-r--r--Examples/ocaml/scoped_enum/Makefile33
-rw-r--r--Examples/ocaml/scoped_enum/README1
-rw-r--r--Examples/ocaml/scoped_enum/example.i7
-rw-r--r--Examples/ocaml/scoped_enum/example_prog.ml4
-rw-r--r--Examples/ocaml/scoped_enum/foo.h5
-rw-r--r--Examples/ocaml/shapes/Makefile34
-rw-r--r--Examples/ocaml/shapes/example.c48
-rw-r--r--Examples/ocaml/shapes/example.h18
-rw-r--r--Examples/ocaml/shapes/example.i10
-rw-r--r--Examples/ocaml/shapes/example_prog.ml76
-rw-r--r--Examples/ocaml/simple/Makefile33
-rw-r--r--Examples/ocaml/simple/example.c18
-rw-r--r--Examples/ocaml/simple/example.i7
-rw-r--r--Examples/ocaml/simple/example_prog.ml33
-rw-r--r--Examples/ocaml/simple/index.html97
-rw-r--r--Examples/ocaml/std_string/Makefile23
-rw-r--r--Examples/ocaml/std_string/README13
-rw-r--r--Examples/ocaml/std_string/example.h36
-rw-r--r--Examples/ocaml/std_string/example.i12
-rw-r--r--Examples/ocaml/std_string/runme.ml24
-rw-r--r--Examples/ocaml/std_vector/Makefile23
-rw-r--r--Examples/ocaml/std_vector/example.h25
-rw-r--r--Examples/ocaml/std_vector/example.i15
-rw-r--r--Examples/ocaml/std_vector/runme.ml35
-rw-r--r--Examples/ocaml/stl/Makefile33
-rw-r--r--Examples/ocaml/stl/example.h17
-rw-r--r--Examples/ocaml/stl/example.i12
-rw-r--r--Examples/ocaml/stl/runme.ml13
-rw-r--r--Examples/ocaml/string_from_ptr/Makefile34
-rw-r--r--Examples/ocaml/string_from_ptr/example_prog.ml23
-rw-r--r--Examples/ocaml/string_from_ptr/foolib.i16
-rw-r--r--Examples/ocaml/strings_test/Makefile28
-rw-r--r--Examples/ocaml/strings_test/example.h37
-rw-r--r--Examples/ocaml/strings_test/example.i14
-rw-r--r--Examples/ocaml/strings_test/runme.ml17
43 files changed, 1018 insertions, 0 deletions
diff --git a/Examples/ocaml/argout_ref/Makefile b/Examples/ocaml/argout_ref/Makefile
new file mode 100644
index 0000000..8a260fe
--- /dev/null
+++ b/Examples/ocaml/argout_ref/Makefile
@@ -0,0 +1,27 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+MLFILE = example.ml
+PROGFILE = example_prog.ml
+OBJS = example.o
+
+all:: static
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static_cpp
+
+dynamic::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_dynamic_cpp
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile MLFILE='$(MLFILE)' ocaml_clean
+
+check: all
diff --git a/Examples/ocaml/argout_ref/example.c b/Examples/ocaml/argout_ref/example.c
new file mode 100644
index 0000000..6f095cd
--- /dev/null
+++ b/Examples/ocaml/argout_ref/example.c
@@ -0,0 +1,19 @@
+/* File : example.c */
+
+/* Compute the greatest common divisor of positive integers */
+int gcd(int x, int y) {
+ int g;
+ g = y;
+ while (x > 0) {
+ g = x;
+ x = y % x;
+ y = g;
+ }
+ return g;
+}
+
+extern "C" void factor( int &x, int &y ) {
+ int gcd_xy = gcd( x,y );
+ x /= gcd_xy;
+ y /= gcd_xy;
+}
diff --git a/Examples/ocaml/argout_ref/example.i b/Examples/ocaml/argout_ref/example.i
new file mode 100644
index 0000000..a0be05f
--- /dev/null
+++ b/Examples/ocaml/argout_ref/example.i
@@ -0,0 +1,4 @@
+/* File : example.i */
+%module example
+
+extern "C" void factor(int &x, int &y);
diff --git a/Examples/ocaml/argout_ref/example_prog.ml b/Examples/ocaml/argout_ref/example_prog.ml
new file mode 100644
index 0000000..c6de345
--- /dev/null
+++ b/Examples/ocaml/argout_ref/example_prog.ml
@@ -0,0 +1,26 @@
+(* example_prog.ml *)
+
+open Swig
+open Example
+
+exception BadReturn
+
+let _ = if Array.length Sys.argv < 3 then
+ begin
+ print_endline
+ ("Usage: " ^ Sys.argv.(0) ^ " n1 n2\n" ^
+ " Displays the least factors of the numbers that have the same\n" ^
+ " relationship, 16 12 -> 4 3\n") ;
+ exit 0
+ end
+
+let x = int_of_string Sys.argv.(1)
+let y = int_of_string Sys.argv.(2)
+let (xf,yf) = match _factor '((x to int),(y to int)) with
+ C_list [ C_int a ; C_int b ] -> a,b
+ | _ -> raise BadReturn
+let _ = print_endline
+ ("Factorization of " ^ (string_of_int x) ^
+ " and " ^ (string_of_int y) ^
+ " is " ^ (string_of_int xf) ^
+ " and " ^ (string_of_int yf))
diff --git a/Examples/ocaml/check.list b/Examples/ocaml/check.list
new file mode 100644
index 0000000..cbdf270
--- /dev/null
+++ b/Examples/ocaml/check.list
@@ -0,0 +1,10 @@
+# see top-level Makefile.in
+simple
+std_string
+std_vector
+stl
+argout_ref
+shapes
+contract
+scoped_enum
+string_from_ptr
diff --git a/Examples/ocaml/contract/Makefile b/Examples/ocaml/contract/Makefile
new file mode 100644
index 0000000..8e0f2a4
--- /dev/null
+++ b/Examples/ocaml/contract/Makefile
@@ -0,0 +1,33 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS =
+TARGET = example
+INTERFACE = example.i
+MLFILE = example.ml
+PROGFILE = example_prog.ml
+OBJS =
+
+all:: static
+
+dynamic::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_dynamic
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static
+
+toplevel::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static_toplevel
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile MLFILE='$(MLFILE)' ocaml_clean
+
+check: all
diff --git a/Examples/ocaml/contract/example.i b/Examples/ocaml/contract/example.i
new file mode 100644
index 0000000..28d9dd7
--- /dev/null
+++ b/Examples/ocaml/contract/example.i
@@ -0,0 +1,18 @@
+%module example
+%{
+#include <math.h>
+%}
+
+/* File : example.i */
+%module example
+
+%contract cos(double d) {
+require:
+ d >= -3.14159265358979323845254338327950;
+ d < 3.14159265358979323846264338327950;
+ensure:
+ cos >= -1.0;
+ cos <= 1.0;
+}
+
+double cos(double d); \ No newline at end of file
diff --git a/Examples/ocaml/contract/example_prog.ml b/Examples/ocaml/contract/example_prog.ml
new file mode 100644
index 0000000..748109c
--- /dev/null
+++ b/Examples/ocaml/contract/example_prog.ml
@@ -0,0 +1,7 @@
+open Swig
+open Example
+
+let _ = print_endline "This won't throw."
+let _ = Printf.printf "Cos 1.0 is %f\n" (_cos '(1.0) as float)
+let _ = print_endline "This will throw."
+let _ = Printf.printf "Cos 5.0 is %f\n" (_cos '(5.0) as float)
diff --git a/Examples/ocaml/scoped_enum/Makefile b/Examples/ocaml/scoped_enum/Makefile
new file mode 100644
index 0000000..45c5edc
--- /dev/null
+++ b/Examples/ocaml/scoped_enum/Makefile
@@ -0,0 +1,33 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS =
+TARGET = example
+INTERFACE = example.i
+MLFILE = example.ml
+PROGFILE = example_prog.ml
+OBJS =
+
+all:: static
+
+dynamic::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_dynamic_cpp
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static_cpp
+
+toplevel::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static_cpp_toplevel
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile MLFILE='$(MLFILE)' ocaml_clean
+
+check: all
diff --git a/Examples/ocaml/scoped_enum/README b/Examples/ocaml/scoped_enum/README
new file mode 100644
index 0000000..88b6693
--- /dev/null
+++ b/Examples/ocaml/scoped_enum/README
@@ -0,0 +1 @@
+This tests our handling of scoped enums. Run with argument Tag1 or Tag2. \ No newline at end of file
diff --git a/Examples/ocaml/scoped_enum/example.i b/Examples/ocaml/scoped_enum/example.i
new file mode 100644
index 0000000..de553f8
--- /dev/null
+++ b/Examples/ocaml/scoped_enum/example.i
@@ -0,0 +1,7 @@
+%module example
+
+%{
+#include "foo.h"
+%}
+
+%include "foo.h" \ No newline at end of file
diff --git a/Examples/ocaml/scoped_enum/example_prog.ml b/Examples/ocaml/scoped_enum/example_prog.ml
new file mode 100644
index 0000000..e19bb34
--- /dev/null
+++ b/Examples/ocaml/scoped_enum/example_prog.ml
@@ -0,0 +1,4 @@
+open Swig
+open Example
+
+let _ = _f (match Sys.argv.(1) with "Tag1" -> ``Tag1 | _ -> ``Tag2)
diff --git a/Examples/ocaml/scoped_enum/foo.h b/Examples/ocaml/scoped_enum/foo.h
new file mode 100644
index 0000000..8238cb6
--- /dev/null
+++ b/Examples/ocaml/scoped_enum/foo.h
@@ -0,0 +1,5 @@
+namespace foo {
+ enum Bar { Tag1, Tag2 };
+ static void f( Bar b ) { printf( "b = %d\n", (int)b ); }
+}
+
diff --git a/Examples/ocaml/shapes/Makefile b/Examples/ocaml/shapes/Makefile
new file mode 100644
index 0000000..31f9934
--- /dev/null
+++ b/Examples/ocaml/shapes/Makefile
@@ -0,0 +1,34 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SWIGOPT =
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+MLFILE = example.ml
+PROGFILE = example_prog.ml
+OBJS = example.o
+
+all:: static static_top
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static_cpp
+
+static_top::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static_cpp_toplevel
+
+dynamic::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)'
+ MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_dynamic_cpp
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile MLFILE='$(MLFILE)' ocaml_clean
+
+check: all
diff --git a/Examples/ocaml/shapes/example.c b/Examples/ocaml/shapes/example.c
new file mode 100644
index 0000000..bf0fff9
--- /dev/null
+++ b/Examples/ocaml/shapes/example.c
@@ -0,0 +1,48 @@
+/* File : example.c */
+#include <stdio.h>
+#include "example.h"
+
+shape::~shape() { }
+
+bool shape::cover( double x, double y ) { return false; }
+
+void draw_shape_coverage( shape *s, int div_x, int div_y ) {
+ double i,j;
+
+ for( i = 0; i < 1.0; i += 1.0 / ((float)div_y) ) {
+ for( j = 0; j < 1.0; j += 1.0 / ((float)div_x) ) {
+ if( s->cover( j,i ) ) putchar( 'x' ); else putchar( ' ' );
+ }
+ printf( "\n" );
+ }
+}
+
+void draw_depth_map( volume *v, int div_x, int div_y ) {
+ double i,j;
+ char depth_map_chars[] = "#*+o;:,. ";
+ double lowbound, highbound;
+ double current = 0.0;
+ bool bounds_set = false;
+
+ for( i = 0; i < 1.0; i += 1.0 / ((float)div_y) ) {
+ for( j = 0; j < 1.0; j += 1.0 / ((float)div_x) ) {
+ current = v->depth( j,i );
+ if( !bounds_set ) {
+ lowbound = current; highbound = current; bounds_set = true;
+ }
+ if( current < lowbound ) lowbound = current;
+ if( current > highbound ) highbound = current;
+ }
+ }
+
+ for( i = 0; i < 1.0; i += 1.0 / ((float)div_y) ) {
+ for( j = 0; j < 1.0; j += 1.0 / ((float)div_x) ) {
+ current = ((v->depth( j,i ) - lowbound) /
+ (highbound - lowbound)) * 8;
+ putchar(depth_map_chars[(int)current]);
+ }
+ putchar('\n');
+ }
+}
+
+double volume::depth( double x, double y ) { return 0.0; }
diff --git a/Examples/ocaml/shapes/example.h b/Examples/ocaml/shapes/example.h
new file mode 100644
index 0000000..4b16444
--- /dev/null
+++ b/Examples/ocaml/shapes/example.h
@@ -0,0 +1,18 @@
+#ifndef EXAMPLE_H
+#define EXAMPLE_H
+
+class shape {
+public:
+ virtual ~shape();
+ virtual bool cover( double x, double y ); // does this shape cover this point?
+};
+
+class volume {
+public:
+ virtual double depth( double x, double y );
+};
+
+extern void draw_shape_coverage( shape *s, int div_x, int div_y );
+extern void draw_depth_map( volume *v, int div_x, int div_y );
+
+#endif//EXAMPLE_H
diff --git a/Examples/ocaml/shapes/example.i b/Examples/ocaml/shapes/example.i
new file mode 100644
index 0000000..ac0fa4a
--- /dev/null
+++ b/Examples/ocaml/shapes/example.i
@@ -0,0 +1,10 @@
+/* File : example.i */
+%module(directors="1") example
+#ifndef SWIGSEXP
+%{
+ #include "example.h"
+%}
+#endif
+
+%feature("director");
+%include "example.h"
diff --git a/Examples/ocaml/shapes/example_prog.ml b/Examples/ocaml/shapes/example_prog.ml
new file mode 100644
index 0000000..b9e3515
--- /dev/null
+++ b/Examples/ocaml/shapes/example_prog.ml
@@ -0,0 +1,76 @@
+(* example_prog.ml *)
+
+open Swig ;;
+open Example ;;
+
+let side_length (ax,ay) (bx,by) =
+ sqrt (((bx -. ax) ** 2.0) +. ((by -. ay) ** 2.0)) ;;
+
+let triangle_area a_pt b_pt c_pt =
+ let a = (side_length a_pt b_pt)
+ and b = (side_length b_pt c_pt)
+ and c = (side_length c_pt a_pt) in
+ let s = (a +. b +. c) /. 2.0 in
+ sqrt (s *. (s -. a) *. (s -. b) *. (s -. c)) ;;
+
+let point_in_triangle (pta,ptb,ptc) x y =
+ let delta = 0.0000001 in (* Error *)
+ let ptx = (x,y) in
+ begin
+ let a_area = triangle_area pta ptb ptx
+ and b_area = triangle_area ptb ptc ptx
+ and c_area = triangle_area ptc pta ptx
+ and x_area = triangle_area pta ptb ptc in
+ let result = (abs_float (a_area +. b_area +. c_area -. x_area)) < delta
+ in
+ result
+ end ;;
+
+let triangle_class pts ob meth args =
+ match meth with
+ "cover" ->
+ (match args with
+ C_list [ x_arg ; y_arg ] ->
+ let xa = x_arg as float
+ and ya = y_arg as float in
+ (point_in_triangle pts xa ya) to bool
+ | _ -> raise (Failure "cover needs two double arguments."))
+ | _ -> (invoke ob) meth args ;;
+
+let dist (ax,ay) (bx,by) =
+ let dx = ax -. bx and dy = ay -. by in
+ sqrt ((dx *. dx) +. (dy *. dy))
+
+let waveplot_depth events distance pt =
+ (List.fold_left (+.) 0.0
+ (List.map
+ (fun (x,y,d) ->
+ let t = dist pt (x,y) in
+ ((sin t) /. t) *. d)
+ events)) +. distance
+
+let waveplot_class events distance ob meth args =
+ match meth with
+ "depth" ->
+ (match args with
+ C_list [ x_arg ; y_arg ] ->
+ let xa = x_arg as float
+ and ya = y_arg as float in
+ (waveplot_depth events distance (xa,ya)) to float
+ | _ -> raise (Failure "cover needs two double arguments."))
+ | _ -> (invoke ob) meth args ;;
+
+let triangle =
+ new_derived_object
+ new_shape
+ (triangle_class ((0.0,0.0),(0.5,1.0),(1.0,0.6)))
+ '() ;;
+
+let waveplot =
+ new_derived_object
+ new_volume
+ (waveplot_class [ 0.01,0.01,3.0 ; 1.01,-2.01,1.5 ] 5.0)
+ '() ;;
+
+let _ = _draw_shape_coverage '(triangle, 60, 20) ;;
+let _ = _draw_depth_map '(waveplot, 60, 20) ;;
diff --git a/Examples/ocaml/simple/Makefile b/Examples/ocaml/simple/Makefile
new file mode 100644
index 0000000..4b85bf3
--- /dev/null
+++ b/Examples/ocaml/simple/Makefile
@@ -0,0 +1,33 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS = example.c
+TARGET = example
+INTERFACE = example.i
+MLFILE = example.ml
+PROGFILE = example_prog.ml
+OBJS = example.o
+
+all:: static
+
+dynamic::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_dynamic
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static
+
+toplevel::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
+ PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static_toplevel
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile MLFILE='$(MLFILE)' ocaml_clean
+
+check: all
diff --git a/Examples/ocaml/simple/example.c b/Examples/ocaml/simple/example.c
new file mode 100644
index 0000000..1c2af78
--- /dev/null
+++ b/Examples/ocaml/simple/example.c
@@ -0,0 +1,18 @@
+/* File : example.c */
+
+/* A global variable */
+double Foo = 3.0;
+
+/* Compute the greatest common divisor of positive integers */
+int gcd(int x, int y) {
+ int g;
+ g = y;
+ while (x > 0) {
+ g = x;
+ x = y % x;
+ y = g;
+ }
+ return g;
+}
+
+
diff --git a/Examples/ocaml/simple/example.i b/Examples/ocaml/simple/example.i
new file mode 100644
index 0000000..24093b9
--- /dev/null
+++ b/Examples/ocaml/simple/example.i
@@ -0,0 +1,7 @@
+/* File : example.i */
+%module example
+
+%inline %{
+extern int gcd(int x, int y);
+extern double Foo;
+%}
diff --git a/Examples/ocaml/simple/example_prog.ml b/Examples/ocaml/simple/example_prog.ml
new file mode 100644
index 0000000..cc3b973
--- /dev/null
+++ b/Examples/ocaml/simple/example_prog.ml
@@ -0,0 +1,33 @@
+(* example_prog.ml *)
+
+open Swig
+open Example
+
+(* Call our gcd() function *)
+
+exception NoReturn
+
+let x = 42 to int
+let y = 105 to int
+let g = _gcd '(x,y) as int
+let _ = Printf.printf "The gcd of %d and %d is %d\n" (x as int) (y as int) g
+
+(* Manipulate the Foo global variable *)
+
+(* Output its current value *)
+let _ = Printf.printf "Foo = %f\n" (_Foo '() as float)
+
+(* Change its value *)
+let _ = _Foo '(3.1415926)
+
+(* See if the change took effect *)
+let _ = Printf.printf "Foo = %f\n" (_Foo '() as float)
+
+
+
+
+
+
+
+
+
diff --git a/Examples/ocaml/simple/index.html b/Examples/ocaml/simple/index.html
new file mode 100644
index 0000000..dace471
--- /dev/null
+++ b/Examples/ocaml/simple/index.html
@@ -0,0 +1,97 @@
+<html>
+<head>
+<title>SWIG:Examples:python:simple</title>
+</head>
+
+<body bgcolor="#ffffff">
+
+
+<tt>SWIG/Examples/python/simple/</tt>
+<hr>
+
+<H2>Simple Python Example</H2>
+
+<p>
+This example illustrates how you can hook Python to a very simple C program containing
+a function and a global variable.
+
+<h2>The C Code</h2>
+
+Suppose you have the following C code:
+
+<blockquote>
+<pre>
+/* File : example.c */
+
+/* A global variable */
+double Foo = 3.0;
+
+/* Compute the greatest common divisor of positive integers */
+int gcd(int x, int y) {
+ int g;
+ g = y;
+ while (x &gt; 0) {
+ g = x;
+ x = y % x;
+ y = g;
+ }
+ return g;
+}
+</pre>
+</blockquote>
+
+<h2>The SWIG interface</h2>
+
+Here is a simple SWIG interface file:
+
+<blockquote>
+<pre>
+/* File: example.i */
+%module example
+
+extern int gcd(int x, int y);
+extern double Foo;
+</pre>
+</blockquote>
+
+<h2>Compilation</h2>
+
+<ol>
+<li><tt>swig -python <a href="example.i">example.i</a></tt>
+<p>
+<li>Compile <tt><a href="example_wrap.c">example_wrap.c</a></tt> and <tt><a href="example.c">example.c</a></tt>
+to create the extension <tt>examplemodule.so</tt>.
+</ol>
+
+<h2>Using the extension</h2>
+
+Click <a href="example.py">here</a> to see a script that calls our C functions from Python.
+
+<h2>Key points</h2>
+
+<ul>
+<li>Use the <tt>import</tt> statement to load your extension module from Python. For example:
+<blockquote>
+<pre>
+import example
+</pre>
+</blockquote>
+
+<li>C functions work just like Python functions. For example:
+<blockquote>
+<pre>
+g = example.gcd(42,105)
+</pre>
+</blockquote>
+
+<li>C global variables are accessed through a special variable called 'cvar'. For example:
+<blockquote>
+<pre>
+a = example.cvar.Foo
+</pre>
+</blockquote>
+</ul>
+
+<hr>
+</body>
+</html>
diff --git a/Examples/ocaml/std_string/Makefile b/Examples/ocaml/std_string/Makefile
new file mode 100644
index 0000000..e5a8017
--- /dev/null
+++ b/Examples/ocaml/std_string/Makefile
@@ -0,0 +1,23 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS =
+TARGET = example
+INTERFACE = example.i
+PROGFILE = runme.ml
+
+all default:: static
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ ocaml_static_cpp
+
+dynamic::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ ocaml_dynamic_cpp
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' ocaml_clean
+
+check: all
diff --git a/Examples/ocaml/std_string/README b/Examples/ocaml/std_string/README
new file mode 100644
index 0000000..5665530
--- /dev/null
+++ b/Examples/ocaml/std_string/README
@@ -0,0 +1,13 @@
+This example shows how to use both std::string and std::wstring in Ocaml,
+and also demonstrates that one might use this to make a locale-obedient
+Ocaml program.
+
+These are two sample runs; note that the output is different based on the
+locale chosen to perform the conversion to wchar_t.
+
+bash-2.05a$ ./example ja_JP.EUC-JP
+the original string contains 2
+the new string contains 1 : [ 29494; ]
+bash-2.05a$ ./example en_US
+the original string contains 2
+the new string contains 2 : [ 205; 177; ]
diff --git a/Examples/ocaml/std_string/example.h b/Examples/ocaml/std_string/example.h
new file mode 100644
index 0000000..60de984
--- /dev/null
+++ b/Examples/ocaml/std_string/example.h
@@ -0,0 +1,36 @@
+/* File : example.h -- stolen from the guile std_vector example */
+
+#include <string>
+#include <algorithm>
+#include <functional>
+#include <numeric>
+#include <stdlib.h>
+#include <locale.h>
+
+std::string from_wstring_with_locale( const std::wstring source,
+ const std::string locale ) {
+ const char *current_locale = setlocale( LC_CTYPE, locale.c_str() );
+ int required_chars = wcstombs( NULL, source.c_str(), 0 );
+ std::string s;
+ char *temp_chars = new char[required_chars + 1];
+ temp_chars[0] = 0;
+ wcstombs( temp_chars, source.c_str(), required_chars + 1 );
+ s = temp_chars;
+ delete [] temp_chars;
+ setlocale( LC_CTYPE, current_locale );
+ return s;
+}
+
+std::wstring to_wstring_with_locale( const std::string source,
+ const std::string locale ) {
+ const char *current_locale = setlocale( LC_CTYPE, locale.c_str() );
+ int required_chars = mbstowcs( NULL, source.c_str(), 0 );
+ std::wstring s;
+ wchar_t *temp_chars = new wchar_t[required_chars + 1];
+ temp_chars[0] = 0;
+ mbstowcs( temp_chars, source.c_str(), required_chars + 1 );
+ s = temp_chars;
+ delete [] temp_chars;
+ setlocale( LC_CTYPE, current_locale );
+ return s;
+}
diff --git a/Examples/ocaml/std_string/example.i b/Examples/ocaml/std_string/example.i
new file mode 100644
index 0000000..7b144ea
--- /dev/null
+++ b/Examples/ocaml/std_string/example.i
@@ -0,0 +1,12 @@
+/* -*- C++ -*- */
+/* File : example.i -- stolen from the guile std_vector example */
+%module example
+
+%{
+#include "example.h"
+%}
+
+%include stl.i
+
+/* Let's just grab the original header file here */
+%include "example.h"
diff --git a/Examples/ocaml/std_string/runme.ml b/Examples/ocaml/std_string/runme.ml
new file mode 100644
index 0000000..199dbe5
--- /dev/null
+++ b/Examples/ocaml/std_string/runme.ml
@@ -0,0 +1,24 @@
+(* This example was mostly lifted from the guile example directory *)
+
+open Swig
+open Example
+
+let y = "\205\177"
+let z = _to_wstring_with_locale '((y to string),(Sys.argv.(1) to string))
+
+let _ =
+ begin
+ print_string "the original string contains " ;
+ print_int (String.length y) ;
+ print_newline () ;
+
+ print_string "the new string contains " ;
+ print_int (z -> size () as int) ;
+ print_string " : [ " ;
+ for i = 0 to (pred ((z -> size ()) as int)) do
+ print_int ((z '[i to int]) as int) ;
+ print_string "; " ;
+ done ;
+ print_string "]" ;
+ print_newline () ;
+ end
diff --git a/Examples/ocaml/std_vector/Makefile b/Examples/ocaml/std_vector/Makefile
new file mode 100644
index 0000000..e5a8017
--- /dev/null
+++ b/Examples/ocaml/std_vector/Makefile
@@ -0,0 +1,23 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS =
+TARGET = example
+INTERFACE = example.i
+PROGFILE = runme.ml
+
+all default:: static
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ ocaml_static_cpp
+
+dynamic::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ ocaml_dynamic_cpp
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' ocaml_clean
+
+check: all
diff --git a/Examples/ocaml/std_vector/example.h b/Examples/ocaml/std_vector/example.h
new file mode 100644
index 0000000..b753592
--- /dev/null
+++ b/Examples/ocaml/std_vector/example.h
@@ -0,0 +1,25 @@
+/* File : example.h -- stolen from the guile std_vector example */
+
+#include <vector>
+#include <algorithm>
+#include <functional>
+#include <numeric>
+
+double average(std::vector<int> v) {
+ return std::accumulate(v.begin(),v.end(),0.0)/v.size();
+}
+
+std::vector<double> half(const std::vector<double>& v) {
+ std::vector<double> w(v);
+ for (unsigned int i=0; i<w.size(); i++)
+ w[i] /= 2.0;
+ return w;
+}
+
+void halve_in_place(std::vector<double>& v) {
+ // would you believe this is the same as the above?
+ std::transform(v.begin(),v.end(),v.begin(),
+ std::bind2nd(std::divides<double>(),2.0));
+}
+
+
diff --git a/Examples/ocaml/std_vector/example.i b/Examples/ocaml/std_vector/example.i
new file mode 100644
index 0000000..60285e5
--- /dev/null
+++ b/Examples/ocaml/std_vector/example.i
@@ -0,0 +1,15 @@
+/* -*- C++ -*- */
+/* File : example.i -- stolen from the guile std_vector example */
+%module example
+
+%{
+#include "example.h"
+%}
+
+%include stl.i
+/* instantiate the required template specializations */
+%template(IntVector) std::vector<int>;
+%template(DoubleVector) std::vector<double>;
+
+/* Let's just grab the original header file here */
+%include "example.h"
diff --git a/Examples/ocaml/std_vector/runme.ml b/Examples/ocaml/std_vector/runme.ml
new file mode 100644
index 0000000..feb70dd
--- /dev/null
+++ b/Examples/ocaml/std_vector/runme.ml
@@ -0,0 +1,35 @@
+(* This example was mostly lifted from the guile example directory *)
+
+open Swig
+open Example
+
+let with_vector v f =
+ for i = 0 to ((v -> size()) as int) - 1 do
+ f v i
+ done
+
+let print_DoubleVector v =
+ begin
+ with_vector v
+ (fun v i ->
+ print_float ((v '[i to int]) as float) ;
+ print_string " ") ;
+ print_endline
+ end
+
+(* Call average with a Ocaml array... *)
+
+let v = new_DoubleVector '()
+let rec fill_dv v x =
+ if x < 0.0001 then v else
+ begin
+ v -> push_back ((x to float)) ;
+ fill_dv v (x *. x)
+ end
+let _ = fill_dv v 0.999
+let _ = print_DoubleVector v ; print_endline ""
+let u = new_IntVector '()
+let _ = for i = 1 to 4 do
+ u -> push_back ((i to int))
+done
+let _ = (print_float ((_average u) as float) ; print_newline ())
diff --git a/Examples/ocaml/stl/Makefile b/Examples/ocaml/stl/Makefile
new file mode 100644
index 0000000..fa4333e
--- /dev/null
+++ b/Examples/ocaml/stl/Makefile
@@ -0,0 +1,33 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS =
+TARGET = example
+INTERFACE = example.i
+PROGFILE = runme.ml
+
+all default:: static
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ ocaml_static_cpp
+
+director::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ ocaml_static_cpp_director
+
+dynamic::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ ocaml_static_cpp
+
+toplevel::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ ocaml_static_cpp_toplevel
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' ocaml_clean
+
+check: all
diff --git a/Examples/ocaml/stl/example.h b/Examples/ocaml/stl/example.h
new file mode 100644
index 0000000..df5762b
--- /dev/null
+++ b/Examples/ocaml/stl/example.h
@@ -0,0 +1,17 @@
+/* File : example.h -- stolen from the guile std_vector example */
+
+#include <string>
+#include <vector>
+#include <algorithm>
+#include <functional>
+#include <numeric>
+
+using std::string;
+
+double vec_write(std::vector<string> v) {
+ int n = 0;
+ for( std::vector<string>::iterator i = v.begin();
+ i != v.end();
+ i++ )
+ printf( "%04d: %s\n", ++n, i->c_str() );
+}
diff --git a/Examples/ocaml/stl/example.i b/Examples/ocaml/stl/example.i
new file mode 100644
index 0000000..19a80a6
--- /dev/null
+++ b/Examples/ocaml/stl/example.i
@@ -0,0 +1,12 @@
+%module example
+%{
+#include "example.h"
+%}
+
+#define ENABLE_CHARPTR_ARRAY
+#define ENABLE_STRING_VECTOR
+%include stl.i
+
+%feature("director");
+
+%include example.h
diff --git a/Examples/ocaml/stl/runme.ml b/Examples/ocaml/stl/runme.ml
new file mode 100644
index 0000000..2fa5d20
--- /dev/null
+++ b/Examples/ocaml/stl/runme.ml
@@ -0,0 +1,13 @@
+(* This example was mostly lifted from the guile example directory *)
+
+open Swig
+open Example
+
+let v = new_StringVector '()
+
+let _ =
+ for i = 0 to (Array.length Sys.argv) - 1 do
+ let str = (Sys.argv.(i)) to string in v -> push_back (str)
+ done
+
+let _ = _vec_write '(v)
diff --git a/Examples/ocaml/string_from_ptr/Makefile b/Examples/ocaml/string_from_ptr/Makefile
new file mode 100644
index 0000000..350d973
--- /dev/null
+++ b/Examples/ocaml/string_from_ptr/Makefile
@@ -0,0 +1,34 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SWIGOPT = -c++
+SRCS =
+TARGET = example
+INTERFACE = foolib.i
+MLFILE = foolib.ml
+PROGFILE = example_prog.ml
+OBJS =
+
+all:: static static_top
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static_cpp
+
+static_top::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_static_cpp_toplevel
+
+dynamic::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)'
+ MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
+ ocaml_dynamic_cpp
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile MLFILE='$(MLFILE)' ocaml_clean
+
+check: all
diff --git a/Examples/ocaml/string_from_ptr/example_prog.ml b/Examples/ocaml/string_from_ptr/example_prog.ml
new file mode 100644
index 0000000..8069dc2
--- /dev/null
+++ b/Examples/ocaml/string_from_ptr/example_prog.ml
@@ -0,0 +1,23 @@
+(* foo_program.ml -- the program using foolib *)
+
+open Swig (* Give access to the swig library *)
+open Foolib (* This is the name of your swig output *)
+
+let results = _foo '() (* Function names are prefixed with _ in order to make
+ them lex as identifiers in ocaml. Consider that
+ uppercase identifiers are module names in ocaml.
+ NOTE: the '() syntax is part of swigp4. You can do:
+ let results = _foo C_void *)
+
+(* Since your function has a return value in addition to the string output,
+ you'll need to match them as a list *)
+
+let result_string =
+ match results with
+ C_list [ C_string result_string ; C_int 0 ] -> (* The return value is
+ last when out arguments appear, but this too can be customized.
+ We're also checking that the function succeeded. *)
+ result_string
+ | _ -> raise (Failure "Expected string, int reply from _foo")
+
+let _ = print_endline result_string
diff --git a/Examples/ocaml/string_from_ptr/foolib.i b/Examples/ocaml/string_from_ptr/foolib.i
new file mode 100644
index 0000000..86e0880
--- /dev/null
+++ b/Examples/ocaml/string_from_ptr/foolib.i
@@ -0,0 +1,16 @@
+%module foolib
+%{
+static int foo( char **buf ) {
+ *buf = "string from c";
+ return 0;
+}
+%}
+
+%typemap(in,numinputs=0) char **buf (char *temp) {
+ $1 = &temp;
+}
+%typemap(argout) char **buf {
+ swig_result = caml_list_append(swig_result,caml_val_string((char *)*$1));
+}
+
+int foo( char **buf );
diff --git a/Examples/ocaml/strings_test/Makefile b/Examples/ocaml/strings_test/Makefile
new file mode 100644
index 0000000..8d1f96e
--- /dev/null
+++ b/Examples/ocaml/strings_test/Makefile
@@ -0,0 +1,28 @@
+TOP = ../..
+SWIG = $(TOP)/../preinst-swig
+SRCS =
+TARGET = example
+INTERFACE = example.i
+PROGFILE = runme.ml
+
+all default:: static top
+
+static::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ ocaml_static_cpp
+
+dynamic::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ ocaml_static_cpp
+
+top::
+ $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
+ PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
+ ocaml_static_cpp_toplevel
+
+clean::
+ $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' ocaml_clean
+
+check: all
diff --git a/Examples/ocaml/strings_test/example.h b/Examples/ocaml/strings_test/example.h
new file mode 100644
index 0000000..3417981
--- /dev/null
+++ b/Examples/ocaml/strings_test/example.h
@@ -0,0 +1,37 @@
+/* -*- mode: c++ -*- */
+/* File : example.h -- Tests all string typemaps */
+
+void takes_std_string( std::string in ) {
+ cout << "takes_std_string( \"" << in << "\" );" << endl;
+}
+
+std::string gives_std_string() {
+ time_t t;
+
+ return std::string( asctime( localtime( &t ) ) );
+}
+
+void takes_char_ptr( char *p ) {
+ cout << "takes_char_ptr( \"" << p << "\" );" << endl;
+}
+
+char *gives_char_ptr() {
+ return "foo";
+}
+
+void takes_and_gives_std_string( std::string &inout ) {
+ inout.insert( inout.begin(), '[' );
+ inout.insert( inout.end(), ']' );
+}
+
+void takes_and_gives_char_ptr( char *&ptr ) {
+ char *pout = strchr( ptr, '.' );
+ if( pout ) ptr = pout + 1;
+ else ptr = "foo";
+}
+
+/*
+ * Local-Variables:
+ * c-indentation-style: "stroustrup"
+ * End:
+ */
diff --git a/Examples/ocaml/strings_test/example.i b/Examples/ocaml/strings_test/example.i
new file mode 100644
index 0000000..be9eabf
--- /dev/null
+++ b/Examples/ocaml/strings_test/example.i
@@ -0,0 +1,14 @@
+%module example
+%{
+#include <iostream>
+#include <string>
+
+using std::cin;
+using std::cout;
+using std::endl;
+using std::string;
+
+#include "example.h"
+%}
+
+%include example.h
diff --git a/Examples/ocaml/strings_test/runme.ml b/Examples/ocaml/strings_test/runme.ml
new file mode 100644
index 0000000..0eb5637
--- /dev/null
+++ b/Examples/ocaml/strings_test/runme.ml
@@ -0,0 +1,17 @@
+(* This example is meant to reach every case in cstring.i *)
+
+open Swig
+open Example
+
+let _ = _takes_std_string (C_string "foo")
+let _ = print_endline
+ ("_gives_std_string <<" ^ (get_string (_gives_std_string C_void)) ^ " >>")
+let _ = _takes_char_ptr (C_string "bar")
+let _ = print_endline
+ ("_gives_char_ptr << " ^ (get_string (_gives_char_ptr C_void)) ^ " >>")
+let _ = print_endline
+ ("_takes_and_gives_std_string << " ^
+ (get_string (_takes_and_gives_std_string (C_string "foo"))) ^ " >>")
+let _ = print_endline
+ ("_takes_and_gives_char_ptr << " ^
+ (get_string (_takes_and_gives_char_ptr (C_string "bar.bar"))) ^ " >>")