typecheck: Foolish omission of break;

mlheading and mliheading: added make_x and get_x accessor and constructor
functions for C_xxx types.  This makes the system a bit more accomodating.
ocaml.swg: Corrected one SWIGSTATIC
std_vector.i: First shot at a working implementation (with example).
typemaps.i: Correction to handling of SWIGTYPE ...


git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@4320 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Art Yerkes 2003-02-16 07:29:34 +00:00
commit c3e98e3627
7 changed files with 204 additions and 206 deletions

View file

@ -1,3 +1,4 @@
(* -*- tuareg -*- *)
open Int32
open Int64
@ -29,26 +30,75 @@ exception LabelNotFromThisEnum of c_obj
let invoke obj = match obj with C_obj o -> o | _ -> raise (NotObject obj)
let fnhelper fin f arg =
let args = match arg with C_list l -> l | C_void -> [] | _ -> [ arg ] in
match f args with
[] -> C_void
| [ x ] -> (if fin then Gc.finalise
(fun x -> ignore ((invoke x) "~" C_void)) x) ; x
| lst -> C_list lst
match f args with
[] -> C_void
| [ x ] -> (if fin then Gc.finalise
(fun x -> ignore ((invoke x) "~" C_void)) x) ; x
| lst -> C_list lst
let rec get_int x =
match x with
C_char c
| C_uchar c -> (int_of_char c)
| C_short s
| C_ushort s
| C_int s -> s
| C_uint u
| C_int32 u -> (Int32.to_int u)
| C_int64 u -> (Int64.to_int u)
| C_float f -> (int_of_float f)
| C_double d -> (int_of_float d)
| C_ptr (p,q) -> (Int64.to_int p)
| C_obj o -> (try (get_int (o "int" C_void))
with _ -> (get_int (o "&" C_void)))
| _ -> raise (Failure "Can't convert to int")
C_bool b -> if b then 1 else 0
| C_char c
| C_uchar c -> (int_of_char c)
| C_short s
| C_ushort s
| C_int s -> s
| C_uint u
| C_int32 u -> (Int32.to_int u)
| C_int64 u -> (Int64.to_int u)
| C_float f -> (int_of_float f)
| C_double d -> (int_of_float d)
| C_ptr (p,q) -> (Int64.to_int p)
| C_obj o -> (try (get_int (o "int" C_void))
with _ -> (get_int (o "&" C_void)))
| _ -> raise (Failure "Can't convert to int")
let rec get_float x =
match x with
C_char c
| C_uchar c -> (float_of_int (int_of_char c))
| C_short s -> (float_of_int s)
| C_ushort s -> (float_of_int s)
| C_int s -> (float_of_int s)
| C_uint u
| C_int32 u -> (float_of_int (Int32.to_int u))
| C_int64 u -> (float_of_int (Int64.to_int u))
| C_float f -> f
| C_double d -> d
| C_obj o -> (try (get_float (o "float" C_void))
with _ -> (get_float (o "double" C_void)))
| _ -> raise (Failure "Can't convert to float")
let rec get_char x =
(char_of_int (get_int x))
let rec get_string x =
match x with
C_string str -> str
| _ -> raise (Failure "Can't convert to string")
let rec get_bool x =
match x with
C_bool b -> b
| _ ->
(try if get_int x != 0 then true else false
with _ -> raise (Failure "Can't convert to bool"))
let addr_of obj = (invoke obj) "&" C_void
let _ = Callback.register "caml_obj_ptr" addr_of
let make_float f = C_float f
let make_double f = C_double f
let make_string s = C_string s
let make_bool b = C_bool b
let make_char c = C_char c
let make_char_i c = C_char (char_of_int c)
let make_uchar c = C_uchar c
let make_uchar_i c = C_uchar (char_of_int c)
let make_short i = C_short i
let make_ushort i = C_ushort i
let make_int i = C_int i
let make_uint i = C_uint (Int32.of_int i)
let make_int32 i = C_int32 (Int32.of_int i)
let make_int64 i = C_int64 (Int64.of_int i)

