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

# · Swig · 190 lines · 49 code · 13 blank · 128 comment · 0 complexity · 0dd538c5be7c870013d199e2edae16aa MD5 · raw file

  1. //
  2. // SWIG Typemap library
  3. // Dave Beazley
  4. // May 5, 1997
  5. //
  6. // Python 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. // Disclaimer : Unless you really understand how typemaps work, this file
  13. // probably isn't going to make much sense.
  14. //
  15. // ------------------------------------------------------------------------
  16. // Pointer handling
  17. //
  18. // These mappings provide support for input/output arguments and common
  19. // uses for C/C++ pointers.
  20. // ------------------------------------------------------------------------
  21. // INPUT typemaps.
  22. // These remap a C pointer to be an "INPUT" value which is passed by value
  23. // instead of reference.
  24. /*
  25. The following methods can be applied to turn a pointer into a simple
  26. "input" value. That is, instead of passing a pointer to an object,
  27. you would use a real value instead.
  28. int *INPUT
  29. short *INPUT
  30. long *INPUT
  31. long long *INPUT
  32. unsigned int *INPUT
  33. unsigned short *INPUT
  34. unsigned long *INPUT
  35. unsigned long long *INPUT
  36. unsigned char *INPUT
  37. bool *INPUT
  38. float *INPUT
  39. double *INPUT
  40. To use these, suppose you had a C function like this :
  41. double fadd(double *a, double *b) {
  42. return *a+*b;
  43. }
  44. You could wrap it with SWIG as follows :
  45. %include <typemaps.i>
  46. double fadd(double *INPUT, double *INPUT);
  47. or you can use the %apply directive :
  48. %include <typemaps.i>
  49. %apply double *INPUT { double *a, double *b };
  50. double fadd(double *a, double *b);
  51. */
  52. // OUTPUT typemaps. These typemaps are used for parameters that
  53. // are output only. The output value is appended to the result as
  54. // a list element.
  55. /*
  56. The following methods can be applied to turn a pointer into an "output"
  57. value. When calling a function, no input value would be given for
  58. a parameter, but an output value would be returned. In the case of
  59. multiple output values, they are returned in the form of a Python tuple.
  60. int *OUTPUT
  61. short *OUTPUT
  62. long *OUTPUT
  63. long long *OUTPUT
  64. unsigned int *OUTPUT
  65. unsigned short *OUTPUT
  66. unsigned long *OUTPUT
  67. unsigned long long *OUTPUT
  68. unsigned char *OUTPUT
  69. bool *OUTPUT
  70. float *OUTPUT
  71. double *OUTPUT
  72. For example, suppose you were trying to wrap the modf() function in the
  73. C math library which splits x into integral and fractional parts (and
  74. returns the integer part in one of its parameters).K:
  75. double modf(double x, double *ip);
  76. You could wrap it with SWIG as follows :
  77. %include <typemaps.i>
  78. double modf(double x, double *OUTPUT);
  79. or you can use the %apply directive :
  80. %include <typemaps.i>
  81. %apply double *OUTPUT { double *ip };
  82. double modf(double x, double *ip);
  83. The Python output of the function would be a tuple containing both
  84. output values.
  85. */
  86. // INOUT
  87. // Mappings for an argument that is both an input and output
  88. // parameter
  89. /*
  90. The following methods can be applied to make a function parameter both
  91. an input and output value. This combines the behavior of both the
  92. "INPUT" and "OUTPUT" methods described earlier. Output values are
  93. returned in the form of a Python tuple.
  94. int *INOUT
  95. short *INOUT
  96. long *INOUT
  97. long long *INOUT
  98. unsigned int *INOUT
  99. unsigned short *INOUT
  100. unsigned long *INOUT
  101. unsigned long long *INOUT
  102. unsigned char *INOUT
  103. bool *INOUT
  104. float *INOUT
  105. double *INOUT
  106. For example, suppose you were trying to wrap the following function :
  107. void neg(double *x) {
  108. *x = -(*x);
  109. }
  110. You could wrap it with SWIG as follows :
  111. %include <typemaps.i>
  112. void neg(double *INOUT);
  113. or you can use the %apply directive :
  114. %include <typemaps.i>
  115. %apply double *INOUT { double *x };
  116. void neg(double *x);
  117. Unlike C, this mapping does not directly modify the input value (since
  118. this makes no sense in Python). Rather, the modified input value shows
  119. up as the return value of the function. Thus, to apply this function
  120. to a Python variable you might do this :
  121. x = neg(x)
  122. Note : previous versions of SWIG used the symbol 'BOTH' to mark
  123. input/output arguments. This is still supported, but will be slowly
  124. phased out in future releases.
  125. */
  126. %include <pyinout.swg>
  127. #ifdef SWIG_INOUT_NODEF
  128. /*
  129. Apply the INPUT/OUTPUT typemaps to all the C types (int, double, ...) if
  130. not already defined.
  131. */
  132. %define %typemap_inout(Code,AsMeth, CheckMeth, FromMeth, AsFrag, CheckFrag, FromFrag, ...)
  133. _PYVAL_INPUT_TYPEMAP(SWIG_arg(Code), SWIG_arg(AsMeth), SWIG_arg(CheckMeth),
  134. SWIG_arg(AsFrag), SWIG_arg(CheckFrag), SWIG_arg(__VA_ARGS__));
  135. _PYVAL_OUTPUT_TYPEMAP(SWIG_arg(FromMeth), SWIG_arg(FromFrag), SWIG_arg(__VA_ARGS__));
  136. _PYVAL_INOUT_TYPEMAP(SWIG_arg(__VA_ARGS__));
  137. %enddef
  138. %define %typemap_inoutn(Code,...)
  139. %typemap_inout(SWIG_arg(Code),
  140. SWIG_arg(SWIG_As(__VA_ARGS__)),
  141. SWIG_arg(SWIG_Check(__VA_ARGS__)),
  142. SWIG_arg(SWIG_From(__VA_ARGS__)),
  143. SWIG_arg(SWIG_As_frag(__VA_ARGS__)),
  144. SWIG_arg(SWIG_Check_frag(__VA_ARGS__)),
  145. SWIG_arg(SWIG_From_frag(__VA_ARGS__)),
  146. SWIG_arg(__VA_ARGS__));
  147. %enddef
  148. %apply_checkctypes(%typemap_inoutn)
  149. #endif