Home | History | Annotate | Download | only in tcl
      1 /* -----------------------------------------------------------------------------
      2  * typemaps.i
      3  *
      4  * SWIG typemap library for Tcl8.  This file contains various sorts
      5  * of typemaps for modifying SWIG's code generation.
      6  * ----------------------------------------------------------------------------- */
      7 
      8 #if !defined(SWIG_USE_OLD_TYPEMAPS)
      9 %include <typemaps/typemaps.swg>
     10 #else
     11 
     12 /*
     13 The SWIG typemap library provides a language independent mechanism for
     14 supporting output arguments, input values, and other C function
     15 calling mechanisms.  The primary use of the library is to provide a
     16 better interface to certain C function--especially those involving
     17 pointers.
     18 */
     19 
     20 // INPUT typemaps.
     21 // These remap a C pointer to be an "INPUT" value which is passed by value
     22 // instead of reference.
     23 
     24 /*
     25 The following methods can be applied to turn a pointer into a simple
     26 "input" value.  That is, instead of passing a pointer to an object,
     27 you would use a real value instead.
     28 
     29          int            *INPUT
     30          short          *INPUT
     31          long           *INPUT
     32          long long      *INPUT
     33          unsigned int   *INPUT
     34          unsigned short *INPUT
     35          unsigned long  *INPUT
     36          unsigned long long *INPUT
     37          unsigned char  *INPUT
     38          bool           *INPUT
     39          float          *INPUT
     40          double         *INPUT
     41 
     42 To use these, suppose you had a C function like this :
     43 
     44         double fadd(double *a, double *b) {
     45                return *a+*b;
     46         }
     47 
     48 You could wrap it with SWIG as follows :
     49 
     50         %include typemaps.i
     51         double fadd(double *INPUT, double *INPUT);
     52 
     53 or you can use the %apply directive :
     54 
     55         %include typemaps.i
     56         %apply double *INPUT { double *a, double *b };
     57         double fadd(double *a, double *b);
     58 
     59 */
     60 
     61 %typemap(in) double *INPUT(double temp), double &INPUT(double temp)
     62 {
     63   if (Tcl_GetDoubleFromObj(interp,$input,&temp) == TCL_ERROR) {
     64     SWIG_fail;
     65   }
     66   $1 = &temp;
     67 }
     68 
     69 %typemap(in) float *INPUT(double dvalue, float  temp), float &INPUT(double dvalue, float temp)
     70 {
     71   if (Tcl_GetDoubleFromObj(interp,$input,&dvalue) == TCL_ERROR) {
     72     SWIG_fail;
     73   }
     74   temp = (float) dvalue;
     75   $1 = &temp;
     76 }
     77 
     78 %typemap(in) int  *INPUT(int temp), int &INPUT(int temp)
     79 {
     80   if (Tcl_GetIntFromObj(interp,$input,&temp) == TCL_ERROR) {
     81     SWIG_fail;
     82   }
     83   $1 = &temp;
     84 }
     85 
     86 %typemap(in) short *INPUT(int ivalue, short temp), short &INPUT(int ivalue, short temp)
     87 {
     88   if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) {
     89     SWIG_fail;
     90   }
     91   temp = (short) ivalue;
     92   $1 = &temp;
     93 }
     94 
     95 %typemap(in) long *INPUT(int ivalue, long temp), long &INPUT(int ivalue, long temp)
     96 {
     97   if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) {
     98     SWIG_fail;
     99   }
    100   temp = (long) ivalue;
    101   $1 = &temp;
    102 }
    103 
    104 %typemap(in) unsigned int  *INPUT(int ivalue, unsigned int temp),
    105              unsigned int  &INPUT(int ivalue, unsigned int temp)
    106 {
    107   if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) {
    108     SWIG_fail;
    109   }
    110   temp = (unsigned int) ivalue;
    111   $1 = &temp;
    112 }
    113 
    114 %typemap(in) unsigned short *INPUT(int ivalue, unsigned short temp),
    115              unsigned short &INPUT(int ivalue, unsigned short temp)
    116 {
    117   if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) {
    118     SWIG_fail;
    119   }
    120   temp = (unsigned short) ivalue;
    121   $1 = &temp;
    122 }
    123 
    124 %typemap(in) unsigned long *INPUT(int ivalue, unsigned long temp),
    125              unsigned long &INPUT(int ivalue, unsigned long temp)
    126 {
    127   if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) {
    128     SWIG_fail;
    129   }
    130   temp = (unsigned long) ivalue;
    131   $1 = &temp;
    132 }
    133 
    134 %typemap(in) unsigned char *INPUT(int ivalue, unsigned char temp),
    135              unsigned char &INPUT(int ivalue, unsigned char temp)
    136 {
    137   if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) {
    138     SWIG_fail;
    139   }
    140   temp = (unsigned char) ivalue;
    141   $1 = &temp;
    142 }
    143 
    144 %typemap(in) signed char *INPUT(int ivalue, signed char temp),
    145              signed char &INPUT(int ivalue, signed char temp)
    146 {
    147   if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) {
    148     SWIG_fail;
    149   }
    150   temp = (signed char) ivalue;
    151   $1 = &temp;
    152 }
    153 
    154 %typemap(in) bool *INPUT(int ivalue, bool temp),
    155              bool &INPUT(int ivalue, bool temp)
    156 {
    157   if (Tcl_GetIntFromObj(interp,$input,&ivalue) == TCL_ERROR) {
    158     SWIG_fail;
    159   }
    160   temp = ivalue ? true : false;
    161   $1 = &temp;
    162 }
    163 
    164 %typemap(in) long long *INPUT($*1_ltype temp),
    165              long long &INPUT($*1_ltype temp)
    166 {
    167   temp = ($*1_ltype) strtoll(Tcl_GetStringFromObj($input,NULL),0,0);
    168   $1 = &temp;
    169 }
    170 
    171 %typemap(in) unsigned long long *INPUT($*1_ltype temp),
    172              unsigned long long &INPUT($*1_ltype temp)
    173 {
    174   temp = ($*1_ltype) strtoull(Tcl_GetStringFromObj($input,NULL),0,0);
    175   $1 = &temp;
    176 }
    177 
    178 // OUTPUT typemaps.   These typemaps are used for parameters that
    179 // are output only.   The output value is appended to the result as
    180 // a list element.
    181 
    182 /*
    183 The following methods can be applied to turn a pointer into an "output"
    184 value.  When calling a function, no input value would be given for
    185 a parameter, but an output value would be returned.  In the case of
    186 multiple output values, they are returned in the form of a Tcl list.
    187 
    188          int            *OUTPUT
    189          short          *OUTPUT
    190          long           *OUTPUT
    191          long long      *OUTPUT
    192          unsigned int   *OUTPUT
    193          unsigned short *OUTPUT
    194          unsigned long  *OUTPUT
    195          unsigned long long *OUTPUT
    196          unsigned char  *OUTPUT
    197          bool           *OUTPUT
    198          float          *OUTPUT
    199          double         *OUTPUT
    200 
    201 For example, suppose you were trying to wrap the modf() function in the
    202 C math library which splits x into integral and fractional parts (and
    203 returns the integer part in one of its parameters).K:
    204 
    205         double modf(double x, double *ip);
    206 
    207 You could wrap it with SWIG as follows :
    208 
    209         %include typemaps.i
    210         double modf(double x, double *OUTPUT);
    211 
    212 or you can use the %apply directive :
    213 
    214         %include typemaps.i
    215         %apply double *OUTPUT { double *ip };
    216         double modf(double x, double *ip);
    217 
    218 The Tcl output of the function would be a list containing both
    219 output values.
    220 
    221 */
    222 
    223 %typemap(in,numinputs=0)     int            *OUTPUT(int temp),
    224                      short          *OUTPUT(short temp),
    225                      long           *OUTPUT(long temp),
    226                      unsigned int   *OUTPUT(unsigned int temp),
    227                      unsigned short *OUTPUT(unsigned short temp),
    228                      unsigned long  *OUTPUT(unsigned long temp),
    229                      unsigned char  *OUTPUT(unsigned char temp),
    230 	             signed char    *OUTPUT(signed char temp),
    231                      bool           *OUTPUT(bool temp),
    232                      float          *OUTPUT(float temp),
    233                      double         *OUTPUT(double temp),
    234                      long long      *OUTPUT($*1_ltype temp),
    235                      unsigned long long *OUTPUT($*1_ltype temp),
    236 	             int            &OUTPUT(int temp),
    237                      short          &OUTPUT(short temp),
    238                      long           &OUTPUT(long temp),
    239                      unsigned int   &OUTPUT(unsigned int temp),
    240                      unsigned short &OUTPUT(unsigned short temp),
    241                      unsigned long  &OUTPUT(unsigned long temp),
    242                      signed char    &OUTPUT(signed char temp),
    243                      bool           &OUTPUT(bool temp),
    244                      unsigned char  &OUTPUT(unsigned char temp),
    245                      float          &OUTPUT(float temp),
    246                      double         &OUTPUT(double temp),
    247                      long long      &OUTPUT($*1_ltype temp),
    248                      unsigned long long &OUTPUT($*1_ltype temp)
    249 "$1 = &temp;";
    250 
    251 %typemap(argout)     int     *OUTPUT, int &OUTPUT,
    252                      short   *OUTPUT, short &OUTPUT,
    253                      long    *OUTPUT, long &OUTPUT,
    254                      unsigned int   *OUTPUT, unsigned int &OUTPUT,
    255                      unsigned short *OUTPUT, unsigned short &OUTPUT,
    256                      unsigned long  *OUTPUT, unsigned long &OUTPUT,
    257                      unsigned char  *OUTPUT, unsigned char &OUTPUT,
    258                      signed char    *OUTPUT, signed char  &OUTPUT,
    259                      bool           *OUTPUT, bool &OUTPUT
    260 {
    261   Tcl_Obj *o;
    262   o = Tcl_NewIntObj((int) *($1));
    263   Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),o);
    264 }
    265 
    266 %typemap(argout) float    *OUTPUT, float &OUTPUT,
    267                  double   *OUTPUT, double &OUTPUT
    268 {
    269   Tcl_Obj *o;
    270   o = Tcl_NewDoubleObj((double) *($1));
    271   Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),o);
    272 }
    273 
    274 %typemap(argout) long long *OUTPUT, long long &OUTPUT
    275 {
    276   char temp[256];
    277   Tcl_Obj *o;
    278   sprintf(temp,"%lld",(long long)*($1));
    279   o = Tcl_NewStringObj(temp,-1);
    280   Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),o);
    281 }
    282 
    283 %typemap(argout) unsigned long long *OUTPUT, unsigned long long &OUTPUT
    284 {
    285   char temp[256];
    286   Tcl_Obj *o;
    287   sprintf(temp,"%llu",(unsigned long long)*($1));
    288   o = Tcl_NewStringObj(temp,-1);
    289   Tcl_ListObjAppendElement(interp,Tcl_GetObjResult(interp),o);
    290 }
    291 
    292 // INOUT
    293 // Mappings for an argument that is both an input and output
    294 // parameter
    295 
    296 /*
    297 The following methods can be applied to make a function parameter both
    298 an input and output value.  This combines the behavior of both the
    299 "INPUT" and "OUTPUT" methods described earlier.  Output values are
    300 returned in the form of a Tcl list.
    301 
    302          int            *INOUT
    303          short          *INOUT
    304          long           *INOUT
    305          long long      *INOUT
    306          unsigned int   *INOUT
    307          unsigned short *INOUT
    308          unsigned long  *INOUT
    309          unsigned long long *INOUT
    310          unsigned char  *INOUT
    311          bool           *INOUT
    312          float          *INOUT
    313          double         *INOUT
    314 
    315 For example, suppose you were trying to wrap the following function :
    316 
    317         void neg(double *x) {
    318              *x = -(*x);
    319         }
    320 
    321 You could wrap it with SWIG as follows :
    322 
    323         %include typemaps.i
    324         void neg(double *INOUT);
    325 
    326 or you can use the %apply directive :
    327 
    328         %include typemaps.i
    329         %apply double *INOUT { double *x };
    330         void neg(double *x);
    331 
    332 Unlike C, this mapping does not directly modify the input value (since
    333 this makes no sense in Tcl).  Rather, the modified input value shows
    334 up as the return value of the function.  Thus, to apply this function
    335 to a Tcl variable you might do this :
    336 
    337        set x [neg $x]
    338 
    339 */
    340 
    341 
    342 %typemap(in) int *INOUT = int *INPUT;
    343 %typemap(in) short *INOUT = short *INPUT;
    344 %typemap(in) long *INOUT = long *INPUT;
    345 %typemap(in) unsigned int *INOUT = unsigned int *INPUT;
    346 %typemap(in) unsigned short *INOUT = unsigned short *INPUT;
    347 %typemap(in) unsigned long *INOUT = unsigned long *INPUT;
    348 %typemap(in) unsigned char *INOUT = unsigned char *INPUT;
    349 %typemap(in) signed char *INOUT = signed char *INPUT;
    350 %typemap(in) bool *INOUT = bool *INPUT;
    351 %typemap(in) float *INOUT = float *INPUT;
    352 %typemap(in) double *INOUT = double *INPUT;
    353 %typemap(in) long long *INOUT = long long *INPUT;
    354 %typemap(in) unsigned long long *INOUT = unsigned long long *INPUT;
    355 
    356 %typemap(in) int &INOUT = int &INPUT;
    357 %typemap(in) short &INOUT = short &INPUT;
    358 %typemap(in) long &INOUT = long &INPUT;
    359 %typemap(in) unsigned int &INOUT = unsigned int &INPUT;
    360 %typemap(in) unsigned short &INOUT = unsigned short &INPUT;
    361 %typemap(in) unsigned long &INOUT = unsigned long &INPUT;
    362 %typemap(in) unsigned char &INOUT = unsigned char &INPUT;
    363 %typemap(in) signed char &INOUT = signed char &INPUT;
    364 %typemap(in) bool &INOUT = bool &INPUT;
    365 %typemap(in) float &INOUT = float &INPUT;
    366 %typemap(in) double &INOUT = double &INPUT;
    367 %typemap(in) long long &INOUT = long long &INPUT;
    368 %typemap(in) unsigned long long &INOUT = unsigned long long &INPUT;
    369 
    370 %typemap(argout) int *INOUT = int *OUTPUT;
    371 %typemap(argout) short *INOUT = short *OUTPUT;
    372 %typemap(argout) long *INOUT = long *OUTPUT;
    373 %typemap(argout) unsigned int *INOUT = unsigned int *OUTPUT;
    374 %typemap(argout) unsigned short *INOUT = unsigned short *OUTPUT;
    375 %typemap(argout) unsigned long *INOUT = unsigned long *OUTPUT;
    376 %typemap(argout) unsigned char *INOUT = unsigned char *OUTPUT;
    377 %typemap(argout) signed char *INOUT = signed char *OUTPUT;
    378 %typemap(argout) bool *INOUT = bool *OUTPUT;
    379 %typemap(argout) float *INOUT = float *OUTPUT;
    380 %typemap(argout) double *INOUT = double *OUTPUT;
    381 %typemap(argout) long long *INOUT = long long *OUTPUT;
    382 %typemap(argout) unsigned long long *INOUT = unsigned long long *OUTPUT;
    383 
    384 %typemap(argout) int &INOUT = int &OUTPUT;
    385 %typemap(argout) short &INOUT = short &OUTPUT;
    386 %typemap(argout) long &INOUT = long &OUTPUT;
    387 %typemap(argout) unsigned int &INOUT = unsigned int &OUTPUT;
    388 %typemap(argout) unsigned short &INOUT = unsigned short &OUTPUT;
    389 %typemap(argout) unsigned long &INOUT = unsigned long &OUTPUT;
    390 %typemap(argout) unsigned char &INOUT = unsigned char &OUTPUT;
    391 %typemap(argout) signed char &INOUT = signed char &OUTPUT;
    392 %typemap(argout) bool &INOUT = bool &OUTPUT;
    393 %typemap(argout) float &INOUT = float &OUTPUT;
    394 %typemap(argout) double &INOUT = double &OUTPUT;
    395 %typemap(argout) long long &INOUT = long long &OUTPUT;
    396 %typemap(argout) unsigned long long &INOUT = unsigned long long &OUTPUT;
    397 
    398 
    399 /* Overloading information */
    400 
    401 %typemap(typecheck) double *INPUT = double;
    402 %typemap(typecheck) bool *INPUT = bool;
    403 %typemap(typecheck) signed char *INPUT = signed char;
    404 %typemap(typecheck) unsigned char *INPUT = unsigned char;
    405 %typemap(typecheck) unsigned long *INPUT = unsigned long;
    406 %typemap(typecheck) unsigned short *INPUT = unsigned short;
    407 %typemap(typecheck) unsigned int *INPUT = unsigned int;
    408 %typemap(typecheck) long *INPUT = long;
    409 %typemap(typecheck) short *INPUT = short;
    410 %typemap(typecheck) int *INPUT = int;
    411 %typemap(typecheck) float *INPUT = float;
    412 %typemap(typecheck) long long *INPUT = long long;
    413 %typemap(typecheck) unsigned long long *INPUT = unsigned long long;
    414 
    415 %typemap(typecheck) double &INPUT = double;
    416 %typemap(typecheck) bool &INPUT = bool;
    417 %typemap(typecheck) signed char &INPUT = signed char;
    418 %typemap(typecheck) unsigned char &INPUT = unsigned char;
    419 %typemap(typecheck) unsigned long &INPUT = unsigned long;
    420 %typemap(typecheck) unsigned short &INPUT = unsigned short;
    421 %typemap(typecheck) unsigned int &INPUT = unsigned int;
    422 %typemap(typecheck) long &INPUT = long;
    423 %typemap(typecheck) short &INPUT = short;
    424 %typemap(typecheck) int &INPUT = int;
    425 %typemap(typecheck) float &INPUT = float;
    426 %typemap(typecheck) long long &INPUT = long long;
    427 %typemap(typecheck) unsigned long long &INPUT = unsigned long long;
    428 
    429 %typemap(typecheck) double *INOUT = double;
    430 %typemap(typecheck) bool *INOUT = bool;
    431 %typemap(typecheck) signed char *INOUT = signed char;
    432 %typemap(typecheck) unsigned char *INOUT = unsigned char;
    433 %typemap(typecheck) unsigned long *INOUT = unsigned long;
    434 %typemap(typecheck) unsigned short *INOUT = unsigned short;
    435 %typemap(typecheck) unsigned int *INOUT = unsigned int;
    436 %typemap(typecheck) long *INOUT = long;
    437 %typemap(typecheck) short *INOUT = short;
    438 %typemap(typecheck) int *INOUT = int;
    439 %typemap(typecheck) float *INOUT = float;
    440 %typemap(typecheck) long long *INOUT = long long;
    441 %typemap(typecheck) unsigned long long *INOUT = unsigned long long;
    442 
    443 %typemap(typecheck) double &INOUT = double;
    444 %typemap(typecheck) bool &INOUT = bool;
    445 %typemap(typecheck) signed char &INOUT = signed char;
    446 %typemap(typecheck) unsigned char &INOUT = unsigned char;
    447 %typemap(typecheck) unsigned long &INOUT = unsigned long;
    448 %typemap(typecheck) unsigned short &INOUT = unsigned short;
    449 %typemap(typecheck) unsigned int &INOUT = unsigned int;
    450 %typemap(typecheck) long &INOUT = long;
    451 %typemap(typecheck) short &INOUT = short;
    452 %typemap(typecheck) int &INOUT = int;
    453 %typemap(typecheck) float &INOUT = float;
    454 %typemap(typecheck) long long &INOUT = long long;
    455 %typemap(typecheck) unsigned long long &INOUT = unsigned long long;
    456 
    457 #endif
    458 
    459 // --------------------------------------------------------------------
    460 // Special types
    461 // --------------------------------------------------------------------
    462 
    463 %include <tclinterp.i>
    464 %include <tclresult.i>
    465