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