Home | History | Annotate | Download | only in perl5
      1 /* -----------------------------------------------------------------------------
      2  * typemaps.i
      3  *
      4  * The SWIG typemap library provides a language independent mechanism for
      5  * supporting output arguments, input values, and other C function
      6  * calling mechanisms.  The primary use of the library is to provide a
      7  * better interface to certain C function--especially those involving
      8  * pointers.
      9  * ----------------------------------------------------------------------------- */
     10 
     11 #if !defined(SWIG_USE_OLD_TYPEMAPS)
     12 %include <typemaps/typemaps.swg>
     13 #else
     14 
     15 
     16 // INPUT typemaps.
     17 // These remap a C pointer to be an "INPUT" value which is passed by value
     18 // instead of reference.
     19 
     20 
     21 /*
     22 The following methods can be applied to turn a pointer into a simple
     23 "input" value.  That is, instead of passing a pointer to an object,
     24 you would use a real value instead.
     25 
     26          int            *INPUT
     27          short          *INPUT
     28          long           *INPUT
     29          long long      *INPUT
     30          unsigned int   *INPUT
     31          unsigned short *INPUT
     32          unsigned long  *INPUT
     33          unsigned long long *INPUT
     34          unsigned char  *INPUT
     35          bool           *INPUT
     36          float          *INPUT
     37          double         *INPUT
     38 
     39 To use these, suppose you had a C function like this :
     40 
     41         double fadd(double *a, double *b) {
     42                return *a+*b;
     43         }
     44 
     45 You could wrap it with SWIG as follows :
     46 
     47         %include typemaps.i
     48         double fadd(double *INPUT, double *INPUT);
     49 
     50 or you can use the %apply directive :
     51 
     52         %include typemaps.i
     53         %apply double *INPUT { double *a, double *b };
     54         double fadd(double *a, double *b);
     55 
     56 */
     57 
     58 %define INPUT_TYPEMAP(type, converter)
     59 %typemap(in) type *INPUT(type temp), type &INPUT(type temp) {
     60   temp = (type) converter($input);
     61   $1 = &temp;
     62 }
     63 %typemap(typecheck) type *INPUT = type;
     64 %typemap(typecheck) type &INPUT = type;
     65 %enddef
     66 
     67 INPUT_TYPEMAP(float, SvNV);
     68 INPUT_TYPEMAP(double, SvNV);
     69 INPUT_TYPEMAP(int, SvIV);
     70 INPUT_TYPEMAP(long, SvIV);
     71 INPUT_TYPEMAP(short, SvIV);
     72 INPUT_TYPEMAP(signed char, SvIV);
     73 INPUT_TYPEMAP(unsigned int, SvUV);
     74 INPUT_TYPEMAP(unsigned long, SvUV);
     75 INPUT_TYPEMAP(unsigned short, SvUV);
     76 INPUT_TYPEMAP(unsigned char, SvUV);
     77 
     78 %typemap(in) bool *INPUT(bool temp), bool &INPUT(bool temp) {
     79   temp = SvIV($input) ? true : false;
     80   $1 = &temp;
     81 }
     82 %typemap(typecheck) bool *INPUT = bool;
     83 %typemap(typecheck) bool &INPUT = bool;
     84 
     85 %typemap(in) long long *INPUT($*1_ltype temp), long long &INPUT($*1_ltype temp) {
     86   temp = strtoll(SvPV_nolen($input), 0, 0);
     87   $1 = &temp;
     88 }
     89 %typemap(typecheck) long long *INPUT = long long;
     90 %typemap(typecheck) long long &INPUT = long long;
     91 
     92 %typemap(in) unsigned long long *INPUT($*1_ltype temp), unsigned long long &INPUT($*1_ltype temp) {
     93   temp = strtoull(SvPV_nolen($input), 0, 0);
     94   $1 = &temp;
     95 }
     96 %typemap(typecheck) unsigned long long *INPUT = unsigned long long;
     97 %typemap(typecheck) unsigned long long &INPUT = unsigned long long;
     98 
     99 
    100 #undef INPUT_TYPEMAP
    101 
    102 // OUTPUT typemaps.   These typemaps are used for parameters that
    103 // are output only.   The output value is appended to the result as
    104 // a list element.
    105 
    106 /*
    107 The following methods can be applied to turn a pointer into an "output"
    108 value.  When calling a function, no input value would be given for
    109 a parameter, but an output value would be returned.  In the case of
    110 multiple output values, functions will return a Perl array.
    111 
    112          int            *OUTPUT
    113          short          *OUTPUT
    114          long           *OUTPUT
    115          long long      *OUTPUT
    116          unsigned int   *OUTPUT
    117          unsigned short *OUTPUT
    118          unsigned long  *OUTPUT
    119          unsigned long long *OUTPUT
    120          unsigned char  *OUTPUT
    121          bool           *OUTPUT
    122          float          *OUTPUT
    123          double         *OUTPUT
    124 
    125 For example, suppose you were trying to wrap the modf() function in the
    126 C math library which splits x into integral and fractional parts (and
    127 returns the integer part in one of its parameters).:
    128 
    129         double modf(double x, double *ip);
    130 
    131 You could wrap it with SWIG as follows :
    132 
    133         %include typemaps.i
    134         double modf(double x, double *OUTPUT);
    135 
    136 or you can use the %apply directive :
    137 
    138         %include typemaps.i
    139         %apply double *OUTPUT { double *ip };
    140         double modf(double x, double *ip);
    141 
    142 The Perl output of the function would be an array containing both
    143 output values.
    144 
    145 */
    146 
    147 // Force the argument to be ignored.
    148 
    149 %typemap(in,numinputs=0) int            *OUTPUT(int temp),  int &OUTPUT(int temp),
    150                  short          *OUTPUT(short temp), short &OUTPUT(short temp),
    151                  long           *OUTPUT(long temp), long &OUTPUT(long temp),
    152                  unsigned int   *OUTPUT(unsigned int temp), unsigned int &OUTPUT(unsigned int temp),
    153                  unsigned short *OUTPUT(unsigned short temp), unsigned short &OUTPUT(unsigned short temp),
    154                  unsigned long  *OUTPUT(unsigned long temp), unsigned long &OUTPUT(unsigned long temp),
    155                  unsigned char  *OUTPUT(unsigned char temp), unsigned char &OUTPUT(unsigned char temp),
    156                  signed char    *OUTPUT(signed char temp), signed char &OUTPUT(signed char temp),
    157                  bool           *OUTPUT(bool temp), bool &OUTPUT(bool temp),
    158                  float          *OUTPUT(float temp), float &OUTPUT(float temp),
    159                  double         *OUTPUT(double temp), double &OUTPUT(double temp),
    160                  long long      *OUTPUT($*1_ltype temp), long long &OUTPUT($*1_ltype temp),
    161                  unsigned long long *OUTPUT($*1_ltype temp), unsigned long long &OUTPUT($*1_ltype temp)
    162 "$1 = &temp;";
    163 
    164 %typemap(argout)  int            *OUTPUT, int &OUTPUT,
    165                   short          *OUTPUT, short &OUTPUT,
    166                   long           *OUTPUT, long &OUTPUT,
    167                   signed char    *OUTPUT, signed char &OUTPUT,
    168                   bool           *OUTPUT, bool &OUTPUT
    169 {
    170   if (argvi >= items) {
    171     EXTEND(sp,1);
    172   }
    173   $result = sv_newmortal();
    174   sv_setiv($result,(IV) *($1));
    175   argvi++;
    176 }
    177 
    178 %typemap(argout)  unsigned int   *OUTPUT, unsigned int &OUTPUT,
    179                   unsigned short *OUTPUT, unsigned short &OUTPUT,
    180                   unsigned long  *OUTPUT, unsigned long &OUTPUT,
    181                   unsigned char  *OUTPUT, unsigned char &OUTPUT
    182 {
    183   if (argvi >= items) {
    184     EXTEND(sp,1);
    185   }
    186   $result = sv_newmortal();
    187   sv_setuv($result,(UV) *($1));
    188   argvi++;
    189 }
    190 
    191 
    192 
    193 %typemap(argout) float    *OUTPUT, float &OUTPUT,
    194                  double   *OUTPUT, double &OUTPUT
    195 {
    196   if (argvi >= items) {
    197     EXTEND(sp,1);
    198   }
    199   $result = sv_newmortal();
    200   sv_setnv($result,(double) *($1));
    201   argvi++;
    202 }
    203 
    204 %typemap(argout) long long *OUTPUT, long long &OUTPUT {
    205     char temp[256];
    206     if (argvi >= items) {
    207 	EXTEND(sp,1);
    208     }
    209     sprintf(temp,"%lld", (long long)*($1));
    210     $result = sv_newmortal();
    211     sv_setpv($result,temp);
    212     argvi++;
    213 }
    214 
    215 %typemap(argout) unsigned long long *OUTPUT, unsigned long long &OUTPUT {
    216     char temp[256];
    217     if (argvi >= items) {
    218 	EXTEND(sp,1);
    219     }
    220     sprintf(temp,"%llu", (unsigned long long)*($1));
    221     $result = sv_newmortal();
    222     sv_setpv($result,temp);
    223     argvi++;
    224 }
    225 
    226 // INOUT
    227 // Mappings for an argument that is both an input and output
    228 // parameter
    229 
    230 /*
    231 The following methods can be applied to make a function parameter both
    232 an input and output value.  This combines the behavior of both the
    233 "INPUT" and "OUTPUT" methods described earlier.  Output values are
    234 returned in the form of a Perl array.
    235 
    236          int            *INOUT
    237          short          *INOUT
    238          long           *INOUT
    239          long long      *INOUT
    240          unsigned int   *INOUT
    241          unsigned short *INOUT
    242          unsigned long  *INOUT
    243          unsigned long long *INOUT
    244          unsigned char  *INOUT
    245          bool           *INOUT
    246          float          *INOUT
    247          double         *INOUT
    248 
    249 For example, suppose you were trying to wrap the following function :
    250 
    251         void neg(double *x) {
    252              *x = -(*x);
    253         }
    254 
    255 You could wrap it with SWIG as follows :
    256 
    257         %include typemaps.i
    258         void neg(double *INOUT);
    259 
    260 or you can use the %apply directive :
    261 
    262         %include typemaps.i
    263         %apply double *INOUT { double *x };
    264         void neg(double *x);
    265 
    266 Unlike C, this mapping does not directly modify the input value.
    267 Rather, the modified input value shows up as the return value of the
    268 function.  Thus, to apply this function to a Perl variable you might
    269 do this :
    270 
    271        $x = neg($x);
    272 
    273 */
    274 
    275 %typemap(in) int *INOUT = int *INPUT;
    276 %typemap(in) short *INOUT = short *INPUT;
    277 %typemap(in) long *INOUT = long *INPUT;
    278 %typemap(in) unsigned *INOUT = unsigned *INPUT;
    279 %typemap(in) unsigned short *INOUT = unsigned short *INPUT;
    280 %typemap(in) unsigned long *INOUT = unsigned long *INPUT;
    281 %typemap(in) unsigned char *INOUT = unsigned char *INPUT;
    282 %typemap(in) signed char *INOUT = signed char *INPUT;
    283 %typemap(in) bool *INOUT = bool *INPUT;
    284 %typemap(in) float *INOUT = float *INPUT;
    285 %typemap(in) double *INOUT = double *INPUT;
    286 %typemap(in) long long *INOUT = long long *INPUT;
    287 %typemap(in) unsigned long long *INOUT = unsigned long long *INPUT;
    288 
    289 %typemap(in) int &INOUT = int &INPUT;
    290 %typemap(in) short &INOUT = short &INPUT;
    291 %typemap(in) long &INOUT = long &INPUT;
    292 %typemap(in) unsigned &INOUT = unsigned &INPUT;
    293 %typemap(in) unsigned short &INOUT = unsigned short &INPUT;
    294 %typemap(in) unsigned long &INOUT = unsigned long &INPUT;
    295 %typemap(in) unsigned char &INOUT = unsigned char &INPUT;
    296 %typemap(in) signed char &INOUT = signed char &INPUT;
    297 %typemap(in) bool &INOUT = bool &INPUT;
    298 %typemap(in) float &INOUT = float &INPUT;
    299 %typemap(in) double &INOUT = double &INPUT;
    300 %typemap(in) long long &INOUT = long long &INPUT;
    301 %typemap(in) unsigned long long &INOUT = unsigned long long &INPUT;
    302 
    303 
    304 %typemap(argout) int *INOUT = int *OUTPUT;
    305 %typemap(argout) short *INOUT = short *OUTPUT;
    306 %typemap(argout) long *INOUT = long *OUTPUT;
    307 %typemap(argout) unsigned *INOUT = unsigned *OUTPUT;
    308 %typemap(argout) unsigned short *INOUT = unsigned short *OUTPUT;
    309 %typemap(argout) unsigned long *INOUT = unsigned long *OUTPUT;
    310 %typemap(argout) unsigned char *INOUT = unsigned char *OUTPUT;
    311 %typemap(argout) signed char *INOUT = signed char *OUTPUT;
    312 %typemap(argout) bool *INOUT = bool *OUTPUT;
    313 %typemap(argout) float *INOUT = float *OUTPUT;
    314 %typemap(argout) double *INOUT = double *OUTPUT;
    315 %typemap(argout) long long *INOUT = long long *OUTPUT;
    316 %typemap(argout) unsigned long long *INOUT = unsigned long long *OUTPUT;
    317 
    318 
    319 %typemap(argout) int &INOUT = int &OUTPUT;
    320 %typemap(argout) short &INOUT = short &OUTPUT;
    321 %typemap(argout) long &INOUT = long &OUTPUT;
    322 %typemap(argout) unsigned &INOUT = unsigned &OUTPUT;
    323 %typemap(argout) unsigned short &INOUT = unsigned short &OUTPUT;
    324 %typemap(argout) unsigned long &INOUT = unsigned long &OUTPUT;
    325 %typemap(argout) unsigned char &INOUT = unsigned char &OUTPUT;
    326 %typemap(argout) signed char &INOUT = signed char &OUTPUT;
    327 %typemap(argout) bool &INOUT = bool &OUTPUT;
    328 %typemap(argout) float &INOUT = float &OUTPUT;
    329 %typemap(argout) double &INOUT = double &OUTPUT;
    330 %typemap(argout) long long &INOUT = long long &OUTPUT;
    331 %typemap(argout) unsigned long long &INOUT = unsigned long long &OUTPUT;
    332 
    333 
    334 /* Overloading information */
    335 
    336 %typemap(typecheck) double *INOUT = double;
    337 %typemap(typecheck) bool *INOUT = bool;
    338 %typemap(typecheck) signed char *INOUT = signed char;
    339 %typemap(typecheck) unsigned char *INOUT = unsigned char;
    340 %typemap(typecheck) unsigned long *INOUT = unsigned long;
    341 %typemap(typecheck) unsigned short *INOUT = unsigned short;
    342 %typemap(typecheck) unsigned int *INOUT = unsigned int;
    343 %typemap(typecheck) long *INOUT = long;
    344 %typemap(typecheck) short *INOUT = short;
    345 %typemap(typecheck) int *INOUT = int;
    346 %typemap(typecheck) float *INOUT = float;
    347 %typemap(typecheck) long long *INOUT = long long;
    348 %typemap(typecheck) unsigned long long *INOUT = unsigned long long;
    349 
    350 %typemap(typecheck) double &INOUT = double;
    351 %typemap(typecheck) bool &INOUT = bool;
    352 %typemap(typecheck) signed char &INOUT = signed char;
    353 %typemap(typecheck) unsigned char &INOUT = unsigned char;
    354 %typemap(typecheck) unsigned long &INOUT = unsigned long;
    355 %typemap(typecheck) unsigned short &INOUT = unsigned short;
    356 %typemap(typecheck) unsigned int &INOUT = unsigned int;
    357 %typemap(typecheck) long &INOUT = long;
    358 %typemap(typecheck) short &INOUT = short;
    359 %typemap(typecheck) int &INOUT = int;
    360 %typemap(typecheck) float &INOUT = float;
    361 %typemap(typecheck) long long &INOUT = long long;
    362 %typemap(typecheck) unsigned long long &INOUT = unsigned long long;
    363 
    364 #endif
    365 
    366 // --------------------------------------------------------------------
    367 // Special types
    368 // --------------------------------------------------------------------
    369 
    370 
    371 %include <reference.i>
    372