/tags/rel-1-3-28/SWIG/Lib/perl5/typemaps.i

# · Swig · 380 lines · 213 code · 39 blank · 128 comment · 0 complexity · 9762bea528b577f041801c9a1522d25b MD5 · raw file

  1. #if !defined(SWIG_USE_OLD_TYPEMAPS)
  2. %include <typemaps/typemaps.swg>
  3. #else
  4. //
  5. // SWIG Typemap library
  6. // Dave Beazley
  7. // May 5, 1997
  8. //
  9. // Perl5 implementation
  10. //
  11. // This library provides standard typemaps for modifying SWIG's behavior.
  12. // With enough entries in this file, I hope that very few people actually
  13. // ever need to write a typemap.
  14. //
  15. /*
  16. The SWIG typemap library provides a language independent mechanism for
  17. supporting output arguments, input values, and other C function
  18. calling mechanisms. The primary use of the library is to provide a
  19. better interface to certain C function--especially those involving
  20. pointers.
  21. */
  22. // INPUT typemaps.
  23. // These remap a C pointer to be an "INPUT" value which is passed by value
  24. // instead of reference.
  25. /*
  26. The following methods can be applied to turn a pointer into a simple
  27. "input" value. That is, instead of passing a pointer to an object,
  28. you would use a real value instead.
  29. int *INPUT
  30. short *INPUT
  31. long *INPUT
  32. long long *INPUT
  33. unsigned int *INPUT
  34. unsigned short *INPUT
  35. unsigned long *INPUT
  36. unsigned long long *INPUT
  37. unsigned char *INPUT
  38. bool *INPUT
  39. float *INPUT
  40. double *INPUT
  41. To use these, suppose you had a C function like this :
  42. double fadd(double *a, double *b) {
  43. return *a+*b;
  44. }
  45. You could wrap it with SWIG as follows :
  46. %include typemaps.i
  47. double fadd(double *INPUT, double *INPUT);
  48. or you can use the %apply directive :
  49. %include typemaps.i
  50. %apply double *INPUT { double *a, double *b };
  51. double fadd(double *a, double *b);
  52. */
  53. %define INPUT_TYPEMAP(type, converter)
  54. %typemap(in) type *INPUT(type temp), type &INPUT(type temp) {
  55. temp = (type) converter($input);
  56. $1 = &temp;
  57. }
  58. %typemap(typecheck) type *INPUT = type;
  59. %typemap(typecheck) type &INPUT = type;
  60. %enddef
  61. INPUT_TYPEMAP(float, SvNV);
  62. INPUT_TYPEMAP(double, SvNV);
  63. INPUT_TYPEMAP(int, SvIV);
  64. INPUT_TYPEMAP(long, SvIV);
  65. INPUT_TYPEMAP(short, SvIV);
  66. INPUT_TYPEMAP(signed char, SvIV);
  67. INPUT_TYPEMAP(unsigned int, SvUV);
  68. INPUT_TYPEMAP(unsigned long, SvUV);
  69. INPUT_TYPEMAP(unsigned short, SvUV);
  70. INPUT_TYPEMAP(unsigned char, SvUV);
  71. %typemap(in) bool *INPUT(bool temp), bool &INPUT(bool temp) {
  72. temp = SvIV($input) ? true : false;
  73. $1 = &temp;
  74. }
  75. %typemap(typecheck) bool *INPUT = bool;
  76. %typemap(typecheck) bool &INPUT = bool;
  77. %typemap(in) long long *INPUT($*1_ltype temp), long long &INPUT($*1_ltype temp) {
  78. temp = strtoll(SvPV($input,PL_na), 0, 0);
  79. $1 = &temp;
  80. }
  81. %typemap(typecheck) long long *INPUT = long long;
  82. %typemap(typecheck) long long &INPUT = long long;
  83. %typemap(in) unsigned long long *INPUT($*1_ltype temp), unsigned long long &INPUT($*1_ltype temp) {
  84. temp = strtoull(SvPV($input,PL_na), 0, 0);
  85. $1 = &temp;
  86. }
  87. %typemap(typecheck) unsigned long long *INPUT = unsigned long long;
  88. %typemap(typecheck) unsigned long long &INPUT = unsigned long long;
  89. #undef INPUT_TYPEMAP
  90. // OUTPUT typemaps. These typemaps are used for parameters that
  91. // are output only. The output value is appended to the result as
  92. // a list element.
  93. /*
  94. The following methods can be applied to turn a pointer into an "output"
  95. value. When calling a function, no input value would be given for
  96. a parameter, but an output value would be returned. In the case of
  97. multiple output values, functions will return a Perl array.
  98. int *OUTPUT
  99. short *OUTPUT
  100. long *OUTPUT
  101. long long *OUTPUT
  102. unsigned int *OUTPUT
  103. unsigned short *OUTPUT
  104. unsigned long *OUTPUT
  105. unsigned long long *OUTPUT
  106. unsigned char *OUTPUT
  107. bool *OUTPUT
  108. float *OUTPUT
  109. double *OUTPUT
  110. For example, suppose you were trying to wrap the modf() function in the
  111. C math library which splits x into integral and fractional parts (and
  112. returns the integer part in one of its parameters).:
  113. double modf(double x, double *ip);
  114. You could wrap it with SWIG as follows :
  115. %include typemaps.i
  116. double modf(double x, double *OUTPUT);
  117. or you can use the %apply directive :
  118. %include typemaps.i
  119. %apply double *OUTPUT { double *ip };
  120. double modf(double x, double *ip);
  121. The Perl output of the function would be an array containing both
  122. output values.
  123. */
  124. // Force the argument to be ignored.
  125. %typemap(in,numinputs=0) int *OUTPUT(int temp), int &OUTPUT(int temp),
  126. short *OUTPUT(short temp), short &OUTPUT(short temp),
  127. long *OUTPUT(long temp), long &OUTPUT(long temp),
  128. unsigned int *OUTPUT(unsigned int temp), unsigned int &OUTPUT(unsigned int temp),
  129. unsigned short *OUTPUT(unsigned short temp), unsigned short &OUTPUT(unsigned short temp),
  130. unsigned long *OUTPUT(unsigned long temp), unsigned long &OUTPUT(unsigned long temp),
  131. unsigned char *OUTPUT(unsigned char temp), unsigned char &OUTPUT(unsigned char temp),
  132. signed char *OUTPUT(signed char temp), signed char &OUTPUT(signed char temp),
  133. bool *OUTPUT(bool temp), bool &OUTPUT(bool temp),
  134. float *OUTPUT(float temp), float &OUTPUT(float temp),
  135. double *OUTPUT(double temp), double &OUTPUT(double temp),
  136. long long *OUTPUT($*1_ltype temp), long long &OUTPUT($*1_ltype temp),
  137. unsigned long long *OUTPUT($*1_ltype temp), unsigned long long &OUTPUT($*1_ltype temp)
  138. "$1 = &temp;";
  139. %typemap(argout) int *OUTPUT, int &OUTPUT,
  140. short *OUTPUT, short &OUTPUT,
  141. long *OUTPUT, long &OUTPUT,
  142. signed char *OUTPUT, signed char &OUTPUT,
  143. bool *OUTPUT, bool &OUTPUT
  144. {
  145. if (argvi >= items) {
  146. EXTEND(sp,1);
  147. }
  148. $result = sv_newmortal();
  149. sv_setiv($result,(IV) *($1));
  150. argvi++;
  151. }
  152. %typemap(argout) unsigned int *OUTPUT, unsigned int &OUTPUT,
  153. unsigned short *OUTPUT, unsigned short &OUTPUT,
  154. unsigned long *OUTPUT, unsigned long &OUTPUT,
  155. unsigned char *OUTPUT, unsigned char &OUTPUT
  156. {
  157. if (argvi >= items) {
  158. EXTEND(sp,1);
  159. }
  160. $result = sv_newmortal();
  161. sv_setuv($result,(UV) *($1));
  162. argvi++;
  163. }
  164. %typemap(argout) float *OUTPUT, float &OUTPUT,
  165. double *OUTPUT, double &OUTPUT
  166. {
  167. if (argvi >= items) {
  168. EXTEND(sp,1);
  169. }
  170. $result = sv_newmortal();
  171. sv_setnv($result,(double) *($1));
  172. argvi++;
  173. }
  174. %typemap(argout) long long *OUTPUT, long long &OUTPUT {
  175. char temp[256];
  176. if (argvi >= items) {
  177. EXTEND(sp,1);
  178. }
  179. sprintf(temp,"%lld", (long long)*($1));
  180. $result = sv_newmortal();
  181. sv_setpv($result,temp);
  182. argvi++;
  183. }
  184. %typemap(argout) unsigned long long *OUTPUT, unsigned long long &OUTPUT {
  185. char temp[256];
  186. if (argvi >= items) {
  187. EXTEND(sp,1);
  188. }
  189. sprintf(temp,"%llu", (unsigned long long)*($1));
  190. $result = sv_newmortal();
  191. sv_setpv($result,temp);
  192. argvi++;
  193. }
  194. // INOUT
  195. // Mappings for an argument that is both an input and output
  196. // parameter
  197. /*
  198. The following methods can be applied to make a function parameter both
  199. an input and output value. This combines the behavior of both the
  200. "INPUT" and "OUTPUT" methods described earlier. Output values are
  201. returned in the form of a Perl array.
  202. int *INOUT
  203. short *INOUT
  204. long *INOUT
  205. long long *INOUT
  206. unsigned int *INOUT
  207. unsigned short *INOUT
  208. unsigned long *INOUT
  209. unsigned long long *INOUT
  210. unsigned char *INOUT
  211. bool *INOUT
  212. float *INOUT
  213. double *INOUT
  214. For example, suppose you were trying to wrap the following function :
  215. void neg(double *x) {
  216. *x = -(*x);
  217. }
  218. You could wrap it with SWIG as follows :
  219. %include typemaps.i
  220. void neg(double *INOUT);
  221. or you can use the %apply directive :
  222. %include typemaps.i
  223. %apply double *INOUT { double *x };
  224. void neg(double *x);
  225. Unlike C, this mapping does not directly modify the input value.
  226. Rather, the modified input value shows up as the return value of the
  227. function. Thus, to apply this function to a Perl variable you might
  228. do this :
  229. $x = neg($x);
  230. */
  231. %typemap(in) int *INOUT = int *INPUT;
  232. %typemap(in) short *INOUT = short *INPUT;
  233. %typemap(in) long *INOUT = long *INPUT;
  234. %typemap(in) unsigned *INOUT = unsigned *INPUT;
  235. %typemap(in) unsigned short *INOUT = unsigned short *INPUT;
  236. %typemap(in) unsigned long *INOUT = unsigned long *INPUT;
  237. %typemap(in) unsigned char *INOUT = unsigned char *INPUT;
  238. %typemap(in) signed char *INOUT = signed char *INPUT;
  239. %typemap(in) bool *INOUT = bool *INPUT;
  240. %typemap(in) float *INOUT = float *INPUT;
  241. %typemap(in) double *INOUT = double *INPUT;
  242. %typemap(in) long long *INOUT = long long *INPUT;
  243. %typemap(in) unsigned long long *INOUT = unsigned long long *INPUT;
  244. %typemap(in) int &INOUT = int &INPUT;
  245. %typemap(in) short &INOUT = short &INPUT;
  246. %typemap(in) long &INOUT = long &INPUT;
  247. %typemap(in) unsigned &INOUT = unsigned &INPUT;
  248. %typemap(in) unsigned short &INOUT = unsigned short &INPUT;
  249. %typemap(in) unsigned long &INOUT = unsigned long &INPUT;
  250. %typemap(in) unsigned char &INOUT = unsigned char &INPUT;
  251. %typemap(in) signed char &INOUT = signed char &INPUT;
  252. %typemap(in) bool &INOUT = bool &INPUT;
  253. %typemap(in) float &INOUT = float &INPUT;
  254. %typemap(in) double &INOUT = double &INPUT;
  255. %typemap(in) long long &INOUT = long long &INPUT;
  256. %typemap(in) unsigned long long &INOUT = unsigned long long &INPUT;
  257. %typemap(argout) int *INOUT = int *OUTPUT;
  258. %typemap(argout) short *INOUT = short *OUTPUT;
  259. %typemap(argout) long *INOUT = long *OUTPUT;
  260. %typemap(argout) unsigned *INOUT = unsigned *OUTPUT;
  261. %typemap(argout) unsigned short *INOUT = unsigned short *OUTPUT;
  262. %typemap(argout) unsigned long *INOUT = unsigned long *OUTPUT;
  263. %typemap(argout) unsigned char *INOUT = unsigned char *OUTPUT;
  264. %typemap(argout) signed char *INOUT = signed char *OUTPUT;
  265. %typemap(argout) bool *INOUT = bool *OUTPUT;
  266. %typemap(argout) float *INOUT = float *OUTPUT;
  267. %typemap(argout) double *INOUT = double *OUTPUT;
  268. %typemap(argout) long long *INOUT = long long *OUTPUT;
  269. %typemap(argout) unsigned long long *INOUT = unsigned long long *OUTPUT;
  270. %typemap(argout) int &INOUT = int &OUTPUT;
  271. %typemap(argout) short &INOUT = short &OUTPUT;
  272. %typemap(argout) long &INOUT = long &OUTPUT;
  273. %typemap(argout) unsigned &INOUT = unsigned &OUTPUT;
  274. %typemap(argout) unsigned short &INOUT = unsigned short &OUTPUT;
  275. %typemap(argout) unsigned long &INOUT = unsigned long &OUTPUT;
  276. %typemap(argout) unsigned char &INOUT = unsigned char &OUTPUT;
  277. %typemap(argout) signed char &INOUT = signed char &OUTPUT;
  278. %typemap(argout) bool &INOUT = bool &OUTPUT;
  279. %typemap(argout) float &INOUT = float &OUTPUT;
  280. %typemap(argout) double &INOUT = double &OUTPUT;
  281. %typemap(argout) long long &INOUT = long long &OUTPUT;
  282. %typemap(argout) unsigned long long &INOUT = unsigned long long &OUTPUT;
  283. /* Overloading information */
  284. %typemap(typecheck) double *INOUT = double;
  285. %typemap(typecheck) bool *INOUT = bool;
  286. %typemap(typecheck) signed char *INOUT = signed char;
  287. %typemap(typecheck) unsigned char *INOUT = unsigned char;
  288. %typemap(typecheck) unsigned long *INOUT = unsigned long;
  289. %typemap(typecheck) unsigned short *INOUT = unsigned short;
  290. %typemap(typecheck) unsigned int *INOUT = unsigned int;
  291. %typemap(typecheck) long *INOUT = long;
  292. %typemap(typecheck) short *INOUT = short;
  293. %typemap(typecheck) int *INOUT = int;
  294. %typemap(typecheck) float *INOUT = float;
  295. %typemap(typecheck) long long *INOUT = long long;
  296. %typemap(typecheck) unsigned long long *INOUT = unsigned long long;
  297. %typemap(typecheck) double &INOUT = double;
  298. %typemap(typecheck) bool &INOUT = bool;
  299. %typemap(typecheck) signed char &INOUT = signed char;
  300. %typemap(typecheck) unsigned char &INOUT = unsigned char;
  301. %typemap(typecheck) unsigned long &INOUT = unsigned long;
  302. %typemap(typecheck) unsigned short &INOUT = unsigned short;
  303. %typemap(typecheck) unsigned int &INOUT = unsigned int;
  304. %typemap(typecheck) long &INOUT = long;
  305. %typemap(typecheck) short &INOUT = short;
  306. %typemap(typecheck) int &INOUT = int;
  307. %typemap(typecheck) float &INOUT = float;
  308. %typemap(typecheck) long long &INOUT = long long;
  309. %typemap(typecheck) unsigned long long &INOUT = unsigned long long;
  310. #endif
  311. // --------------------------------------------------------------------
  312. // Special types
  313. // --------------------------------------------------------------------
  314. %include <reference.i>