/trunk/Lib/ocaml/typemaps.i

# · Swig · 335 lines · 247 code · 51 blank · 37 comment · 0 complexity · f68df14129b0bb8f37f4162ceda8ec1d MD5 · raw file

  1. /* -----------------------------------------------------------------------------
  2. * typemaps.i
  3. *
  4. * The Ocaml module handles all types uniformly via typemaps. Here
  5. * are the definitions.
  6. * ----------------------------------------------------------------------------- */
  7. /* Pointers */
  8. %typemap(in) void ""
  9. %typemap(out) void "$result = Val_int(0);"
  10. %typemap(in) void * {
  11. $1 = caml_ptr_val($input,$descriptor);
  12. }
  13. %typemap(varin) void * {
  14. $1 = ($ltype)caml_ptr_val($input,$descriptor);
  15. }
  16. %typemap(out) void * {
  17. $result = caml_val_ptr($1,$descriptor);
  18. }
  19. %typemap(varout) void * {
  20. $result = caml_val_ptr($1,$descriptor);
  21. }
  22. #ifdef __cplusplus
  23. %typemap(in) SWIGTYPE & {
  24. /* %typemap(in) SWIGTYPE & */
  25. $1 = ($ltype) caml_ptr_val($input,$1_descriptor);
  26. }
  27. %typemap(varin) SWIGTYPE & {
  28. /* %typemap(varin) SWIGTYPE & */
  29. $1 = *(($ltype) caml_ptr_val($input,$1_descriptor));
  30. }
  31. %typemap(out) SWIGTYPE & {
  32. /* %typemap(out) SWIGTYPE & */
  33. CAML_VALUE *fromval = caml_named_value("create_$ntype_from_ptr");
  34. if( fromval ) {
  35. $result = callback(*fromval,caml_val_ptr((void *) &$1,$1_descriptor));
  36. } else {
  37. $result = caml_val_ptr ((void *) &$1,$1_descriptor);
  38. }
  39. }
  40. #if 0
  41. %typemap(argout) SWIGTYPE & {
  42. CAML_VALUE *fromval = caml_named_value("create_$ntype_from_ptr");
  43. if( fromval ) {
  44. swig_result =
  45. caml_list_append(swig_result,
  46. callback(*fromval,caml_val_ptr((void *) $1,
  47. $1_descriptor)));
  48. } else {
  49. swig_result =
  50. caml_list_append(swig_result,
  51. caml_val_ptr ((void *) $1,$1_descriptor));
  52. }
  53. }
  54. #endif
  55. %typemap(argout) const SWIGTYPE & { }
  56. %typemap(in) SWIGTYPE {
  57. $1 = *(($&1_ltype) caml_ptr_val($input,$&1_descriptor)) ;
  58. }
  59. %typemap(out) SWIGTYPE {
  60. /* %typemap(out) SWIGTYPE */
  61. $&1_ltype temp = new $ltype((const $1_ltype &) $1);
  62. CAML_VALUE *fromval = caml_named_value("create_$ntype_from_ptr");
  63. if( fromval ) {
  64. $result = callback(*fromval,caml_val_ptr((void *)temp,$&1_descriptor));
  65. } else {
  66. $result = caml_val_ptr ((void *)temp,$&1_descriptor);
  67. }
  68. }
  69. %typemap(in) char *& (char *temp) {
  70. /* %typemap(in) char *& */
  71. temp = (char*)caml_val_ptr($1,$descriptor);
  72. $1 = &temp;
  73. }
  74. %typemap(argout) char *& {
  75. /* %typemap(argout) char *& */
  76. swig_result = caml_list_append(swig_result,caml_val_string_len(*$1, strlen(*$1)));
  77. }
  78. #else
  79. %typemap(in) SWIGTYPE {
  80. $1 = *(($&1_ltype) caml_ptr_val($input,$&1_descriptor)) ;
  81. }
  82. %typemap(out) SWIGTYPE {
  83. /* %typemap(out) SWIGTYPE */
  84. void *temp = calloc(1,sizeof($ltype));
  85. CAML_VALUE *fromval = caml_named_value("create_$ntype_from_ptr");
  86. memmove( temp, &$1, sizeof( $1_type ) );
  87. if( fromval ) {
  88. $result = callback(*fromval,caml_val_ptr((void *)temp,$&1_descriptor));
  89. } else {
  90. $result = caml_val_ptr ((void *)temp,$&1_descriptor);
  91. }
  92. }
  93. %apply SWIGTYPE { const SWIGTYPE & };
  94. #endif
  95. /* The SIMPLE_MAP macro below defines the whole set of typemaps needed
  96. for simple types. */
  97. %define SIMPLE_MAP(C_NAME, C_TO_MZ, MZ_TO_C)
  98. /* In */
  99. %typemap(in) C_NAME {
  100. $1 = MZ_TO_C($input);
  101. }
  102. %typemap(varin) C_NAME {
  103. $1 = MZ_TO_C($input);
  104. }
  105. %typemap(in) C_NAME & ($*1_ltype temp) {
  106. temp = ($*1_ltype) MZ_TO_C($input);
  107. $1 = &temp;
  108. }
  109. %typemap(varin) C_NAME & {
  110. $1 = MZ_TO_C($input);
  111. }
  112. %typemap(directorout) C_NAME {
  113. $1 = MZ_TO_C($input);
  114. }
  115. %typemap(in) C_NAME *INPUT ($*1_ltype temp) {
  116. temp = ($*1_ltype) MZ_TO_C($input);
  117. $1 = &temp;
  118. }
  119. %typemap(in,numinputs=0) C_NAME *OUTPUT ($*1_ltype temp) {
  120. $1 = &temp;
  121. }
  122. /* Out */
  123. %typemap(out) C_NAME {
  124. $result = C_TO_MZ($1);
  125. }
  126. %typemap(varout) C_NAME {
  127. $result = C_TO_MZ($1);
  128. }
  129. %typemap(varout) C_NAME & {
  130. /* %typemap(varout) C_NAME & (generic) */
  131. $result = C_TO_MZ($1);
  132. }
  133. %typemap(argout) C_NAME *OUTPUT {
  134. swig_result = caml_list_append(swig_result,C_TO_MZ((long)*$1));
  135. }
  136. %typemap(out) C_NAME & {
  137. /* %typemap(out) C_NAME & (generic) */
  138. $result = C_TO_MZ(*$1);
  139. }
  140. %typemap(argout) C_NAME & {
  141. swig_result = caml_list_append(swig_result,C_TO_MZ((long)*$1));
  142. }
  143. %typemap(directorin) C_NAME {
  144. args = caml_list_append(args,C_TO_MZ($1));
  145. }
  146. %enddef
  147. SIMPLE_MAP(bool, caml_val_bool, caml_long_val);
  148. SIMPLE_MAP(oc_bool, caml_val_bool, caml_long_val);
  149. SIMPLE_MAP(char, caml_val_char, caml_long_val);
  150. SIMPLE_MAP(signed char, caml_val_char, caml_long_val);
  151. SIMPLE_MAP(unsigned char, caml_val_uchar, caml_long_val);
  152. SIMPLE_MAP(int, caml_val_int, caml_long_val);
  153. SIMPLE_MAP(short, caml_val_short, caml_long_val);
  154. SIMPLE_MAP(wchar_t, caml_val_short, caml_long_val);
  155. SIMPLE_MAP(long, caml_val_long, caml_long_val);
  156. SIMPLE_MAP(ptrdiff_t, caml_val_int, caml_long_val);
  157. SIMPLE_MAP(unsigned int, caml_val_uint, caml_long_val);
  158. SIMPLE_MAP(unsigned short, caml_val_ushort, caml_long_val);
  159. SIMPLE_MAP(unsigned long, caml_val_ulong, caml_long_val);
  160. SIMPLE_MAP(size_t, caml_val_int, caml_long_val);
  161. SIMPLE_MAP(float, caml_val_float, caml_double_val);
  162. SIMPLE_MAP(double, caml_val_double, caml_double_val);
  163. SIMPLE_MAP(long long,caml_val_ulong,caml_long_val);
  164. SIMPLE_MAP(unsigned long long,caml_val_ulong,caml_long_val);
  165. /* Void */
  166. %typemap(out) void "$result = Val_unit;";
  167. /* Pass through value */
  168. %typemap (in) value,caml::value,CAML_VALUE "$1=$input;";
  169. %typemap (out) value,caml::value,CAML_VALUE "$result=$1;";
  170. /* Arrays */
  171. %typemap(in) ArrayCarrier * {
  172. $1 = ($ltype)caml_ptr_val($input,$1_descriptor);
  173. }
  174. %typemap(out) ArrayCarrier * {
  175. CAML_VALUE *fromval = caml_named_value("create_$ntype_from_ptr");
  176. if( fromval ) {
  177. $result = callback(*fromval,caml_val_ptr((void *)$1,$1_descriptor));
  178. } else {
  179. $result = caml_val_ptr ((void *)$1,$1_descriptor);
  180. }
  181. }
  182. #if 0
  183. %include <carray.i>
  184. #endif
  185. /* Handle char arrays as strings */
  186. %define %char_ptr_in(how)
  187. %typemap(how) char *, signed char *, unsigned char * {
  188. /* %typemap(how) char * ... */
  189. $1 = ($ltype)caml_string_val($input);
  190. }
  191. /* Again work around the empty array bound bug */
  192. %typemap(how) char [ANY], signed char [ANY], unsigned char [ANY] {
  193. /* %typemap(how) char [ANY] ... */
  194. char *temp = caml_string_val($input);
  195. strcpy((char *)$1,temp);
  196. /* strncpy would be better but we might not have an array size */
  197. }
  198. %enddef
  199. %char_ptr_in(in);
  200. %char_ptr_in(varin);
  201. %char_ptr_in(directorout);
  202. %define %char_ptr_out(how)
  203. %typemap(how)
  204. char *, signed char *, unsigned char *,
  205. const char *, const signed char *, const unsigned char * {
  206. $result = caml_val_string((char *)$1);
  207. }
  208. /* I'd like to use the length here but can't because it might be empty */
  209. %typemap(how)
  210. char [ANY], signed char [ANY], unsigned char [ANY],
  211. const char [ANY], const signed char [ANY], const unsigned char [ANY] {
  212. $result = caml_val_string((char *)$1);
  213. }
  214. %enddef
  215. %char_ptr_out(out);
  216. %char_ptr_out(varout);
  217. %char_ptr_out(directorin);
  218. %define %swigtype_ptr_in(how)
  219. %typemap(how) SWIGTYPE * {
  220. /* %typemap(how) SWIGTYPE * */
  221. $1 = ($ltype)caml_ptr_val($input,$1_descriptor);
  222. }
  223. %typemap(how) SWIGTYPE (CLASS::*) {
  224. /* %typemap(how) SWIGTYPE (CLASS::*) */
  225. void *v = caml_ptr_val($input,$1_descriptor);
  226. memcpy(& $1, &v, sizeof(v));
  227. }
  228. %enddef
  229. %define %swigtype_ptr_out(how)
  230. %typemap(out) SWIGTYPE * {
  231. /* %typemap(how) SWIGTYPE *, SWIGTYPE (CLASS::*) */
  232. CAML_VALUE *fromval = caml_named_value("create_$ntype_from_ptr");
  233. if( fromval ) {
  234. $result = callback(*fromval,caml_val_ptr((void *)$1,$1_descriptor));
  235. } else {
  236. $result = caml_val_ptr ((void *)$1,$1_descriptor);
  237. }
  238. }
  239. %typemap(how) SWIGTYPE (CLASS::*) {
  240. /* %typemap(how) SWIGTYPE *, SWIGTYPE (CLASS::*) */
  241. void *v;
  242. memcpy(&v,& $1, sizeof(void *));
  243. $result = caml_val_ptr (v,$1_descriptor);
  244. }
  245. %enddef
  246. %swigtype_ptr_in(in);
  247. %swigtype_ptr_in(varin);
  248. %swigtype_ptr_in(directorout);
  249. %swigtype_ptr_out(out);
  250. %swigtype_ptr_out(varout);
  251. %swigtype_ptr_out(directorin);
  252. %define %swigtype_array_fail(how,msg)
  253. %typemap(how) SWIGTYPE [] {
  254. failwith(msg);
  255. }
  256. %enddef
  257. %swigtype_array_fail(in,"Array arguments for arbitrary types need a typemap");
  258. %swigtype_array_fail(varin,"Assignment to global arrays for arbitrary types need a typemap");
  259. %swigtype_array_fail(out,"Array arguments for arbitrary types need a typemap");
  260. %swigtype_array_fail(varout,"Array variables need a typemap");
  261. %swigtype_array_fail(directorin,"Array results with arbitrary types need a typemap");
  262. %swigtype_array_fail(directorout,"Array arguments with arbitrary types need a typemap");
  263. /* C++ References */
  264. /* Enums */
  265. %define %swig_enum_in(how)
  266. %typemap(how) enum SWIGTYPE {
  267. $1 = ($type)caml_long_val_full($input,"$type_marker");
  268. }
  269. %enddef
  270. %define %swig_enum_out(how)
  271. %typemap(how) enum SWIGTYPE {
  272. $result = callback2(*caml_named_value(SWIG_MODULE "_int_to_enum"),*caml_named_value("$type_marker"),Val_int((int)$1));
  273. }
  274. %enddef
  275. %swig_enum_in(in)
  276. %swig_enum_in(varin)
  277. %swig_enum_in(directorout)
  278. %swig_enum_out(out)
  279. %swig_enum_out(varout)
  280. %swig_enum_out(directorin)
  281. /* Array reference typemaps */
  282. %apply SWIGTYPE & { SWIGTYPE ((&)[ANY]) }
  283. /* const pointers */
  284. %apply SWIGTYPE * { SWIGTYPE *const }