Home | History | Annotate | Download | only in typemaps
      1 /*
      2   Fragments
      3   =========
      4   See the "Typemap fragments" section in the documentation for understanding
      5   fragments. Below is some info on how fragments and automatic type
      6   specialization is used.
      7 
      8   Macros that make the automatic generation of typemaps easier are provided.
      9 
     10   Consider the following code:
     11 
     12       %fragment(SWIG_From_frag(bool), "header") {
     13       static PyObject*
     14       SWIG_From_dec(bool)(bool value)
     15       {
     16         PyObject *obj = value ? Py_True : Py_False;
     17         Py_INCREF(obj);
     18         return obj;
     19       }
     20       }
     21 
     22       %typemap(out, fragment=SWIG_From_frag(bool)) bool {
     23         $result = SWIG_From(bool)($1));
     24       }
     25 
     26   Here the macros
     27 
     28       SWIG_From_frag  => fragment
     29       SWIG_From_dec   => declaration
     30       SWIG_From       => call
     31 
     32   allow you to define/include a fragment, and declare and call the
     33   'from-bool' method as needed. In the simpler case, these macros
     34   just return something like
     35 
     36       SWIG_From_frag(bool)  => "SWIG_From_bool"
     37       SWIG_From_dec(bool)   =>  SWIG_From_bool
     38       SWIG_From(bool)       =>  SWIG_From_bool
     39 
     40   But they are specialized for the different languages requirements,
     41   such as perl or tcl that requires passing the interpreter pointer,
     42   and also they can manage C++ ugly types, for example:
     43 
     44       SWIG_From_frag(std::complex<double>)  => "SWIG_From_std_complex_Sl_double_Sg_"
     45       SWIG_From_dec(std::complex<double>)   =>  SWIG_From_std_complex_Sl_double_Sg_
     46       SWIG_From(std::complex<double>)       =>  SWIG_From_std_complex_Sl_double_Sg_
     47 
     48 
     49   Hence, to declare methods to use with typemaps, always use the
     50   SWIG_From* macros. In the same way, the SWIG_AsVal* and SWIG_AsPtr*
     51   set of macros are provided.
     52 
     53 */
     54 
     55 
     56 /* -----------------------------------------------------------------------------
     57  * Define the basic macros to 'normalize' the type fragments
     58  * ----------------------------------------------------------------------------- */
     59 
     60 #ifndef SWIG_AS_DECL_ARGS
     61 #define SWIG_AS_DECL_ARGS
     62 #endif
     63 
     64 #ifndef SWIG_FROM_DECL_ARGS
     65 #define SWIG_FROM_DECL_ARGS
     66 #endif
     67 
     68 #ifndef SWIG_AS_CALL_ARGS
     69 #define SWIG_AS_CALL_ARGS
     70 #endif
     71 
     72 #ifndef SWIG_FROM_CALL_ARGS
     73 #define SWIG_FROM_CALL_ARGS
     74 #endif
     75 
     76 #define %fragment_name(Name, Type...)     %string_name(Name) "_" {Type}
     77 
     78 #define SWIG_Traits_frag(Type...) %fragment_name(Traits, Type)
     79 #define SWIG_AsPtr_frag(Type...)  %fragment_name(AsPtr, Type)
     80 #define SWIG_AsVal_frag(Type...)  %fragment_name(AsVal, Type)
     81 #define SWIG_From_frag(Type...)   %fragment_name(From, Type)
     82 
     83 #define SWIG_AsVal_name(Type...)  %symbol_name(AsVal, Type)
     84 #define SWIG_AsPtr_name(Type...)  %symbol_name(AsPtr, Type)
     85 #define SWIG_From_name(Type...)   %symbol_name(From, Type)
     86 
     87 #define SWIG_AsVal_dec(Type...)   SWIG_AsVal_name(Type) SWIG_AS_DECL_ARGS
     88 #define SWIG_AsPtr_dec(Type...)   SWIG_AsPtr_name(Type) SWIG_AS_DECL_ARGS
     89 #define SWIG_From_dec(Type...)    SWIG_From_name(Type)  SWIG_FROM_DECL_ARGS
     90 
     91 #define SWIG_AsVal(Type...)       SWIG_AsVal_name(Type) SWIG_AS_CALL_ARGS
     92 #define SWIG_AsPtr(Type...)  	  SWIG_AsPtr_name(Type) SWIG_AS_CALL_ARGS
     93 #define SWIG_From(Type...)   	  SWIG_From_name(Type)  SWIG_FROM_CALL_ARGS
     94 
     95 /* ------------------------------------------------------------
     96  * common fragments
     97  * ------------------------------------------------------------ */
     98 
     99 /* Default compiler options for gcc allow long_long but not LLONG_MAX.
    100  * Define SWIG_NO_LLONG_MAX if this added limits support is not wanted. */
    101 %fragment("<limits.h>","header") %{
    102 #include <limits.h>
    103 #if !defined(SWIG_NO_LLONG_MAX)
    104 # if !defined(LLONG_MAX) && defined(__GNUC__) && defined (__LONG_LONG_MAX__)
    105 #   define LLONG_MAX __LONG_LONG_MAX__
    106 #   define LLONG_MIN (-LLONG_MAX - 1LL)
    107 #   define ULLONG_MAX (LLONG_MAX * 2ULL + 1ULL)
    108 # endif
    109 #endif
    110 %}
    111 
    112 %fragment("<math.h>","header") %{
    113 #include <math.h>
    114 %}
    115 
    116 %fragment("<wchar.h>","header") %{
    117 #include <wchar.h>
    118 #include <limits.h>
    119 #ifndef WCHAR_MIN
    120 #  define WCHAR_MIN 0
    121 #endif
    122 #ifndef WCHAR_MAX
    123 #  define WCHAR_MAX 65535
    124 #endif
    125 %}
    126 
    127 %fragment("<float.h>","header") %{
    128 #include <float.h>
    129 %}
    130 
    131 %fragment("<stdio.h>","header") %{
    132 #include <stdio.h>
    133 #if defined(_MSC_VER) || defined(__BORLANDC__) || defined(_WATCOM)
    134 # ifndef snprintf
    135 #  define snprintf _snprintf
    136 # endif
    137 #endif
    138 %}
    139 
    140 %fragment("<stdlib.h>","header") %{
    141 #include <stdlib.h>
    142 #ifdef _MSC_VER
    143 # ifndef strtoull
    144 #  define strtoull _strtoui64
    145 # endif
    146 # ifndef strtoll
    147 #  define strtoll _strtoi64
    148 # endif
    149 #endif
    150 %}
    151 
    152 %fragment("<stddef.h>", "header")  %{
    153   #include <stddef.h>
    154 %}
    155 
    156 %fragment("SWIG_isfinite","header",fragment="<math.h>,<float.h>") %{
    157 /* Getting isfinite working pre C99 across multiple platforms is non-trivial. Users can provide SWIG_isfinite on older platforms. */
    158 #ifndef SWIG_isfinite
    159 # if defined(isfinite)
    160 #  define SWIG_isfinite(X) (isfinite(X))
    161 # elif defined(_MSC_VER)
    162 #  define SWIG_isfinite(X) (_finite(X))
    163 # elif defined(__sun) && defined(__SVR4)
    164 #  include <ieeefp.h>
    165 #  define SWIG_isfinite(X) (finite(X))
    166 # endif
    167 #endif
    168 %}
    169 
    170 %fragment("SWIG_Float_Overflow_Check","header",fragment="<float.h>,SWIG_isfinite") %{
    171 /* Accept infinite as a valid float value unless we are unable to check if a value is finite */
    172 #ifdef SWIG_isfinite
    173 # define SWIG_Float_Overflow_Check(X) ((X < -FLT_MAX || X > FLT_MAX) && SWIG_isfinite(X))
    174 #else
    175 # define SWIG_Float_Overflow_Check(X) ((X < -FLT_MAX || X > FLT_MAX))
    176 #endif
    177 %}
    178 
    179 /* -----------------------------------------------------------------------------
    180  * special macros for fragments
    181  * ----------------------------------------------------------------------------- */
    182 
    183 /* Macros to derive numeric types */
    184 
    185 %define %numeric_type_from(Type, Base)
    186 %fragment(SWIG_From_frag(Type),"header",
    187 	  fragment=SWIG_From_frag(Base)) {
    188 SWIGINTERNINLINE SWIG_Object
    189 SWIG_From_dec(Type)(Type value)
    190 {
    191   return SWIG_From(Base)(value);
    192 }
    193 }
    194 %enddef
    195 
    196 %define %numeric_type_asval(Type, Base, Frag, OverflowCond)
    197 %fragment(SWIG_AsVal_frag(Type),"header",
    198 	  fragment=Frag,
    199 	  fragment=SWIG_AsVal_frag(Base)) {
    200 SWIGINTERN int
    201 SWIG_AsVal_dec(Type)(SWIG_Object obj, Type *val)
    202 {
    203   Base v;
    204   int res = SWIG_AsVal(Base)(obj, &v);
    205   if (SWIG_IsOK(res)) {
    206     if (OverflowCond) {
    207       return SWIG_OverflowError;
    208     } else {
    209       if (val) *val = %numeric_cast(v, Type);
    210     }
    211   }
    212   return res;
    213 }
    214 }
    215 %enddef
    216 
    217 #define %numeric_signed_type_asval(Type, Base, Frag, Min, Max) \
    218 %numeric_type_asval(Type, Base, Frag, (v < Min || v > Max))
    219 
    220 #define %numeric_unsigned_type_asval(Type, Base, Frag, Max) \
    221 %numeric_type_asval(Type, Base, Frag, (v > Max))
    222 
    223 
    224 /* Macro for 'signed long' derived types */
    225 
    226 %define %numeric_slong(Type, Frag, Min, Max)
    227 %numeric_type_from(Type, long)
    228 %numeric_signed_type_asval(Type, long, Frag , Min, Max)
    229 %enddef
    230 
    231 /* Macro for 'unsigned long' derived types */
    232 
    233 %define %numeric_ulong(Type, Frag, Max)
    234 %numeric_type_from(Type, unsigned long)
    235 %numeric_unsigned_type_asval(Type, unsigned long, Frag, Max)
    236 %enddef
    237 
    238 
    239 /* Macro for floating point derived types (original macro) */
    240 
    241 %define %numeric_double(Type, Frag, Min, Max)
    242 %numeric_type_from(Type, double)
    243 %numeric_signed_type_asval(Type, double, Frag , Min, Max)
    244 %enddef
    245 
    246 /* Macro for floating point derived types */
    247 
    248 %define %numeric_float(Type, Frag, OverflowCond)
    249 %numeric_type_from(Type, double)
    250 %numeric_type_asval(Type, double, Frag, OverflowCond)
    251 %enddef
    252 
    253 
    254 /* Macros for missing fragments */
    255 
    256 %define %ensure_fragment(Fragment)
    257 %fragment(`Fragment`,"header") {
    258 %#error "SWIG language implementation must provide the Fragment fragment"
    259 }
    260 %enddef
    261 
    262 %define %ensure_type_fragments(Type)
    263 %fragment(SWIG_From_frag(Type),"header") {
    264 %#error "SWIG language implementation must provide a SWIG_From_frag(Type) fragment"
    265 }
    266 %fragment(SWIG_AsVal_frag(Type),"header") {
    267 %#error "SWIG language implementation must provide a SWIG_AsVal_frag(Type) fragment"
    268 }
    269 %enddef
    270