PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/chicken/typemaps.i

#
Swig | 315 lines | 149 code | 26 blank | 140 comment | 0 complexity | b4608607e4ebf8d7cb6ff95c83454c96 MD5 | raw file
  1/* -----------------------------------------------------------------------------
  2 * typemaps.i
  3 *
  4 * Pointer handling
  5 *
  6 * These mappings provide support for input/output arguments and
  7 * common uses for C/C++ pointers.  INOUT mappings allow for C/C++
  8 * pointer variables in addition to input/output arguments.
  9 * ----------------------------------------------------------------------------- */
 10
 11// INPUT typemaps.
 12// These remap a C pointer to be an "INPUT" value which is passed by value
 13// instead of reference.
 14
 15/* 
 16The following methods can be applied to turn a pointer into a simple
 17"input" value.  That is, instead of passing a pointer to an object,
 18you would use a real value instead.
 19
 20         int            *INPUT
 21         short          *INPUT
 22         long           *INPUT
 23	 long long      *INPUT
 24         unsigned int   *INPUT
 25         unsigned short *INPUT
 26         unsigned long  *INPUT
 27         unsigned long long *INPUT
 28         unsigned char  *INPUT
 29         char           *INPUT
 30         bool           *INPUT
 31         float          *INPUT
 32         double         *INPUT
 33         
 34To use these, suppose you had a C function like this :
 35
 36        double fadd(double *a, double *b) {
 37               return *a+*b;
 38        }
 39
 40You could wrap it with SWIG as follows :
 41        
 42        %include <typemaps.i>
 43        double fadd(double *INPUT, double *INPUT);
 44
 45or you can use the %apply directive :
 46
 47        %include <typemaps.i>
 48        %apply double *INPUT { double *a, double *b };
 49        double fadd(double *a, double *b);
 50
 51*/
 52
 53// OUTPUT typemaps.   These typemaps are used for parameters that
 54// are output only.   The output value is appended to the result as
 55// a list element.
 56
 57/* 
 58The following methods can be applied to turn a pointer into an "output"
 59value.  When calling a function, no input value would be given for
 60a parameter, but an output value would be returned.  In the case of
 61multiple output values, they are returned in the form of a Scheme list.
 62
 63         int            *OUTPUT
 64         short          *OUTPUT
 65         long           *OUTPUT
 66         long long      *OUTPUT
 67         unsigned int   *OUTPUT
 68         unsigned short *OUTPUT
 69         unsigned long  *OUTPUT
 70         unsigned long long *OUTPUT
 71         unsigned char  *OUTPUT
 72         char           *OUTPUT
 73         bool           *OUTPUT
 74         float          *OUTPUT
 75         double         *OUTPUT
 76         
 77For example, suppose you were trying to wrap the modf() function in the
 78C math library which splits x into integral and fractional parts (and
 79returns the integer part in one of its parameters).K:
 80
 81        double modf(double x, double *ip);
 82
 83You could wrap it with SWIG as follows :
 84
 85        %include <typemaps.i>
 86        double modf(double x, double *OUTPUT);
 87
 88or you can use the %apply directive :
 89
 90        %include <typemaps.i>
 91        %apply double *OUTPUT { double *ip };
 92        double modf(double x, double *ip);
 93
 94*/
 95
 96// These typemaps contributed by Robin Dunn
 97//----------------------------------------------------------------------
 98//
 99// T_OUTPUT typemap (and helper function) to return multiple argouts as
