PageRenderTime 21ms CodeModel.GetById 11ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

/trunk/Lib/go/typemaps.i

#
Swig | 316 lines | 107 code | 32 blank | 177 comment | 0 complexity | 986b3528a405643892ede959a7d92717 MD5 | raw file
  1/* -----------------------------------------------------------------------------
  2 * typemaps.i
  3 *
  4 * Pointer and reference handling typemap library
  5 *
  6 * These mappings provide support for input/output arguments and common
  7 * uses for C/C++ pointers and C++ references.
  8 * ----------------------------------------------------------------------------- */
  9
 10/*
 11INPUT typemaps
 12--------------
 13
 14These typemaps remap a C pointer or C++ reference to be an "INPUT" value which is
 15passed by value instead of reference.
 16
 17The following typemaps can be applied to turn a pointer or reference into a simple
 18input value.  That is, instead of passing a pointer or reference to an object,
 19you would use a real value instead.
 20
 21        bool               *INPUT, bool               &INPUT
 22        signed char        *INPUT, signed char        &INPUT
 23        unsigned char      *INPUT, unsigned char      &INPUT
 24        short              *INPUT, short              &INPUT
 25        unsigned short     *INPUT, unsigned short     &INPUT
 26        int                *INPUT, int                &INPUT
 27        unsigned int       *INPUT, unsigned int       &INPUT
 28        long               *INPUT, long               &INPUT
 29        unsigned long      *INPUT, unsigned long      &INPUT
 30        long long          *INPUT, long long          &INPUT
 31        unsigned long long *INPUT, unsigned long long &INPUT
 32        float              *INPUT, float              &INPUT
 33        double             *INPUT, double             &INPUT
 34         
 35To use these, suppose you had a C function like this :
 36
 37        double fadd(double *a, double *b) {
 38               return *a+*b;
 39        }
 40
 41You could wrap it with SWIG as follows :
 42        
 43        %include <typemaps.i>
 44        double fadd(double *INPUT, double *INPUT);
 45
 46or you can use the %apply directive :
 47
 48        %include <typemaps.i>
 49        %apply double *INPUT { double *a, double *b };
 50        double fadd(double *a, double *b);
 51
 52In Go you could then use it like this:
 53        answer := modulename.Fadd(10.0, 20.0)
 54
 55There are no char *INPUT typemaps, however you can apply the signed
 56char * typemaps instead:
 57        %include <typemaps.i>
 58        %apply signed char *INPUT {char *input};
 59        void f(char *input);
 60*/
 61
 62%define INPUT_TYPEMAP(TYPE, GOTYPE)
 63%typemap(gotype) TYPE *INPUT, TYPE &INPUT "GOTYPE"
 64
 65%typemap(in) TYPE *INPUT
 66%{ $1 = ($1_ltype)&$input; %}
 67
 68%typemap(in) TYPE &INPUT
 69%{ $1 = ($1_ltype)$input; %}
 70
 71%typemap(out) TYPE *INPUT, TYPE &INPUT ""
 72
 73%typemap(freearg) TYPE *INPUT, TYPE &INPUT ""
 74
 75%typemap(argout) TYPE *INPUT, TYPE &INPUT ""
 76
 77// %typemap(typecheck) TYPE *INPUT = TYPE;
 78// %typemap(typecheck) TYPE &INPUT = TYPE;
 79%enddef
 80
 81INPUT_TYPEMAP(bool, bool);
 82INPUT_TYPEMAP(signed char, int8);
 83INPUT_TYPEMAP(char, byte);
 84INPUT_TYPEMAP(unsigned char, byte);
 85INPUT_TYPEMAP(short, int16);
 86INPUT_TYPEMAP(unsigned short, uint16);
 87INPUT_TYPEMAP(int, int);
 88INPUT_TYPEMAP(unsigned int, uint);
 89INPUT_TYPEMAP(long, int64);
 90INPUT_TYPEMAP(unsigned long, uint64);
 91INPUT_TYPEMAP(long long, int64);
 92INPUT_TYPEMAP(unsigned long long, uint64);
 93INPUT_TYPEMAP(float, float32);
 94INPUT_TYPEMAP(double, float64);
 95
 96#undef INPUT_TYPEMAP
 97
 98// OUTPUT typemaps.   These typemaps are used for parameters that
 99// are output only.   An array replaces the c pointer or reference parameter. 
