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

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

#
Swig | 592 lines | 375 code | 46 blank | 171 comment | 0 complexity | c6232f9113ed10a6f25ea69da02e9bd2 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. //
  2. // SWIG Typemap library
  3. // Dave Beazley
  4. // May 5, 1997
  5. //
  6. // Perl5 implementation
  7. //
  8. // This library provides standard typemaps for modifying SWIG's behavior.
  9. // With enough entries in this file, I hope that very few people actually
  10. // ever need to write a typemap.
  11. //
  12. /*
  13. The SWIG typemap library provides a language independent mechanism for
  14. supporting output arguments, input values, and other C function
  15. calling mechanisms. The primary use of the library is to provide a
  16. better interface to certain C function--especially those involving
  17. pointers.
  18. */
  19. // INPUT typemaps.
  20. // These remap a C pointer to be an "INPUT" value which is passed by value
  21. // instead of reference.
  22. /*
  23. The following methods can be applied to turn a pointer into a simple
  24. "input" value. That is, instead of passing a pointer to an object,
  25. you would use a real value instead.
  26. int *INPUT
  27. short *INPUT
  28. long *INPUT
  29. long long *INPUT
  30. unsigned int *INPUT
  31. unsigned short *INPUT
  32. unsigned long *INPUT
  33. unsigned long long *INPUT
  34. unsigned char *INPUT
  35. bool *INPUT
  36. float *INPUT
  37. double *INPUT
  38. To use these, suppose you had a C function like this :
  39. double fadd(double *a, double *b) {
  40. return *a+*b;
  41. }
  42. You could wrap it with SWIG as follows :
  43. %include typemaps.i
  44. double fadd(double *INPUT, double *INPUT);
  45. or you can use the %apply directive :
  46. %include typemaps.i
  47. %apply double *INPUT { double *a, double *b };
  48. double fadd(double *a, double *b);
  49. */
  50. %define INPUT_TYPEMAP(type, converter)
  51. %typemap(in) type *INPUT(type temp), type &INPUT(type temp) {
  52. temp = (type) converter($input);
  53. $1 = &temp;
  54. }
  55. %typemap(typecheck) type *INPUT = type;
  56. %typemap(typecheck) type &INPUT = type;
  57. %enddef
  58. INPUT_TYPEMAP(float, SvNV);
  59. INPUT_TYPEMAP(double, SvNV);
  60. INPUT_TYPEMAP(int, SvIV);
  61. INPUT_TYPEMAP(long, SvIV);
  62. INPUT_TYPEMAP(short, SvIV);
  63. INPUT_TYPEMAP(signed char, SvIV);
  64. INPUT_TYPEMAP(unsigned int, SvUV);
  65. INPUT_TYPEMAP(unsigned long, SvUV);
  66. INPUT_TYPEMAP(unsigned short, SvUV);
  67. INPUT_TYPEMAP(unsigned char, SvUV);
  68. INPUT_TYPEMAP(bool, SvIV);
  69. %typemap(in) long long *INPUT($*1_ltype temp), long long &INPUT($*1_ltype temp) {
  70. temp = strtoll(SvPV($input,PL_na), 0, 0);
  71. $1 = &temp;
  72. }
  73. %typemap(typecheck) long long *INPUT = long long;
  74. %typemap(typecheck) long long &INPUT = long long;
  75. %typemap(in) unsigned long long *INPUT($*1_ltype temp), unsigned long long &INPUT($*1_ltype temp) {
  76. temp = strtoull(SvPV($input,PL_na), 0, 0);
  77. $1 = &temp;
  78. }
  79. %typemap(typecheck) unsigned long long *INPUT = unsigned long long;
  80. %typemap(typecheck) unsigned long long &INPUT = unsigned long long;
  81. #undef INPUT_TYPEMAP
  82. // OUTPUT typemaps. These typemaps are used for parameters that
  83. // are output only. The output value is appended to the result as
  84. // a list element.
  85. /*
  86. The following methods can be applied to turn a pointer into an "output"
  87. value. When calling a function, no input value would be given for
  88. a parameter, but an output value would be returned. In the case of
  89. multiple output values, functions will return a Perl array.
  90. int *OUTPUT
  91. short *OUTPUT
  92. long *OUTPUT
  93. long long *OUTPUT
  94. unsigned int *OUTPUT
  95. unsigned short *OUTPUT
  96. unsigned long *OUTPUT
  97. unsigned long long *OUTPUT
  98. unsigned char *OUTPUT
  99. bool *OUTPUT
  100. float *OUTPUT
  101. double *OUTPUT
  102. For example, suppose you were trying to wrap the modf() function in the
  103. C math library which splits x into integral and fractional parts (and
  104. returns the integer part in one of its parameters).:
  105. double modf(double x, double *ip);
  106. You could wrap it with SWIG as follows :
  107. %include typemaps.i
  108. double modf(double x, double *OUTPUT);
  109. or you can use the %apply directive :
  110. %include typemaps.i
  111. %apply double *OUTPUT { double *ip };
  112. double modf(double x, double *ip);
  113. The Perl output of the function would be an array containing both
  114. output values.
  115. */
  116. // Force the argument to be ignored.
  117. %typemap(in,numinputs=0) int *OUTPUT(int temp), int &OUTPUT(int temp),
  118. short *OUTPUT(short temp), short &OUTPUT(short temp),
  119. long *OUTPUT(long temp), long &OUTPUT(long temp),
  120. unsigned int *OUTPUT(unsigned int temp), unsigned int &OUTPUT(unsigned int temp),
  121. unsigned short *OUTPUT(unsigned short temp), unsigned short &OUTPUT(unsigned short temp),
  122. unsigned long *OUTPUT(unsigned long temp), unsigned long &OUTPUT(unsigned long temp),
  123. unsigned char *OUTPUT(unsigned char temp), unsigned char &OUTPUT(unsigned char temp),
  124. signed char *OUTPUT(signed char temp), signed char &OUTPUT(signed char temp),
  125. bool *OUTPUT(bool temp), bool &OUTPUT(bool temp),
  126. float *OUTPUT(float temp), float &OUTPUT(float temp),
  127. double *OUTPUT(double temp), double &OUTPUT(double temp),
  128. long long *OUTPUT($*1_ltype temp), long long &OUTPUT($*1_ltype temp),
  129. unsigned long long *OUTPUT($*1_ltype temp), unsigned long long &OUTPUT($*1_ltype temp)
  130. "$1 = &temp;";
  131. %typemap(argout) int *OUTPUT, int &OUTPUT,
  132. short *OUTPUT, short &OUTPUT,
  133. long *OUTPUT, long &OUTPUT,
  134. signed char *OUTPUT, signed char &OUTPUT,
  135. bool *OUTPUT, bool &OUTPUT
  136. {
  137. if (argvi >= items) {
  138. EXTEND(sp,1);
  139. }
  140. $result = sv_newmortal();
  141. sv_setiv($result,(IV) *($1));
  142. argvi++;
  143. }
  144. %typemap(argout) unsigned int *OUTPUT, unsigned int &OUTPUT,
  145. unsigned short *OUTPUT, unsigned short &OUTPUT,
  146. unsigned long *OUTPUT, unsigned long &OUTPUT,
  147. unsigned char *OUTPUT, unsigned char &OUTPUT
  148. {
  149. if (argvi >= items) {
  150. EXTEND(sp,1);
  151. }
  152. $result = sv_newmortal();
  153. sv_setuv($result,(UV) *($1));
  154. argvi++;
  155. }
  156. %typemap(argout) float *OUTPUT, float &OUTPUT,
  157. double *OUTPUT, double &OUTPUT
  158. {
  159. if (argvi >= items) {
  160. EXTEND(sp,1);
  161. }
  162. $result = sv_newmortal();
  163. sv_setnv($result,(double) *($1));
  164. argvi++;
  165. }
  166. %typemap(argout) long long *OUTPUT, long long &OUTPUT {
  167. char temp[256];
  168. if (argvi >= items) {
  169. EXTEND(sp,1);
  170. }
  171. sprintf(temp,"%lld", (long long)*($1));
  172. $result = sv_newmortal();
  173. sv_setpv($result,temp);
  174. argvi++;
  175. }
  176. %typemap(argout) unsigned long long *OUTPUT, unsigned long long &OUTPUT {
  177. char temp[256];
  178. if (argvi >= items) {
  179. EXTEND(sp,1);
  180. }
  181. sprintf(temp,"%llu", (unsigned long long)*($1));
  182. $result = sv_newmortal();
  183. sv_setpv($result,temp);
  184. argvi++;
  185. }
  186. // INOUT
  187. // Mappings for an argument that is both an input and output
  188. // parameter
  189. /*
  190. The following methods can be applied to make a function parameter both
  191. an input and output value. This combines the behavior of both the
  192. "INPUT" and "OUTPUT" methods described earlier. Output values are
  193. returned in the form of a Perl array.
  194. int *INOUT
  195. short *INOUT
  196. long *INOUT
  197. long long *INOUT
  198. unsigned int *INOUT
  199. unsigned short *INOUT
  200. unsigned long *INOUT
  201. unsigned long long *INOUT
  202. unsigned char *INOUT
  203. bool *INOUT
  204. float *INOUT
  205. double *INOUT
  206. For example, suppose you were trying to wrap the following function :
  207. void neg(double *x) {
  208. *x = -(*x);
  209. }
  210. You could wrap it with SWIG as follows :
  211. %include typemaps.i
  212. void neg(double *INOUT);
  213. or you can use the %apply directive :
  214. %include typemaps.i
  215. %apply double *INOUT { double *x };
  216. void neg(double *x);
  217. Unlike C, this mapping does not directly modify the input value.
  218. Rather, the modified input value shows up as the return value of the
  219. function. Thus, to apply this function to a Perl variable you might
  220. do this :
  221. $x = neg($x);
  222. */
  223. %typemap(in) int *INOUT = int *INPUT;
  224. %typemap(in) short *INOUT = short *INPUT;
  225. %typemap(in) long *INOUT = long *INPUT;
  226. %typemap(in) unsigned *INOUT = unsigned *INPUT;
  227. %typemap(in) unsigned short *INOUT = unsigned short *INPUT;
  228. %typemap(in) unsigned long *INOUT = unsigned long *INPUT;
  229. %typemap(in) unsigned char *INOUT = unsigned char *INPUT;
  230. %typemap(in) signed char *INOUT = signed char *INPUT;
  231. %typemap(in) bool *INOUT = bool *INPUT;
  232. %typemap(in) float *INOUT = float *INPUT;
  233. %typemap(in) double *INOUT = double *INPUT;
  234. %typemap(in) long long *INOUT = long long *INPUT;
  235. %typemap(in) unsigned long long *INOUT = unsigned long long *INPUT;
  236. %typemap(in) int &INOUT = int &INPUT;
  237. %typemap(in) short &INOUT = short &INPUT;
  238. %typemap(in) long &INOUT = long &INPUT;
  239. %typemap(in) unsigned &INOUT = unsigned &INPUT;
  240. %typemap(in) unsigned short &INOUT = unsigned short &INPUT;
  241. %typemap(in) unsigned long &INOUT = unsigned long &INPUT;
  242. %typemap(in) unsigned char &INOUT = unsigned char &INPUT;
  243. %typemap(in) signed char &INOUT = signed char &INPUT;
  244. %typemap(in) bool &INOUT = bool &INPUT;
  245. %typemap(in) float &INOUT = float &INPUT;
  246. %typemap(in) double &INOUT = double &INPUT;
  247. %typemap(in) long long &INOUT = long long &INPUT;
  248. %typemap(in) unsigned long long &INOUT = unsigned long long &INPUT;
  249. %typemap(argout) int *INOUT = int *OUTPUT;
  250. %typemap(argout) short *INOUT = short *OUTPUT;
  251. %typemap(argout) long *INOUT = long *OUTPUT;
  252. %typemap(argout) unsigned *INOUT = unsigned *OUTPUT;
  253. %typemap(argout) unsigned short *INOUT = unsigned short *OUTPUT;
  254. %typemap(argout) unsigned long *INOUT = unsigned long *OUTPUT;
  255. %typemap(argout) unsigned char *INOUT = unsigned char *OUTPUT;
  256. %typemap(argout) signed char *INOUT = signed char *OUTPUT;
  257. %typemap(argout) bool *INOUT = bool *OUTPUT;
  258. %typemap(argout) float *INOUT = float *OUTPUT;
  259. %typemap(argout) double *INOUT = double *OUTPUT;
  260. %typemap(argout) long long *INOUT = long long *OUTPUT;
  261. %typemap(argout) unsigned long long *INOUT = unsigned long long *OUTPUT;
  262. %typemap(argout) int &INOUT = int &OUTPUT;
  263. %typemap(argout) short &INOUT = short &OUTPUT;
  264. %typemap(argout) long &INOUT = long &OUTPUT;
  265. %typemap(argout) unsigned &INOUT = unsigned &OUTPUT;
  266. %typemap(argout) unsigned short &INOUT = unsigned short &OUTPUT;
  267. %typemap(argout) unsigned long &INOUT = unsigned long &OUTPUT;
  268. %typemap(argout) unsigned char &INOUT = unsigned char &OUTPUT;
  269. %typemap(argout) signed char &INOUT = signed char &OUTPUT;
  270. %typemap(argout) bool &INOUT = bool &OUTPUT;
  271. %typemap(argout) float &INOUT = float &OUTPUT;
  272. %typemap(argout) double &INOUT = double &OUTPUT;
  273. %typemap(argout) long long &INOUT = long long &OUTPUT;
  274. %typemap(argout) unsigned long long &INOUT = unsigned long long &OUTPUT;
  275. // REFERENCE
  276. // Accept Perl references as pointers
  277. /*
  278. The following methods make Perl references work like simple C
  279. pointers. References can only be used for simple input/output
  280. values, not C arrays however. It should also be noted that
  281. REFERENCES are specific to Perl and not supported in other
  282. scripting languages at this time.
  283. int *REFERENCE
  284. short *REFERENCE
  285. long *REFERENCE
  286. unsigned int *REFERENCE
  287. unsigned short *REFERENCE
  288. unsigned long *REFERENCE
  289. unsigned char *REFERENCE
  290. float *REFERENCE
  291. double *REFERENCE
  292. For example, suppose you were trying to wrap the following function :
  293. void neg(double *x) {
  294. *x = -(*x);
  295. }
  296. You could wrap it with SWIG as follows :
  297. %include typemaps.i
  298. void neg(double *REFERENCE);
  299. or you can use the %apply directive :
  300. %include typemaps.i
  301. %apply double *REFERENCE { double *x };
  302. void neg(double *x);
  303. Unlike the INOUT mapping described previous, this approach directly
  304. modifies the value of a Perl reference. Thus, you could use it
  305. as follows :
  306. $x = 3;
  307. neg(\$x);
  308. print "$x\n"; # Should print out -3.
  309. */
  310. %typemap(in) double *REFERENCE (double dvalue), double &REFERENCE(double dvalue)
  311. {
  312. SV *tempsv;
  313. if (!SvROK($input)) {
  314. SWIG_croak("expected a reference");
  315. }
  316. tempsv = SvRV($input);
  317. if ((!SvNOK(tempsv)) && (!SvIOK(tempsv))) {
  318. printf("Received %d\n", SvTYPE(tempsv));
  319. SWIG_croak("Expected a double reference.");
  320. }
  321. dvalue = SvNV(tempsv);
  322. $1 = &dvalue;
  323. }
  324. %typemap(in) float *REFERENCE (float dvalue), float &REFERENCE(float dvalue)
  325. {
  326. SV *tempsv;
  327. if (!SvROK($input)) {
  328. SWIG_croak("expected a reference");
  329. }
  330. tempsv = SvRV($input);
  331. if ((!SvNOK(tempsv)) && (!SvIOK(tempsv))) {
  332. SWIG_croak("expected a double reference");
  333. }
  334. dvalue = (float) SvNV(tempsv);
  335. $1 = &dvalue;
  336. }
  337. %typemap(in) int *REFERENCE (int dvalue), int &REFERENCE (int dvalue)
  338. {
  339. SV *tempsv;
  340. if (!SvROK($input)) {
  341. SWIG_croak("expected a reference");
  342. }
  343. tempsv = SvRV($input);
  344. if (!SvIOK(tempsv)) {
  345. SWIG_croak("expected a integer reference");
  346. }
  347. dvalue = SvIV(tempsv);
  348. $1 = &dvalue;
  349. }
  350. %typemap(in) short *REFERENCE (short dvalue), short &REFERENCE(short dvalue)
  351. {
  352. SV *tempsv;
  353. if (!SvROK($input)) {
  354. SWIG_croak("expected a reference");
  355. }
  356. tempsv = SvRV($input);
  357. if (!SvIOK(tempsv)) {
  358. SWIG_croak("expected a integer reference");
  359. }
  360. dvalue = (short) SvIV(tempsv);
  361. $1 = &dvalue;
  362. }
  363. %typemap(in) long *REFERENCE (long dvalue), long &REFERENCE(long dvalue)
  364. {
  365. SV *tempsv;
  366. if (!SvROK($input)) {
  367. SWIG_croak("expected a reference");
  368. }
  369. tempsv = SvRV($input);
  370. if (!SvIOK(tempsv)) {
  371. SWIG_croak("expected a integer reference");
  372. }
  373. dvalue = (long) SvIV(tempsv);
  374. $1 = &dvalue;
  375. }
  376. %typemap(in) unsigned int *REFERENCE (unsigned int dvalue), unsigned int &REFERENCE(unsigned int dvalue)
  377. {
  378. SV *tempsv;
  379. if (!SvROK($input)) {
  380. SWIG_croak("expected a reference");
  381. }
  382. tempsv = SvRV($input);
  383. if (!SvIOK(tempsv)) {
  384. SWIG_croak("expected a integer reference");
  385. }
  386. dvalue = (unsigned int) SvUV(tempsv);
  387. $1 = &dvalue;
  388. }
  389. %typemap(in) unsigned short *REFERENCE (unsigned short dvalue), unsigned short &REFERENCE(unsigned short dvalue)
  390. {
  391. SV *tempsv;
  392. if (!SvROK($input)) {
  393. SWIG_croak("expected a reference");
  394. }
  395. tempsv = SvRV($input);
  396. if (!SvIOK(tempsv)) {
  397. SWIG_croak("expected a integer reference");
  398. }
  399. dvalue = (unsigned short) SvUV(tempsv);
  400. $1 = &dvalue;
  401. }
  402. %typemap(in) unsigned long *REFERENCE (unsigned long dvalue), unsigned long &REFERENCE(unsigned long dvalue)
  403. {
  404. SV *tempsv;
  405. if (!SvROK($input)) {
  406. SWIG_croak("expected a reference");
  407. }
  408. tempsv = SvRV($input);
  409. if (!SvIOK(tempsv)) {
  410. SWIG_croak("expected a integer reference");
  411. }
  412. dvalue = (unsigned long) SvUV(tempsv);
  413. $1 = &dvalue;
  414. }
  415. %typemap(in) unsigned char *REFERENCE (unsigned char dvalue), unsigned char &REFERENCE(unsigned char dvalue)
  416. {
  417. SV *tempsv;
  418. if (!SvROK($input)) {
  419. SWIG_croak("expected a reference");
  420. }
  421. tempsv = SvRV($input);
  422. if (!SvIOK(tempsv)) {
  423. SWIG_croak("expected a integer reference");
  424. }
  425. dvalue = (unsigned char) SvUV(tempsv);
  426. $1 = &dvalue;
  427. }
  428. %typemap(in) signed char *REFERENCE (signed char dvalue), signed char &REFERENCE(signed char dvalue)
  429. {
  430. SV *tempsv;
  431. if (!SvROK($input)) {
  432. SWIG_croak("expected a reference");
  433. }
  434. tempsv = SvRV($input);
  435. if (!SvIOK(tempsv)) {
  436. SWIG_croak("expected a integer reference");
  437. }
  438. dvalue = (signed char) SvIV(tempsv);
  439. $1 = &dvalue;
  440. }
  441. %typemap(in) bool *REFERENCE (bool dvalue), bool &REFERENCE(bool dvalue)
  442. {
  443. SV *tempsv;
  444. if (!SvROK($input)) {
  445. SWIG_croak("expected a reference");
  446. }
  447. tempsv = SvRV($input);
  448. if (!SvIOK(tempsv)) {
  449. SWIG_croak("expected a integer reference");
  450. }
  451. dvalue = (bool) SvIV(tempsv);
  452. $1 = &dvalue;
  453. }
  454. %typemap(argout) double *REFERENCE, double &REFERENCE,
  455. float *REFERENCE, float &REFERENCE
  456. {
  457. SV *tempsv;
  458. tempsv = SvRV($arg);
  459. if (!$1) SWIG_croak("expected a reference");
  460. sv_setnv(tempsv, (double) *$1);
  461. }
  462. %typemap(argout) int *REFERENCE, int &REFERENCE,
  463. short *REFERENCE, short &REFERENCE,
  464. long *REFERENCE, long &REFERENCE,
  465. signed char *REFERENCE, unsigned char &REFERENCE,
  466. bool *REFERENCE, bool &REFERENCE
  467. {
  468. SV *tempsv;
  469. tempsv = SvRV($input);
  470. if (!$1) SWIG_croak("expected a reference");
  471. sv_setiv(tempsv, (IV) *$1);
  472. }
  473. %typemap(argout) unsigned int *REFERENCE, unsigned int &REFERENCE,
  474. unsigned short *REFERENCE, unsigned short &REFERENCE,
  475. unsigned long *REFERENCE, unsigned long &REFERENCE,
  476. unsigned char *REFERENCE, unsigned char &REFERENCE
  477. {
  478. SV *tempsv;
  479. tempsv = SvRV($input);
  480. if (!$1) SWIG_croak("expected a reference");
  481. sv_setuv(tempsv, (UV) *$1);
  482. }
  483. /* Overloading information */
  484. %typemap(typecheck) double *INOUT = double;
  485. %typemap(typecheck) bool *INOUT = bool;
  486. %typemap(typecheck) signed char *INOUT = signed char;
  487. %typemap(typecheck) unsigned char *INOUT = unsigned char;
  488. %typemap(typecheck) unsigned long *INOUT = unsigned long;
  489. %typemap(typecheck) unsigned short *INOUT = unsigned short;
  490. %typemap(typecheck) unsigned int *INOUT = unsigned int;
  491. %typemap(typecheck) long *INOUT = long;
  492. %typemap(typecheck) short *INOUT = short;
  493. %typemap(typecheck) int *INOUT = int;
  494. %typemap(typecheck) float *INOUT = float;
  495. %typemap(typecheck) long long *INOUT = long long;
  496. %typemap(typecheck) unsigned long long *INOUT = unsigned long long;
  497. %typemap(typecheck) double &INOUT = double;
  498. %typemap(typecheck) bool &INOUT = bool;
  499. %typemap(typecheck) signed char &INOUT = signed char;
  500. %typemap(typecheck) unsigned char &INOUT = unsigned char;
  501. %typemap(typecheck) unsigned long &INOUT = unsigned long;
  502. %typemap(typecheck) unsigned short &INOUT = unsigned short;
  503. %typemap(typecheck) unsigned int &INOUT = unsigned int;
  504. %typemap(typecheck) long &INOUT = long;
  505. %typemap(typecheck) short &INOUT = short;
  506. %typemap(typecheck) int &INOUT = int;
  507. %typemap(typecheck) float &INOUT = float;
  508. %typemap(typecheck) long long &INOUT = long long;
  509. %typemap(typecheck) unsigned long long &INOUT = unsigned long long;