1 %insert(mli) %{ 2 type _value = c_obj 3 %} 4 5 %insert(ml) %{ 6 type _value = c_obj 7 %} 8 9 %define %array_tmap_out(type,what,out_f) 10 %typemap(type) what [ANY] { 11 int i; 12 $result = caml_array_new($1_dim0); 13 for( i = 0; i < $1_dim0; i++ ) { 14 caml_array_set($result,i,out_f($1[i])); 15 } 16 } 17 %enddef 18 19 %define %array_tmap_in(type,what,in_f) 20 %typemap(type) what [ANY] { 21 int i; 22 $1 = ($*1_type *)malloc( $1_size ); 23 for( i = 0; i < $1_dim0 && i < caml_array_len($input); i++ ) { 24 $1[i] = in_f(caml_array_nth($input,i)); 25 } 26 } 27 28 %typemap(free) what [ANY] { 29 free( (void *)$1 ); 30 } 31 %enddef 32 33 %define %make_simple_array_typemap(type,out_f,in_f) 34 %array_tmap_out(out,type,out_f); 35 %array_tmap_out(varout,type,out_f); 36 %array_tmap_out(directorin,type,out_f); 37 38 %array_tmap_in(in,type,in_f); 39 %array_tmap_in(varin,type,in_f); 40 %array_tmap_in(directorout,type,in_f); 41 %enddef 42 43 %make_simple_array_typemap(bool,caml_val_bool,caml_long_val); 44 %make_simple_array_typemap(short,caml_val_short,caml_long_val); 45 %make_simple_array_typemap(unsigned short,caml_val_ushort,caml_long_val); 46 %make_simple_array_typemap(int,caml_val_int,caml_long_val); 47 %make_simple_array_typemap(unsigned int,caml_val_uint,caml_long_val); 48 %make_simple_array_typemap(long,caml_val_long,caml_long_val); 49 %make_simple_array_typemap(unsigned long,caml_val_ulong,caml_long_val); 50 %make_simple_array_typemap(size_t,caml_val_int,caml_long_val); 51 %make_simple_array_typemap(float,caml_val_float,caml_double_val); 52 %make_simple_array_typemap(double,caml_val_double,caml_double_val); 53 54 #ifdef __cplusplus 55 %typemap(in) SWIGTYPE [] { 56 int i; 57 58 $1 = new $*1_type [$1_dim0]; 59 for( i = 0; i < $1_dim0 && i < caml_array_len($input); i++ ) { 60 $1[i] = *(($*1_ltype *) 61 caml_ptr_val(caml_array_nth($input,i), 62 $*1_descriptor)) ; 63 } 64 } 65 #else 66 %typemap(in) SWIGTYPE [] { 67 int i; 68 69 $1 = ($*1_type *)malloc( $1_size ); 70 for( i = 0; i < $1_dim0 && i < caml_array_len($input); i++ ) { 71 $1[i] = *(($*1_ltype) 72 caml_ptr_val(caml_array_nth($input), 73 $*1_descriptor)); 74 } 75 } 76 #endif 77 78 %typemap(out) SWIGTYPE [] { 79 int i; 80 const CAML_VALUE *fromval = caml_named_value("create_$ntype_from_ptr"); 81 $result = caml_array_new($1_dim0); 82 83 for( i = 0; i < $1_dim0; i++ ) { 84 if( fromval ) { 85 caml_array_set 86 ($result, 87 i, 88 caml_callback(*fromval,caml_val_ptr((void *)&$1[i],$*1_descriptor))); 89 } else { 90 caml_array_set 91 ($result, 92 i, 93 caml_val_ptr ((void *)&$1[i],$&1_descriptor)); 94 } 95 } 96 } 97 98 %typemap(in) enum SWIGTYPE [] { 99 int i; 100 101 $1 = ($*1_type *)malloc( $1_size ); 102 for( i = 0; i < $1_dim0 && i < caml_array_len($input); i++ ) { 103 $1[i] = ($type) 104 caml_long_val_full(caml_array_nth($input), 105 "$type_marker"); 106 } 107 } 108 109 %typemap(out) enum SWIGTYPE [] { 110 int i; 111 $result = caml_array_new($1_dim0); 112 113 for( i = 0; i < $1_dim0; i++ ) { 114 caml_array_set 115 ($result, 116 i, 117 caml_callback2(*caml_named_value(SWIG_MODULE "_int_to_enum"), 118 *caml_named_value("$type_marker"), 119 Val_int($1[i]))); 120 } 121 } 122 123 #ifdef __cplusplus 124 %typemap(freearg) SWIGTYPE [ANY] { 125 delete [] $1; 126 } 127 #else 128 %typemap(freearg) SWIGTYPE [ANY] { 129 free( (void *)$1 ); 130 } 131 #endif 132