Home | History | Annotate | Download | only in typemaps
      1 /*---------------------------------------------------------------------
      2  * Value typemaps (Type, const Type&) for value types, such as
      3  * fundamental types (int, double), that define the AsVal/From
      4  * methods.
      5  *
      6  * To apply them, just use one of the following macros:
      7  *
      8  *   %typemaps_from(FromMeth, FromFrag, Type)
      9  *   %typemaps_asval(CheckCode, AsValMeth, AsValFrag, Type)
     10  *   %typemaps_asvalfrom(CheckCode, AsValMeth, FromMeth, AsValFrag, FromFrag, Type)
     11  *
     12  * or the simpler and normalize form:
     13  *
     14  *   %typemaps_asvalfromn(CheckCode, Type)
     15  *
     16  * Also, you can use the individual typemap definitions:
     17  *
     18  *    %value_in_typemap(asval_meth,frag,Type)
     19  *    %value_varin_typemap(asval_meth,frag,Type)
     20  *    %value_typecheck_typemap(checkcode,asval_meth,frag,Type)
     21  *    %value_directorout_typemap(asval_meth,frag,Type)
     22  *
     23  *    %value_out_typemap(from_meth,frag,Type)
     24  *    %value_varout_typemap(from_meth,frag,Type)
     25  *    %value_constcode_typemap(from_meth,frag,Type)
     26  *    %value_directorin_typemap(from_meth,frag,Type)
     27  *    %value_throws_typemap(from_meth,frag,Type)
     28  *
     29  *---------------------------------------------------------------------*/
     30 
     31 /* in */
     32 
     33 %define %value_in_typemap(asval_meth,frag,Type...)
     34   %typemap(in,noblock=1,fragment=frag) Type (Type val, int ecode = 0) {
     35     ecode = asval_meth($input, &val);
     36     if (!SWIG_IsOK(ecode)) {
     37       %argument_fail(ecode, "$ltype", $symname, $argnum);
     38     }
     39     $1 = %static_cast(val,$ltype);
     40   }
     41   %typemap(freearg) Type "";
     42   %typemap(in,noblock=1,fragment=frag) const Type & ($*ltype temp, Type val, int ecode = 0) {
     43     ecode = asval_meth($input, &val);
     44     if (!SWIG_IsOK(ecode)) {
     45       %argument_fail(ecode, "$*ltype", $symname, $argnum);
     46     }
     47     temp = %static_cast(val, $*ltype);
     48     $1 = &temp;
     49   }
     50   %typemap(freearg) const Type& "";
     51 %enddef
     52 
     53 /* out */
     54 
     55 %define %value_out_typemap(from_meth,frag,Type...)
     56   %typemap(out,noblock=1,fragment=frag) Type, const Type {
     57     %set_output(from_meth(%static_cast($1,Type)));
     58   }
     59   %typemap(out,noblock=1,fragment=frag) const Type& {
     60     %set_output(from_meth(%static_cast(*$1,Type)));
     61   }
     62 %enddef
     63 
     64 /* varin */
     65 
     66 %define %value_varin_typemap(asval_meth,frag,Type...)
     67   %typemap(varin,fragment=frag) Type {
     68     Type val;
     69     int res = asval_meth($input, &val);
     70     if (!SWIG_IsOK(res)) {
     71       %variable_fail(res, "$type", "$name");
     72     }
     73     $1 = %static_cast(val,$ltype);
     74   }
     75 %enddef
     76 
     77 /* varout */
     78 
     79 %define %value_varout_typemap(from_meth,frag,Type...)
     80   %typemap(varout,noblock=1,fragment=frag) Type, const Type&  {
     81     %set_varoutput(from_meth(%static_cast($1,Type)));
     82   }
     83 %enddef
     84 
     85 /* constant installation code */
     86 
     87 %define %value_constcode_typemap(from_meth,frag,Type...)
     88   %typemap(constcode,noblock=1,fragment=frag) Type {
     89     %set_constant("$symname", from_meth(%static_cast($value,Type)));
     90   }
     91 %enddef
     92 
     93 
     94 #if defined(SWIG_DIRECTOR_TYPEMAPS)
     95 
     96 /* directorin */
     97 
     98 %define %value_directorin_typemap(from_meth,frag,Type...)
     99   %typemap(directorin,noblock=1,fragment=frag) Type *DIRECTORIN {
    100     $input = from_meth(%static_cast(*$1,Type));
    101   }
    102   %typemap(directorin,noblock=1,fragment=frag) Type, const Type& {
    103     $input = from_meth(%static_cast($1,Type));
    104   }
    105 %enddef
    106 
    107 /* directorout */
    108 
    109 %define %value_directorout_typemap(asval_meth,frag,Type...)
    110   %typemap(directorargout,noblock=1,fragment=frag) Type *DIRECTOROUT(Type swig_val, int swig_res) {
    111     swig_res = asval_meth($result, &swig_val);
    112     if (!SWIG_IsOK(swig_res)) {
    113       %dirout_fail(swig_res, "$type");
    114     }
    115     *$1 = swig_val;
    116   }
    117   %typemap(directorout,noblock=1,fragment=frag) Type {
    118     Type swig_val;
    119     int swig_res = asval_meth($input, &swig_val);
    120     if (!SWIG_IsOK(swig_res)) {
    121       %dirout_fail(swig_res, "$type");
    122     }
    123     $result = %static_cast(swig_val,$type);
    124   }
    125   %typemap(directorout,noblock=1,fragment=frag,warning=SWIGWARN_TYPEMAP_DIRECTOROUT_PTR_MSG) const Type& {
    126     Type swig_val;
    127     int swig_res = asval_meth($input, &swig_val);
    128     if (!SWIG_IsOK(swig_res)) {
    129       %dirout_fail(swig_res, "$type");
    130     }
    131     $basetype *temp = new $basetype(($basetype)swig_val);
    132     swig_acquire_ownership(temp);
    133     $result = temp;
    134   }
    135   %typemap(directorfree,noblock=1) const Type & {
    136     if (director) {
    137       director->swig_release_ownership(%as_voidptr($input));
    138     }
    139   }
    140   %typemap(directorout,fragment=frag) Type &DIRECTOROUT = Type
    141 %enddef
    142 
    143 #else
    144 
    145 #define %value_directorin_typemap(from_meth,frag,Type...)
    146 #define %value_directorout_typemap(asval_meth,frag,Type...)
    147 
    148 #endif /* SWIG_DIRECTOR_TYPEMAPS */
    149 
    150 
    151 /* throws */
    152 
    153 %define %value_throws_typemap(from_meth,frag,Type...)
    154   %typemap(throws,noblock=1,fragment=frag) Type {
    155     %raise(from_meth(%static_cast($1,Type)), "$type", 0);
    156   }
    157 %enddef
    158 
    159 /* typecheck */
    160 
    161 %define %value_typecheck_typemap(check,asval_meth,frag,Type...)
    162   %typemap(typecheck,precedence=check,fragment=frag) Type, const Type& {
    163     int res = asval_meth($input, NULL);
    164     $1 = SWIG_CheckState(res);
    165   }
    166 %enddef
    167 
    168 /*---------------------------------------------------------------------
    169  * typemap definition for types with AsVal methods
    170  *---------------------------------------------------------------------*/
    171 %define %typemaps_asval(CheckCode, AsValMeth, AsValFrag, Type...)
    172   %value_in_typemap(%arg(AsValMeth), %arg(AsValFrag), Type);
    173   %value_varin_typemap(%arg(AsValMeth), %arg(AsValFrag), Type);
    174   %value_directorout_typemap(%arg(AsValMeth), %arg(AsValFrag), Type);
    175   %value_typecheck_typemap(%arg(CheckCode), %arg(AsValMeth), %arg(AsValFrag), Type);
    176   %value_input_typemap(%arg(CheckCode), %arg(AsValMeth), %arg(AsValFrag), Type);
    177 %enddef
    178 
    179 
    180 /*---------------------------------------------------------------------
    181  * typemap definition for types with from method
    182  *---------------------------------------------------------------------*/
    183 %define %typemaps_from(FromMeth, FromFrag, Type...)
    184   %value_out_typemap(%arg(FromMeth), %arg(FromFrag), Type);
    185   %value_varout_typemap(%arg(FromMeth), %arg(FromFrag), Type);
    186   %value_constcode_typemap(%arg(FromMeth), %arg(FromFrag), Type);
    187   %value_directorin_typemap(%arg(FromMeth), %arg(FromFrag), Type);
    188   %value_throws_typemap(%arg(FromMeth), %arg(FromFrag), Type);
    189   %value_output_typemap(%arg(FromMeth), %arg(FromFrag), Type);
    190 %enddef
    191 
    192 
    193 /*---------------------------------------------------------------------
    194  * typemap definition for types with alval/from method
    195  *---------------------------------------------------------------------*/
    196 
    197 %define %typemaps_asvalfrom(CheckCode, AsValMeth, FromMeth,
    198 			   AsValFrag, FromFrag, Type...)
    199   %typemaps_asval(%arg(CheckCode), %arg(AsValMeth), %arg(AsValFrag), Type);
    200   %typemaps_from(%arg(FromMeth), %arg(FromFrag), Type);
    201   %value_inout_typemap(Type);
    202 %enddef
    203 
    204 
    205 /*---------------------------------------------------------------------
    206  * typemap definition for types  with for 'normalized' asval/from methods
    207  *---------------------------------------------------------------------*/
    208 %define %typemaps_asvalfromn(CheckCode, Type...)
    209   %typemaps_asvalfrom(%arg(CheckCode),
    210 		     SWIG_AsVal(Type),
    211 		     SWIG_From(Type),
    212 		     %arg(SWIG_AsVal_frag(Type)),
    213 		     %arg(SWIG_From_frag(Type)),
    214 		     Type);
    215 %enddef
    216