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 * Pointer and reference handling typemap library
9 * These mappings provide support for input/output arguments and common
10 * uses for C/C++ pointers and C++ references.
11 * ----------------------------------------------------------------------------- */
17 These typemaps are used for pointer/reference parameters that are input only
18 and are mapped to a C# input parameter.
20 The following typemaps can be applied to turn a pointer or reference into a simple
21 input value. That is, instead of passing a pointer or reference to an object,
22 you would use a real value instead.
24 bool *INPUT, bool &INPUT
25 signed char *INPUT, signed char &INPUT
26 unsigned char *INPUT, unsigned char &INPUT
27 short *INPUT, short &INPUT
28 unsigned short *INPUT, unsigned short &INPUT
29 int *INPUT, int &INPUT
30 unsigned int *INPUT, unsigned int &INPUT
31 long *INPUT, long &INPUT
32 unsigned long *INPUT, unsigned long &INPUT
33 long long *INPUT, long long &INPUT
34 unsigned long long *INPUT, unsigned long long &INPUT
35 float *INPUT, float &INPUT
36 double *INPUT, double &INPUT
38 To use these, suppose you had a C function like this :
40 double fadd(double *a, double *b) {
44 You could wrap it with SWIG as follows :
47 double fadd(double *INPUT, double *INPUT);
49 or you can use the %apply directive :
52 %apply double *INPUT { double *a, double *b };
53 double fadd(double *a, double *b);
55 In C# you could then use it like this:
56 double answer = modulename.fadd(10.0, 20.0);
60 %define INPUT_TYPEMAP(TYPE, CTYPE, CSTYPE)
61 %typemap(ctype) TYPE *INPUT, TYPE &INPUT "CTYPE"
62 %typemap(imtype) TYPE *INPUT, TYPE &INPUT "CSTYPE"
63 %typemap(cstype) TYPE *INPUT, TYPE &INPUT "CSTYPE"
64 %typemap(csin) TYPE *INPUT, TYPE &INPUT "$csinput"
65 %typemap(csdirectorin) TYPE *INPUT, TYPE &INPUT "$iminput"
66 %typemap(csdirectorout) TYPE *INPUT, TYPE &INPUT "$cscall"
68 %typemap(in) TYPE *INPUT, TYPE &INPUT
69 %{ $1 = ($1_ltype)&$input; %}
71 %typemap(directorout) TYPE *INPUT, TYPE &INPUT
72 %{ $result = ($1_ltype)&$input; %}
74 %typemap(directorin) TYPE &INPUT
75 %{ $input = (CTYPE *)$1; %}
77 %typemap(directorin) TYPE *INPUT
78 %{ $input = (CTYPE *)$1; %}
80 %typemap(typecheck) TYPE *INPUT = TYPE;
81 %typemap(typecheck) TYPE &INPUT = TYPE;
84 INPUT_TYPEMAP(bool, unsigned int, bool)
85 //INPUT_TYPEMAP(char, char, char)
86 INPUT_TYPEMAP(signed char, signed char, sbyte)
87 INPUT_TYPEMAP(unsigned char, unsigned char, byte)
88 INPUT_TYPEMAP(short, short, short)
89 INPUT_TYPEMAP(unsigned short, unsigned short, ushort)
90 INPUT_TYPEMAP(int, int, int)
91 INPUT_TYPEMAP(unsigned int, unsigned int, uint)
92 INPUT_TYPEMAP(long, long, int)
93 INPUT_TYPEMAP(unsigned long, unsigned long, uint)
94 INPUT_TYPEMAP(long long, long long, long)
95 INPUT_TYPEMAP(unsigned long long, unsigned long long, ulong)
96 INPUT_TYPEMAP(float, float, float)
97 INPUT_TYPEMAP(double, double, double)
105 These typemaps are used for pointer/reference parameters that are output only and
106 are mapped to a C# output parameter.
108 The following typemaps can be applied to turn a pointer or reference into an "output"
109 value. When calling a function, no input value would be given for
110 a parameter, but an output value would be returned. In C#, the 'out' keyword is
111 used when passing the parameter to a function that takes an output parameter.
113 bool *OUTPUT, bool &OUTPUT
114 signed char *OUTPUT, signed char &OUTPUT
115 unsigned char *OUTPUT, unsigned char &OUTPUT
116 short *OUTPUT, short &OUTPUT
117 unsigned short *OUTPUT, unsigned short &OUTPUT
118 int *OUTPUT, int &OUTPUT
119 unsigned int *OUTPUT, unsigned int &OUTPUT
120 long *OUTPUT, long &OUTPUT
121 unsigned long *OUTPUT, unsigned long &OUTPUT
122 long long *OUTPUT, long long &OUTPUT
123 unsigned long long *OUTPUT, unsigned long long &OUTPUT
124 float *OUTPUT, float &OUTPUT
125 double *OUTPUT, double &OUTPUT
127 For example, suppose you were trying to wrap the modf() function in the
128 C math library which splits x into integral and fractional parts (and
129 returns the integer part in one of its parameters):
131 double modf(double x, double *ip);
133 You could wrap it with SWIG as follows :
135 %include <typemaps.i>
136 double modf(double x, double *OUTPUT);
138 or you can use the %apply directive :
140 %include <typemaps.i>
141 %apply double *OUTPUT { double *ip };
142 double modf(double x, double *ip);
144 The C# output of the function would be the function return value and the
145 value returned in the second output parameter. In C# you would use it like this:
148 double fraction = modulename.modf(5, out dptr);
152 %define OUTPUT_TYPEMAP(TYPE, CTYPE, CSTYPE, TYPECHECKPRECEDENCE)
153 %typemap(ctype) TYPE *OUTPUT, TYPE &OUTPUT "CTYPE *"
154 %typemap(imtype) TYPE *OUTPUT, TYPE &OUTPUT "out CSTYPE"
155 %typemap(cstype) TYPE *OUTPUT, TYPE &OUTPUT "out CSTYPE"
156 %typemap(csin) TYPE *OUTPUT, TYPE &OUTPUT "out $csinput"
157 %typemap(csdirectorin) TYPE *OUTPUT, TYPE &OUTPUT "$iminput"
158 %typemap(csdirectorout) TYPE *OUTPUT, TYPE &OUTPUT "$cscall"
161 %typemap(in) TYPE *OUTPUT, TYPE &OUTPUT
162 %{ $1 = ($1_ltype)$input; %}
164 %typemap(directorout,warning="Need to provide TYPE *OUTPUT directorout typemap") TYPE *OUTPUT, TYPE &OUTPUT {
167 %typemap(directorin) TYPE &OUTPUT
170 %typemap(directorin,warning="Need to provide TYPE *OUTPUT directorin typemap, TYPE array length is unknown") TYPE *OUTPUT
175 %typecheck(SWIG_TYPECHECK_##TYPECHECKPRECEDENCE) TYPE *OUTPUT, TYPE &OUTPUT ""
178 OUTPUT_TYPEMAP(bool, unsigned int, bool, BOOL_PTR)
179 //OUTPUT_TYPEMAP(char, char, char, CHAR_PTR)
180 OUTPUT_TYPEMAP(signed char, signed char, sbyte, INT8_PTR)
181 OUTPUT_TYPEMAP(unsigned char, unsigned char, byte, UINT8_PTR)
182 OUTPUT_TYPEMAP(short, short, short, INT16_PTR)
183 OUTPUT_TYPEMAP(unsigned short, unsigned short, ushort, UINT16_PTR)
184 OUTPUT_TYPEMAP(int, int, int, INT32_PTR)
185 OUTPUT_TYPEMAP(unsigned int, unsigned int, uint, UINT32_PTR)
186 OUTPUT_TYPEMAP(long, long, int, INT32_PTR)
187 OUTPUT_TYPEMAP(unsigned long, unsigned long, uint, UINT32_PTR)
188 OUTPUT_TYPEMAP(long long, long long, long, INT64_PTR)
189 OUTPUT_TYPEMAP(unsigned long long, unsigned long long, ulong, UINT64_PTR)
190 OUTPUT_TYPEMAP(float, float, float, FLOAT_PTR)
191 OUTPUT_TYPEMAP(double, double, double, DOUBLE_PTR)
193 #undef OUTPUT_TYPEMAP
195 %typemap(in) bool *OUTPUT, bool &OUTPUT
197 $1 = ($1_ltype)$input; %}
204 These typemaps are for pointer/reference parameters that are both input and
205 output and are mapped to a C# reference parameter.
207 The following typemaps can be applied to turn a pointer or reference into a
208 reference parameters, that is the parameter is both an input and an output.
209 In C#, the 'ref' keyword is used for reference parameters.
211 bool *INOUT, bool &INOUT
212 signed char *INOUT, signed char &INOUT
213 unsigned char *INOUT, unsigned char &INOUT
214 short *INOUT, short &INOUT
215 unsigned short *INOUT, unsigned short &INOUT
216 int *INOUT, int &INOUT
217 unsigned int *INOUT, unsigned int &INOUT
218 long *INOUT, long &INOUT
219 unsigned long *INOUT, unsigned long &INOUT
220 long long *INOUT, long long &INOUT
221 unsigned long long *INOUT, unsigned long long &INOUT
222 float *INOUT, float &INOUT
223 double *INOUT, double &INOUT
225 For example, suppose you were trying to wrap the following function :
227 void neg(double *x) {
231 You could wrap it with SWIG as follows :
233 %include <typemaps.i>
234 void neg(double *INOUT);
236 or you can use the %apply directive :
238 %include <typemaps.i>
239 %apply double *INOUT { double *x };
242 The C# output of the function would be the new value returned by the
243 reference parameter. In C# you would use it like this:
249 The implementation of the OUTPUT and INOUT typemaps is different to the scripting
250 languages in that the scripting languages will return the output value as part
251 of the function return value.
255 %define INOUT_TYPEMAP(TYPE, CTYPE, CSTYPE, TYPECHECKPRECEDENCE)
256 %typemap(ctype) TYPE *INOUT, TYPE &INOUT "CTYPE *"
257 %typemap(imtype) TYPE *INOUT, TYPE &INOUT "ref CSTYPE"
258 %typemap(cstype) TYPE *INOUT, TYPE &INOUT "ref CSTYPE"
259 %typemap(csin) TYPE *INOUT, TYPE &INOUT "ref $csinput"
260 %typemap(csdirectorin) TYPE *INOUT, TYPE &INOUT "$iminput"
261 %typemap(csdirectorout) TYPE *INOUT, TYPE &INOUT "$cscall"
263 %typemap(in) TYPE *INOUT, TYPE &INOUT
264 %{ $1 = ($1_ltype)$input; %}
266 %typemap(directorout,warning="Need to provide TYPE *INOUT directorout typemap") TYPE *INOUT, TYPE &INOUT {
269 %typemap(directorin) TYPE &INOUT
272 %typemap(directorin,warning="Need to provide TYPE *INOUT directorin typemap, TYPE array length is unknown") TYPE *INOUT, TYPE &INOUT
276 %typecheck(SWIG_TYPECHECK_##TYPECHECKPRECEDENCE) TYPE *INOUT, TYPE &INOUT ""
279 INOUT_TYPEMAP(bool, unsigned int, bool, BOOL_PTR)
280 //INOUT_TYPEMAP(char, char, char, CHAR_PTR)
281 INOUT_TYPEMAP(signed char, signed char, sbyte, INT8_PTR)
282 INOUT_TYPEMAP(unsigned char, unsigned char, byte, UINT8_PTR)
283 INOUT_TYPEMAP(short, short, short, INT16_PTR)
284 INOUT_TYPEMAP(unsigned short, unsigned short, ushort, UINT16_PTR)
285 INOUT_TYPEMAP(int, int, int, INT32_PTR)
286 INOUT_TYPEMAP(unsigned int, unsigned int, uint, UINT32_PTR)
287 INOUT_TYPEMAP(long, long, int, INT32_PTR)
288 INOUT_TYPEMAP(unsigned long, unsigned long, uint, UINT32_PTR)
289 INOUT_TYPEMAP(long long, long long, long, INT64_PTR)
290 INOUT_TYPEMAP(unsigned long long, unsigned long long, ulong, UINT64_PTR)
291 INOUT_TYPEMAP(float, float, float, FLOAT_PTR)
292 INOUT_TYPEMAP(double, double, double, DOUBLE_PTR)