Initial commit

This commit is contained in:
Bassem Girgis
2018-12-20 17:34:07 -06:00
parent 7a2d899662
commit 81b4b9e273
34743 changed files with 5940233 additions and 0 deletions

View File

@@ -0,0 +1,31 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS = example.c
TARGET = example
INTERFACE = example.i
MLFILE = example.ml
PROGFILE = example_prog.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)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_static_cpp
dynamic:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_dynamic_cpp
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' MLFILE='$(MLFILE)' ocaml_clean

View File

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

View File

@@ -0,0 +1,8 @@
/* File : example.i */
%module example
%{
extern "C" void factor(int &x, int &y);
%}
extern "C" void factor(int &x, int &y);

View File

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

View File

@@ -0,0 +1,10 @@
# see top-level Makefile.in
simple
std_string
std_vector
stl
argout_ref
shapes
contract
scoped_enum
string_from_ptr

View File

@@ -0,0 +1,38 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS =
TARGET = example
INTERFACE = example.i
MLFILE = example.ml
PROGFILE = example_prog.ml
OBJS =
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_run
build: static
dynamic:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_dynamic
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_static
toplevel:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_static_toplevel
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' MLFILE='$(MLFILE)' ocaml_clean

View File

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

View File

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

View File

@@ -0,0 +1,38 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS =
TARGET = example
INTERFACE = example.i
MLFILE = example.ml
PROGFILE = example_prog.ml
OBJS =
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_run
build: static
dynamic:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_dynamic_cpp
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_static_cpp
toplevel:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_static_cpp_toplevel
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' MLFILE='$(MLFILE)' ocaml_clean

View File

@@ -0,0 +1 @@
This tests our handling of scoped enums. Run with argument Tag1 or Tag2.

View File

@@ -0,0 +1,7 @@
%module example
%{
#include "foo.h"
%}
%include "foo.h"

View File

@@ -0,0 +1,4 @@
open Swig
open Example
let _ = _f (match Sys.argv.(1) with "Tag1" -> ``Tag1 | _ -> ``Tag2)

View File

@@ -0,0 +1,5 @@
namespace foo {
enum Bar { Tag1, Tag2 };
static void f( Bar b ) { printf( "b = %d\n", (int)b ); }
}

View File

@@ -0,0 +1,39 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SWIGOPT =
SRCS = example.c
TARGET = example
INTERFACE = example.i
MLFILE = example.ml
PROGFILE = example_prog.ml
OBJS = example.o
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_run
build: static static_top
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
MLFILE='$(MLFILE)' 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)' \
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_static_cpp_toplevel
dynamic:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)'
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_dynamic_cpp
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' MLFILE='$(MLFILE)' ocaml_clean

View File

@@ -0,0 +1,50 @@
/* 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; }
volume::~volume() { }

View File

@@ -0,0 +1,19 @@
#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 );
virtual ~volume();
};
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

View File

@@ -0,0 +1,10 @@
/* File : example.i */
%module(directors="1") example
#ifndef SWIGSEXP
%{
#include "example.h"
%}
#endif
%feature("director");
%include "example.h"

View File

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

View File

@@ -0,0 +1,38 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS = example.c
TARGET = example
INTERFACE = example.i
MLFILE = example.ml
PROGFILE = example_prog.ml
OBJS = example.o
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_run
build: static
dynamic:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_dynamic
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_static
toplevel:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MLFILE='$(MLFILE)' \
PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_static_toplevel
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' MLFILE='$(MLFILE)' ocaml_clean

View File

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

View File

@@ -0,0 +1,7 @@
/* File : example.i */
%module example
%inline %{
extern int gcd(int x, int y);
extern double Foo;
%}

View File

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

View File

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

View File

@@ -0,0 +1,27 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS =
TARGET = example
INTERFACE = example.i
PROGFILE = runme.ml
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)' \
PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
ocaml_static_cpp
dynamic:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
ocaml_dynamic_cpp
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_clean

View File

@@ -0,0 +1,19 @@
--- New ---
It's not possible to access std::(w)string at the moment as it breaks other string examples.
--- Old ---
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; ]

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,27 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS =
TARGET = example
INTERFACE = example.i
PROGFILE = runme.ml
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)' \
PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
ocaml_static_cpp
dynamic:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
ocaml_dynamic_cpp
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_clean

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,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"

View File

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

View File

@@ -0,0 +1,39 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS =
TARGET = example
INTERFACE = example.i
PROGFILE = runme.ml
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)' \
PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
ocaml_static_cpp
director:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
ocaml_static_cpp_director
dynamic:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
ocaml_static_cpp
toplevel:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
ocaml_static_cpp_toplevel
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_clean

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,39 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SWIGOPT = -c++
SRCS =
TARGET = example
INTERFACE = foolib.i
MLFILE = foolib.ml
PROGFILE = example_prog.ml
OBJS =
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_run
build: static static_top
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
MLFILE='$(MLFILE)' 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)' \
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_static_cpp_toplevel
dynamic:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
SWIGOPT='$(SWIGOPT)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)'
MLFILE='$(MLFILE)' PROGFILE='$(PROGFILE)' OBJS='$(OBJS)' \
ocaml_dynamic_cpp
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' MLFILE='$(MLFILE)' ocaml_clean

View File

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

View File

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

View File

@@ -0,0 +1,33 @@
TOP = ../..
SWIGEXE = $(TOP)/../swig
SWIG_LIB_DIR = $(TOP)/../$(TOP_BUILDDIR_TO_TOP_SRCDIR)Lib
SRCS =
TARGET = example
INTERFACE = example.i
PROGFILE = runme.ml
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_run
build: static top
static:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
ocaml_static_cpp
dynamic:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
ocaml_static_cpp
top:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' \
SWIG_LIB_DIR='$(SWIG_LIB_DIR)' SWIGEXE='$(SWIGEXE)' \
PROGFILE='$(PROGFILE)' TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' \
ocaml_static_cpp_toplevel
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' TARGET='$(TARGET)' ocaml_clean

View File

@@ -0,0 +1,41 @@
/* -*- mode: c++ -*- */
/* File : example.h -- Tests all string typemaps */
#include <sys/time.h>
#include <time.h>
void takes_std_string( std::string in ) {
cout << "takes_std_string( \"" << in << "\" );" << endl;
}
std::string gives_std_string() {
struct timeval tv;
gettimeofday(&tv, NULL);
return std::string( asctime( localtime( &tv.tv_sec ) ) );
}
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 *&inout ) {
char *pout = strchr( inout, '.' );
if( pout ) inout = pout + 1;
else inout = "foo";
}
/*
* Local-Variables:
* c-indentation-style: "stroustrup"
* End:
*/

View File

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

View File

@@ -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"))) ^ " >>")