View file

@ -1,3 +1,4 @@
(* -*- tuareg -*- *)
type c_obj =
C_void
| C_bool of bool
@ -25,3 +26,22 @@ exception LabelNotFromThisEnum of c_obj
val invoke : c_obj -> (string -> c_obj -> c_obj)
val get_int : c_obj -> int
val get_float : c_obj -> float
val get_string : c_obj -> string
val get_char : c_obj -> char
val get_bool : c_obj -> bool
val make_float : float -> c_obj
val make_double : float -> c_obj
val make_string : string -> c_obj
val make_bool : bool -> c_obj
val make_char : char -> c_obj
val make_char_i : int -> c_obj
val make_uchar : char -> c_obj
val make_uchar_i : int -> c_obj
val make_short : int -> c_obj
val make_ushort : int -> c_obj
val make_int : int -> c_obj
val make_uint : int -> c_obj
val make_int32 : int -> c_obj
val make_int64 : int -> c_obj

View file

@ -21,10 +21,6 @@
#define SWIG_malloc(size) swig_malloc(size, FUNC_NAME)
#define SWIG_free(mem) free(mem)
/* Guile compatibility kludges */
#define SCM_VALIDATE_VECTOR(argnum, value) (void)0
#define SCM_VALIDATE_LIST(argnum, value) (void)0
/* Read in standard typemaps. */
%include "swig.swg"
%include "typemaps.i"

View file

