diff --git a/SWIG/Examples/ocaml/std_vector/Makefile b/SWIG/Examples/ocaml/std_vector/Makefile new file mode 100644 index 000000000..9f43e0615 --- /dev/null +++ b/SWIG/Examples/ocaml/std_vector/Makefile @@ -0,0 +1,22 @@ +TOP = ../.. +SWIG = $(TOP)/../swig -I/usr/include/g++-3 +SRCS = +TARGET = example +INTERFACE = example.i +PROGFILE = runme.ml + +default:: static + +all:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' ocaml_cpp + +static:: + $(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \ + PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \ + ocaml_static_cpp + +clean:: + $(MAKE) -f $(TOP)/Makefile TARGET='$(TARGET)' ocaml_clean + +check: all diff --git a/SWIG/Examples/ocaml/std_vector/example.h b/SWIG/Examples/ocaml/std_vector/example.h new file mode 100644 index 000000000..b75359243 --- /dev/null +++ b/SWIG/Examples/ocaml/std_vector/example.h @@ -0,0 +1,25 @@ +/* File : example.h -- stolen from the guile std_vector example */ + +#include +#include +#include +#include + +double average(std::vector v) { + return std::accumulate(v.begin(),v.end(),0.0)/v.size(); +} + +std::vector half(const std::vector& v) { + std::vector w(v); + for (unsigned int i=0; i& v) { + // would you believe this is the same as the above? + std::transform(v.begin(),v.end(),v.begin(), + std::bind2nd(std::divides(),2.0)); +} + + diff --git a/SWIG/Examples/ocaml/std_vector/example.i b/SWIG/Examples/ocaml/std_vector/example.i new file mode 100644 index 000000000..385353ade --- /dev/null +++ b/SWIG/Examples/ocaml/std_vector/example.i @@ -0,0 +1,17 @@ +/* -*- C++ -*- */ +/* File : example.i -- stolen from the guile std_vector example */ +%module example + +%{ +#include "example.h" +%} + +%include stl.i +/* instantiate the required template specializations */ +namespace std { + %template(IntVector) std::vector; + %template(DoubleVector) std::vector; +} + +/* Let's just grab the original header file here */ +%include "example.h" diff --git a/SWIG/Examples/ocaml/std_vector/runme.ml b/SWIG/Examples/ocaml/std_vector/runme.ml new file mode 100644 index 000000000..c6c8846f1 --- /dev/null +++ b/SWIG/Examples/ocaml/std_vector/runme.ml @@ -0,0 +1,40 @@ +(* This example was mostly lifted from the guile example directory *) + +open Example + +(* repeatedly invoke a procedure with v and an index as arguments *) +let with_vector v proc size = + let rec with_vector_item = + (fun v i -> if (i < size) then + begin + proc v i ; + with_vector_item v (succ i) + end) in + with_vector_item v 0 + +let with_vector v proc = + with_vector v proc (get_int ((invoke v) "size" (C_void))) + + + +let dbl_vector_ref v i = + match ((invoke v) "[]" (C_int i)) with + C_float f | C_double f -> f | _ -> 0.0 + +let print_DoubleVector v = + begin + with_vector v + (fun v i -> + (print_float (dbl_vector_ref v i)) ; + (print_string " ")) ; + print_endline + end + +let fill_int_vector_from_array a v i = + (invoke v) "set" (C_list [ C_int i ; C_int a.(i) ]) + +(* Call average with a Ocaml array... *) + +let y = new_IntVector (C_uint (Int32.of_int 4)) +let _ = with_vector y (fill_int_vector_from_array [| 1;2;3;4 |]) +let _ = (print_float (get_float (_average y)) ; print_newline ())