Home | History | Annotate | Download | only in guile
      1 /* -----------------------------------------------------------------------------
      2  * typemaps.i
      3  *
      4  * Guile-specific typemaps
      5  * ----------------------------------------------------------------------------- */
      6 
      7 /* Pointers */
      8 
      9 %typemap(in) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] {
     10   $1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, 0);
     11 }
     12 %typemap(freearg) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] "";
     13 
     14 %typemap(in) void * {
     15   $1 = ($1_ltype)SWIG_MustGetPtr($input, NULL, $argnum, 0);
     16 }
     17 %typemap(freearg) void * "";
     18 
     19 %typemap(varin) SWIGTYPE * {
     20   $1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, 1, 0);
     21 }
     22 
     23 %typemap(varin) SWIGTYPE & {
     24   $1 = *(($1_ltype)SWIG_MustGetPtr($input, $descriptor, 1, 0));
     25 }
     26 
     27 %typemap(varin) SWIGTYPE [] {
     28   scm_wrong_type_arg((char *) FUNC_NAME, 1, $input);
     29 }
     30 
     31 %typemap(varin) SWIGTYPE [ANY] {
     32   void *temp;
     33   int ii;
     34   $1_basetype *b = 0;
     35   temp = SWIG_MustGetPtr($input, $1_descriptor, 1, 0);
     36   b = ($1_basetype *) $1;
     37   for (ii = 0; ii < $1_size; ii++) b[ii] = *(($1_basetype *) temp + ii);
     38 }
     39 
     40 %typemap(varin) void * {
     41   $1 = SWIG_MustGetPtr($input, NULL, 1, 0);
     42 }
     43 
     44 %typemap(out) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] {
     45   $result = SWIG_NewPointerObj ($1, $descriptor, $owner);
     46 }
     47 
     48 %typemap(out) SWIGTYPE *DYNAMIC, SWIGTYPE &DYNAMIC {
     49   swig_type_info *ty = SWIG_TypeDynamicCast($1_descriptor,(void **) &$1);
     50   $result = SWIG_NewPointerObj ($1, ty, $owner);
     51 }
     52 
     53 %typemap(varout) SWIGTYPE *, SWIGTYPE [] {
     54   $result = SWIG_NewPointerObj ($1, $descriptor, 0);
     55 }
     56 
     57 %typemap(varout) SWIGTYPE & {
     58   $result = SWIG_NewPointerObj((void *) &$1, $1_descriptor, 0);
     59 }
     60 
     61 %typemap(throws) SWIGTYPE {
     62   $&ltype temp = new $ltype($1);
     63   scm_throw(scm_from_locale_symbol((char *) "swig-exception"),
     64 	    scm_listify(SWIG_NewPointerObj(temp, $&descriptor, 1),
     65 		    SCM_UNDEFINED));
     66 }
     67 
     68 %typemap(throws) SWIGTYPE & {
     69   scm_throw(scm_from_locale_symbol((char *) "swig-exception"),
     70 	    scm_listify(SWIG_NewPointerObj(&$1, $descriptor, 1),
     71 		    SCM_UNDEFINED));
     72 }
     73 
     74 %typemap(throws) SWIGTYPE * {
     75   scm_throw(scm_from_locale_symbol((char *) "swig-exception"),
     76 	    scm_listify(SWIG_NewPointerObj($1, $descriptor, 1),
     77 		    SCM_UNDEFINED));
     78 }
     79 
     80 %typemap(throws) SWIGTYPE [] {
     81   scm_throw(scm_from_locale_symbol((char *) "swig-exception"),
     82 	    scm_listify(SWIG_NewPointerObj($1, $descriptor, 1),
     83 		    SCM_UNDEFINED));
     84 }
     85 
     86 /* Change of object ownership, and interaction of destructor-like functions and the
     87    garbage-collector */
     88 
     89 %typemap(in, doc="$NAME is of type <$type> and gets destroyed by the function") SWIGTYPE *DESTROYED {
     90   $1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, 0);
     91 }
     92 
     93 %typemap(freearg) SWIGTYPE *DESTROYED {
     94   SWIG_Guile_MarkPointerDestroyed($input);
     95 }
     96 
     97 %typemap(in, doc="$NAME is of type <$type> and is consumed by the function") SWIGTYPE *CONSUMED {
     98   $1 = ($1_ltype)SWIG_MustGetPtr($input, $descriptor, $argnum, 0);
     99   SWIG_Guile_MarkPointerNoncollectable($input);
    100 }
    101 
    102 /* Pass-by-value */
    103 
    104 %typemap(in) SWIGTYPE($&1_ltype argp) {
    105   argp = ($&1_ltype)SWIG_MustGetPtr($input, $&1_descriptor, $argnum, 0);
    106   $1 = *argp;
    107 }
    108 
    109 %typemap(varin) SWIGTYPE {
    110   $&1_ltype argp;
    111   argp = ($&1_ltype)SWIG_MustGetPtr($input, $&1_descriptor, 1, 0);
    112   $1 = *argp;
    113 }
    114 
    115 %typemap(out) SWIGTYPE
    116 #ifdef __cplusplus
    117 {
    118   $&1_ltype resultptr;
    119   resultptr = new $1_ltype((const $1_ltype &) $1);
    120   $result =  SWIG_NewPointerObj (resultptr, $&1_descriptor, 1);
    121 }
    122 #else
    123 {
    124   $&1_ltype resultptr;
    125   resultptr = ($&1_ltype) malloc(sizeof($1_type));
    126   memmove(resultptr, &$1, sizeof($1_type));
    127   $result = SWIG_NewPointerObj(resultptr, $&1_descriptor, 1);
    128 }
    129 #endif
    130 
    131 %typemap(varout) SWIGTYPE
    132 #ifdef __cplusplus
    133 {
    134   $&1_ltype resultptr;
    135   resultptr = new $1_ltype((const $1_ltype&) $1);
    136   $result =  SWIG_NewPointerObj (resultptr, $&1_descriptor, 0);
    137 }
    138 #else
    139 {
    140   $&1_ltype resultptr;
    141   resultptr = ($&1_ltype) malloc(sizeof($1_type));
    142   memmove(resultptr, &$1, sizeof($1_type));
    143   $result = SWIG_NewPointerObj(resultptr, $&1_descriptor, 0);
    144 }
    145 #endif
    146 
    147 /* Enums */
    148 
    149 %typemap(in)     enum SWIGTYPE  { $1 = ($1_type) scm_to_int($input); }
    150 /* The complicated construction below needed to deal with anonymous
    151    enums, which cannot be cast to. */
    152 %typemap(varin)  enum SWIGTYPE  {
    153   if (sizeof(int) != sizeof($1)) {
    154     scm_error(scm_from_locale_symbol("swig-error"),
    155 	      (char *) FUNC_NAME,
    156 	      (char *) "enum variable '$name' cannot be set",
    157 	      SCM_EOL, SCM_BOOL_F);
    158   }
    159   * (int *) &($1) = scm_to_int($input);
    160 }
    161 %typemap(out)    enum SWIGTYPE  { $result = scm_from_long($1); }
    162 %typemap(varout) enum SWIGTYPE  { $result = scm_from_long($1); }
    163 %typemap(throws) enum SWIGTYPE {
    164   scm_throw(scm_from_locale_symbol((char *) "swig-exception"),
    165      scm_listify(scm_from_long($1), SCM_UNDEFINED));
    166 }
    167 
    168 /* The SIMPLE_MAP_WITH_EXPR macro below defines the whole set of
    169    typemaps needed for simple types.
    170    -- SCM_TO_C_EXPR is a C expression that translates the Scheme value
    171       "swig_scm_value" to a C value.
    172    -- C_TO_SCM_EXPR is a C expression that translates the C value
    173       "swig_c_value" to a Scheme value. */
    174 
    175 %define SIMPLE_MAP_WITH_EXPR(C_NAME, SCM_TO_C_EXPR, C_TO_SCM_EXPR, SCM_NAME)
    176  %typemap (in,     doc="$NAME is of type <" #SCM_NAME ">") C_NAME
    177      { SCM swig_scm_value = $input;
    178        $1 = SCM_TO_C_EXPR; }
    179  %typemap (varin,  doc="NEW-VALUE is of type <" #SCM_NAME ">") C_NAME
    180      { SCM swig_scm_value = $input;
    181        $1 = SCM_TO_C_EXPR; }
    182  %typemap (out,    doc="<" #SCM_NAME ">") C_NAME
    183      { C_NAME swig_c_value = $1;
    184        $result = C_TO_SCM_EXPR; }
    185  %typemap (varout, doc="<" #SCM_NAME ">") C_NAME
    186      { C_NAME swig_c_value = $1;
    187        $result = C_TO_SCM_EXPR; }
    188  /* INPUT and OUTPUT */
    189  %typemap (in, doc="$NAME is of type <" #SCM_NAME ">)")
    190      C_NAME *INPUT(C_NAME temp) {
    191        SCM swig_scm_value = $input;
    192        temp = (C_NAME) SCM_TO_C_EXPR; $1 = &temp; }
    193  %typemap (in,numinputs=0)      C_NAME *OUTPUT (C_NAME temp)
    194      {$1 = &temp;}
    195  %typemap (argout,doc="$name (of type <" #SCM_NAME ">)") C_NAME *OUTPUT
    196      { C_NAME swig_c_value = *$1;
    197        SWIG_APPEND_VALUE(C_TO_SCM_EXPR); }
    198  %typemap (in)          C_NAME *BOTH = C_NAME *INPUT;
    199  %typemap (argout)      C_NAME *BOTH = C_NAME *OUTPUT;
    200  %typemap (in)          C_NAME *INOUT = C_NAME *INPUT;
    201  %typemap (argout)      C_NAME *INOUT = C_NAME *OUTPUT;
    202  /* Const primitive references.  Passed by value */
    203  %typemap(in, doc="$NAME is of type <" #SCM_NAME ">") const C_NAME & (C_NAME temp)
    204      { SCM swig_scm_value = $input;
    205        temp = SCM_TO_C_EXPR;
    206        $1 = &temp; }
    207  %typemap(out, doc="<" #SCM_NAME ">")  const C_NAME &
    208      { C_NAME swig_c_value = *$1;
    209        $result = C_TO_SCM_EXPR; }
    210  /* Throw typemap */
    211  %typemap(throws) C_NAME {
    212    C_NAME swig_c_value = $1;
    213    scm_throw(scm_from_locale_symbol((char *) "swig-exception"),
    214 	     scm_listify(C_TO_SCM_EXPR, SCM_UNDEFINED));
    215  }
    216 %enddef
    217 
    218 /* The SIMPLE_MAP macro below defines the whole set of typemaps needed
    219    for simple types.  It generates slightly simpler code than the
    220    macro above, but it is only suitable for very simple conversion
    221    expressions. */
    222 
    223 %define SIMPLE_MAP(C_NAME, SCM_TO_C, C_TO_SCM, SCM_NAME)
    224  %typemap (in,     doc="$NAME is of type <" #SCM_NAME ">")
    225      C_NAME {$1 = ($1_ltype) SCM_TO_C($input);}
    226  %typemap (varin,  doc="NEW-VALUE is of type <" #SCM_NAME ">")
    227      C_NAME {$1 = ($1_ltype) SCM_TO_C($input);}
    228  %typemap (out,    doc="<" #SCM_NAME ">")
    229      C_NAME {$result = C_TO_SCM($1);}
    230  %typemap (varout, doc="<" #SCM_NAME ">")
    231      C_NAME {$result = C_TO_SCM($1);}
    232  /* INPUT and OUTPUT */
    233  %typemap (in, doc="$NAME is of type <" #SCM_NAME ">)")
    234      C_NAME *INPUT(C_NAME temp), C_NAME &INPUT(C_NAME temp) {
    235    temp = (C_NAME) SCM_TO_C($input); $1 = &temp;
    236  }
    237  %typemap (in,numinputs=0)      C_NAME *OUTPUT (C_NAME temp), C_NAME &OUTPUT(C_NAME temp)
    238    {$1 = &temp;}
    239  %typemap (argout,doc="$name (of type <" #SCM_NAME ">)") C_NAME *OUTPUT, C_NAME &OUTPUT
    240    {SWIG_APPEND_VALUE(C_TO_SCM(*$1));}
    241  %typemap (in)          C_NAME *BOTH = C_NAME *INPUT;
    242  %typemap (argout)      C_NAME *BOTH = C_NAME *OUTPUT;
    243  %typemap (in)          C_NAME *INOUT = C_NAME *INPUT;
    244  %typemap (argout)      C_NAME *INOUT = C_NAME *OUTPUT;
    245  %typemap (in)          C_NAME &INOUT = C_NAME &INPUT;
    246  %typemap (argout)      C_NAME &INOUT = C_NAME &OUTPUT;
    247  /* Const primitive references.  Passed by value */
    248  %typemap(in, doc="$NAME is of type <" #SCM_NAME ">") const C_NAME & (C_NAME temp) {
    249    temp = SCM_TO_C($input);
    250    $1 = ($1_ltype) &temp;
    251  }
    252  %typemap(out, doc="<" #SCM_NAME ">")  const C_NAME & {
    253    $result = C_TO_SCM(*$1);
    254  }
    255  /* Throw typemap */
    256  %typemap(throws) C_NAME {
    257    scm_throw(scm_from_locale_symbol((char *) "swig-exception"),
    258 	     scm_listify(C_TO_SCM($1), SCM_UNDEFINED));
    259  }
    260 %enddef
    261 
    262  SIMPLE_MAP(bool, scm_is_true, scm_from_bool, boolean);
    263  SIMPLE_MAP(char, SCM_CHAR, SCM_MAKE_CHAR, char);
    264  SIMPLE_MAP(unsigned char, SCM_CHAR, SCM_MAKE_CHAR, char);
    265  SIMPLE_MAP(signed char, SCM_CHAR, SCM_MAKE_CHAR, char);
    266  SIMPLE_MAP(int, scm_to_int, scm_from_long, integer);
    267  SIMPLE_MAP(short, scm_to_short, scm_from_long, integer);
    268  SIMPLE_MAP(long, scm_to_long, scm_from_long, integer);
    269  SIMPLE_MAP(ptrdiff_t, scm_to_long, scm_from_long, integer);
    270  SIMPLE_MAP(unsigned int, scm_to_uint, scm_from_ulong, integer);
    271  SIMPLE_MAP(unsigned short, scm_to_ushort, scm_from_ulong, integer);
    272  SIMPLE_MAP(unsigned long, scm_to_ulong, scm_from_ulong, integer);
    273  SIMPLE_MAP(size_t, scm_to_ulong, scm_from_ulong, integer);
    274  SIMPLE_MAP(float, scm_to_double, scm_from_double, real);
    275  SIMPLE_MAP(double, scm_to_double, scm_from_double, real);
    276 // SIMPLE_MAP(char *, SWIG_scm2str, SWIG_str02scm, string);
    277 // SIMPLE_MAP(const char *, SWIG_scm2str, SWIG_str02scm, string);
    278 
    279 /* Define long long typemaps -- uses functions that are only defined
    280    in recent versions of Guile, availability also depends on Guile's
    281    configuration. */
    282 
    283 SIMPLE_MAP(long long, scm_to_long_long, scm_from_long_long, integer);
    284 SIMPLE_MAP(unsigned long long, scm_to_ulong_long, scm_from_ulong_long, integer);
    285 
    286 /* Strings */
    287 
    288  %typemap (in,     doc="$NAME is a string")      char *(int must_free = 0) {
    289   $1 = ($1_ltype)SWIG_scm2str($input);
    290   must_free = 1;
    291  }
    292  %typemap (varin,  doc="NEW-VALUE is a string")  char * {$1 = ($1_ltype)SWIG_scm2str($input);}
    293  %typemap (out,    doc="<string>")              char * {$result = SWIG_str02scm((const char *)$1);}
    294  %typemap (varout, doc="<string>")              char * {$result = SWIG_str02scm($1);}
    295  %typemap (in, doc="$NAME is a string")          char **INPUT(char * temp, int must_free = 0) {
    296    temp = (char *) SWIG_scm2str($input); $1 = &temp;
    297    must_free = 1;
    298  }
    299  %typemap (in,numinputs=0)  char **OUTPUT (char * temp)
    300    {$1 = &temp;}
    301  %typemap (argout,doc="$NAME (a string)") char **OUTPUT
    302    {SWIG_APPEND_VALUE(SWIG_str02scm(*$1));}
    303  %typemap (in)          char **BOTH = char **INPUT;
    304  %typemap (argout)      char **BOTH = char **OUTPUT;
    305  %typemap (in)          char **INOUT = char **INPUT;
    306  %typemap (argout)      char **INOUT = char **OUTPUT;
    307 
    308 /* SWIG_scm2str makes a malloc'ed copy of the string, so get rid of it after
    309    the function call. */
    310 
    311 %typemap (freearg) char * "if (must_free$argnum && $1) SWIG_free($1);";
    312 %typemap (freearg) char **INPUT, char **BOTH "if (must_free$argnum && (*$1)) SWIG_free(*$1);"
    313 %typemap (freearg) char **OUTPUT "SWIG_free(*$1);"
    314 
    315 /* But this shall not apply if we try to pass a single char by
    316    reference. */
    317 
    318 %typemap (freearg) char *OUTPUT, char *BOTH "";
    319 
    320 /* If we set a string variable, delete the old result first, unless const. */
    321 
    322 %typemap (varin) char * {
    323     if ($1) free($1);
    324     $1 = ($1_ltype) SWIG_scm2str($input);
    325 }
    326 
    327 %typemap (varin) const char * {
    328     $1 = ($1_ltype) SWIG_scm2str($input);
    329 }
    330 
    331 %typemap(throws) char * {
    332   scm_throw(scm_from_locale_symbol((char *) "swig-exception"),
    333 	    scm_listify(SWIG_str02scm($1), SCM_UNDEFINED));
    334 }
    335 
    336 /* Void */
    337 
    338 %typemap (out,doc="") void "gswig_result = SCM_UNSPECIFIED;";
    339 
    340 /* SCM is passed through */
    341 
    342 typedef unsigned long SCM;
    343 %typemap (in) SCM "$1=$input;";
    344 %typemap (out) SCM "$result=$1;";
    345 %typecheck(SWIG_TYPECHECK_POINTER) SCM "$1=1;";
    346 
    347 /* ------------------------------------------------------------
    348  * String & length
    349  * ------------------------------------------------------------ */
    350 
    351 %typemap(in) (char *STRING, int LENGTH), (char *STRING, size_t LENGTH) {
    352     size_t temp;
    353     $1 = ($1_ltype) SWIG_Guile_scm2newstr($input, &temp);
    354     $2 = ($2_ltype) temp;
    355 }
    356 
    357 /* ------------------------------------------------------------
    358  * CLASS::* (member function pointer) typemaps
    359  * taken from typemaps/swigtype.swg
    360  * ------------------------------------------------------------ */
    361 
    362 #define %set_output(obj)                  $result = obj
    363 #define %set_varoutput(obj)               $result = obj
    364 #define %argument_fail(code, type, name, argn)	scm_wrong_type_arg((char *) FUNC_NAME, argn, $input);
    365 #define %as_voidptr(ptr)		(void*)(ptr)
    366 
    367 %typemap(in) SWIGTYPE (CLASS::*) {
    368   int res = SWIG_ConvertMember($input, %as_voidptr(&$1), sizeof($type),$descriptor);
    369   if (!SWIG_IsOK(res)) {
    370     %argument_fail(res,"$type",$symname, $argnum);
    371   }
    372 }
    373 
    374 %typemap(out,noblock=1) SWIGTYPE (CLASS::*) {
    375   %set_output(SWIG_NewMemberObj(%as_voidptr(&$1), sizeof($type), $descriptor));
    376 }
    377 
    378 %typemap(varin) SWIGTYPE (CLASS::*) {
    379   int res = SWIG_ConvertMember($input,%as_voidptr(&$1), sizeof($type), $descriptor);
    380   if (!SWIG_IsOK(res)) {
    381     scm_wrong_type_arg((char *) FUNC_NAME, 1, $input);
    382   }
    383 }
    384 
    385 %typemap(varout,noblock=1) SWIGTYPE (CLASS::*) {
    386   %set_varoutput(SWIG_NewMemberObj(%as_voidptr(&$1), sizeof($type), $descriptor));
    387 }
    388 
    389 /* ------------------------------------------------------------
    390  * Typechecking rules
    391  * ------------------------------------------------------------ */
    392 
    393 /* adapted from python.swg */
    394 
    395 %typecheck(SWIG_TYPECHECK_INTEGER)
    396 	 int, short, long,
    397  	 unsigned int, unsigned short, unsigned long,
    398 	 signed char, unsigned char,
    399 	 long long, unsigned long long,
    400          size_t, ptrdiff_t,
    401          std::size_t, std::ptrdiff_t,
    402 	 const int &, const short &, const long &,
    403  	 const unsigned int &, const unsigned short &, const unsigned long &,
    404 	 const long long &, const unsigned long long &,
    405          const size_t &, const ptrdiff_t &,
    406          const std::size_t &, const std::ptrdiff_t &,
    407 	 enum SWIGTYPE
    408 {
    409   $1 = scm_is_true(scm_integer_p($input)) && scm_is_true(scm_exact_p($input))? 1 : 0;
    410 }
    411 
    412 %typecheck(SWIG_TYPECHECK_BOOL)
    413 	bool, bool&, const bool&
    414 {
    415   $1 = SCM_BOOLP($input) ? 1 : 0;
    416 }
    417 
    418 %typecheck(SWIG_TYPECHECK_DOUBLE)
    419 	float, double,
    420 	const float &, const double &
    421 {
    422   $1 = scm_is_true(scm_real_p($input)) ? 1 : 0;
    423 }
    424 
    425 %typecheck(SWIG_TYPECHECK_CHAR) char {
    426   $1 = SCM_CHARP($input) ? 1 : 0;
    427 }
    428 
    429 %typecheck(SWIG_TYPECHECK_STRING) char * {
    430   $1 = scm_is_string($input) ? 1 : 0;
    431 }
    432 
    433 %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [] {
    434   void *ptr;
    435   int res = SWIG_ConvertPtr($input, &ptr, $1_descriptor, 0);
    436   $1 = SWIG_CheckState(res);
    437 }
    438 
    439 %typecheck(SWIG_TYPECHECK_POINTER) SWIGTYPE {
    440   void *ptr;
    441   int res = SWIG_ConvertPtr($input, &ptr, $&descriptor, 0);
    442   $1 = SWIG_CheckState(res);
    443 }
    444 
    445 %typecheck(SWIG_TYPECHECK_VOIDPTR) void * {
    446   void *ptr;
    447   int res = SWIG_ConvertPtr($input, &ptr, 0, 0);
    448   $1 = SWIG_CheckState(res);
    449 }
    450 
    451 /* Array reference typemaps */
    452 %apply SWIGTYPE & { SWIGTYPE ((&)[ANY]) }
    453 
    454 /* const pointers */
    455 %apply SWIGTYPE * { SWIGTYPE *const }
    456 
    457 /* typemaps.i ends here */
    458