PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/go/typemaps.i

#
Swig | 316 lines | 107 code | 32 blank | 177 comment | 0 complexity | 986b3528a405643892ede959a7d92717 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * typemaps.i
  3. *
  4. * Pointer and reference handling typemap library
  5. *
  6. * These mappings provide support for input/output arguments and common
  7. * uses for C/C++ pointers and C++ references.
  8. * ----------------------------------------------------------------------------- */
  9. /*
  10. INPUT typemaps
  11. --------------
  12. These typemaps remap a C pointer or C++ reference to be an "INPUT" value which is
  13. passed by value instead of reference.
  14. The following typemaps can be applied to turn a pointer or reference into a simple
  15. input value. That is, instead of passing a pointer or reference to an object,
  16. you would use a real value instead.
  17. bool *INPUT, bool &INPUT
  18. signed char *INPUT, signed char &INPUT
  19. unsigned char *INPUT, unsigned char &INPUT
  20. short *INPUT, short &INPUT
  21. unsigned short *INPUT, unsigned short &INPUT
  22. int *INPUT, int &INPUT
  23. unsigned int *INPUT, unsigned int &INPUT
  24. long *INPUT, long &INPUT
  25. unsigned long *INPUT, unsigned long &INPUT
  26. long long *INPUT, long long &INPUT
  27. unsigned long long *INPUT, unsigned long long &INPUT
  28. float *INPUT, float &INPUT
  29. double *INPUT, double &INPUT
  30. To use these, suppose you had a C function like this :
  31. double fadd(double *a, double *b) {
  32. return *a+*b;
  33. }
  34. You could wrap it with SWIG as follows :
  35. %include <typemaps.i>
  36. double fadd(double *INPUT, double *INPUT);
  37. or you can use the %apply directive :
  38. %include <typemaps.i>
  39. %apply double *INPUT { double *a, double *b };
  40. double fadd(double *a, double *b);
  41. In Go you could then use it like this:
  42. answer := modulename.Fadd(10.0, 20.0)
  43. There are no char *INPUT typemaps, however you can apply the signed
  44. char * typemaps instead:
  45. %include <typemaps.i>
  46. %apply signed char *INPUT {char *input};
  47. void f(char *input);
  48. */
  49. %define INPUT_TYPEMAP(TYPE, GOTYPE)
  50. %typemap(gotype) TYPE *INPUT, TYPE &INPUT "GOTYPE"
  51. %typemap(in) TYPE *INPUT
  52. %{ $1 = ($1_ltype)&$input; %}
  53. %typemap(in) TYPE &INPUT
  54. %{ $1 = ($1_ltype)$input; %}
  55. %typemap(out) TYPE *INPUT, TYPE &INPUT ""
  56. %typemap(freearg) TYPE *INPUT, TYPE &INPUT ""
  57. %typemap(argout) TYPE *INPUT, TYPE &INPUT ""
  58. // %typemap(typecheck) TYPE *INPUT = TYPE;
  59. // %typemap(typecheck) TYPE &INPUT = TYPE;
  60. %enddef
  61. INPUT_TYPEMAP(bool, bool);
  62. INPUT_TYPEMAP(signed char, int8);
  63. INPUT_TYPEMAP(char, byte);
  64. INPUT_TYPEMAP(unsigned char, byte);
  65. INPUT_TYPEMAP(short, int16);
  66. INPUT_TYPEMAP(unsigned short, uint16);
  67. INPUT_TYPEMAP(int, int);
  68. INPUT_TYPEMAP(unsigned int, uint);
  69. INPUT_TYPEMAP(long, int64);
  70. INPUT_TYPEMAP(unsigned long, uint64);
  71. INPUT_TYPEMAP(long long, int64);
  72. INPUT_TYPEMAP(unsigned long long, uint64);
  73. INPUT_TYPEMAP(float, float32);
  74. INPUT_TYPEMAP(double, float64);
  75. #undef INPUT_TYPEMAP
  76. // OUTPUT typemaps. These typemaps are used for parameters that
  77. // are output only. An array replaces the c pointer or reference parameter.
  78. // The output value is returned in this array passed in.
  79. /*
  80. OUTPUT typemaps
  81. ---------------
  82. The following typemaps can be applied to turn a pointer or reference
  83. into an "output" value. When calling a function, no input value would
  84. be given for a parameter, but an output value would be returned. This
  85. works by a Go slice being passed as a parameter where a c pointer or
  86. reference is required. As with any Go function, the array is passed
  87. by reference so that any modifications to the array will be picked up
  88. in the calling function. Note that the array passed in MUST have at
  89. least one element, but as the c function does not require any input,
  90. the value can be set to anything.
  91. bool *OUTPUT, bool &OUTPUT
  92. signed char *OUTPUT, signed char &OUTPUT
  93. unsigned char *OUTPUT, unsigned char &OUTPUT
  94. short *OUTPUT, short &OUTPUT
  95. unsigned short *OUTPUT, unsigned short &OUTPUT
  96. int *OUTPUT, int &OUTPUT
  97. unsigned int *OUTPUT, unsigned int &OUTPUT
  98. long *OUTPUT, long &OUTPUT
  99. unsigned long *OUTPUT, unsigned long &OUTPUT
  100. long long *OUTPUT, long long &OUTPUT
  101. unsigned long long *OUTPUT, unsigned long long &OUTPUT
  102. float *OUTPUT, float &OUTPUT
  103. double *OUTPUT, double &OUTPUT
  104. For example, suppose you were trying to wrap the modf() function in the
  105. C math library which splits x into integral and fractional parts (and
  106. returns the integer part in one of its parameters):
  107. double modf(double x, double *ip);
  108. You could wrap it with SWIG as follows :
  109. %include <typemaps.i>
  110. double modf(double x, double *OUTPUT);
  111. or you can use the %apply directive :
  112. %include <typemaps.i>
  113. %apply double *OUTPUT { double *ip };
  114. double modf(double x, double *ip);
  115. The Go output of the function would be the function return value and the
  116. value in the single element array. In Go you would use it like this:
  117. ptr := []float64{0.0}
  118. fraction := modulename.Modf(5.0,ptr)
  119. There are no char *OUTPUT typemaps, however you can apply the signed
  120. char * typemaps instead:
  121. %include <typemaps.i>
  122. %apply signed char *OUTPUT {char *output};
  123. void f(char *output);
  124. */
  125. %define OUTPUT_TYPEMAP(TYPE, GOTYPE)
  126. %typemap(gotype) TYPE *OUTPUT, TYPE &OUTPUT %{[]GOTYPE%}
  127. %typemap(in) TYPE *OUTPUT($*1_ltype temp)
  128. {
  129. if ($input.len == 0) {
  130. _swig_gopanic("array must contain at least 1 element");
  131. }
  132. $1 = &temp;
  133. }
  134. %typemap(in) TYPE &OUTPUT($*1_ltype temp)
  135. {
  136. if ($input->len == 0) {
  137. _swig_gopanic("array must contain at least 1 element");
  138. }
  139. $1 = &temp;
  140. }
  141. %typemap(out) TYPE *OUTPUT, TYPE &OUTPUT ""
  142. %typemap(freearg) TYPE *OUTPUT, TYPE &OUTPUT ""
  143. %typemap(argout) TYPE *OUTPUT
  144. {
  145. TYPE* a = (TYPE *) $input.array;
  146. a[0] = temp$argnum;
  147. }
  148. %typemap(argout) TYPE &OUTPUT
  149. {
  150. TYPE* a = (TYPE *) $input->array;
  151. a[0] = temp$argnum;
  152. }
  153. %enddef
  154. OUTPUT_TYPEMAP(bool, bool);
  155. OUTPUT_TYPEMAP(signed char, int8);
  156. OUTPUT_TYPEMAP(char, byte);
  157. OUTPUT_TYPEMAP(unsigned char, byte);
  158. OUTPUT_TYPEMAP(short, int16);
  159. OUTPUT_TYPEMAP(unsigned short, uint16);
  160. OUTPUT_TYPEMAP(int, int);
  161. OUTPUT_TYPEMAP(unsigned int, uint);
  162. OUTPUT_TYPEMAP(long, int64);
  163. OUTPUT_TYPEMAP(unsigned long, uint64);
  164. OUTPUT_TYPEMAP(long long, int64);
  165. OUTPUT_TYPEMAP(unsigned long long, uint64);
  166. OUTPUT_TYPEMAP(float, float32);
  167. OUTPUT_TYPEMAP(double, float64);
  168. #undef OUTPUT_TYPEMAP
  169. /*
  170. INOUT typemaps
  171. --------------
  172. Mappings for a parameter that is both an input and an output parameter
  173. The following typemaps can be applied to make a function parameter both
  174. an input and output value. This combines the behavior of both the
  175. "INPUT" and "OUTPUT" typemaps described earlier. Output values are
  176. returned as an element in a Go slice.
  177. bool *INOUT, bool &INOUT
  178. signed char *INOUT, signed char &INOUT
  179. unsigned char *INOUT, unsigned char &INOUT
  180. short *INOUT, short &INOUT
  181. unsigned short *INOUT, unsigned short &INOUT
  182. int *INOUT, int &INOUT
  183. unsigned int *INOUT, unsigned int &INOUT
  184. long *INOUT, long &INOUT
  185. unsigned long *INOUT, unsigned long &INOUT
  186. long long *INOUT, long long &INOUT
  187. unsigned long long *INOUT, unsigned long long &INOUT
  188. float *INOUT, float &INOUT
  189. double *INOUT, double &INOUT
  190. For example, suppose you were trying to wrap the following function :
  191. void neg(double *x) {
  192. *x = -(*x);
  193. }
  194. You could wrap it with SWIG as follows :
  195. %include <typemaps.i>
  196. void neg(double *INOUT);
  197. or you can use the %apply directive :
  198. %include <typemaps.i>
  199. %apply double *INOUT { double *x };
  200. void neg(double *x);
  201. This works similarly to C in that the mapping directly modifies the
  202. input value - the input must be an array with a minimum of one element.
  203. The element in the array is the input and the output is the element in
  204. the array.
  205. x := []float64{5.0}
  206. Neg(x);
  207. The implementation of the OUTPUT and INOUT typemaps is different to
  208. other languages in that other languages will return the output value
  209. as part of the function return value. This difference is due to Go
  210. being a typed language.
  211. There are no char *INOUT typemaps, however you can apply the signed
  212. char * typemaps instead:
  213. %include <typemaps.i>
  214. %apply signed char *INOUT {char *inout};
  215. void f(char *inout);
  216. */
  217. %define INOUT_TYPEMAP(TYPE, GOTYPE)
  218. %typemap(gotype) TYPE *INOUT, TYPE &INOUT %{[]GOTYPE%}
  219. %typemap(in) TYPE *INOUT {
  220. if ($input.len == 0) {
  221. _swig_gopanic("array must contain at least 1 element");
  222. }
  223. $1 = ($1_ltype) $input.array;
  224. }
  225. %typemap(in) TYPE &INOUT {
  226. if ($input->len == 0) {
  227. _swig_gopanic("array must contain at least 1 element");
  228. }
  229. $1 = ($1_ltype) $input->array;
  230. }
  231. %typemap(out) TYPE *INOUT, TYPE &INOUT ""
  232. %typemap(freearg) TYPE *INOUT, TYPE &INOUT ""
  233. %typemap(argout) TYPE *INOUT, TYPE &INOUT ""
  234. %enddef
  235. INOUT_TYPEMAP(bool, bool);
  236. INOUT_TYPEMAP(signed char, int8);
  237. INOUT_TYPEMAP(char, byte);
  238. INOUT_TYPEMAP(unsigned char, byte);
  239. INOUT_TYPEMAP(short, int16);
  240. INOUT_TYPEMAP(unsigned short, uint16);
  241. INOUT_TYPEMAP(int, int);
  242. INOUT_TYPEMAP(unsigned int, uint);
  243. INOUT_TYPEMAP(long, int64);
  244. INOUT_TYPEMAP(unsigned long, uint64);
  245. INOUT_TYPEMAP(long long, int64);
  246. INOUT_TYPEMAP(unsigned long long, uint64);
  247. INOUT_TYPEMAP(float, float32);
  248. INOUT_TYPEMAP(double, float64);
  249. #undef INOUT_TYPEMAP