Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | // |
2 | // Uncomment the following definition if you don't want the in/out | |
3 | // typemaps by default, ie, you prefer to use typemaps.i. | |
4 | // | |
5 | //#define SWIG_INOUT_NODEF | |
6 | ||
7 | // | |
8 | // Use the following definition to enable the INPUT parameters to | |
9 | // accept both 'by value' and 'pointer' objects. | |
10 | // | |
11 | #define SWIG_INPUT_ACCEPT_PTRS | |
12 | ||
13 | // ------------------------------------------------------------------------ | |
14 | // Pointer handling | |
15 | // | |
16 | // These mappings provide support for input/output arguments and common | |
17 | // uses for C/C++ pointers. | |
18 | // ------------------------------------------------------------------------ | |
19 | ||
20 | // INPUT typemaps. | |
21 | // These remap a C pointer to be an "INPUT" value which is passed by value | |
22 | // instead of reference. | |
23 | ||
24 | /* | |
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. | |
28 | ||
29 | To use these, suppose you had a C function like this : | |
30 | ||
31 | double fadd(double *a, double *b) { | |
32 | return *a+*b; | |
33 | } | |
34 | ||
35 | You could wrap it with SWIG as follows : | |
36 | ||
37 | double fadd(double *INPUT, double *INPUT); | |
38 | ||
39 | or you can use the %apply directive : | |
40 | ||
41 | %apply double *INPUT { double *a, double *b }; | |
42 | double fadd(double *a, double *b); | |
43 | ||
44 | */ | |
45 | #ifdef SWIG_INPUT_ACCEPT_PTRS | |
46 | #define SWIG_CheckInputPtr(input,arg,desc,disown) (SWIG_ConvertPtr(input,arg,desc,disown) != -1) | |
47 | #else | |
48 | #define SWIG_CheckInputPtr(input,arg,desc,disown) (0) | |
49 | #endif | |
50 | ||
51 | %define _PYVAL_INPUT_TYPEMAP(code,as_meth,check_meth,as_frag,check_frag,Type) | |
52 | %typemap(in,fragment=as_frag) Type *INPUT ($*1_ltype temp, int res = 0) { | |
53 | if (!SWIG_CheckInputPtr($input,(void **)(&$1),$1_descriptor,$disown)) { | |
54 | temp = as_meth($input); | |
55 | if (SWIG_arg_fail($argnum)) SWIG_fail; | |
56 | $1 = &temp; | |
57 | res = SWIG_NEWOBJ; | |
58 | } | |
59 | } | |
60 | %typemap(in,fragment=as_frag) Type &INPUT($*1_ltype temp, int res = 0) { | |
61 | if (!SWIG_CheckInputPtr($input,(void **)(&$1),$1_descriptor,$disown)) { | |
62 | temp = as_meth($input); | |
63 | if (SWIG_arg_fail($argnum)) SWIG_fail; | |
64 | $1 = &temp; | |
65 | res = SWIG_NEWOBJ; | |
66 | } | |
67 | if (!$1) { | |
68 | SWIG_null_ref("$basetype"); | |
69 | } | |
70 | if (SWIG_arg_fail($argnum)) SWIG_fail; | |
71 | } | |
72 | %typemap(typecheck,precedence=code,fragment=check_frag) Type *INPUT, Type &INPUT { | |
73 | void *ptr; | |
74 | $1 = (check_meth($input) || (SWIG_CheckInputPtr($input,&ptr,$1_descriptor,0))); | |
75 | } | |
76 | %enddef | |
77 | ||
78 | %define _PYPTR_INPUT_TYPEMAP(code,asptr_meth,asptr_frag,Type) | |
79 | %typemap(in,fragment=asptr_frag) Type *INPUT(int res = 0) { | |
80 | res = asptr_meth($input, &$1); | |
81 | if (!res) { | |
82 | SWIG_type_error("$basetype", $input); | |
83 | } | |
84 | if (SWIG_arg_fail($argnum)) SWIG_fail; | |
85 | } | |
86 | ||
87 | %typemap(in,fragment=asptr_frag) Type &INPUT(int res = 0) { | |
88 | res = asptr_meth($input, &$1); | |
89 | if (!res) { | |
90 | SWIG_type_error("$basetype", $input); | |
91 | } else { | |
92 | if (!$1) { | |
93 | SWIG_null_ref("$basetype"); | |
94 | } | |
95 | } | |
96 | if (SWIG_arg_fail($argnum)) SWIG_fail; | |
97 | } | |
98 | %typemap(freearg) Type *INPUT, Type &INPUT | |
99 | "if (res$argnum == SWIG_NEWOBJ) delete $1;"; | |
100 | %typemap(typecheck,precedence=code,fragment=asptr_frag) Type *INPUT, Type &INPUT | |
101 | "$1 = asptr_meth($input, (Type**)0) != 0;" | |
102 | %enddef | |
103 | ||
104 | // OUTPUT typemaps. These typemaps are used for parameters that | |
105 | // are output only. The output value is appended to the result as | |
106 | // a list element. | |
107 | ||
108 | /* | |
109 | The following methods can be applied to turn a pointer into an "output" | |
110 | value. When calling a function, no input value would be given for | |
111 | a parameter, but an output value would be returned. In the case of | |
112 | multiple output values, they are returned in the form of a Python tuple. | |
113 | ||
114 | ||
115 | For example, suppose you were trying to wrap the modf() function in the | |
116 | C math library which splits x into integral and fractional parts (and | |
117 | returns the integer part in one of its parameters).K: | |
118 | ||
119 | double modf(double x, double *ip); | |
120 | ||
121 | You could wrap it with SWIG as follows : | |
122 | ||
123 | double modf(double x, double *OUTPUT); | |
124 | ||
125 | or you can use the %apply directive : | |
126 | ||
127 | %apply double *OUTPUT { double *ip }; | |
128 | double modf(double x, double *ip); | |
129 | ||
130 | The Python output of the function would be a tuple containing both | |
131 | output values. | |
132 | ||
133 | */ | |
134 | ||
135 | // These typemaps contributed by Robin Dunn | |
136 | //---------------------------------------------------------------------- | |
137 | // | |
138 | // T_OUTPUT typemap (and helper function) to return multiple argouts as | |
139 | // a tuple instead of a list. | |
140 | // | |
141 | // Author: Robin Dunn | |
142 | //---------------------------------------------------------------------- | |
143 | ||
144 | %include <pytuplehlp.swg> | |
145 | ||
146 | %define _PYVAL_OUTPUT_TYPEMAP(from_meth, from_frag, Type) | |
147 | %typemap(in,numinputs=0) Type *OUTPUT ($*1_ltype temp, int res = 0), | |
148 | Type &OUTPUT ($*1_ltype temp, int res = 0) | |
149 | "$1 = &temp; res = SWIG_NEWOBJ;"; | |
150 | %fragment("t_out_helper"{Type},"header", | |
151 | fragment="t_output_helper",fragment=from_frag) {} | |
152 | %typemap(argout,fragment="t_out_helper"{Type}) Type *OUTPUT, Type &OUTPUT | |
153 | "$result = t_output_helper($result, ((res$argnum == SWIG_NEWOBJ) ? | |
154 | from_meth((*$1)) : SWIG_NewPointerObj((void*)($1), $1_descriptor, 0)));"; | |
155 | ||
156 | %enddef | |
157 | ||
158 | ||
159 | // INOUT | |
160 | // Mappings for an argument that is both an input and output | |
161 | // parameter | |
162 | ||
163 | /* | |
164 | The following methods can be applied to make a function parameter both | |
165 | an input and output value. This combines the behavior of both the | |
166 | "INPUT" and "OUTPUT" methods described earlier. Output values are | |
167 | returned in the form of a Python tuple. | |
168 | ||
169 | For example, suppose you were trying to wrap the following function : | |
170 | ||
171 | void neg(double *x) { | |
172 | *x = -(*x); | |
173 | } | |
174 | ||
175 | You could wrap it with SWIG as follows : | |
176 | ||
177 | void neg(double *INOUT); | |
178 | ||
179 | or you can use the %apply directive : | |
180 | ||
181 | %apply double *INOUT { double *x }; | |
182 | void neg(double *x); | |
183 | ||
184 | Unlike C, this mapping does not directly modify the input value (since | |
185 | this makes no sense in Python). Rather, the modified input value shows | |
186 | up as the return value of the function. Thus, to apply this function | |
187 | to a Python variable you might do this : | |
188 | ||
189 | x = neg(x) | |
190 | ||
191 | Note : previous versions of SWIG used the symbol 'BOTH' to mark | |
192 | input/output arguments. This is still supported, but will be slowly | |
193 | phased out in future releases. | |
194 | ||
195 | */ | |
196 | ||
197 | %define _PYVAL_INOUT_TYPEMAP(Type) | |
198 | %typemap(in) Type *INOUT = Type *INPUT; | |
199 | %typemap(in) Type &INOUT = Type &INPUT; | |
200 | %typemap(typecheck) Type *INOUT = Type *INPUT; | |
201 | %typemap(typecheck) Type &INOUT = Type &INPUT; | |
202 | %typemap(argout) Type *INOUT = Type *OUTPUT; | |
203 | %typemap(argout) Type &INOUT = Type &OUTPUT; | |
204 | %enddef | |
205 | ||
206 | ||
207 | %define _PYPTR_INOUT_TYPEMAP(Type) | |
208 | _PYVAL_INOUT_TYPEMAP(SWIG_arg(Type)) | |
209 | %typemap(freearg) Type *INOUT = Type *INPUT; | |
210 | %typemap(freearg) Type &INOUT = Type &INPUT; | |
211 | %enddef | |
212 | ||
213 | #ifndef SWIG_INOUT_NODEF | |
214 | #define PYVAL_INPUT_TYPEMAP(code,_a,_c,_af,_cf,...) \ | |
215 | _PYVAL_INPUT_TYPEMAP(SWIG_arg(code),SWIG_arg(_a),SWIG_arg(_c), \ | |
216 | SWIG_arg(_af),SWIG_arg(_cf),SWIG_arg(__VA_ARGS__)) | |
217 | ||
218 | #define PYPTR_INPUT_TYPEMAP(code,_a,_af,...) \ | |
219 | _PYPTR_INPUT_TYPEMAP(SWIG_arg(code),SWIG_arg(_a),SWIG_arg(_af), \ | |
220 | SWIG_arg(__VA_ARGS__)) | |
221 | ||
222 | #define PYVAL_OUTPUT_TYPEMAP(_f,_ff,...) \ | |
223 | _PYVAL_OUTPUT_TYPEMAP(SWIG_arg(_f),SWIG_arg(_ff),SWIG_arg(__VA_ARGS__)) | |
224 | ||
225 | #define PYVAL_INOUT_TYPEMAP(...) _PYVAL_INOUT_TYPEMAP(SWIG_arg(__VA_ARGS__)) | |
226 | #define PYPTR_INOUT_TYPEMAP(...) _PYPTR_INOUT_TYPEMAP(SWIG_arg(__VA_ARGS__)) | |
227 | #else /* You need to include typemaps.i */ | |
228 | #define PYVAL_OUTPUT_TYPEMAP(...) | |
229 | #define PYVAL_INPUT_TYPEMAP(...) | |
230 | #define PYVAL_INOUT_TYPEMAP(...) | |
231 | #define PYPTR_INPUT_TYPEMAP(...) | |
232 | #define PYPTR_INOUT_TYPEMAP(...) | |
233 | #endif /* SWIG_INOUT_DEFAULT */ |