Added needed files.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@4322 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Art Yerkes 2003-02-16 07:37:15 +00:00
commit a8ef2143b0
4 changed files with 104 additions and 0 deletions

View file

@ -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

View file

@ -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));
}

View file

@ -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<int>;
%template(DoubleVector) std::vector<double>;
}
/* Let's just grab the original header file here */
%include "example.h"

View file

@ -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 ())