PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/typemaps/swigmacros.swg

#
Unknown | 245 lines | 189 code | 56 blank | 0 comment | 0 complexity | 6e405455fb8b31805eda0ded59793e41 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* -----------------------------------------------------------------------------
  2. * SWIG API. Portion only visible from SWIG
  3. * ----------------------------------------------------------------------------- */
  4. /*
  5. This file implements the internal macros of the 'SWIG API', which
  6. are useful to implement all the SWIG target languges.
  7. Basic preprocessor macros:
  8. --------------------------
  9. %arg(Arg) Safe argument wrap
  10. %str(Arg) Stringtify the argument
  11. %begin_block Begin a execution block
  12. %end_block End a execution block
  13. %block(Block) Execute Block as a excecution block
  14. %define_as(Def, Val) Define 'Def' as 'Val', expanding Def and Val first
  15. %ifcplusplus(V1, V2) if C++ Mode; then V1; else V2; fi
  16. Casting Operations:
  17. -------------------
  18. SWIG provides the following casting macros, which implement the
  19. corresponding C++ casting operations:
  20. %const_cast(a, Type) const_cast<Type >(a)
  21. %static_cast(a, Type) static_cast<Type >(a)
  22. %reinterpret_cast(a, Type) reinterpret_cast<Type >(a)
  23. %numeric_cast(a, Type) static_cast<Type >(a)
  24. %as_voidptr(a) const_cast<void *>(static_cast<const void *>(a))
  25. %as_voidptrptr(a) reinterpret_cast<void **>(a)
  26. or their C unsafe versions. In C++ we use the safe version unless
  27. SWIG_NO_CPLUSPLUS_CAST is defined (usually via the -nocppcast swig flag).
  28. Memory allocation:
  29. ------------------
  30. These allocation/freeing macros are safe to use in C or C++ and
  31. dispatch the proper new/delete/delete[] or free/malloc calls as
  32. needed.
  33. %new_instance(Type) Allocate a new instance of given Type
  34. %new_copy(value,Type) Allocate and initialize a new instance with 'value'
  35. %new_array(size,Type) Allocate a new array with given size and Type
  36. %new_copy_array(cptr,size,Type) Allocate and initialize a new array from 'cptr'
  37. %delete(cptr) Delete an instance
  38. %delete_array(cptr) Delete an array
  39. Auxiliary loop macros:
  40. ----------------------
  41. %formacro(Macro, Args...) or %formacro_1(Macro, Args...)
  42. for i in Args
  43. do
  44. Macro($i)
  45. done
  46. %formacro_2(Macro2, Args...)
  47. for i,j in Args
  48. do
  49. Macro2($i, $j)
  50. done
  51. Flags and conditional macros:
  52. -----------------------------
  53. %mark_flag(flag)
  54. flag := True
  55. %evalif(flag,expr)
  56. if flag; then
  57. expr
  58. fi
  59. %evalif_2(flag1 flag2,expr)
  60. if flag1 and flag2; then
  61. expr
  62. fi
  63. */
  64. /* -----------------------------------------------------------------------------
  65. * Basic preprocessor macros
  66. * ----------------------------------------------------------------------------- */
  67. #define %arg(Arg...) Arg
  68. #define %str(Arg) `Arg`
  69. #ifndef %begin_block
  70. # define %begin_block do {
  71. #endif
  72. #ifndef %end_block
  73. # define %end_block } while(0)
  74. #endif
  75. #define %block(Block...) %begin_block Block; %end_block
  76. /* define a new macro */
  77. %define %define_as(Def, Val...)%#define Def Val %enddef
  78. /* include C++ or else value */
  79. %define %ifcplusplus(cppval, nocppval)
  80. #ifdef __cplusplus
  81. cppval
  82. #else
  83. nocppval
  84. #endif
  85. %enddef
  86. /* insert the SWIGVERSION in the interface and the wrapper code */
  87. #if SWIG_VERSION
  88. %insert("header") {
  89. %define_as(SWIGVERSION, SWIG_VERSION)
  90. %#define SWIG_VERSION SWIGVERSION
  91. }
  92. #endif
  93. /* -----------------------------------------------------------------------------
  94. * Casting operators
  95. * ----------------------------------------------------------------------------- */
  96. #if defined(SWIG_NO_CPLUSPLUS_CAST)
  97. /* Disable 'modern' cplusplus casting operators */
  98. # if defined(SWIG_CPLUSPLUS_CAST)
  99. # undef SWIG_CPLUSPLUS_CAST
  100. # endif
  101. #endif
  102. #if defined(__cplusplus) && defined(SWIG_CPLUSPLUS_CAST)
  103. # define %const_cast(a,Type...) const_cast< Type >(a)
  104. # define %static_cast(a,Type...) static_cast< Type >(a)
  105. # define %reinterpret_cast(a,Type...) reinterpret_cast< Type >(a)
  106. # define %numeric_cast(a,Type...) static_cast< Type >(a)
  107. #else /* C case */
  108. # define %const_cast(a,Type...) (Type)(a)
  109. # define %static_cast(a,Type...) (Type)(a)
  110. # define %reinterpret_cast(a,Type...) (Type)(a)
  111. # define %numeric_cast(a,Type...) (Type)(a)
  112. #endif /* __cplusplus */
  113. #define %as_voidptr(a) SWIG_as_voidptr(a)
  114. #define %as_voidptrptr(a) SWIG_as_voidptrptr(a)
  115. %insert("header") {
  116. %define_as(SWIG_as_voidptr(a), %const_cast(%static_cast(a,const void *), void *))
  117. %define_as(SWIG_as_voidptrptr(a), ((void)%as_voidptr(*a),%reinterpret_cast(a, void**)))
  118. }
  119. /* -----------------------------------------------------------------------------
  120. * Allocating/freeing elements
  121. * ----------------------------------------------------------------------------- */
  122. #if defined(__cplusplus)
  123. # define %new_instance(Type...) (new Type)
  124. # define %new_copy(val,Type...) (new Type(%static_cast(val, const Type&)))
  125. # define %new_array(size,Type...) (new Type[size])
  126. # define %new_copy_array(ptr,size,Type...) %reinterpret_cast(memcpy(%new_array(size,Type), ptr, sizeof(Type)*(size)), Type*)
  127. # define %delete(cptr) delete cptr
  128. # define %delete_array(cptr) delete[] cptr
  129. #else /* C case */
  130. # define %new_instance(Type...) (Type *)malloc(sizeof(Type))
  131. # define %new_copy(val,Type...) (Type *)memcpy(%new_instance(Type),&val,sizeof(Type))
  132. # define %new_array(size,Type...) (Type *)malloc((size)*sizeof(Type))
  133. # define %new_copy_array(ptr,size,Type...) (Type *)memcpy(%new_array(size,Type), ptr, sizeof(Type)*(size))
  134. # define %delete(cptr) free((char*)cptr)
  135. # define %delete_array(cptr) free((char*)cptr)
  136. #endif /* __cplusplus */
  137. /* -----------------------------------------------------------------------------
  138. * SWIG names and mangling
  139. * ----------------------------------------------------------------------------- */
  140. #define %mangle(Type...) #@Type
  141. #define %descriptor(Type...) SWIGTYPE_ ## #@Type
  142. #define %string_name(Name) "SWIG_" %str(Name)
  143. #define %symbol_name(Name, Type...) SWIG_ ## Name ## _ #@Type
  144. #define %checkcode(Code) SWIG_TYPECHECK_ ## Code
  145. /* -----------------------------------------------------------------------------
  146. * Auxiliary loop macros
  147. * ----------------------------------------------------------------------------- */
  148. /* for loop for macro with one argument */
  149. %define %_formacro_1(macro, arg1,...)macro(arg1)
  150. #if #__VA_ARGS__ != "__fordone__"
  151. %_formacro_1(macro, __VA_ARGS__)
  152. #endif
  153. %enddef
  154. /* for loop for macro with one argument */
  155. %define %formacro_1(macro,...)%_formacro_1(macro,__VA_ARGS__,__fordone__)%enddef
  156. %define %formacro(macro,...)%_formacro_1(macro,__VA_ARGS__,__fordone__)%enddef
  157. /* for loop for macro with two arguments */
  158. %define %_formacro_2(macro, arg1, arg2, ...)macro(arg1, arg2)
  159. #if #__VA_ARGS__ != "__fordone__"
  160. %_formacro_2(macro, __VA_ARGS__)
  161. #endif
  162. %enddef
  163. /* for loop for macro with two arguments */
  164. %define %formacro_2(macro,...)%_formacro_2(macro, __VA_ARGS__, __fordone__)%enddef
  165. /* -----------------------------------------------------------------------------
  166. * SWIG flags
  167. * ----------------------------------------------------------------------------- */
  168. /*
  169. mark a flag, ie, define a macro name but ignore it in
  170. the interface.
  171. the flag can be later used with %evalif
  172. */
  173. %define %mark_flag(x) %define x 1 %enddef %enddef
  174. /*
  175. %evalif and %evalif_2 are use to evaluate or process
  176. an expression if the given predicate is 'true' (1).
  177. */
  178. %define %_evalif(_x,_expr)
  179. #if _x == 1
  180. _expr
  181. #endif
  182. %enddef
  183. %define %_evalif_2(_x,_y,_expr)
  184. #if _x == 1 && _y == 1
  185. _expr
  186. #endif
  187. %enddef
  188. %define %evalif(_x,_expr...) %_evalif(%arg(_x),%arg(_expr)) %enddef
  189. %define %evalif_2(_x,_y,_expr...) %_evalif_2(%arg(_x),%arg(_y),%arg(_expr)) %enddef