@ -24,44 +24,9 @@ extern "C" {
#define C_string 15
#define C_enum 16
struct custom_block_contents {
swig_type_info *type;
void *object;
char *delete_fn;
};
static void generic_delete_fn( value v ) {
CAMLparam1(v);
CAMLlocal1(x);
struct custom_block_contents *new_proxy;
value *deleter = NULL;
new_proxy = (struct custom_block_contents *)(Data_custom_val(v));
if( new_proxy->delete_fn )
deleter = caml_named_value(new_proxy->delete_fn);
if( *deleter )
x = callback( *deleter, v );
free( new_proxy->delete_fn );
CAMLreturn0;
}
static struct custom_operations makeptr_custom_ops = {
"SWIG-Wrapped Object",
generic_delete_fn,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
};
static value _wrap_delete_void( value v ) {
CAMLparam0();
CAMLreturn(Val_unit);
}
/* Cast a pointer if possible; returns 1 if successful */
static int
SWIGSTATIC int
SWIG_Cast (void *source, swig_type_info *source_type,
void **ptr, swig_type_info *dest_type)
{
@ -101,9 +66,9 @@ static value _wrap_delete_void( value v ) {
}
}
static void caml_print_list( value v );
SWIGSTATIC void caml_print_list( value v );
static void caml_print_val( value v ) {
SWIGSTATIC void caml_print_val( value v ) {
switch( Tag_val(v) ) {
case C_bool:
if( Bool_val(Field(v,0)) ) fprintf( stderr, "true " );
@ -143,7 +108,7 @@ static value _wrap_delete_void( value v ) {
for( i = 0; i < Wosize_val( Field(v,0) ); i++ )
caml_print_val( Field(Field(v,0),i) );
}
break;
break;
case C_list:
caml_print_list( Field(v,0) );
break;
@ -156,7 +121,7 @@ static value _wrap_delete_void( value v ) {
}
}
static void caml_print_list( value v ) {
SWIGSTATIC void caml_print_list( value v ) {
CAMLparam1(v);
while( v && Is_block(v) ) {
fprintf( stderr, "[ " );
@ -166,56 +131,56 @@ static value _wrap_delete_void( value v ) {
}
}
static value caml_list_nth( value lst, int n ) {
CAMLparam1(lst);
int i = 0;
while( i < n && lst && Is_block(lst) ) {
i++; lst = Field(lst,1);
SWIGSTATIC value caml_list_nth( value lst, int n ) {
CAMLparam1(lst);
int i = 0;
while( i < n && lst && Is_block(lst) ) {
i++; lst = Field(lst,1);
}
if( lst == Val_unit ) CAMLreturn(Val_unit);
else CAMLreturn(Field(lst,0));
}
if( lst == Val_unit ) CAMLreturn(Val_unit);
else CAMLreturn(Field(lst,0));
}
static value caml_list_append( value lst, value elt ) {
CAMLparam2(lst,elt);
CAMLlocal3(v,vt,lh);
lh = Val_unit;
v = Val_unit;
SWIGSTATIC value caml_list_append( value lst, value elt ) {
CAMLparam2(lst,elt);
CAMLlocal3(v,vt,lh);
lh = Val_unit;
v = Val_unit;
/* Appending C_void should have no effect */
if( !Is_block(elt) ) return lst;
/* Appending C_void should have no effect */
if( !Is_block(elt) ) return lst;
while( lst && Is_block(lst) ) {
if( v && v != Val_unit ) {
while( lst && Is_block(lst) ) {
if( v && v != Val_unit ) {
vt = alloc_tuple(2);
Store_field(v,1,vt);
v = vt;
} else {
v = lh = alloc_tuple(2);
}
Store_field(v,0,Field(lst,0));
lst = Field(lst,1);
}
if( v && Is_block(v) ) {
vt = alloc_tuple(2);
Store_field(v,1,vt);
v = vt;
} else {
v = lh = alloc_tuple(2);
}
Store_field(v,0,Field(lst,0));
lst = Field(lst,1);
Store_field(v,0,elt);
Store_field(v,1,Val_unit);
CAMLreturn(lh);
}
if( v && Is_block(v) ) {
vt = alloc_tuple(2);
Store_field(v,1,vt);
v = vt;
} else {
v = lh = alloc_tuple(2);
SWIGSTATIC int caml_list_length( value lst ) {
CAMLparam1(lst);
int i = 0;
while( lst && Is_block(lst) ) { i++; lst = Field(lst,1); }
CAMLreturn(i);
}
Store_field(v,0,elt);
Store_field(v,1,Val_unit);
CAMLreturn(lh);
}
static int caml_list_length( value lst ) {
CAMLparam1(lst);
int i = 0;
while( lst && Is_block(lst) ) { i++; lst = Field(lst,1); }
CAMLreturn(i);
}
#ifdef __cplusplus
namespace caml {
@ -226,17 +191,14 @@ static int caml_list_length( value lst ) {
};
#endif
#ifdef __cplusplus
extern "C"
#endif
value caml_swig_alloc(int x,int y) {
SWIGSTATIC value caml_swig_alloc(int x,int y) {
#ifdef __cplusplus
using namespace caml;
#endif
return alloc(x,y);
}
static value caml_val_bool( int b ) {
SWIGSTATIC value caml_val_bool( int b ) {
CAMLparam0();
CAMLlocal1(bv);
bv = caml_swig_alloc(1,C_bool);
@ -244,7 +206,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(bv);
}
static value caml_val_char( char c ) {
SWIGSTATIC value caml_val_char( char c ) {
CAMLparam0();
CAMLlocal1(cv);
cv = caml_swig_alloc(1,C_char);
@ -252,7 +214,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(cv);
}
static value caml_val_uchar( unsigned char uc ) {
SWIGSTATIC value caml_val_uchar( unsigned char uc ) {
CAMLparam0();
CAMLlocal1(ucv);
ucv = caml_swig_alloc(1,C_uchar);
@ -260,7 +222,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(ucv);
}
static value caml_val_short( short s ) {
SWIGSTATIC value caml_val_short( short s ) {
CAMLparam0();
CAMLlocal1(sv);
sv = caml_swig_alloc(1,C_short);
@ -268,7 +230,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(sv);
}
static value caml_val_ushort( unsigned short us ) {
SWIGSTATIC value caml_val_ushort( unsigned short us ) {
CAMLparam0();
CAMLlocal1(usv);
usv = caml_swig_alloc(1,C_ushort);
@ -276,7 +238,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(usv);
}
static value caml_val_int( int i ) {
SWIGSTATIC value caml_val_int( int i ) {
CAMLparam0();
CAMLlocal1(iv);
iv = caml_swig_alloc(1,C_int);
@ -284,7 +246,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(iv);
}
static value caml_val_uint( unsigned int ui ) {
SWIGSTATIC value caml_val_uint( unsigned int ui ) {
CAMLparam0();
CAMLlocal1(uiv);
uiv = caml_swig_alloc(1,C_int);
@ -292,7 +254,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(uiv);
}
static value caml_val_long( long l ) {
SWIGSTATIC value caml_val_long( long l ) {
CAMLparam0();
CAMLlocal1(lv);
lv = caml_swig_alloc(1,C_int64);
@ -300,7 +262,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(lv);
}
static value caml_val_ulong( unsigned long ul ) {
SWIGSTATIC value caml_val_ulong( unsigned long ul ) {
CAMLparam0();
CAMLlocal1(ulv);
ulv = caml_swig_alloc(1,C_int64);
@ -308,7 +270,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(ulv);
}
static value caml_val_float( float f ) {
SWIGSTATIC value caml_val_float( float f ) {
CAMLparam0();
CAMLlocal1(fv);
fv = caml_swig_alloc(1,C_float);
@ -316,7 +278,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(fv);
}
static value caml_val_double( double d ) {
SWIGSTATIC value caml_val_double( double d ) {
CAMLparam0();
CAMLlocal1(fv);
fv = caml_swig_alloc(1,C_double);
@ -324,7 +286,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(fv);
}
static value caml_val_ptr( void *p, swig_type_info *info ) {
SWIGSTATIC value caml_val_ptr( void *p, swig_type_info *info ) {
CAMLparam0();
CAMLlocal1(vv);
vv = caml_swig_alloc(2,C_ptr);
@ -333,7 +295,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(vv);
}
static value caml_val_string( char *p ) {
SWIGSTATIC value caml_val_string( char *p ) {
CAMLparam0();
CAMLlocal1(vv);
if( !p ) CAMLreturn(caml_val_ptr( (void *)p, 0 ));
@ -342,7 +304,7 @@ static int caml_list_length( value lst ) {
CAMLreturn(vv);
}
static value caml_val_string_len( char *p, int len ) {
SWIGSTATIC value caml_val_string_len( char *p, int len ) {
CAMLparam0();
CAMLlocal1(vv);
if( !p || len < 0 ) CAMLreturn(caml_val_ptr( (void *)p, 0 ));
@ -352,14 +314,14 @@ static int caml_list_length( value lst ) {
CAMLreturn(vv);
}
static value caml_val_obj( void *v, char *object_type ) {
SWIGSTATIC value caml_val_obj( void *v, char *object_type ) {
CAMLparam0();
CAMLreturn(callback2(*caml_named_value("caml_create_object_fn"),
caml_val_ptr(v,SWIG_TypeQuery(object_type)),
copy_string(object_type)));
}
static long caml_long_val_full( value v, char *name ) {
SWIGSTATIC long caml_long_val_full( value v, char *name ) {
CAMLparam1(v);
if( !Is_block(v) ) return 0;
@ -395,11 +357,11 @@ static int caml_list_length( value lst ) {
}
}
static long caml_long_val( value v ) {
SWIGSTATIC long caml_long_val( value v ) {
return caml_long_val_full(v,0);
}
static double caml_double_val( value v ) {
SWIGSTATIC double caml_double_val( value v ) {
CAMLparam1(v);
if( !Is_block(v) ) return 0.0;
switch( Tag_val(v) ) {
@ -424,7 +386,7 @@ static int caml_list_length( value lst ) {
}
}
static int caml_ptr_val_internal( value v, void **out,
SWIGSTATIC int caml_ptr_val_internal( value v, void **out,
swig_type_info *descriptor ) {
CAMLparam1(v);
void *outptr = NULL;
@ -443,14 +405,15 @@ static int caml_list_length( value lst ) {
outdescr = (swig_type_info *)(long)Int64_val(Field(v,1));
break;
default:
outptr = (void *)caml_long_val(v);
*out = 0;
CAMLreturn(1);
break;
}
CAMLreturn(SWIG_GetPtr(outptr,out,descriptor,outdescr));
}
static void *caml_ptr_val( value v, swig_type_info *descriptor ) {
SWIGSTATIC void *caml_ptr_val( value v, swig_type_info *descriptor ) {
CAMLparam0();
void *out = NULL;
if( !caml_ptr_val_internal( v, &out, descriptor ) )
@ -459,11 +422,11 @@ static int caml_list_length( value lst ) {
failwith( "No appropriate conversion found." );
}
static char *caml_string_val( value v ) {
SWIGSTATIC char *caml_string_val( value v ) {
return (char *)caml_ptr_val( v, 0 );
}
static int caml_bool_check( value v ) {
SWIGSTATIC int caml_bool_check( value v ) {
CAMLparam1(v);
if( !Is_block(v) ) return 0;
@ -478,7 +441,7 @@ static int caml_list_length( value lst ) {
}
}
static int caml_int_check( value v ) {
SWIGSTATIC int caml_int_check( value v ) {
CAMLparam1(v);
if( !Is_block(v) ) return 0;
@ -499,7 +462,7 @@ static int caml_list_length( value lst ) {
}
}
static int caml_float_check( value v ) {
SWIGSTATIC int caml_float_check( value v ) {
CAMLparam1(v);
if( !Is_block(v) ) return 0;
@ -513,7 +476,7 @@ static int caml_list_length( value lst ) {
}
}
static int caml_ptr_check( value v ) {
SWIGSTATIC int caml_ptr_check( value v ) {
CAMLparam1(v);
if( !Is_block(v) ) return 0;

View file

@ -7,42 +7,6 @@
// Ocaml implementation
%include std_common.i
%include exception.i
// __getitem__ is required to raise an IndexError for for-loops to work
// other methods which can raise are made to throw an IndexError as well
%exception std::vector::__getitem__ {
try {
$action
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
}
}
%exception std::vector::__setitem__ {
try {
$action
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
}
}
%exception std::vector::__delitem__ {
try {
$action
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
}
}
%exception std::vector::pop {
try {
$action
} catch (std::out_of_range& e) {
SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what()));
}
}
// ------------------------------------------------------------------------
// std::vector
@ -72,19 +36,26 @@
#include <vector>
#include <algorithm>
#include <stdexcept>
%}
%}
// exported class
namespace std {
template<class T> class vector {
template <class T> class vector {
public:
vector(unsigned int size = 0);
vector(unsigned int size, const T& value);
vector(const vector<T>&);
unsigned int size() const;
bool empty() const;
void clear();
void push_back(const T& x);
T operator [] ( int f );
vector <T> &operator = ( vector <T> &other );
%extend {
void set( int i, T x ) {
(*self)[i] = x;
}
};
};
// Partial specialization for vectors of pointers. [ beazley ]
template<class T> class vector<T*> {
};
}
};

View file

@ -8,8 +8,8 @@
if( !Is_block($input) ) $1 = 0;
else {
switch( Tag_val($input) ) {
case C_char: $1 = 1;
default: $1 = 0;
case C_char: $1 = 1; break;
default: $1 = 0; break;
}
}
}
@ -18,8 +18,8 @@
if( !Is_block($input) ) $1 = 0;
else {
switch( Tag_val($input) ) {
case C_uchar: $1 = 1;
default: $1 = 0;
case C_uchar: $1 = 1; break;
default: $1 = 0; break;
}
}
}
@ -28,8 +28,8 @@
if( !Is_block($input) ) $1 = 0;
else {
switch( Tag_val($input) ) {
case C_short: $1 = 1;
default: $1 = 0;
case C_short: $1 = 1; break;
default: $1 = 0; break;
}
}
}
@ -38,8 +38,8 @@
if( !Is_block($input) ) $1 = 0;
else {
switch( Tag_val($input) ) {
case C_ushort: $1 = 1;
default: $1 = 0;
case C_ushort: $1 = 1; break;
default: $1 = 0; break;
}
}
}
@ -51,8 +51,8 @@
if( !Is_block($input) ) $1 = 0;
else {
switch( Tag_val($input) ) {
case C_int: $1 = 1;
default: $1 = 0;
case C_int: $1 = 1; break;
default: $1 = 0; break;
}
}
}
@ -61,9 +61,9 @@
if( !Is_block($input) ) $1 = 0;
else {
switch( Tag_val($input) ) {
case C_uint: $1 = 1;
case C_int32: $1 = 1;
default: $1 = 0;
case C_uint: $1 = 1; break;
case C_int32: $1 = 1; break;
default: $1 = 0; break;
}
}
}
@ -72,8 +72,8 @@
if( !Is_block($input) ) $1 = 0;
else {
switch( Tag_val($input) ) {
case C_int64: $1 = 1;
default: $1 = 0;
case C_int64: $1 = 1; break;
default: $1 = 0; break;
}
}
}
@ -82,8 +82,8 @@
if( !Is_block($input) ) $1 = 0;
else {
switch( Tag_val($input) ) {
case C_bool: $1 = 1;
default: $1 = 0;
case C_bool: $1 = 1; break;
default: $1 = 0; break;
}
}
}
@ -92,8 +92,8 @@
if( !Is_block($input) ) $1 = 0;
else {
switch( Tag_val($input) ) {
case C_float: $1 = 1;
default: $1 = 0;
case C_float: $1 = 1; break;
default: $1 = 0; break;
}
}
}
@ -102,8 +102,8 @@
if( !Is_block($input) ) $1 = 0;
else {
switch( Tag_val($input) ) {
case C_double: $1 = 1;
default: $1 = 0;
case C_double: $1 = 1; break;
default: $1 = 0; break;
}
}
}

View file

@ -48,7 +48,7 @@
}
%typemap(ocaml,out) SWIGTYPE * {
value *fromval = caml_named_value("create_$*1_type_from_ptr");
value *fromval = caml_named_value("create_$ntype_from_ptr");
if( fromval ) {
$result = callback(*fromval,caml_val_ptr((void *)$1,$descriptor));
} else {
@ -61,7 +61,7 @@
}
%typemap(ocaml,varout) SWIGTYPE * {
value *fromval = caml_named_value("create_$*1_type_from_ptr");
value *fromval = caml_named_value("create_$ntype_from_ptr");
if( fromval ) {
$result = callback(*fromval,caml_val_ptr((void *)$1,$descriptor));
} else {
@ -78,7 +78,7 @@
}
%typemap(ocaml,out) SWIGTYPE & {
value *fromval = caml_named_value("create_$*1_ltype_from_ptr");
value *fromval = caml_named_value("create_$ntype_from_ptr");
if( fromval ) {
$result = callback(*fromval,caml_val_ptr((void *) $1,$descriptor));
} else {
@ -86,7 +86,7 @@
}
}
#else
#endif
%typemap(ocaml,in) SWIGTYPE {
$1 = *(($&1_ltype) caml_ptr_val($input,$descriptor)) ;
@ -94,7 +94,7 @@
%typemap(ocaml,out) SWIGTYPE {
void *temp = calloc(1,sizeof($ltype));
value *fromval = caml_named_value("create_$ltype_from_ptr");
value *fromval = caml_named_value("create_$ntype_from_ptr");
*(($ltype *)temp) = $1;
if( fromval ) {
$result = callback(*fromval,caml_val_ptr((void *)temp,$descriptor));
@ -103,8 +103,6 @@
}
}
#endif
/* Arrays */
/* Enums */