Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | // |
2 | // SWIG typemaps for std::vector | |
3 | // Luigi Ballabio | |
4 | // Apr 8, 2002 | |
5 | // | |
6 | // Guile implementation | |
7 | ||
8 | %include <std_common.i> | |
9 | ||
10 | // ------------------------------------------------------------------------ | |
11 | // std::vector | |
12 | // | |
13 | // The aim of all that follows would be to integrate std::vector with | |
14 | // Guile as much as possible, namely, to allow the user to pass and | |
15 | // be returned Guile vectors or lists. | |
16 | // const declarations are used to guess the intent of the function being | |
17 | // exported; therefore, the following rationale is applied: | |
18 | // | |
19 | // -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*): | |
20 | // the parameter being read-only, either a Guile sequence or a | |
21 | // previously wrapped std::vector<T> can be passed. | |
22 | // -- f(std::vector<T>&), f(std::vector<T>*): | |
23 | // the parameter must be modified; therefore, only a wrapped std::vector | |
24 | // can be passed. | |
25 | // -- std::vector<T> f(): | |
26 | // the vector is returned by copy; therefore, a Guile vector of T:s | |
27 | // is returned which is most easily used in other Guile functions | |
28 | // -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(), | |
29 | // const std::vector<T>* f(): | |
30 | // the vector is returned by reference; therefore, a wrapped std::vector | |
31 | // is returned | |
32 | // ------------------------------------------------------------------------ | |
33 | ||
34 | %{ | |
35 | #include <vector> | |
36 | #include <algorithm> | |
37 | #include <stdexcept> | |
38 | %} | |
39 | ||
40 | // exported class | |
41 | ||
42 | namespace std { | |
43 | ||
44 | template<class T> class vector { | |
45 | %typemap(in) vector<T> { | |
46 | if (gh_vector_p($input)) { | |
47 | unsigned long size = gh_vector_length($input); | |
48 | $1 = std::vector<T >(size); | |
49 | for (unsigned long i=0; i<size; i++) { | |
50 | SCM o = gh_vector_ref($input,gh_ulong2scm(i)); | |
51 | (($1_type &)$1)[i] = | |
52 | *((T*) SWIG_MustGetPtr(o,$descriptor(T *),$argnum, 0)); | |
53 | } | |
54 | } else if (gh_null_p($input)) { | |
55 | $1 = std::vector<T >(); | |
56 | } else if (gh_pair_p($input)) { | |
57 | SCM head, tail; | |
58 | $1 = std::vector<T >(); | |
59 | tail = $input; | |
60 | while (!gh_null_p(tail)) { | |
61 | head = gh_car(tail); | |
62 | tail = gh_cdr(tail); | |
63 | $1.push_back(*((T*)SWIG_MustGetPtr(head, | |
64 | $descriptor(T *), | |
65 | $argnum, 0))); | |
66 | } | |
67 | } else { | |
68 | $1 = *(($&1_type) | |
69 | SWIG_MustGetPtr($input,$&1_descriptor,$argnum, 0)); | |
70 | } | |
71 | } | |
72 | %typemap(in) const vector<T>& (std::vector<T> temp), | |
73 | const vector<T>* (std::vector<T> temp) { | |
74 | if (gh_vector_p($input)) { | |
75 | unsigned long size = gh_vector_length($input); | |
76 | temp = std::vector<T >(size); | |
77 | $1 = &temp; | |
78 | for (unsigned long i=0; i<size; i++) { | |
79 | SCM o = gh_vector_ref($input,gh_ulong2scm(i)); | |
80 | temp[i] = *((T*) SWIG_MustGetPtr(o, | |
81 | $descriptor(T *), | |
82 | $argnum, 0)); | |
83 | } | |
84 | } else if (gh_null_p($input)) { | |
85 | temp = std::vector<T >(); | |
86 | $1 = &temp; | |
87 | } else if (gh_pair_p($input)) { | |
88 | temp = std::vector<T >(); | |
89 | $1 = &temp; | |
90 | SCM head, tail; | |
91 | tail = $input; | |
92 | while (!gh_null_p(tail)) { | |
93 | head = gh_car(tail); | |
94 | tail = gh_cdr(tail); | |
95 | temp.push_back(*((T*) SWIG_MustGetPtr(head, | |
96 | $descriptor(T *), | |
97 | $argnum, 0))); | |
98 | } | |
99 | } else { | |
100 | $1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum, 0); | |
101 | } | |
102 | } | |
103 | %typemap(out) vector<T> { | |
104 | $result = gh_make_vector(gh_long2scm($1.size()),SCM_UNSPECIFIED); | |
105 | for (unsigned int i=0; i<$1.size(); i++) { | |
106 | T* x = new T((($1_type &)$1)[i]); | |
107 | gh_vector_set_x($result,gh_long2scm(i), | |
108 | SWIG_NewPointerObj(x, $descriptor(T *), 1)); | |
109 | } | |
110 | } | |
111 | %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> { | |
112 | /* native sequence? */ | |
113 | if (gh_vector_p($input)) { | |
114 | unsigned int size = gh_vector_length($input); | |
115 | if (size == 0) { | |
116 | /* an empty sequence can be of any type */ | |
117 | $1 = 1; | |
118 | } else { | |
119 | /* check the first element only */ | |
120 | SCM o = gh_vector_ref($input,gh_ulong2scm(0)); | |
121 | T* x; | |
122 | if (SWIG_ConvertPtr(o,(void**) &x, | |
123 | $descriptor(T *), 0) != -1) | |
124 | $1 = 1; | |
125 | else | |
126 | $1 = 0; | |
127 | } | |
128 | } else if (gh_null_p($input)) { | |
129 | /* again, an empty sequence can be of any type */ | |
130 | $1 = 1; | |
131 | } else if (gh_pair_p($input)) { | |
132 | /* check the first element only */ | |
133 | T* x; | |
134 | SCM head = gh_car($input); | |
135 | if (SWIG_ConvertPtr(head,(void**) &x, | |
136 | $descriptor(T *), 0) != -1) | |
137 | $1 = 1; | |
138 | else | |
139 | $1 = 0; | |
140 | } else { | |
141 | /* wrapped vector? */ | |
142 | std::vector<T >* v; | |
143 | if (SWIG_ConvertPtr($input,(void **) &v, | |
144 | $&1_descriptor, 0) != -1) | |
145 | $1 = 1; | |
146 | else | |
147 | $1 = 0; | |
148 | } | |
149 | } | |
150 | %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&, | |
151 | const vector<T>* { | |
152 | /* native sequence? */ | |
153 | if (gh_vector_p($input)) { | |
154 | unsigned int size = gh_vector_length($input); | |
155 | if (size == 0) { | |
156 | /* an empty sequence can be of any type */ | |
157 | $1 = 1; | |
158 | } else { | |
159 | /* check the first element only */ | |
160 | T* x; | |
161 | SCM o = gh_vector_ref($input,gh_ulong2scm(0)); | |
162 | if (SWIG_ConvertPtr(o,(void**) &x, | |
163 | $descriptor(T *), 0) != -1) | |
164 | $1 = 1; | |
165 | else | |
166 | $1 = 0; | |
167 | } | |
168 | } else if (gh_null_p($input)) { | |
169 | /* again, an empty sequence can be of any type */ | |
170 | $1 = 1; | |
171 | } else if (gh_pair_p($input)) { | |
172 | /* check the first element only */ | |
173 | T* x; | |
174 | SCM head = gh_car($input); | |
175 | if (SWIG_ConvertPtr(head,(void**) &x, | |
176 | $descriptor(T *), 0) != -1) | |
177 | $1 = 1; | |
178 | else | |
179 | $1 = 0; | |
180 | } else { | |
181 | /* wrapped vector? */ | |
182 | std::vector<T >* v; | |
183 | if (SWIG_ConvertPtr($input,(void **) &v, | |
184 | $1_descriptor, 0) != -1) | |
185 | $1 = 1; | |
186 | else | |
187 | $1 = 0; | |
188 | } | |
189 | } | |
190 | public: | |
191 | vector(unsigned int size = 0); | |
192 | vector(unsigned int size, const T& value); | |
193 | vector(const vector<T>&); | |
194 | %rename(length) size; | |
195 | unsigned int size() const; | |
196 | %rename("empty?") empty; | |
197 | bool empty() const; | |
198 | %rename("clear!") clear; | |
199 | void clear(); | |
200 | %rename("set!") set; | |
201 | %rename("pop!") pop; | |
202 | %rename("push!") push_back; | |
203 | void push_back(const T& x); | |
204 | %extend { | |
205 | T pop() throw (std::out_of_range) { | |
206 | if (self->size() == 0) | |
207 | throw std::out_of_range("pop from empty vector"); | |
208 | T x = self->back(); | |
209 | self->pop_back(); | |
210 | return x; | |
211 | } | |
212 | T& ref(int i) throw (std::out_of_range) { | |
213 | int size = int(self->size()); | |
214 | if (i>=0 && i<size) | |
215 | return (*self)[i]; | |
216 | else | |
217 | throw std::out_of_range("vector index out of range"); | |
218 | } | |
219 | void set(int i, const T& x) throw (std::out_of_range) { | |
220 | int size = int(self->size()); | |
221 | if (i>=0 && i<size) | |
222 | (*self)[i] = x; | |
223 | else | |
224 | throw std::out_of_range("vector index out of range"); | |
225 | } | |
226 | } | |
227 | }; | |
228 | ||
229 | ||
230 | // specializations for built-ins | |
231 | %define specialize_stl_vector(T,CHECK,CONVERT_FROM,CONVERT_TO) | |
232 | template<> class vector<T> { | |
233 | %typemap(in) vector<T> { | |
234 | if (gh_vector_p($input)) { | |
235 | unsigned long size = gh_vector_length($input); | |
236 | $1 = std::vector<T >(size); | |
237 | for (unsigned long i=0; i<size; i++) { | |
238 | SCM o = gh_vector_ref($input,gh_ulong2scm(i)); | |
239 | if (CHECK(o)) | |
240 | (($1_type &)$1)[i] = (T)(CONVERT_FROM(o)); | |
241 | else | |
242 | scm_wrong_type_arg(FUNC_NAME, $argnum, $input); | |
243 | } | |
244 | } else if (gh_null_p($input)) { | |
245 | $1 = std::vector<T >(); | |
246 | } else if (gh_pair_p($input)) { | |
247 | SCM v = gh_list_to_vector($input); | |
248 | unsigned long size = gh_vector_length(v); | |
249 | $1 = std::vector<T >(size); | |
250 | for (unsigned long i=0; i<size; i++) { | |
251 | SCM o = gh_vector_ref(v,gh_ulong2scm(i)); | |
252 | if (CHECK(o)) | |
253 | (($1_type &)$1)[i] = (T)(CONVERT_FROM(o)); | |
254 | else | |
255 | scm_wrong_type_arg(FUNC_NAME, $argnum, $input); | |
256 | } | |
257 | } else { | |
258 | $1 = *(($&1_type) | |
259 | SWIG_MustGetPtr($input,$&1_descriptor,$argnum, 0)); | |
260 | } | |
261 | } | |
262 | %typemap(in) const vector<T>& (std::vector<T> temp), | |
263 | const vector<T>* (std::vector<T> temp) { | |
264 | if (gh_vector_p($input)) { | |
265 | unsigned long size = gh_vector_length($input); | |
266 | temp = std::vector<T >(size); | |
267 | $1 = &temp; | |
268 | for (unsigned long i=0; i<size; i++) { | |
269 | SCM o = gh_vector_ref($input,gh_ulong2scm(i)); | |
270 | if (CHECK(o)) | |
271 | temp[i] = (T)(CONVERT_FROM(o)); | |
272 | else | |
273 | scm_wrong_type_arg(FUNC_NAME, $argnum, $input); | |
274 | } | |
275 | } else if (gh_null_p($input)) { | |
276 | temp = std::vector<T >(); | |
277 | $1 = &temp; | |
278 | } else if (gh_pair_p($input)) { | |
279 | SCM v = gh_list_to_vector($input); | |
280 | unsigned long size = gh_vector_length(v); | |
281 | temp = std::vector<T >(size); | |
282 | $1 = &temp; | |
283 | for (unsigned long i=0; i<size; i++) { | |
284 | SCM o = gh_vector_ref(v,gh_ulong2scm(i)); | |
285 | if (CHECK(o)) | |
286 | temp[i] = (T)(CONVERT_FROM(o)); | |
287 | else | |
288 | scm_wrong_type_arg(FUNC_NAME, $argnum, $input); | |
289 | } | |
290 | } else { | |
291 | $1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum, 0); | |
292 | } | |
293 | } | |
294 | %typemap(out) vector<T> { | |
295 | $result = gh_make_vector(gh_long2scm($1.size()),SCM_UNSPECIFIED); | |
296 | for (unsigned int i=0; i<$1.size(); i++) { | |
297 | SCM x = CONVERT_TO((($1_type &)$1)[i]); | |
298 | gh_vector_set_x($result,gh_long2scm(i),x); | |
299 | } | |
300 | } | |
301 | %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> { | |
302 | /* native sequence? */ | |
303 | if (gh_vector_p($input)) { | |
304 | unsigned int size = gh_vector_length($input); | |
305 | if (size == 0) { | |
306 | /* an empty sequence can be of any type */ | |
307 | $1 = 1; | |
308 | } else { | |
309 | /* check the first element only */ | |
310 | T* x; | |
311 | SCM o = gh_vector_ref($input,gh_ulong2scm(0)); | |
312 | $1 = CHECK(o) ? 1 : 0; | |
313 | } | |
314 | } else if (gh_null_p($input)) { | |
315 | /* again, an empty sequence can be of any type */ | |
316 | $1 = 1; | |
317 | } else if (gh_pair_p($input)) { | |
318 | /* check the first element only */ | |
319 | T* x; | |
320 | SCM head = gh_car($input); | |
321 | $1 = CHECK(head) ? 1 : 0; | |
322 | } else { | |
323 | /* wrapped vector? */ | |
324 | std::vector<T >* v; | |
325 | $1 = (SWIG_ConvertPtr($input,(void **) &v, | |
326 | $&1_descriptor, 0) != -1) ? 1 : 0; | |
327 | } | |
328 | } | |
329 | %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&, | |
330 | const vector<T>* { | |
331 | /* native sequence? */ | |
332 | if (gh_vector_p($input)) { | |
333 | unsigned int size = gh_vector_length($input); | |
334 | if (size == 0) { | |
335 | /* an empty sequence can be of any type */ | |
336 | $1 = 1; | |
337 | } else { | |
338 | /* check the first element only */ | |
339 | T* x; | |
340 | SCM o = gh_vector_ref($input,gh_ulong2scm(0)); | |
341 | $1 = CHECK(o) ? 1 : 0; | |
342 | } | |
343 | } else if (gh_null_p($input)) { | |
344 | /* again, an empty sequence can be of any type */ | |
345 | $1 = 1; | |
346 | } else if (gh_pair_p($input)) { | |
347 | /* check the first element only */ | |
348 | T* x; | |
349 | SCM head = gh_car($input); | |
350 | $1 = CHECK(head) ? 1 : 0; | |
351 | } else { | |
352 | /* wrapped vector? */ | |
353 | std::vector<T >* v; | |
354 | $1 = (SWIG_ConvertPtr($input,(void **) &v, | |
355 | $1_descriptor, 0) != -1) ? 1 : 0; | |
356 | } | |
357 | } | |
358 | public: | |
359 | vector(unsigned int size = 0); | |
360 | vector(unsigned int size, const T& value); | |
361 | vector(const vector<T>&); | |
362 | %rename(length) size; | |
363 | unsigned int size() const; | |
364 | %rename("empty?") empty; | |
365 | bool empty() const; | |
366 | %rename("clear!") clear; | |
367 | void clear(); | |
368 | %rename("set!") set; | |
369 | %rename("pop!") pop; | |
370 | %rename("push!") push_back; | |
371 | void push_back(T x); | |
372 | %extend { | |
373 | T pop() throw (std::out_of_range) { | |
374 | if (self->size() == 0) | |
375 | throw std::out_of_range("pop from empty vector"); | |
376 | T x = self->back(); | |
377 | self->pop_back(); | |
378 | return x; | |
379 | } | |
380 | T ref(int i) throw (std::out_of_range) { | |
381 | int size = int(self->size()); | |
382 | if (i>=0 && i<size) | |
383 | return (*self)[i]; | |
384 | else | |
385 | throw std::out_of_range("vector index out of range"); | |
386 | } | |
387 | void set(int i, T x) throw (std::out_of_range) { | |
388 | int size = int(self->size()); | |
389 | if (i>=0 && i<size) | |
390 | (*self)[i] = x; | |
391 | else | |
392 | throw std::out_of_range("vector index out of range"); | |
393 | } | |
394 | } | |
395 | }; | |
396 | %enddef | |
397 | ||
398 | specialize_stl_vector(bool,gh_boolean_p,gh_scm2bool,SWIG_bool2scm); | |
399 | specialize_stl_vector(char,gh_number_p,gh_scm2long,gh_long2scm); | |
400 | specialize_stl_vector(int,gh_number_p,gh_scm2long,gh_long2scm); | |
401 | specialize_stl_vector(long,gh_number_p,gh_scm2long,gh_long2scm); | |
402 | specialize_stl_vector(short,gh_number_p,gh_scm2long,gh_long2scm); | |
403 | specialize_stl_vector(unsigned char,gh_number_p,gh_scm2ulong,gh_ulong2scm); | |
404 | specialize_stl_vector(unsigned int,gh_number_p,gh_scm2ulong,gh_ulong2scm); | |
405 | specialize_stl_vector(unsigned long,gh_number_p,gh_scm2ulong,gh_ulong2scm); | |
406 | specialize_stl_vector(unsigned short,gh_number_p,gh_scm2ulong,gh_ulong2scm); | |
407 | specialize_stl_vector(float,gh_number_p,gh_scm2double,gh_double2scm); | |
408 | specialize_stl_vector(double,gh_number_p,gh_scm2double,gh_double2scm); | |
409 | specialize_stl_vector(std::string,gh_string_p,SWIG_scm2string,SWIG_string2scm); | |
410 | } | |
411 |