PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1.3.35/Lib/perl5/reference.i

#
Swig | 235 lines | 173 code | 11 blank | 51 comment | 0 complexity | 616482c6f426140c42edb95df05b1258 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * See the LICENSE file for information on copyright, usage and redistribution
  3. * of SWIG, and the README file for authors - http://www.swig.org/release.html.
  4. *
  5. * reference.i
  6. *
  7. * Accept Perl references as pointers
  8. * ----------------------------------------------------------------------------- */
  9. /*
  10. The following methods make Perl references work like simple C
  11. pointers. References can only be used for simple input/output
  12. values, not C arrays however. It should also be noted that
  13. REFERENCES are specific to Perl and not supported in other
  14. scripting languages at this time.
  15. int *REFERENCE
  16. short *REFERENCE
  17. long *REFERENCE
  18. unsigned int *REFERENCE
  19. unsigned short *REFERENCE
  20. unsigned long *REFERENCE
  21. unsigned char *REFERENCE
  22. float *REFERENCE
  23. double *REFERENCE
  24. For example, suppose you were trying to wrap the following function :
  25. void neg(double *x) {
  26. *x = -(*x);
  27. }
  28. You could wrap it with SWIG as follows :
  29. %include reference.i
  30. void neg(double *REFERENCE);
  31. or you can use the %apply directive :
  32. %include reference.i
  33. %apply double *REFERENCE { double *x };
  34. void neg(double *x);
  35. Unlike the INOUT mapping described in typemaps.i, this approach directly
  36. modifies the value of a Perl reference. Thus, you could use it
  37. as follows :
  38. $x = 3;
  39. neg(\$x);
  40. print "$x\n"; # Should print out -3.
  41. */
  42. %typemap(in) double *REFERENCE (double dvalue), double &REFERENCE(double dvalue)
  43. {
  44. SV *tempsv;
  45. if (!SvROK($input)) {
  46. SWIG_croak("expected a reference");
  47. }
  48. tempsv = SvRV($input);
  49. if ((!SvNOK(tempsv)) && (!SvIOK(tempsv))) {
  50. printf("Received %d\n", SvTYPE(tempsv));
  51. SWIG_croak("Expected a double reference.");
  52. }
  53. dvalue = SvNV(tempsv);
  54. $1 = &dvalue;
  55. }
  56. %typemap(in) float *REFERENCE (float dvalue), float &REFERENCE(float dvalue)
  57. {
  58. SV *tempsv;
  59. if (!SvROK($input)) {
  60. SWIG_croak("expected a reference");
  61. }
  62. tempsv = SvRV($input);
  63. if ((!SvNOK(tempsv)) && (!SvIOK(tempsv))) {
  64. SWIG_croak("expected a double reference");
  65. }
  66. dvalue = (float) SvNV(tempsv);
  67. $1 = &dvalue;
  68. }
  69. %typemap(in) int *REFERENCE (int dvalue), int &REFERENCE (int dvalue)
  70. {
  71. SV *tempsv;
  72. if (!SvROK($input)) {
  73. SWIG_croak("expected a reference");
  74. }
  75. tempsv = SvRV($input);
  76. if (!SvIOK(tempsv)) {
  77. SWIG_croak("expected a integer reference");
  78. }
  79. dvalue = SvIV(tempsv);
  80. $1 = &dvalue;
  81. }
  82. %typemap(in) short *REFERENCE (short dvalue), short &REFERENCE(short dvalue)
  83. {
  84. SV *tempsv;
  85. if (!SvROK($input)) {
  86. SWIG_croak("expected a reference");
  87. }
  88. tempsv = SvRV($input);
  89. if (!SvIOK(tempsv)) {
  90. SWIG_croak("expected a integer reference");
  91. }
  92. dvalue = (short) SvIV(tempsv);
  93. $1 = &dvalue;
  94. }
  95. %typemap(in) long *REFERENCE (long dvalue), long &REFERENCE(long dvalue)
  96. {
  97. SV *tempsv;
  98. if (!SvROK($input)) {
  99. SWIG_croak("expected a reference");
  100. }
  101. tempsv = SvRV($input);
  102. if (!SvIOK(tempsv)) {
  103. SWIG_croak("expected a integer reference");
  104. }
  105. dvalue = (long) SvIV(tempsv);
  106. $1 = &dvalue;
  107. }
  108. %typemap(in) unsigned int *REFERENCE (unsigned int dvalue), unsigned int &REFERENCE(unsigned int dvalue)
  109. {
  110. SV *tempsv;
  111. if (!SvROK($input)) {
  112. SWIG_croak("expected a reference");
  113. }
  114. tempsv = SvRV($input);
  115. if (!SvIOK(tempsv)) {
  116. SWIG_croak("expected a integer reference");
  117. }
  118. dvalue = (unsigned int) SvUV(tempsv);
  119. $1 = &dvalue;
  120. }
  121. %typemap(in) unsigned short *REFERENCE (unsigned short dvalue), unsigned short &REFERENCE(unsigned short dvalue)
  122. {
  123. SV *tempsv;
  124. if (!SvROK($input)) {
  125. SWIG_croak("expected a reference");
  126. }
  127. tempsv = SvRV($input);
  128. if (!SvIOK(tempsv)) {
  129. SWIG_croak("expected a integer reference");
  130. }
  131. dvalue = (unsigned short) SvUV(tempsv);
  132. $1 = &dvalue;
  133. }
  134. %typemap(in) unsigned long *REFERENCE (unsigned long dvalue), unsigned long &REFERENCE(unsigned long dvalue)
  135. {
  136. SV *tempsv;
  137. if (!SvROK($input)) {
  138. SWIG_croak("expected a reference");
  139. }
  140. tempsv = SvRV($input);
  141. if (!SvIOK(tempsv)) {
  142. SWIG_croak("expected a integer reference");
  143. }
  144. dvalue = (unsigned long) SvUV(tempsv);
  145. $1 = &dvalue;
  146. }
  147. %typemap(in) unsigned char *REFERENCE (unsigned char dvalue), unsigned char &REFERENCE(unsigned char dvalue)
  148. {
  149. SV *tempsv;
  150. if (!SvROK($input)) {
  151. SWIG_croak("expected a reference");
  152. }
  153. tempsv = SvRV($input);
  154. if (!SvIOK(tempsv)) {
  155. SWIG_croak("expected a integer reference");
  156. }
  157. dvalue = (unsigned char) SvUV(tempsv);
  158. $1 = &dvalue;
  159. }
  160. %typemap(in) signed char *REFERENCE (signed char dvalue), signed char &REFERENCE(signed char dvalue)
  161. {
  162. SV *tempsv;
  163. if (!SvROK($input)) {
  164. SWIG_croak("expected a reference");
  165. }
  166. tempsv = SvRV($input);
  167. if (!SvIOK(tempsv)) {
  168. SWIG_croak("expected a integer reference");
  169. }
  170. dvalue = (signed char) SvIV(tempsv);
  171. $1 = &dvalue;
  172. }
  173. %typemap(in) bool *REFERENCE (bool dvalue), bool &REFERENCE(bool dvalue)
  174. {
  175. SV *tempsv;
  176. if (!SvROK($input)) {
  177. SWIG_croak("expected a reference");
  178. }
  179. tempsv = SvRV($input);
  180. if (!SvIOK(tempsv)) {
  181. SWIG_croak("expected a integer reference");
  182. }
  183. dvalue = (bool) SvIV(tempsv);
  184. $1 = &dvalue;
  185. }
  186. %typemap(argout) double *REFERENCE, double &REFERENCE,
  187. float *REFERENCE, float &REFERENCE
  188. {
  189. SV *tempsv;
  190. tempsv = SvRV($arg);
  191. if (!$1) SWIG_croak("expected a reference");
  192. sv_setnv(tempsv, (double) *$1);
  193. }
  194. %typemap(argout) int *REFERENCE, int &REFERENCE,
  195. short *REFERENCE, short &REFERENCE,
  196. long *REFERENCE, long &REFERENCE,
  197. signed char *REFERENCE, unsigned char &REFERENCE,
  198. bool *REFERENCE, bool &REFERENCE
  199. {
  200. SV *tempsv;
  201. tempsv = SvRV($input);
  202. if (!$1) SWIG_croak("expected a reference");
  203. sv_setiv(tempsv, (IV) *$1);
  204. }
  205. %typemap(argout) unsigned int *REFERENCE, unsigned int &REFERENCE,
  206. unsigned short *REFERENCE, unsigned short &REFERENCE,
  207. unsigned long *REFERENCE, unsigned long &REFERENCE,
  208. unsigned char *REFERENCE, unsigned char &REFERENCE
  209. {
  210. SV *tempsv;
  211. tempsv = SvRV($input);
  212. if (!$1) SWIG_croak("expected a reference");
  213. sv_setuv(tempsv, (UV) *$1);
  214. }