Home | History | Annotate | Download | only in typemaps
      1 /* ------------------------------------------------------------
      2  * Primitive type fragments and macros
      3  * ------------------------------------------------------------ */
      4 
      5 /*
      6   This file provide fragments and macros for the C/C++ primitive types.
      7 
      8   The file defines default fragments for the following types:
      9 
     10     bool
     11     signed char
     12     unsigned char
     13     signed wchar_t     // in C++
     14     unsigned wchar_t   // in C++
     15     short
     16     unsigned short
     17     int
     18     unsigned int
     19     float
     20     size_t
     21     ptrdiff_t
     22 
     23   which can always be redefined in the swig target language if needed.
     24 
     25   The fragments for the following types, however, always need to be
     26   defined in the target language:
     27 
     28     long
     29     unsigned long
     30     long long
     31     unsigned long long
     32     double
     33 
     34   If they are not provided, an #error directive will appear in the
     35   wrapped code.
     36 
     37   --------------------------------------------------------------------
     38 
     39   This file provides the macro
     40 
     41     %typemaps_primitive(CheckCode, Type)
     42 
     43   which generates the typemaps for a primitive type with a given
     44   checkcode. It is assumed that the primitive type is 'normalized' and
     45   the corresponding SWIG_AsVal(Type) and SWIG_From(Type) methods are
     46   provided via fragments.
     47 
     48 
     49   The following auxiliary macros (explained with bash pseudo code) are
     50   also defined:
     51 
     52     %apply_ctypes(Macro)
     53       for i in C Type
     54       do
     55         Macro($i)
     56       done
     57 
     58     %apply_cpptypes(Macro)
     59       for i in C++ Type
     60       do
     61         Macro($i)
     62       done
     63 
     64     %apply_ctypes_2(Macro2)
     65        for i in C Type
     66        do
     67          for j in C Type
     68          do
     69             Macro_2($i, $j)
     70          done
     71        done
     72 
     73     %apply_cpptypes_2(Macro2)
     74        for i in C++ Type
     75        do
     76          for j in C++ Type
     77          do
     78             Macro_2($i, $j)
     79          done
     80        done
     81 
     82     %apply_checkctypes(Macro2)
     83        for i in Check Type
     84        do
     85          Macro2(%checkcode($i), $i)
     86        done
     87 
     88 */
     89 
     90 
     91 /* ------------------------------------------------------------
     92  * Primitive type fragments
     93  * ------------------------------------------------------------ */
     94 /* boolean */
     95 
     96 %fragment(SWIG_From_frag(bool),"header",fragment=SWIG_From_frag(long)) {
     97 SWIGINTERN SWIG_Object
     98 SWIG_From_dec(bool)(bool value)
     99 {
    100   return SWIG_From(long)(value ? 1 : 0);
    101 }
    102 }
    103 
    104 %fragment(SWIG_AsVal_frag(bool),"header",fragment=SWIG_AsVal_frag(long)) {
    105 SWIGINTERN int
    106 SWIG_AsVal_dec(bool)(SWIG_Object obj, bool *val)
    107 {
    108   long v;
    109   int res = SWIG_AsVal(long)(obj, val ? &v : 0);
    110   if (SWIG_IsOK(res)) {
    111     if (val) *val = v ? true : false;
    112     return res;
    113   }
    114   return SWIG_TypeError;
    115 }
    116 }
    117 
    118 /* signed/unsigned char */
    119 
    120 %numeric_slong(signed char,     "<limits.h>", SCHAR_MIN, SCHAR_MAX)
    121 %numeric_ulong(unsigned char,   "<limits.h>", UCHAR_MAX)
    122 
    123 /* short/unsigned short */
    124 
    125 %numeric_slong(short,           "<limits.h>", SHRT_MIN, SHRT_MAX)
    126 %numeric_ulong(unsigned short,  "<limits.h>", USHRT_MAX)
    127 
    128 /* int/unsigned int */
    129 
    130 %numeric_slong(int,             "<limits.h>", INT_MIN, INT_MAX)
    131 %numeric_ulong(unsigned int,    "<limits.h>", UINT_MAX)
    132 
    133 /* signed/unsigned wchar_t */
    134 
    135 #ifdef __cplusplus
    136 %numeric_slong(signed wchar_t,   "<wchar.h>", WCHAR_MIN, WCHAR_MAX)
    137 %numeric_ulong(unsigned wchar_t, "<wchar.h>", UWCHAR_MAX)
    138 #endif
    139 
    140 /* float */
    141 
    142 %numeric_float(float,           "SWIG_Float_Overflow_Check", SWIG_Float_Overflow_Check(v))
    143 
    144 /* long/unsigned long */
    145 
    146 %ensure_type_fragments(long)
    147 %ensure_type_fragments(unsigned long)
    148 
    149 /* long long/unsigned long long */
    150 
    151 %ensure_type_fragments(long long)
    152 %ensure_type_fragments(unsigned long long)
    153 
    154 /* double */
    155 
    156 %ensure_type_fragments(double)
    157 
    158 /* size_t */
    159 
    160 %fragment(SWIG_From_frag(size_t),"header",fragment=SWIG_From_frag(unsigned long)) {
    161 SWIGINTERNINLINE SWIG_Object
    162 SWIG_From_dec(size_t)(size_t value)
    163 {
    164   return SWIG_From(unsigned long)(%numeric_cast(value, unsigned long));
    165 }
    166 }
    167 
    168 %fragment(SWIG_AsVal_frag(size_t),"header",fragment=SWIG_AsVal_frag(unsigned long)) {
    169 SWIGINTERNINLINE int
    170 SWIG_AsVal_dec(size_t)(SWIG_Object obj, size_t *val)
    171 {
    172   unsigned long v;
    173   int res = SWIG_AsVal(unsigned long)(obj, val ? &v : 0);
    174   if (SWIG_IsOK(res) && val) *val = %numeric_cast(v, size_t);
    175   return res;
    176 }
    177 }
    178 
    179 /* ptrdiff_t */
    180 
    181 %fragment(SWIG_From_frag(ptrdiff_t),"header",fragment=SWIG_From_frag(long)) {
    182 SWIGINTERNINLINE SWIG_Object
    183 SWIG_From_dec(ptrdiff_t)(ptrdiff_t value)
    184 {
    185   return SWIG_From(long)(%numeric_cast(value,long));
    186 }
    187 }
    188 
    189 %fragment(SWIG_AsVal_frag(ptrdiff_t),"header",fragment=SWIG_AsVal_frag(long)) {
    190 SWIGINTERNINLINE int
    191 SWIG_AsVal_dec(ptrdiff_t)(SWIG_Object obj, ptrdiff_t *val)
    192 {
    193   long v;
    194   int res = SWIG_AsVal(long)(obj, val ? &v : 0);
    195   if (SWIG_IsOK(res) && val) *val = %numeric_cast(v, ptrdiff_t);
    196   return res;
    197 }
    198 }
    199 
    200 
    201 %fragment("SWIG_CanCastAsInteger","header",
    202 	  fragment=SWIG_AsVal_frag(double),
    203 	  fragment="<float.h>",
    204 	  fragment="<math.h>") {
    205 SWIGINTERNINLINE int
    206 SWIG_CanCastAsInteger(double *d, double min, double max) {
    207   double x = *d;
    208   if ((min <= x && x <= max)) {
    209    double fx = floor(x);
    210    double cx = ceil(x);
    211    double rd =  ((x - fx) < 0.5) ? fx : cx; /* simple rint */
    212    if ((errno == EDOM) || (errno == ERANGE)) {
    213      errno = 0;
    214    } else {
    215      double summ, reps, diff;
    216      if (rd < x) {
    217        diff = x - rd;
    218      } else if (rd > x) {
    219        diff = rd - x;
    220      } else {
    221        return 1;
    222      }
    223      summ = rd + x;
    224      reps = diff/summ;
    225      if (reps < 8*DBL_EPSILON) {
    226        *d = rd;
    227        return 1;
    228      }
    229    }
    230   }
    231   return 0;
    232 }
    233 }
    234 
    235 /* ------------------------------------------------------------
    236  * Generate the typemaps for primitive type
    237  * ------------------------------------------------------------ */
    238 
    239 #define %typemaps_primitive(Code, Type) %typemaps_asvalfromn(%arg(Code), Type)
    240 
    241 /* ------------------------------------------------------------
    242  * Primitive Type Macros
    243  * ------------------------------------------------------------ */
    244 
    245 /* useful macros to derive typemap declarations from primitive types */
    246 
    247 %define _apply_macro(macro, arg2, arg1...)
    248 #if #arg1 != ""
    249 macro(%arg(arg1),arg2);
    250 #else
    251 macro(arg2);
    252 #endif
    253 %enddef
    254 
    255 /* Apply macro to the C-types */
    256 %define %apply_ctypes(Macro, Arg2...)
    257 _apply_macro(Macro, bool               , Arg2);
    258 _apply_macro(Macro, signed char        , Arg2);
    259 _apply_macro(Macro, unsigned char      , Arg2);
    260 _apply_macro(Macro, short              , Arg2);
    261 _apply_macro(Macro, unsigned short     , Arg2);
    262 _apply_macro(Macro, int                , Arg2);
    263 _apply_macro(Macro, unsigned int       , Arg2);
    264 _apply_macro(Macro, long               , Arg2);
    265 _apply_macro(Macro, unsigned long      , Arg2);
    266 _apply_macro(Macro, long long          , Arg2);
    267 _apply_macro(Macro, unsigned long long , Arg2);
    268 _apply_macro(Macro, float              , Arg2);
    269 _apply_macro(Macro, double             , Arg2);
    270 _apply_macro(Macro, char               , Arg2);
    271 _apply_macro(Macro, wchar_t            , Arg2);
    272 _apply_macro(Macro, size_t             , Arg2);
    273 _apply_macro(Macro, ptrdiff_t          , Arg2);
    274 %enddef
    275 
    276 /* apply the Macro2(Type1, Type2) to all  C types  */
    277 #define %apply_ctypes_2(Macro2) %apply_ctypes(%apply_ctypes, Macro2)
    278 
    279 
    280 /* apply the Macro(Type) to all  C++ types  */
    281 %define %apply_cpptypes(Macro, Arg2...)
    282 %apply_ctypes(Macro, Arg2)
    283 _apply_macro(Macro, std::size_t, Arg2);
    284 _apply_macro(Macro, std::ptrdiff_t, Arg2);
    285 _apply_macro(Macro, std::string, Arg2);
    286 _apply_macro(Macro, std::wstring, Arg2);
    287 _apply_macro(Macro, std::complex<float>, Arg2);
    288 _apply_macro(Macro, std::complex<double>, Arg2);
    289 %enddef
    290 
    291 /* apply the Macro2(Type1, Type2) to all  C++ types  */
    292 #define %apply_cpptypes_2(Macro2) %apply_cpptypes(%apply_cpptypes, Macro2)
    293 
    294 /* apply the Macro2(CheckCode,Type) to all  Checked Types */
    295 %define %apply_checkctypes(Macro2)
    296 Macro2(%checkcode(BOOL),    bool);
    297 Macro2(%checkcode(INT8),    signed char);
    298 Macro2(%checkcode(UINT8),   unsigned char);
    299 Macro2(%checkcode(INT16),   short);
    300 Macro2(%checkcode(UINT16),  unsigned short);
    301 Macro2(%checkcode(INT32),   int);
    302 Macro2(%checkcode(UINT32),  unsigned int);
    303 Macro2(%checkcode(INT64),   long);
    304 Macro2(%checkcode(UINT64),  unsigned long);
    305 Macro2(%checkcode(INT128),  long long);
    306 Macro2(%checkcode(UINT128), unsigned long long);
    307 Macro2(%checkcode(FLOAT),   float);
    308 Macro2(%checkcode(DOUBLE),  double);
    309 Macro2(%checkcode(CHAR),    char);
    310 Macro2(%checkcode(UNICHAR), wchar_t);
    311 Macro2(%checkcode(SIZE),    size_t);
    312 Macro2(%checkcode(PTRDIFF), ptrdiff_t);
    313 %enddef
    314 
    315 
    316 /* ------------------------------------------------------------
    317  * Generate the typemaps for all the primitive types with checkcode
    318  * ------------------------------------------------------------ */
    319 
    320 %apply_checkctypes(%typemaps_primitive);
    321 
    322