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