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

/trunk/Lib/python/typemaps.i

#
Swig | 148 lines | 10 code | 7 blank | 131 comment | 0 complexity | ca33baa4a719ab2395bf8ce4a46c9539 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * typemaps.i
  3. *
  4. * Pointer handling
  5. * These mappings provide support for input/output arguments and common
  6. * uses for C/C++ pointers.
  7. * ----------------------------------------------------------------------------- */
  8. // INPUT typemaps.
  9. // These remap a C pointer to be an "INPUT" value which is passed by value
  10. // instead of reference.
  11. /*
  12. The following methods can be applied to turn a pointer into a simple
  13. "input" value. That is, instead of passing a pointer to an object,
  14. you would use a real value instead.
  15. int *INPUT
  16. short *INPUT
  17. long *INPUT
  18. long long *INPUT
  19. unsigned int *INPUT
  20. unsigned short *INPUT
  21. unsigned long *INPUT
  22. unsigned long long *INPUT
  23. unsigned char *INPUT
  24. bool *INPUT
  25. float *INPUT
  26. double *INPUT
  27. To use these, suppose you had a C function like this :
  28. double fadd(double *a, double *b) {
  29. return *a+*b;
  30. }
  31. You could wrap it with SWIG as follows :
  32. %include <typemaps.i>
  33. double fadd(double *INPUT, double *INPUT);
  34. or you can use the %apply directive :
  35. %include <typemaps.i>
  36. %apply double *INPUT { double *a, double *b };
  37. double fadd(double *a, double *b);
  38. */
  39. // OUTPUT typemaps. These typemaps are used for parameters that
  40. // are output only. The output value is appended to the result as
  41. // a list element.
  42. /*
  43. The following methods can be applied to turn a pointer into an "output"
  44. value. When calling a function, no input value would be given for
  45. a parameter, but an output value would be returned. In the case of
  46. multiple output values, they are returned in the form of a Python tuple.
  47. int *OUTPUT
  48. short *OUTPUT
  49. long *OUTPUT
  50. long long *OUTPUT
  51. unsigned int *OUTPUT
  52. unsigned short *OUTPUT
  53. unsigned long *OUTPUT
  54. unsigned long long *OUTPUT
  55. unsigned char *OUTPUT
  56. bool *OUTPUT
  57. float *OUTPUT
  58. double *OUTPUT
  59. For example, suppose you were trying to wrap the modf() function in the
  60. C math library which splits x into integral and fractional parts (and
  61. returns the integer part in one of its parameters).K:
  62. double modf(double x, double *ip);
  63. You could wrap it with SWIG as follows :
  64. %include <typemaps.i>
  65. double modf(double x, double *OUTPUT);
  66. or you can use the %apply directive :
  67. %include <typemaps.i>
  68. %apply double *OUTPUT { double *ip };
  69. double modf(double x, double *ip);
  70. The Python output of the function would be a tuple containing both
  71. output values.
  72. */
  73. // INOUT
  74. // Mappings for an argument that is both an input and output
  75. // parameter
  76. /*
  77. The following methods can be applied to make a function parameter both
  78. an input and output value. This combines the behavior of both the
  79. "INPUT" and "OUTPUT" methods described earlier. Output values are
  80. returned in the form of a Python tuple.
  81. int *INOUT
  82. short *INOUT
  83. long *INOUT
  84. long long *INOUT
  85. unsigned int *INOUT
  86. unsigned short *INOUT
  87. unsigned long *INOUT
  88. unsigned long long *INOUT
  89. unsigned char *INOUT
  90. bool *INOUT
  91. float *INOUT
  92. double *INOUT
  93. For example, suppose you were trying to wrap the following function :
  94. void neg(double *x) {
  95. *x = -(*x);
  96. }
  97. You could wrap it with SWIG as follows :
  98. %include <typemaps.i>
  99. void neg(double *INOUT);
  100. or you can use the %apply directive :
  101. %include <typemaps.i>
  102. %apply double *INOUT { double *x };
  103. void neg(double *x);
  104. Unlike C, this mapping does not directly modify the input value (since
  105. this makes no sense in Python). Rather, the modified input value shows
  106. up as the return value of the function. Thus, to apply this function
  107. to a Python variable you might do this :
  108. x = neg(x)
  109. Note : previous versions of SWIG used the symbol 'BOTH' to mark
  110. input/output arguments. This is still supported, but will be slowly
  111. phased out in future releases.
  112. */
  113. %include <typemaps/typemaps.swg>