PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/guile/typemaps.i

#
Swig | 457 lines | 335 code | 69 blank | 53 comment | 0 complexity | d6e9422b1bdd36e950ca0e44e0c1dcd9 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * typemaps.i
  3. *
  4. * Guile-specific typemaps
  5. * ----------------------------------------------------------------------------- */
  6. /* Pointers */
  7. %typemap(in) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] {
  8. $1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, 0);
  9. }
  10. %typemap(freearg) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] "";
  11. %typemap(in) void * {
  12. $1 = ($1_ltype)SWIG_MustGetPtr($input, NULL, $argnum, 0);
  13. }
  14. %typemap(freearg) void * "";
  15. %typemap(varin) SWIGTYPE * {
  16. $1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, 1, 0);
  17. }
  18. %typemap(varin) SWIGTYPE & {
  19. $1 = *(($1_ltype)SWIG_MustGetPtr($input, $descriptor, 1, 0));
  20. }
  21. %typemap(varin) SWIGTYPE [] {
  22. scm_wrong_type_arg((char *) FUNC_NAME, 1, $input);
  23. }
  24. %typemap(varin) SWIGTYPE [ANY] {
  25. void *temp;
  26. int ii;
  27. $1_basetype *b = 0;
  28. temp = SWIG_MustGetPtr($input, $1_descriptor, 1, 0);
  29. b = ($1_basetype *) $1;
  30. for (ii = 0; ii < $1_size; ii++) b[ii] = *(($1_basetype *) temp + ii);
  31. }
  32. %typemap(varin) void * {
  33. $1 = SWIG_MustGetPtr($input, NULL, 1, 0);
  34. }
  35. %typemap(out) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] {
  36. $result = SWIG_NewPointerObj ($1, $descriptor, $owner);
  37. }
  38. %typemap(out) SWIGTYPE *DYNAMIC, SWIGTYPE &DYNAMIC {
  39. swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor,(void **) &$1);
  40. $result = SWIG_NewPointerObj ($1, ty, $owner);
  41. }
  42. %typemap(varout) SWIGTYPE *, SWIGTYPE [] {
  43. $result = SWIG_NewPointerObj ($1, $descriptor, 0);
  44. }
  45. %typemap(varout) SWIGTYPE & {
  46. $result = SWIG_NewPointerObj((void *) &$1, $1_descriptor, 0);
  47. }
  48. %typemap(throws) SWIGTYPE {
  49. $&ltype temp = new $ltype($1);
  50. scm_throw(gh_symbol2scm((char *) "swig-exception"),
  51. gh_list(SWIG_NewPointerObj(temp, $&descriptor, 1),
  52. SCM_UNDEFINED));
  53. }
  54. %typemap(throws) SWIGTYPE & {
  55. scm_throw(gh_symbol2scm((char *) "swig-exception"),
  56. gh_list(SWIG_NewPointerObj(&$1, $descriptor, 1),
  57. SCM_UNDEFINED));
  58. }
  59. %typemap(throws) SWIGTYPE * {
  60. scm_throw(gh_symbol2scm((char *) "swig-exception"),
  61. gh_list(SWIG_NewPointerObj($1, $descriptor, 1),
  62. SCM_UNDEFINED));
  63. }
  64. %typemap(throws) SWIGTYPE [] {
  65. scm_throw(gh_symbol2scm((char *) "swig-exception"),
  66. gh_list(SWIG_NewPointerObj($1, $descriptor, 1),
  67. SCM_UNDEFINED));
  68. }
  69. /* Change of object ownership, and interaction of destructor-like functions and the
  70. garbage-collector */
  71. %typemap(in, doc="$NAME is of type <$type> and gets destroyed by the function") SWIGTYPE *DESTROYED {
  72. $1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, 0);
  73. }
  74. %typemap(freearg) SWIGTYPE *DESTROYED {
  75. SWIG_Guile_MarkPointerDestroyed($input);
  76. }
  77. %typemap(in, doc="$NAME is of type <$type> and is consumed by the function") SWIGTYPE *CONSUMED {
  78. $1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, 0);
  79. SWIG_Guile_MarkPointerNoncollectable($input);
  80. }
  81. /* Pass-by-value */
  82. %typemap(in) SWIGTYPE($&1_ltype argp) {
  83. argp = ($&1_ltype)SWIG_MustGetPtr($input, $&1_descriptor, $argnum, 0);
  84. $1 = *argp;
  85. }
  86. %typemap(varin) SWIGTYPE {
  87. $&1_ltype argp;
  88. argp = ($&1_ltype)SWIG_MustGetPtr($input, $&1_descriptor, 1, 0);
  89. $1 = *argp;
  90. }
  91. %typemap(out) SWIGTYPE
  92. #ifdef __cplusplus
  93. {
  94. $&1_ltype resultptr;
  95. resultptr = new $1_ltype((const $1_ltype &) $1);
  96. $result = SWIG_NewPointerObj (resultptr, $&1_descriptor, 1);
  97. }
  98. #else
  99. {
  100. $&1_ltype resultptr;
  101. resultptr = ($&1_ltype) malloc(sizeof($1_type));
  102. memmove(resultptr, &$1, sizeof($1_type));
  103. $result = SWIG_NewPointerObj(resultptr, $&1_descriptor, 1);
  104. }
  105. #endif
  106. %typemap(varout) SWIGTYPE
  107. #ifdef __cplusplus
  108. {
  109. $&1_ltype resultptr;
  110. resultptr = new $1_ltype((const $1_ltype&) $1);
  111. $result = SWIG_NewPointerObj (resultptr, $&1_descriptor, 0);
  112. }
  113. #else
  114. {
  115. $&1_ltype resultptr;
  116. resultptr = ($&1_ltype) malloc(sizeof($1_type));
  117. memmove(resultptr, &$1, sizeof($1_type));
  118. $result = SWIG_NewPointerObj(resultptr, $&1_descriptor, 0);
  119. }
  120. #endif
  121. /* Enums */
  122. %typemap(in) enum SWIGTYPE { $1 = ($1_type) gh_scm2int($input); }
  123. /* The complicated construction below needed to deal with anonymous
  124. enums, which cannot be cast to. */
  125. %typemap(varin) enum SWIGTYPE {
  126. if (sizeof(int) != sizeof($1)) {
  127. scm_error(scm_str2symbol("swig-error"),
  128. (char *) FUNC_NAME,
  129. (char *) "enum variable '$name' cannot be set",
  130. SCM_EOL, SCM_BOOL_F);
  131. }
  132. * (int *) &($1) = gh_scm2int($input);
  133. }
  134. %typemap(out) enum SWIGTYPE { $result = gh_int2scm($1); }
  135. %typemap(varout) enum SWIGTYPE { $result = gh_int2scm($1); }
  136. %typemap(throws) enum SWIGTYPE {
  137. scm_throw(gh_symbol2scm((char *) "swig-exception"),
  138. gh_list(gh_int2scm($1), SCM_UNDEFINED));
  139. }
  140. /* The SIMPLE_MAP_WITH_EXPR macro below defines the whole set of
  141. typemaps needed for simple types.
  142. -- SCM_TO_C_EXPR is a C expression that translates the Scheme value
  143. "swig_scm_value" to a C value.
  144. -- C_TO_SCM_EXPR is a C expression that translates the C value
  145. "swig_c_value" to a Scheme value. */
  146. %define SIMPLE_MAP_WITH_EXPR(C_NAME, SCM_TO_C_EXPR, C_TO_SCM_EXPR, SCM_NAME)
  147. %typemap (in, doc="$NAME is of type <" #SCM_NAME ">") C_NAME
  148. { SCM swig_scm_value = $input;
  149. $1 = SCM_TO_C_EXPR; }
  150. %typemap (varin, doc="NEW-VALUE is of type <" #SCM_NAME ">") C_NAME
  151. { SCM swig_scm_value = $input;
  152. $1 = SCM_TO_C_EXPR; }
  153. %typemap (out, doc="<" #SCM_NAME ">") C_NAME
  154. { C_NAME swig_c_value = $1;
  155. $result = C_TO_SCM_EXPR; }
  156. %typemap (varout, doc="<" #SCM_NAME ">") C_NAME
  157. { C_NAME swig_c_value = $1;
  158. $result = C_TO_SCM_EXPR; }
  159. /* INPUT and OUTPUT */
  160. %typemap (in, doc="$NAME is of type <" #SCM_NAME ">)")
  161. C_NAME *INPUT(C_NAME temp) {
  162. SCM swig_scm_value = $input;
  163. temp = (C_NAME) SCM_TO_C_EXPR; $1 = &temp; }
  164. %typemap (in,numinputs=0) C_NAME *OUTPUT (C_NAME temp)
  165. {$1 = &temp;}
  166. %typemap (argout,doc="$name (of type <" #SCM_NAME ">)") C_NAME *OUTPUT
  167. { C_NAME swig_c_value = *$1;
  168. SWIG_APPEND_VALUE(C_TO_SCM_EXPR); }
  169. %typemap (in) C_NAME *BOTH = C_NAME *INPUT;
  170. %typemap (argout) C_NAME *BOTH = C_NAME *OUTPUT;
  171. %typemap (in) C_NAME *INOUT = C_NAME *INPUT;
  172. %typemap (argout) C_NAME *INOUT = C_NAME *OUTPUT;
  173. /* Const primitive references. Passed by value */
  174. %typemap(in, doc="$NAME is of type <" #SCM_NAME ">") const C_NAME & (C_NAME temp)
  175. { SCM swig_scm_value = $input;
  176. temp = SCM_TO_C_EXPR;
  177. $1 = &temp; }
  178. %typemap(out, doc="<" #SCM_NAME ">") const C_NAME &
  179. { C_NAME swig_c_value = *$1;
  180. $result = C_TO_SCM_EXPR; }
  181. /* Throw typemap */
  182. %typemap(throws) C_NAME {
  183. C_NAME swig_c_value = $1;
  184. scm_throw(gh_symbol2scm((char *) "swig-exception"),
  185. gh_list(C_TO_SCM_EXPR, SCM_UNDEFINED));
  186. }
  187. %enddef
  188. /* The SIMPLE_MAP macro below defines the whole set of typemaps needed
  189. for simple types. It generates slightly simpler code than the
  190. macro above, but it is only suitable for very simple conversion
  191. expressions. */
  192. %define SIMPLE_MAP(C_NAME, SCM_TO_C, C_TO_SCM, SCM_NAME)
  193. %typemap (in, doc="$NAME is of type <" #SCM_NAME ">")
  194. C_NAME {$1 = ($1_ltype) SCM_TO_C($input);}
  195. %typemap (varin, doc="NEW-VALUE is of type <" #SCM_NAME ">")
  196. C_NAME {$1 = ($1_ltype) SCM_TO_C($input);}
  197. %typemap (out, doc="<" #SCM_NAME ">")
  198. C_NAME {$result = C_TO_SCM($1);}
  199. %typemap (varout, doc="<" #SCM_NAME ">")
  200. C_NAME {$result = C_TO_SCM($1);}
  201. /* INPUT and OUTPUT */
  202. %typemap (in, doc="$NAME is of type <" #SCM_NAME ">)")
  203. C_NAME *INPUT(C_NAME temp), C_NAME &INPUT(C_NAME temp) {
  204. temp = (C_NAME) SCM_TO_C($input); $1 = &temp;
  205. }
  206. %typemap (in,numinputs=0) C_NAME *OUTPUT (C_NAME temp), C_NAME &OUTPUT(C_NAME temp)
  207. {$1 = &temp;}
  208. %typemap (argout,doc="$name (of type <" #SCM_NAME ">)") C_NAME *OUTPUT, C_NAME &OUTPUT
  209. {SWIG_APPEND_VALUE(C_TO_SCM(*$1));}
  210. %typemap (in) C_NAME *BOTH = C_NAME *INPUT;
  211. %typemap (argout) C_NAME *BOTH = C_NAME *OUTPUT;
  212. %typemap (in) C_NAME *INOUT = C_NAME *INPUT;
  213. %typemap (argout) C_NAME *INOUT = C_NAME *OUTPUT;
  214. %typemap (in) C_NAME &INOUT = C_NAME &INPUT;
  215. %typemap (argout) C_NAME &INOUT = C_NAME &OUTPUT;
  216. /* Const primitive references. Passed by value */
  217. %typemap(in, doc="$NAME is of type <" #SCM_NAME ">") const C_NAME & (C_NAME temp) {
  218. temp = SCM_TO_C($input);
  219. $1 = ($1_ltype) &temp;
  220. }
  221. %typemap(out, doc="<" #SCM_NAME ">") const C_NAME & {
  222. $result = C_TO_SCM(*$1);
  223. }
  224. /* Throw typemap */
  225. %typemap(throws) C_NAME {
  226. scm_throw(gh_symbol2scm((char *) "swig-exception"),
  227. gh_list(C_TO_SCM($1), SCM_UNDEFINED));
  228. }
  229. %enddef
  230. SIMPLE_MAP(bool, gh_scm2bool, gh_bool2scm, boolean);
  231. SIMPLE_MAP(char, gh_scm2char, gh_char2scm, char);
  232. SIMPLE_MAP(unsigned char, gh_scm2char, gh_char2scm, char);
  233. SIMPLE_MAP(signed char, gh_scm2char, gh_char2scm, char);
  234. SIMPLE_MAP(int, gh_scm2int, gh_int2scm, integer);
  235. SIMPLE_MAP(short, gh_scm2short, gh_int2scm, integer);
  236. SIMPLE_MAP(long, gh_scm2long, gh_long2scm, integer);
  237. SIMPLE_MAP(ptrdiff_t, gh_scm2long, gh_long2scm, integer);
  238. SIMPLE_MAP(unsigned int, gh_scm2uint, gh_ulong2scm, integer);
  239. SIMPLE_MAP(unsigned short, gh_scm2ushort, gh_ulong2scm, integer);
  240. SIMPLE_MAP(unsigned long, gh_scm2ulong, gh_ulong2scm, integer);
  241. SIMPLE_MAP(size_t, gh_scm2ulong, gh_ulong2scm, integer);
  242. SIMPLE_MAP(float, gh_scm2double, gh_double2scm, real);
  243. SIMPLE_MAP(double, gh_scm2double, gh_double2scm, real);
  244. // SIMPLE_MAP(char *, SWIG_scm2str, gh_str02scm, string);
  245. // SIMPLE_MAP(const char *, SWIG_scm2str, gh_str02scm, string);
  246. /* Define long long typemaps -- uses functions that are only defined
  247. in recent versions of Guile, availability also depends on Guile's
  248. configuration. */
  249. SIMPLE_MAP(long long, gh_scm2long_long, gh_long_long2scm, integer);
  250. SIMPLE_MAP(unsigned long long, gh_scm2ulong_long, gh_ulong_long2scm, integer);
  251. /* Strings */
  252. %typemap (in, doc="$NAME is a string") char *(int must_free = 0) {
  253. $1 = ($1_ltype)SWIG_scm2str($input);
  254. must_free = 1;
  255. }
  256. %typemap (varin, doc="NEW-VALUE is a string") char * {$1 = ($1_ltype)SWIG_scm2str($input);}
  257. %typemap (out, doc="<string>") char * {$result = gh_str02scm((const char *)$1);}
  258. %typemap (varout, doc="<string>") char * {$result = gh_str02scm($1);}
  259. %typemap (in, doc="$NAME is a string") char **INPUT(char * temp, int must_free = 0) {
  260. temp = (char *) SWIG_scm2str($input); $1 = &temp;
  261. must_free = 1;
  262. }
  263. %typemap (in,numinputs=0) char **OUTPUT (char * temp)
  264. {$1 = &temp;}
  265. %typemap (argout,doc="$NAME (a string)") char **OUTPUT
  266. {SWIG_APPEND_VALUE(gh_str02scm(*$1));}
  267. %typemap (in) char **BOTH = char **INPUT;
  268. %typemap (argout) char **BOTH = char **OUTPUT;
  269. %typemap (in) char **INOUT = char **INPUT;
  270. %typemap (argout) char **INOUT = char **OUTPUT;
  271. /* SWIG_scm2str makes a malloc'ed copy of the string, so get rid of it after
  272. the function call. */
  273. %typemap (freearg) char * "if (must_free$argnum && $1) SWIG_free($1);";
  274. %typemap (freearg) char **INPUT, char **BOTH "if (must_free$argnum && (*$1)) SWIG_free(*$1);"
  275. %typemap (freearg) char **OUTPUT "SWIG_free(*$1);"
  276. /* But this shall not apply if we try to pass a single char by
  277. reference. */
  278. %typemap (freearg) char *OUTPUT, char *BOTH "";
  279. /* If we set a string variable, delete the old result first, unless const. */
  280. %typemap (varin) char * {
  281. if ($1) free($1);
  282. $1 = ($1_ltype) SWIG_scm2str($input);
  283. }
  284. %typemap (varin) const char * {
  285. $1 = ($1_ltype) SWIG_scm2str($input);
  286. }
  287. %typemap(throws) char * {
  288. scm_throw(gh_symbol2scm((char *) "swig-exception"),
  289. gh_list(gh_str02scm($1), SCM_UNDEFINED));
  290. }
  291. /* Void */
  292. %typemap (out,doc="") void "gswig_result = SCM_UNSPECIFIED;";
  293. /* SCM is passed through */
  294. typedef unsigned long SCM;
  295. %typemap (in) SCM "$1=$input;";
  296. %typemap (out) SCM "$result=$1;";
  297. %typecheck(SWIG_TYPECHECK_POINTER) SCM "$1=1;";
  298. /* ------------------------------------------------------------
  299. * String & length
  300. * ------------------------------------------------------------ */
  301. %typemap(in) (char *STRING, int LENGTH), (char *STRING, size_t LENGTH) {
  302. size_t temp;
  303. $1 = ($1_ltype) gh_scm2newstr($input, &temp);
  304. $2 = ($2_ltype) temp;
  305. }
  306. /* ------------------------------------------------------------
  307. * CLASS::* (member function pointer) typemaps
  308. * taken from typemaps/swigtype.swg
  309. * ------------------------------------------------------------ */
  310. #define %set_output(obj) $result = obj
  311. #define %set_varoutput(obj) $result = obj
  312. #define %argument_fail(code, type, name, argn) scm_wrong_type_arg((char *) FUNC_NAME, argn, $input);
  313. #define %as_voidptr(ptr) (void*)(ptr)
  314. %typemap(in) SWIGTYPE (CLASS::*) {
  315. int res = SWIG_ConvertMember($input, %as_voidptr(&$1), sizeof($type),$descriptor);
  316. if (!SWIG_IsOK(res)) {
  317. %argument_fail(res,"$type",$symname, $argnum);
  318. }
  319. }
  320. %typemap(out,noblock=1) SWIGTYPE (CLASS::*) {
  321. %set_output(SWIG_NewMemberObj(%as_voidptr(&$1), sizeof($type), $descriptor));
  322. }
  323. %typemap(varin) SWIGTYPE (CLASS::*) {
  324. int res = SWIG_ConvertMember($input,%as_voidptr(&$1), sizeof($type), $descriptor);
  325. if (!SWIG_IsOK(res)) {
  326. scm_wrong_type_arg((char *) FUNC_NAME, 1, $input);
  327. }
  328. }
  329. %typemap(varout,noblock=1) SWIGTYPE (CLASS::*) {
  330. %set_varoutput(SWIG_NewMemberObj(%as_voidptr(&$1), sizeof($type), $descriptor));
  331. }
  332. /* ------------------------------------------------------------
  333. * Typechecking rules
  334. * ------------------------------------------------------------ */
  335. /* adapted from python.swg */
  336. %typecheck(SWIG_TYPECHECK_INTEGER)
  337. int, short, long,
  338. unsigned int, unsigned short, unsigned long,
  339. signed char, unsigned char,
  340. long long, unsigned long long,
  341. size_t, ptrdiff_t,
  342. std::size_t, std::ptrdiff_t,
  343. const int &, const short &, const long &,
  344. const unsigned int &, const unsigned short &, const unsigned long &,
  345. const long long &, const unsigned long long &,
  346. const size_t &, const ptrdiff_t &,
  347. const std::size_t &, const std::ptrdiff_t &,
  348. enum SWIGTYPE
  349. {
  350. $1 = SCM_NFALSEP(scm_integer_p($input)) && SCM_NFALSEP(scm_exact_p($input))? 1 : 0;
  351. }
  352. %typecheck(SWIG_TYPECHECK_BOOL)
  353. bool, bool&, const bool&
  354. {
  355. $1 = SCM_BOOLP($input) ? 1 : 0;
  356. }
  357. %typecheck(SWIG_TYPECHECK_DOUBLE)
  358. float, double,
  359. const float &, const double &
  360. {
  361. $1 = SCM_NFALSEP(scm_real_p($input)) ? 1 : 0;
  362. }
  363. %typecheck(SWIG_TYPECHECK_CHAR) char {
  364. $1 = SCM_CHARP($input) ? 1 : 0;
  365. }
  366. %typecheck(SWIG_TYPECHECK_STRING) char * {
  367. $1 = SCM_STRINGP($input) ? 1 : 0;
  368. }
  369. %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] {
  370. void *ptr;
  371. int res = SWIG_ConvertPtr($input, &ptr, $1_descriptor, 0);
  372. $1 = SWIG_CheckState(res);
  373. }
  374. %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE {
  375. void *ptr;
  376. int res = SWIG_ConvertPtr($input, &ptr, $&descriptor, 0);
  377. $1 = SWIG_CheckState(res);
  378. }
  379. %typecheck(SWIG_TYPECHECK_VOIDPTR) void * {
  380. void *ptr;
  381. int res = SWIG_ConvertPtr($input, &ptr, 0, 0);
  382. $1 = SWIG_CheckState(res);
  383. }
  384. /* Array reference typemaps */
  385. %apply SWIGTYPE & { SWIGTYPE ((&)[ANY]) }
  386. /* const pointers */
  387. %apply SWIGTYPE * { SWIGTYPE *const }
  388. /* typemaps.i ends here */