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

/tags/rel-1-3-27/SWIG/Lib/python/pyinout.swg

#
Unknown | 233 lines | 184 code | 49 blank | 0 comment | 0 complexity | 4992b11bb9eb16271f57282c5337f381 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. //
  2. // Uncomment the following definition if you don't want the in/out
  3. // typemaps by default, ie, you prefer to use typemaps.i.
  4. //
  5. //#define SWIG_INOUT_NODEF
  6. //
  7. // Use the following definition to enable the INPUT parameters to
  8. // accept both 'by value' and 'pointer' objects.
  9. //
  10. #define SWIG_INPUT_ACCEPT_PTRS
  11. // ------------------------------------------------------------------------
  12. // Pointer handling
  13. //
  14. // These mappings provide support for input/output arguments and common
  15. // uses for C/C++ pointers.
  16. // ------------------------------------------------------------------------
  17. // INPUT typemaps.
  18. // These remap a C pointer to be an "INPUT" value which is passed by value
  19. // instead of reference.
  20. /*
  21. The following methods can be applied to turn a pointer into a simple
  22. "input" value. That is, instead of passing a pointer to an object,
  23. you would use a real value instead.
  24. To use these, suppose you had a C function like this :
  25. double fadd(double *a, double *b) {
  26. return *a+*b;
  27. }
  28. You could wrap it with SWIG as follows :
  29. double fadd(double *INPUT, double *INPUT);
  30. or you can use the %apply directive :
  31. %apply double *INPUT { double *a, double *b };
  32. double fadd(double *a, double *b);
  33. */
  34. #ifdef SWIG_INPUT_ACCEPT_PTRS
  35. #define SWIG_CheckInputPtr(input,arg,desc,disown) (SWIG_ConvertPtr(input,arg,desc,disown) != -1)
  36. #else
  37. #define SWIG_CheckInputPtr(input,arg,desc,disown) (0)
  38. #endif
  39. %define _PYVAL_INPUT_TYPEMAP(code,as_meth,check_meth,as_frag,check_frag,Type)
  40. %typemap(in,fragment=as_frag) Type *INPUT ($*1_ltype temp, int res = 0) {
  41. if (!SWIG_CheckInputPtr($input,(void **)(&$1),$1_descriptor,$disown)) {
  42. temp = as_meth($input);
  43. if (SWIG_arg_fail($argnum)) SWIG_fail;
  44. $1 = &temp;
  45. res = SWIG_NEWOBJ;
  46. }
  47. }
  48. %typemap(in,fragment=as_frag) Type &INPUT($*1_ltype temp, int res = 0) {
  49. if (!SWIG_CheckInputPtr($input,(void **)(&$1),$1_descriptor,$disown)) {
  50. temp = as_meth($input);
  51. if (SWIG_arg_fail($argnum)) SWIG_fail;
  52. $1 = &temp;
  53. res = SWIG_NEWOBJ;
  54. }
  55. if (!$1) {
  56. SWIG_null_ref("$basetype");
  57. }
  58. if (SWIG_arg_fail($argnum)) SWIG_fail;
  59. }
  60. %typemap(typecheck,precedence=code,fragment=check_frag) Type *INPUT, Type &INPUT {
  61. void *ptr;
  62. $1 = (check_meth($input) || (SWIG_CheckInputPtr($input,&ptr,$1_descriptor,0)));
  63. }
  64. %enddef
  65. %define _PYPTR_INPUT_TYPEMAP(code,asptr_meth,asptr_frag,Type)
  66. %typemap(in,fragment=asptr_frag) Type *INPUT(int res = 0) {
  67. res = asptr_meth($input, &$1);
  68. if (!res) {
  69. SWIG_type_error("$basetype", $input);
  70. }
  71. if (SWIG_arg_fail($argnum)) SWIG_fail;
  72. }
  73. %typemap(in,fragment=asptr_frag) Type &INPUT(int res = 0) {
  74. res = asptr_meth($input, &$1);
  75. if (!res) {
  76. SWIG_type_error("$basetype", $input);
  77. } else {
  78. if (!$1) {
  79. SWIG_null_ref("$basetype");
  80. }
  81. }
  82. if (SWIG_arg_fail($argnum)) SWIG_fail;
  83. }
  84. %typemap(freearg) Type *INPUT, Type &INPUT
  85. "if (res$argnum == SWIG_NEWOBJ) delete $1;";
  86. %typemap(typecheck,precedence=code,fragment=asptr_frag) Type *INPUT, Type &INPUT
  87. "$1 = asptr_meth($input, (Type**)0) != 0;"
  88. %enddef
  89. // OUTPUT typemaps. These typemaps are used for parameters that
  90. // are output only. The output value is appended to the result as
  91. // a list element.
  92. /*
  93. The following methods can be applied to turn a pointer into an "output"
  94. value. When calling a function, no input value would be given for
  95. a parameter, but an output value would be returned. In the case of
  96. multiple output values, they are returned in the form of a Python tuple.
  97. For example, suppose you were trying to wrap the modf() function in the
  98. C math library which splits x into integral and fractional parts (and
  99. returns the integer part in one of its parameters).K:
  100. double modf(double x, double *ip);
  101. You could wrap it with SWIG as follows :
  102. double modf(double x, double *OUTPUT);
  103. or you can use the %apply directive :
  104. %apply double *OUTPUT { double *ip };
  105. double modf(double x, double *ip);
  106. The Python output of the function would be a tuple containing both
  107. output values.
  108. */
  109. // These typemaps contributed by Robin Dunn
  110. //----------------------------------------------------------------------
  111. //
  112. // T_OUTPUT typemap (and helper function) to return multiple argouts as
  113. // a tuple instead of a list.
  114. //
  115. // Author: Robin Dunn
  116. //----------------------------------------------------------------------
  117. %include <pytuplehlp.swg>
  118. %define _PYVAL_OUTPUT_TYPEMAP(from_meth, from_frag, Type)
  119. %typemap(in,numinputs=0) Type *OUTPUT ($*1_ltype temp, int res = 0),
  120. Type &OUTPUT ($*1_ltype temp, int res = 0)
  121. "$1 = &temp; res = SWIG_NEWOBJ;";
  122. %fragment("t_out_helper"{Type},"header",
  123. fragment="t_output_helper",fragment=from_frag) {}
  124. %typemap(argout,fragment="t_out_helper"{Type}) Type *OUTPUT, Type &OUTPUT
  125. "$result = t_output_helper($result, ((res$argnum == SWIG_NEWOBJ) ?
  126. from_meth((*$1)) : SWIG_NewPointerObj((void*)($1), $1_descriptor, 0)));";
  127. %enddef
  128. // INOUT
  129. // Mappings for an argument that is both an input and output
  130. // parameter
  131. /*
  132. The following methods can be applied to make a function parameter both
  133. an input and output value. This combines the behavior of both the
  134. "INPUT" and "OUTPUT" methods described earlier. Output values are
  135. returned in the form of a Python tuple.
  136. For example, suppose you were trying to wrap the following function :
  137. void neg(double *x) {
  138. *x = -(*x);
  139. }
  140. You could wrap it with SWIG as follows :
  141. void neg(double *INOUT);
  142. or you can use the %apply directive :
  143. %apply double *INOUT { double *x };
  144. void neg(double *x);
  145. Unlike C, this mapping does not directly modify the input value (since
  146. this makes no sense in Python). Rather, the modified input value shows
  147. up as the return value of the function. Thus, to apply this function
  148. to a Python variable you might do this :
  149. x = neg(x)
  150. Note : previous versions of SWIG used the symbol 'BOTH' to mark
  151. input/output arguments. This is still supported, but will be slowly
  152. phased out in future releases.
  153. */
  154. %define _PYVAL_INOUT_TYPEMAP(Type)
  155. %typemap(in) Type *INOUT = Type *INPUT;
  156. %typemap(in) Type &INOUT = Type &INPUT;
  157. %typemap(typecheck) Type *INOUT = Type *INPUT;
  158. %typemap(typecheck) Type &INOUT = Type &INPUT;
  159. %typemap(argout) Type *INOUT = Type *OUTPUT;
  160. %typemap(argout) Type &INOUT = Type &OUTPUT;
  161. %enddef
  162. %define _PYPTR_INOUT_TYPEMAP(Type)
  163. _PYVAL_INOUT_TYPEMAP(SWIG_arg(Type))
  164. %typemap(freearg) Type *INOUT = Type *INPUT;
  165. %typemap(freearg) Type &INOUT = Type &INPUT;
  166. %enddef
  167. #ifndef SWIG_INOUT_NODEF
  168. #define PYVAL_INPUT_TYPEMAP(code,_a,_c,_af,_cf,...) \
  169. _PYVAL_INPUT_TYPEMAP(SWIG_arg(code),SWIG_arg(_a),SWIG_arg(_c), \
  170. SWIG_arg(_af),SWIG_arg(_cf),SWIG_arg(__VA_ARGS__))
  171. #define PYPTR_INPUT_TYPEMAP(code,_a,_af,...) \
  172. _PYPTR_INPUT_TYPEMAP(SWIG_arg(code),SWIG_arg(_a),SWIG_arg(_af), \
  173. SWIG_arg(__VA_ARGS__))
  174. #define PYVAL_OUTPUT_TYPEMAP(_f,_ff,...) \
  175. _PYVAL_OUTPUT_TYPEMAP(SWIG_arg(_f),SWIG_arg(_ff),SWIG_arg(__VA_ARGS__))
  176. #define PYVAL_INOUT_TYPEMAP(...) _PYVAL_INOUT_TYPEMAP(SWIG_arg(__VA_ARGS__))
  177. #define PYPTR_INOUT_TYPEMAP(...) _PYPTR_INOUT_TYPEMAP(SWIG_arg(__VA_ARGS__))
  178. #else /* You need to include typemaps.i */
  179. #define PYVAL_OUTPUT_TYPEMAP(...)
  180. #define PYVAL_INPUT_TYPEMAP(...)
  181. #define PYVAL_INOUT_TYPEMAP(...)
  182. #define PYPTR_INPUT_TYPEMAP(...)
  183. #define PYPTR_INOUT_TYPEMAP(...)
  184. #endif /* SWIG_INOUT_DEFAULT */