100// The output value is returned in this array passed in. 
101
102/*
103OUTPUT typemaps
104---------------
105
106The following typemaps can be applied to turn a pointer or reference
107into an "output" value.  When calling a function, no input value would
108be given for a parameter, but an output value would be returned.  This
109works by a Go slice being passed as a parameter where a c pointer or
110reference is required.  As with any Go function, the array is passed
111by reference so that any modifications to the array will be picked up
112in the calling function.  Note that the array passed in MUST have at
113least one element, but as the c function does not require any input,
114the value can be set to anything.
115
116        bool               *OUTPUT, bool               &OUTPUT
117        signed char        *OUTPUT, signed char        &OUTPUT
118        unsigned char      *OUTPUT, unsigned char      &OUTPUT
119        short              *OUTPUT, short              &OUTPUT
120        unsigned short     *OUTPUT, unsigned short     &OUTPUT
121        int                *OUTPUT, int                &OUTPUT
122        unsigned int       *OUTPUT, unsigned int       &OUTPUT
123        long               *OUTPUT, long               &OUTPUT
124        unsigned long      *OUTPUT, unsigned long      &OUTPUT
125        long long          *OUTPUT, long long          &OUTPUT
126        unsigned long long *OUTPUT, unsigned long long &OUTPUT
127        float              *OUTPUT, float              &OUTPUT
128        double             *OUTPUT, double             &OUTPUT
129         
130For example, suppose you were trying to wrap the modf() function in the
131C math library which splits x into integral and fractional parts (and
132returns the integer part in one of its parameters):
133
134        double modf(double x, double *ip);
135
136You could wrap it with SWIG as follows :
137
138        %include <typemaps.i>
139        double modf(double x, double *OUTPUT);
140
141or you can use the %apply directive :
142
143        %include <typemaps.i>
144        %apply double *OUTPUT { double *ip };
145        double modf(double x, double *ip);
146
147The Go output of the function would be the function return value and the 
148value in the single element array. In Go you would use it like this:
149
150    ptr := []float64{0.0}
151    fraction := modulename.Modf(5.0,ptr)
152
153There are no char *OUTPUT typemaps, however you can apply the signed
154char * typemaps instead:
155        %include <typemaps.i>
156        %apply signed char *OUTPUT {char *output};
157        void f(char *output);
158*/
159
160%define OUTPUT_TYPEMAP(TYPE, GOTYPE)
161%typemap(gotype) TYPE *OUTPUT, TYPE &OUTPUT %{[]GOTYPE%}
162
163%typemap(in) TYPE *OUTPUT($*1_ltype temp)
164{
165  if ($input.len == 0) {
166    _swig_gopanic("array must contain at least 1 element");
167  }
168  $1 = &temp;
169}
170
171%typemap(in) TYPE &OUTPUT($*1_ltype temp)
172{
173  if ($input->len == 0) {
174    _swig_gopanic("array must contain at least 1 element");
175  }
176  $1 = &temp;
177}
178
179%typemap(out) TYPE *OUTPUT, TYPE &OUTPUT ""
180
181%typemap(freearg) TYPE *OUTPUT, TYPE &OUTPUT ""
182
183%typemap(argout) TYPE *OUTPUT
184{
185  TYPE* a = (TYPE *) $input.array;
186  a[0] = temp$argnum;
187}
188
189%typemap(argout) TYPE &OUTPUT 
190{
191  TYPE* a = (TYPE *) $input->array;
192  a[0] = temp$argnum;
193}
194
195%enddef
196
197OUTPUT_TYPEMAP(bool, bool);
198OUTPUT_TYPEMAP(signed char, int8);
199OUTPUT_TYPEMAP(char, byte);
200OUTPUT_TYPEMAP(unsigned char, byte);
201OUTPUT_TYPEMAP(short, int16);
202OUTPUT_TYPEMAP(unsigned short, uint16);
203OUTPUT_TYPEMAP(int, int);
204OUTPUT_TYPEMAP(unsigned int, uint);
205OUTPUT_TYPEMAP(long, int64);
206OUTPUT_TYPEMAP(unsigned long, uint64);
207OUTPUT_TYPEMAP(long long, int64);
208OUTPUT_TYPEMAP(unsigned long long, uint64);
209OUTPUT_TYPEMAP(float, float32);
210OUTPUT_TYPEMAP(double, float64);
211
212#undef OUTPUT_TYPEMAP
213
214/*
215INOUT typemaps
216--------------
217
218Mappings for a parameter that is both an input and an output parameter
219
220The following typemaps can be applied to make a function parameter both
221an input and output value.  This combines the behavior of both the
222"INPUT" and "OUTPUT" typemaps described earlier.  Output values are
223returned as an element in a Go slice.
224
225        bool               *INOUT, bool               &INOUT
226        signed char        *INOUT, signed char        &INOUT
227        unsigned char      *INOUT, unsigned char      &INOUT
228        short              *INOUT, short              &INOUT
229        unsigned short     *INOUT, unsigned short     &INOUT
230        int                *INOUT, int                &INOUT
231        unsigned int       *INOUT, unsigned int       &INOUT
232        long               *INOUT, long               &INOUT
233        unsigned long      *INOUT, unsigned long      &INOUT
234        long long          *INOUT, long long          &INOUT
235        unsigned long long *INOUT, unsigned long long &INOUT
236        float              *INOUT, float              &INOUT
237        double             *INOUT, double             &INOUT
238         
239For example, suppose you were trying to wrap the following function :
240
241        void neg(double *x) {
242             *x = -(*x);
243        }
244
245You could wrap it with SWIG as follows :
246
247        %include <typemaps.i>
248        void neg(double *INOUT);
249
250or you can use the %apply directive :
251
252        %include <typemaps.i>
253        %apply double *INOUT { double *x };
254        void neg(double *x);
255
256This works similarly to C in that the mapping directly modifies the
257input value - the input must be an array with a minimum of one element. 
258The element in the array is the input and the output is the element in 
259the array.
260
261       x := []float64{5.0}
262       Neg(x);
263
264The implementation of the OUTPUT and INOUT typemaps is different to
265other languages in that other languages will return the output value
266as part of the function return value. This difference is due to Go
267being a typed language.
268
269There are no char *INOUT typemaps, however you can apply the signed
270char * typemaps instead:
271        %include <typemaps.i>
272        %apply signed char *INOUT {char *inout};
273        void f(char *inout);
274*/
275
276%define INOUT_TYPEMAP(TYPE, GOTYPE)
277%typemap(gotype) TYPE *INOUT, TYPE &INOUT %{[]GOTYPE%}
278
279%typemap(in) TYPE *INOUT {
280  if ($input.len == 0) {
281    _swig_gopanic("array must contain at least 1 element");
282  }
283  $1 = ($1_ltype) $input.array;
284}
285
286%typemap(in) TYPE &INOUT {
287  if ($input->len == 0) {
288    _swig_gopanic("array must contain at least 1 element");
289  }
290  $1 = ($1_ltype) $input->array;
291}
292
293%typemap(out) TYPE *INOUT, TYPE &INOUT ""
294
295%typemap(freearg) TYPE *INOUT, TYPE &INOUT ""
296
297%typemap(argout) TYPE *INOUT, TYPE &INOUT ""
298
299%enddef
300
301INOUT_TYPEMAP(bool, bool);
302INOUT_TYPEMAP(signed char, int8);
303INOUT_TYPEMAP(char, byte);
304INOUT_TYPEMAP(unsigned char, byte);
305INOUT_TYPEMAP(short, int16);
306INOUT_TYPEMAP(unsigned short, uint16);
307INOUT_TYPEMAP(int, int);
308INOUT_TYPEMAP(unsigned int, uint);
309INOUT_TYPEMAP(long, int64);
310INOUT_TYPEMAP(unsigned long, uint64);
311INOUT_TYPEMAP(long long, int64);
312INOUT_TYPEMAP(unsigned long long, uint64);
313INOUT_TYPEMAP(float, float32);
314INOUT_TYPEMAP(double, float64);
315
316#undef INOUT_TYPEMAP