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.
7 * The SWIG typemap library provides a language independent mechanism for
8 * supporting output arguments, input values, and other C function
9 * calling mechanisms. The primary use of the library is to provide a
10 * better interface to certain C function--especially those involving
12 * ----------------------------------------------------------------------------- */
14 #if !defined(SWIG_USE_OLD_TYPEMAPS)
15 %include <typemaps/typemaps.swg>
20 // These remap a C pointer to be an "INPUT" value which is passed by value
21 // instead of reference.
25 The following methods can be applied to turn a pointer into a simple
26 "input" value. That is, instead of passing a pointer to an object,
27 you would use a real value instead.
36 unsigned long long *INPUT
42 To use these, suppose you had a C function like this :
44 double fadd(double *a, double *b) {
48 You could wrap it with SWIG as follows :
51 double fadd(double *INPUT, double *INPUT);
53 or you can use the %apply directive :
56 %apply double *INPUT { double *a, double *b };
57 double fadd(double *a, double *b);
61 %define INPUT_TYPEMAP(type, converter)
62 %typemap(in) type *INPUT(type temp), type &INPUT(type temp) {
63 temp = (type) converter($input);
66 %typemap(typecheck) type *INPUT = type;
67 %typemap(typecheck) type &INPUT = type;
70 INPUT_TYPEMAP(float, SvNV);
71 INPUT_TYPEMAP(double, SvNV);
72 INPUT_TYPEMAP(int, SvIV);
73 INPUT_TYPEMAP(long, SvIV);
74 INPUT_TYPEMAP(short, SvIV);
75 INPUT_TYPEMAP(signed char, SvIV);
76 INPUT_TYPEMAP(unsigned int, SvUV);
77 INPUT_TYPEMAP(unsigned long, SvUV);
78 INPUT_TYPEMAP(unsigned short, SvUV);
79 INPUT_TYPEMAP(unsigned char, SvUV);
81 %typemap(in) bool *INPUT(bool temp), bool &INPUT(bool temp) {
82 temp = SvIV($input) ? true : false;
85 %typemap(typecheck) bool *INPUT = bool;
86 %typemap(typecheck) bool &INPUT = bool;
88 %typemap(in) long long *INPUT($*1_ltype temp), long long &INPUT($*1_ltype temp) {
89 temp = strtoll(SvPV_nolen($input), 0, 0);
92 %typemap(typecheck) long long *INPUT = long long;
93 %typemap(typecheck) long long &INPUT = long long;
95 %typemap(in) unsigned long long *INPUT($*1_ltype temp), unsigned long long &INPUT($*1_ltype temp) {
96 temp = strtoull(SvPV_nolen($input), 0, 0);
99 %typemap(typecheck) unsigned long long *INPUT = unsigned long long;
100 %typemap(typecheck) unsigned long long &INPUT = unsigned long long;
105 // OUTPUT typemaps. These typemaps are used for parameters that
106 // are output only. The output value is appended to the result as
110 The following methods can be applied to turn a pointer into an "output"
111 value. When calling a function, no input value would be given for
112 a parameter, but an output value would be returned. In the case of
113 multiple output values, functions will return a Perl array.
120 unsigned short *OUTPUT
121 unsigned long *OUTPUT
122 unsigned long long *OUTPUT
123 unsigned char *OUTPUT
128 For example, suppose you were trying to wrap the modf() function in the
129 C math library which splits x into integral and fractional parts (and
130 returns the integer part in one of its parameters).:
132 double modf(double x, double *ip);
134 You could wrap it with SWIG as follows :
137 double modf(double x, double *OUTPUT);
139 or you can use the %apply directive :
142 %apply double *OUTPUT { double *ip };
143 double modf(double x, double *ip);
145 The Perl output of the function would be an array containing both
150 // Force the argument to be ignored.
152 %typemap(in,numinputs=0) int *OUTPUT(int temp), int &OUTPUT(int temp),
153 short *OUTPUT(short temp), short &OUTPUT(short temp),
154 long *OUTPUT(long temp), long &OUTPUT(long temp),
155 unsigned int *OUTPUT(unsigned int temp), unsigned int &OUTPUT(unsigned int temp),
156 unsigned short *OUTPUT(unsigned short temp), unsigned short &OUTPUT(unsigned short temp),
157 unsigned long *OUTPUT(unsigned long temp), unsigned long &OUTPUT(unsigned long temp),
158 unsigned char *OUTPUT(unsigned char temp), unsigned char &OUTPUT(unsigned char temp),
159 signed char *OUTPUT(signed char temp), signed char &OUTPUT(signed char temp),
160 bool *OUTPUT(bool temp), bool &OUTPUT(bool temp),
161 float *OUTPUT(float temp), float &OUTPUT(float temp),
162 double *OUTPUT(double temp), double &OUTPUT(double temp),
163 long long *OUTPUT($*1_ltype temp), long long &OUTPUT($*1_ltype temp),
164 unsigned long long *OUTPUT($*1_ltype temp), unsigned long long &OUTPUT($*1_ltype temp)
167 %typemap(argout) int *OUTPUT, int &OUTPUT,
168 short *OUTPUT, short &OUTPUT,
169 long *OUTPUT, long &OUTPUT,
170 signed char *OUTPUT, signed char &OUTPUT,
171 bool *OUTPUT, bool &OUTPUT
173 if (argvi >= items) {
176 $result = sv_newmortal();
177 sv_setiv($result,(IV) *($1));
181 %typemap(argout) unsigned int *OUTPUT, unsigned int &OUTPUT,
182 unsigned short *OUTPUT, unsigned short &OUTPUT,
183 unsigned long *OUTPUT, unsigned long &OUTPUT,
184 unsigned char *OUTPUT, unsigned char &OUTPUT
186 if (argvi >= items) {
189 $result = sv_newmortal();
190 sv_setuv($result,(UV) *($1));
196 %typemap(argout) float *OUTPUT, float &OUTPUT,
197 double *OUTPUT, double &OUTPUT
199 if (argvi >= items) {
202 $result = sv_newmortal();
203 sv_setnv($result,(double) *($1));
207 %typemap(argout) long long *OUTPUT, long long &OUTPUT {
209 if (argvi >= items) {
212 sprintf(temp,"%lld", (long long)*($1));
213 $result = sv_newmortal();
214 sv_setpv($result,temp);
218 %typemap(argout) unsigned long long *OUTPUT, unsigned long long &OUTPUT {
220 if (argvi >= items) {
223 sprintf(temp,"%llu", (unsigned long long)*($1));
224 $result = sv_newmortal();
225 sv_setpv($result,temp);
230 // Mappings for an argument that is both an input and output
234 The following methods can be applied to make a function parameter both
235 an input and output value. This combines the behavior of both the
236 "INPUT" and "OUTPUT" methods described earlier. Output values are
237 returned in the form of a Perl array.
244 unsigned short *INOUT
246 unsigned long long *INOUT
252 For example, suppose you were trying to wrap the following function :
254 void neg(double *x) {
258 You could wrap it with SWIG as follows :
261 void neg(double *INOUT);
263 or you can use the %apply directive :
266 %apply double *INOUT { double *x };
269 Unlike C, this mapping does not directly modify the input value.
270 Rather, the modified input value shows up as the return value of the
271 function. Thus, to apply this function to a Perl variable you might
278 %typemap(in) int *INOUT = int *INPUT;
279 %typemap(in) short *INOUT = short *INPUT;
280 %typemap(in) long *INOUT = long *INPUT;
281 %typemap(in) unsigned *INOUT = unsigned *INPUT;
282 %typemap(in) unsigned short *INOUT = unsigned short *INPUT;
283 %typemap(in) unsigned long *INOUT = unsigned long *INPUT;
284 %typemap(in) unsigned char *INOUT = unsigned char *INPUT;
285 %typemap(in) signed char *INOUT = signed char *INPUT;
286 %typemap(in) bool *INOUT = bool *INPUT;
287 %typemap(in) float *INOUT = float *INPUT;
288 %typemap(in) double *INOUT = double *INPUT;
289 %typemap(in) long long *INOUT = long long *INPUT;
290 %typemap(in) unsigned long long *INOUT = unsigned long long *INPUT;
292 %typemap(in) int &INOUT = int &INPUT;
293 %typemap(in) short &INOUT = short &INPUT;
294 %typemap(in) long &INOUT = long &INPUT;
295 %typemap(in) unsigned &INOUT = unsigned &INPUT;
296 %typemap(in) unsigned short &INOUT = unsigned short &INPUT;
297 %typemap(in) unsigned long &INOUT = unsigned long &INPUT;
298 %typemap(in) unsigned char &INOUT = unsigned char &INPUT;
299 %typemap(in) signed char &INOUT = signed char &INPUT;
300 %typemap(in) bool &INOUT = bool &INPUT;
301 %typemap(in) float &INOUT = float &INPUT;
302 %typemap(in) double &INOUT = double &INPUT;
303 %typemap(in) long long &INOUT = long long &INPUT;
304 %typemap(in) unsigned long long &INOUT = unsigned long long &INPUT;
307 %typemap(argout) int *INOUT = int *OUTPUT;
308 %typemap(argout) short *INOUT = short *OUTPUT;
309 %typemap(argout) long *INOUT = long *OUTPUT;
310 %typemap(argout) unsigned *INOUT = unsigned *OUTPUT;
311 %typemap(argout) unsigned short *INOUT = unsigned short *OUTPUT;
312 %typemap(argout) unsigned long *INOUT = unsigned long *OUTPUT;
313 %typemap(argout) unsigned char *INOUT = unsigned char *OUTPUT;
314 %typemap(argout) signed char *INOUT = signed char *OUTPUT;
315 %typemap(argout) bool *INOUT = bool *OUTPUT;
316 %typemap(argout) float *INOUT = float *OUTPUT;
317 %typemap(argout) double *INOUT = double *OUTPUT;
318 %typemap(argout) long long *INOUT = long long *OUTPUT;
319 %typemap(argout) unsigned long long *INOUT = unsigned long long *OUTPUT;
322 %typemap(argout) int &INOUT = int &OUTPUT;
323 %typemap(argout) short &INOUT = short &OUTPUT;
324 %typemap(argout) long &INOUT = long &OUTPUT;
325 %typemap(argout) unsigned &INOUT = unsigned &OUTPUT;
326 %typemap(argout) unsigned short &INOUT = unsigned short &OUTPUT;
327 %typemap(argout) unsigned long &INOUT = unsigned long &OUTPUT;
328 %typemap(argout) unsigned char &INOUT = unsigned char &OUTPUT;
329 %typemap(argout) signed char &INOUT = signed char &OUTPUT;
330 %typemap(argout) bool &INOUT = bool &OUTPUT;
331 %typemap(argout) float &INOUT = float &OUTPUT;
332 %typemap(argout) double &INOUT = double &OUTPUT;
333 %typemap(argout) long long &INOUT = long long &OUTPUT;
334 %typemap(argout) unsigned long long &INOUT = unsigned long long &OUTPUT;
337 /* Overloading information */
339 %typemap(typecheck) double *INOUT = double;
340 %typemap(typecheck) bool *INOUT = bool;
341 %typemap(typecheck) signed char *INOUT = signed char;
342 %typemap(typecheck) unsigned char *INOUT = unsigned char;
343 %typemap(typecheck) unsigned long *INOUT = unsigned long;
344 %typemap(typecheck) unsigned short *INOUT = unsigned short;
345 %typemap(typecheck) unsigned int *INOUT = unsigned int;
346 %typemap(typecheck) long *INOUT = long;
347 %typemap(typecheck) short *INOUT = short;
348 %typemap(typecheck) int *INOUT = int;
349 %typemap(typecheck) float *INOUT = float;
350 %typemap(typecheck) long long *INOUT = long long;
351 %typemap(typecheck) unsigned long long *INOUT = unsigned long long;
353 %typemap(typecheck) double &INOUT = double;
354 %typemap(typecheck) bool &INOUT = bool;
355 %typemap(typecheck) signed char &INOUT = signed char;
356 %typemap(typecheck) unsigned char &INOUT = unsigned char;
357 %typemap(typecheck) unsigned long &INOUT = unsigned long;
358 %typemap(typecheck) unsigned short &INOUT = unsigned short;
359 %typemap(typecheck) unsigned int &INOUT = unsigned int;
360 %typemap(typecheck) long &INOUT = long;
361 %typemap(typecheck) short &INOUT = short;
362 %typemap(typecheck) int &INOUT = int;
363 %typemap(typecheck) float &INOUT = float;
364 %typemap(typecheck) long long &INOUT = long long;
365 %typemap(typecheck) unsigned long long &INOUT = unsigned long long;
369 // --------------------------------------------------------------------
371 // --------------------------------------------------------------------
374 %include <reference.i>