100// a tuple instead of a list.
101//
102//----------------------------------------------------------------------
103
104// Simple types
105
106%define INOUT_TYPEMAP(type_, from_scheme, to_scheme, checker, convtype, storage_)
107
108%typemap(in) type_ *INPUT($*1_ltype temp), type_ &INPUT($*1_ltype temp)
109%{  if (!checker ($input)) {
110    swig_barf (SWIG_BARF1_BAD_ARGUMENT_TYPE, "Argument #$argnum is not of type 'type_'");
111  }
112  temp = ($*1_ltype) from_scheme ($input);
113  $1 = &temp; %}
114
115%typemap(typecheck) type_ *INPUT = type_;
116%typemap(typecheck) type_ &INPUT = type_;
117
118%typemap(in, numinputs=0) type_ *OUTPUT($*1_ltype temp), type_ &OUTPUT($*1_ltype temp)
119"  $1 = &temp;"
120
121#if "storage_" == "0"
122
123%typemap(argout) type_ *OUTPUT, type_ &OUTPUT 
124%{ 
125  if ($1 == NULL) {
126    swig_barf (SWIG_BARF1_ARGUMENT_NULL, "Argument #$argnum must be non-null");
127  }
128  SWIG_APPEND_VALUE(to_scheme (convtype (*$1)));
129%}
130
131#else
132
133%typemap(argout) type_ *OUTPUT, type_ &OUTPUT 
134%{
135  {
136    C_word *known_space = C_alloc(storage_);
137    if ($1 == NULL) {
138      swig_barf (SWIG_BARF1_ARGUMENT_NULL, "Variable '$1' must be non-null");
139    }
140    SWIG_APPEND_VALUE(to_scheme (&known_space, convtype (*$1)));
141  }
142%}
143
144#endif
145
146%enddef
147
148INOUT_TYPEMAP(int, C_num_to_int, C_fix, C_swig_is_number, (int), 0);
149INOUT_TYPEMAP(enum SWIGTYPE, C_num_to_int, C_fix, C_swig_is_number, (int), 0);
150INOUT_TYPEMAP(short, C_num_to_int, C_fix, C_swig_is_number, (int), 0);
151INOUT_TYPEMAP(long, C_num_to_long, C_long_to_num, C_swig_is_long, (long), C_SIZEOF_FLONUM);
152INOUT_TYPEMAP(long long, C_num_to_long, C_long_to_num, C_swig_is_long, (long), C_SIZEOF_FLONUM);
153INOUT_TYPEMAP(unsigned int, C_num_to_unsigned_int, C_unsigned_int_to_num, C_swig_is_number, (int), C_SIZEOF_FLONUM);
154INOUT_TYPEMAP(unsigned short, C_num_to_unsigned_int, C_fix, C_swig_is_number, (unsigned int), 0);
155INOUT_TYPEMAP(unsigned long, C_num_to_unsigned_long, C_unsigned_long_to_num, C_swig_is_long, (unsigned long), C_SIZEOF_FLONUM);
156INOUT_TYPEMAP(unsigned long long, C_num_to_unsigned_long, C_unsigned_long_to_num, C_swig_is_long, (unsigned long), C_SIZEOF_FLONUM);
157INOUT_TYPEMAP(unsigned char, C_character_code, C_make_character, C_swig_is_char, (unsigned int), 0);
158INOUT_TYPEMAP(signed char, C_character_code, C_make_character, C_swig_is_char, (int), 0);
159INOUT_TYPEMAP(char, C_character_code, C_make_character, C_swig_is_char, (char), 0);
160INOUT_TYPEMAP(bool, C_truep, C_mk_bool, C_swig_is_bool, (bool), 0);
161INOUT_TYPEMAP(float, C_c_double, C_flonum, C_swig_is_number, (double), C_SIZEOF_FLONUM);
162INOUT_TYPEMAP(double, C_c_double, C_flonum, C_swig_is_number, (double), C_SIZEOF_FLONUM);
163
164// INOUT
165// Mappings for an argument that is both an input and output
166// parameter
167
168/*
169The following methods can be applied to make a function parameter both
170an input and output value.  This combines the behavior of both the
171"INPUT" and "OUTPUT" methods described earlier.  Output values are
172returned in the form of a CHICKEN tuple.  
173
174         int            *INOUT
175         short          *INOUT
176         long           *INOUT
177         long long      *INOUT
178         unsigned int   *INOUT
179         unsigned short *INOUT
180         unsigned long  *INOUT
181         unsigned long long *INOUT
182         unsigned char  *INOUT
183         char           *INOUT
184         bool           *INOUT
185         float          *INOUT
186         double         *INOUT
187         
188For example, suppose you were trying to wrap the following function :
189
190        void neg(double *x) {
191             *x = -(*x);
192        }
193
194You could wrap it with SWIG as follows :
195
196        %include <typemaps.i>
197        void neg(double *INOUT);
198
199or you can use the %apply directive :
200
201        %include <typemaps.i>
202        %apply double *INOUT { double *x };
203        void neg(double *x);
204
205As well, you can wrap variables with :
206
207        %include <typemaps.i>
208        %apply double *INOUT { double *y };
209        extern double *y;
210
211Unlike C, this mapping does not directly modify the input value (since
212this makes no sense in CHICKEN).  Rather, the modified input value shows
213up as the return value of the function.  Thus, to apply this function
214to a CHICKEN variable you might do this :
215
216       x = neg(x)
217
218Note : previous versions of SWIG used the symbol 'BOTH' to mark
219input/output arguments.   This is still supported, but will be slowly
220phased out in future releases.
221
222*/
223
224%typemap(in) int *INOUT = int *INPUT;
225%typemap(in) enum SWIGTYPE *INOUT = enum SWIGTYPE *INPUT;
226%typemap(in) short *INOUT = short *INPUT;
227%typemap(in) long *INOUT = long *INPUT;
228%typemap(in) long long *INOUT = long long *INPUT;
229%typemap(in) unsigned *INOUT = unsigned *INPUT;
230%typemap(in) unsigned short *INOUT = unsigned short *INPUT;
231%typemap(in) unsigned long *INOUT = unsigned long *INPUT;
232%typemap(in) unsigned long long *INOUT = unsigned long long *INPUT;
233%typemap(in) unsigned char *INOUT = unsigned char *INPUT;
234%typemap(in) char *INOUT = char *INPUT;
235%typemap(in) bool *INOUT = bool *INPUT;
236%typemap(in) float *INOUT = float *INPUT;
237%typemap(in) double *INOUT = double *INPUT;
238
239%typemap(in) int &INOUT = int &INPUT;
240%typemap(in) enum SWIGTYPE &INOUT = enum SWIGTYPE &INPUT;
241%typemap(in) short &INOUT = short &INPUT;
242%typemap(in) long &INOUT = long &INPUT;
243%typemap(in) long long &INOUT = long long &INPUT;
244%typemap(in) unsigned &INOUT = unsigned &INPUT;
245%typemap(in) unsigned short &INOUT = unsigned short &INPUT;
246%typemap(in) unsigned long &INOUT = unsigned long &INPUT;
247%typemap(in) unsigned long long &INOUT = unsigned long long &INPUT;
248%typemap(in) unsigned char &INOUT = unsigned char &INPUT;
249%typemap(in) char &INOUT = char &INPUT;
250%typemap(in) bool &INOUT = bool &INPUT;
251%typemap(in) float &INOUT = float &INPUT;
252%typemap(in) double &INOUT = double &INPUT;
253
254%typemap(argout) int *INOUT = int *OUTPUT;
255%typemap(argout) enum SWIGTYPE *INOUT = enum SWIGTYPE *OUTPUT;
256%typemap(argout) short *INOUT = short *OUTPUT;
257%typemap(argout) long *INOUT = long *OUTPUT;
258%typemap(argout) long long *INOUT = long long *OUTPUT;
259%typemap(argout) unsigned *INOUT = unsigned *OUTPUT;
260%typemap(argout) unsigned short *INOUT = unsigned short *OUTPUT;
261%typemap(argout) unsigned long *INOUT = unsigned long *OUTPUT;
262%typemap(argout) unsigned long long *INOUT = unsigned long long *OUTPUT;
263%typemap(argout) unsigned char *INOUT = unsigned char *OUTPUT;
264%typemap(argout) bool *INOUT = bool *OUTPUT;
265%typemap(argout) float *INOUT = float *OUTPUT;
266%typemap(argout) double *INOUT = double *OUTPUT;
267
268%typemap(argout) int &INOUT = int &OUTPUT;
269%typemap(argout) enum SWIGTYPE &INOUT = enum SWIGTYPE &OUTPUT;
270%typemap(argout) short &INOUT = short &OUTPUT;
271%typemap(argout) long &INOUT = long &OUTPUT;
272%typemap(argout) long long &INOUT = long long &OUTPUT;
273%typemap(argout) unsigned &INOUT = unsigned &OUTPUT;
274%typemap(argout) unsigned short &INOUT = unsigned short &OUTPUT;
275%typemap(argout) unsigned long &INOUT = unsigned long &OUTPUT;
276%typemap(argout) unsigned long long &INOUT = unsigned long long &OUTPUT;
277%typemap(argout) unsigned char &INOUT = unsigned char &OUTPUT;
278%typemap(argout) char &INOUT = char &OUTPUT;
279%typemap(argout) bool &INOUT = bool &OUTPUT;
280%typemap(argout) float &INOUT = float &OUTPUT;
281%typemap(argout) double &INOUT = double &OUTPUT;
282
283/* Overloading information */
284
285%typemap(typecheck) double *INOUT = double;
286%typemap(typecheck) bool *INOUT = bool;
287%typemap(typecheck) char *INOUT = char;
288%typemap(typecheck) signed char *INOUT = signed char;
289%typemap(typecheck) unsigned char *INOUT = unsigned char;
290%typemap(typecheck) unsigned long *INOUT = unsigned long;
291%typemap(typecheck) unsigned long long *INOUT = unsigned long long;
292%typemap(typecheck) unsigned short *INOUT = unsigned short;
293%typemap(typecheck) unsigned int *INOUT = unsigned int;
294%typemap(typecheck) long *INOUT = long;
295%typemap(typecheck) long long *INOUT = long long;
296%typemap(typecheck) short *INOUT = short;
297%typemap(typecheck) int *INOUT = int;
298%typemap(typecheck) enum SWIGTYPE *INOUT = enum SWIGTYPE;
299%typemap(typecheck) float *INOUT = float;
300
301%typemap(typecheck) double &INOUT = double;
302%typemap(typecheck) bool &INOUT = bool;
303%typemap(typecheck) char &INOUT = char;
304%typemap(typecheck) signed char &INOUT = signed char;
305%typemap(typecheck) unsigned char &INOUT = unsigned char;
306%typemap(typecheck) unsigned long &INOUT = unsigned long;
307%typemap(typecheck) unsigned long long &INOUT = unsigned long long;
308%typemap(typecheck) unsigned short &INOUT = unsigned short;
309%typemap(typecheck) unsigned int &INOUT = unsigned int;
310%typemap(typecheck) long &INOUT = long;
311%typemap(typecheck) long long &INOUT = long long;
312%typemap(typecheck) short &INOUT = short;
313%typemap(typecheck) int &INOUT = int;
314%typemap(typecheck) enum SWIGTYPE &INOUT = enum SWIGTYPE;
315%typemap(typecheck) float &INOUT = float;