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 * SWIG typemaps for std::vector
8 * ----------------------------------------------------------------------------- */
10 %include <std_common.i>
12 // ------------------------------------------------------------------------
15 // The aim of all that follows would be to integrate std::vector with
16 // MzScheme as much as possible, namely, to allow the user to pass and
17 // be returned MzScheme vectors or lists.
18 // const declarations are used to guess the intent of the function being
19 // exported; therefore, the following rationale is applied:
21 // -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
22 // the parameter being read-only, either a MzScheme sequence or a
23 // previously wrapped std::vector<T> can be passed.
24 // -- f(std::vector<T>&), f(std::vector<T>*):
25 // the parameter must be modified; therefore, only a wrapped std::vector
27 // -- std::vector<T> f():
28 // the vector is returned by copy; therefore, a MzScheme vector of T:s
29 // is returned which is most easily used in other MzScheme functions
30 // -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(),
31 // const std::vector<T>* f():
32 // the vector is returned by reference; therefore, a wrapped std::vector
34 // ------------------------------------------------------------------------
46 template<class T> class vector {
47 %typemap(in) vector<T> {
48 if (SCHEME_VECTORP($input)) {
49 unsigned int size = SCHEME_VEC_SIZE($input);
50 $1 = std::vector<T >(size);
51 Scheme_Object** items = SCHEME_VEC_ELS($input);
52 for (unsigned int i=0; i<size; i++) {
54 *((T*) SWIG_MustGetPtr(items[i],
58 } else if (SCHEME_NULLP($input)) {
59 $1 = std::vector<T >();
60 } else if (SCHEME_PAIRP($input)) {
61 Scheme_Object *head, *tail;
62 $1 = std::vector<T >();
64 while (!SCHEME_NULLP(tail)) {
65 head = scheme_car(tail);
66 tail = scheme_cdr(tail);
67 $1.push_back(*((T*)SWIG_MustGetPtr(head,
73 SWIG_MustGetPtr($input,$&1_descriptor,$argnum, 0));
76 %typemap(in) const vector<T>& (std::vector<T> temp),
77 const vector<T>* (std::vector<T> temp) {
78 if (SCHEME_VECTORP($input)) {
79 unsigned int size = SCHEME_VEC_SIZE($input);
80 temp = std::vector<T >(size);
82 Scheme_Object** items = SCHEME_VEC_ELS($input);
83 for (unsigned int i=0; i<size; i++) {
84 temp[i] = *((T*) SWIG_MustGetPtr(items[i],
88 } else if (SCHEME_NULLP($input)) {
89 temp = std::vector<T >();
91 } else if (SCHEME_PAIRP($input)) {
92 temp = std::vector<T >();
94 Scheme_Object *head, *tail;
96 while (!SCHEME_NULLP(tail)) {
97 head = scheme_car(tail);
98 tail = scheme_cdr(tail);
99 temp.push_back(*((T*) SWIG_MustGetPtr(head,
104 $1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum, 0);
107 %typemap(out) vector<T> {
108 $result = scheme_make_vector($1.size(),scheme_undefined);
109 Scheme_Object** els = SCHEME_VEC_ELS($result);
110 for (unsigned int i=0; i<$1.size(); i++) {
111 T* x = new T((($1_type &)$1)[i]);
112 els[i] = SWIG_NewPointerObj(x,$descriptor(T *), 1);
115 %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
116 /* native sequence? */
117 if (SCHEME_VECTORP($input)) {
118 unsigned int size = SCHEME_VEC_SIZE($input);
120 /* an empty sequence can be of any type */
123 /* check the first element only */
125 Scheme_Object** items = SCHEME_VEC_ELS($input);
126 if (SWIG_ConvertPtr(items[0],(void**) &x,
127 $descriptor(T *), 0) != -1)
132 } else if (SCHEME_NULLP($input)) {
133 /* again, an empty sequence can be of any type */
135 } else if (SCHEME_PAIRP($input)) {
136 /* check the first element only */
138 Scheme_Object *head = scheme_car($input);
139 if (SWIG_ConvertPtr(head,(void**) &x,
140 $descriptor(T *), 0) != -1)
145 /* wrapped vector? */
147 if (SWIG_ConvertPtr($input,(void **) &v,
148 $&1_descriptor, 0) != -1)
154 %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
156 /* native sequence? */
157 if (SCHEME_VECTORP($input)) {
158 unsigned int size = SCHEME_VEC_SIZE($input);
160 /* an empty sequence can be of any type */
163 /* check the first element only */
165 Scheme_Object** items = SCHEME_VEC_ELS($input);
166 if (SWIG_ConvertPtr(items[0],(void**) &x,
167 $descriptor(T *), 0) != -1)
172 } else if (SCHEME_NULLP($input)) {
173 /* again, an empty sequence can be of any type */
175 } else if (SCHEME_PAIRP($input)) {
176 /* check the first element only */
178 Scheme_Object *head = scheme_car($input);
179 if (SWIG_ConvertPtr(head,(void**) &x,
180 $descriptor(T *), 0) != -1)
185 /* wrapped vector? */
187 if (SWIG_ConvertPtr($input,(void **) &v,
188 $1_descriptor, 0) != -1)
195 vector(unsigned int size = 0);
196 vector(unsigned int size, const T& value);
197 vector(const vector<T>&);
198 %rename(length) size;
199 unsigned int size() const;
200 %rename("empty?") empty;
202 %rename("clear!") clear;
206 %rename("push!") push_back;
207 void push_back(const T& x);
209 T pop() throw (std::out_of_range) {
210 if (self->size() == 0)
211 throw std::out_of_range("pop from empty vector");
216 T& ref(int i) throw (std::out_of_range) {
217 int size = int(self->size());
221 throw std::out_of_range("vector index out of range");
223 void set(int i, const T& x) throw (std::out_of_range) {
224 int size = int(self->size());
228 throw std::out_of_range("vector index out of range");
234 // specializations for built-ins
236 %define specialize_std_vector(T,CHECK,CONVERT_FROM,CONVERT_TO)
237 template<> class vector<T> {
238 %typemap(in) vector<T> {
239 if (SCHEME_VECTORP($input)) {
240 unsigned int size = SCHEME_VEC_SIZE($input);
241 $1 = std::vector<T >(size);
242 Scheme_Object** items = SCHEME_VEC_ELS($input);
243 for (unsigned int i=0; i<size; i++) {
244 Scheme_Object* o = items[i];
246 (($1_type &)$1)[i] = (T)(CONVERT_FROM(o));
248 scheme_wrong_type(FUNC_NAME, "vector<" #T ">",
249 $argnum - 1, argc, argv);
251 } else if (SCHEME_NULLP($input)) {
252 $1 = std::vector<T >();
253 } else if (SCHEME_PAIRP($input)) {
254 Scheme_Object *head, *tail;
255 $1 = std::vector<T >();
257 while (!SCHEME_NULLP(tail)) {
258 head = scheme_car(tail);
259 tail = scheme_cdr(tail);
261 $1.push_back((T)(CONVERT_FROM(head)));
263 scheme_wrong_type(FUNC_NAME, "vector<" #T ">",
264 $argnum - 1, argc, argv);
268 SWIG_MustGetPtr($input,$&1_descriptor,$argnum, 0));
271 %typemap(in) const vector<T>& (std::vector<T> temp),
272 const vector<T>* (std::vector<T> temp) {
273 if (SCHEME_VECTORP($input)) {
274 unsigned int size = SCHEME_VEC_SIZE($input);
275 temp = std::vector<T >(size);
277 Scheme_Object** items = SCHEME_VEC_ELS($input);
278 for (unsigned int i=0; i<size; i++) {
279 Scheme_Object* o = items[i];
281 temp[i] = (T)(CONVERT_FROM(o));
283 scheme_wrong_type(FUNC_NAME, "vector<" #T ">",
284 $argnum - 1, argc, argv);
286 } else if (SCHEME_NULLP($input)) {
287 temp = std::vector<T >();
289 } else if (SCHEME_PAIRP($input)) {
290 temp = std::vector<T >();
292 Scheme_Object *head, *tail;
294 while (!SCHEME_NULLP(tail)) {
295 head = scheme_car(tail);
296 tail = scheme_cdr(tail);
298 temp.push_back((T)(CONVERT_FROM(head)));
300 scheme_wrong_type(FUNC_NAME, "vector<" #T ">",
301 $argnum - 1, argc, argv);
304 $1 = ($1_ltype) SWIG_MustGetPtr($input,$1_descriptor,$argnum - 1, 0);
307 %typemap(out) vector<T> {
308 $result = scheme_make_vector($1.size(),scheme_undefined);
309 Scheme_Object** els = SCHEME_VEC_ELS($result);
310 for (unsigned int i=0; i<$1.size(); i++)
311 els[i] = CONVERT_TO((($1_type &)$1)[i]);
313 %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
314 /* native sequence? */
315 if (SCHEME_VECTORP($input)) {
316 unsigned int size = SCHEME_VEC_SIZE($input);
318 /* an empty sequence can be of any type */
321 /* check the first element only */
323 Scheme_Object** items = SCHEME_VEC_ELS($input);
324 $1 = CHECK(items[0]) ? 1 : 0;
326 } else if (SCHEME_NULLP($input)) {
327 /* again, an empty sequence can be of any type */
329 } else if (SCHEME_PAIRP($input)) {
330 /* check the first element only */
332 Scheme_Object *head = scheme_car($input);
333 $1 = CHECK(head) ? 1 : 0;
335 /* wrapped vector? */
337 $1 = (SWIG_ConvertPtr($input,(void **) &v,
338 $&1_descriptor, 0) != -1) ? 1 : 0;
341 %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
343 /* native sequence? */
344 if (SCHEME_VECTORP($input)) {
345 unsigned int size = SCHEME_VEC_SIZE($input);
347 /* an empty sequence can be of any type */
350 /* check the first element only */
352 Scheme_Object** items = SCHEME_VEC_ELS($input);
353 $1 = CHECK(items[0]) ? 1 : 0;
355 } else if (SCHEME_NULLP($input)) {
356 /* again, an empty sequence can be of any type */
358 } else if (SCHEME_PAIRP($input)) {
359 /* check the first element only */
361 Scheme_Object *head = scheme_car($input);
362 $1 = CHECK(head) ? 1 : 0;
364 /* wrapped vector? */
366 $1 = (SWIG_ConvertPtr($input,(void **) &v,
367 $1_descriptor, 0) != -1) ? 1 : 0;
371 vector(unsigned int size = 0);
372 vector(unsigned int size, const T& value);
373 vector(const vector<T>&);
374 %rename(length) size;
375 unsigned int size() const;
376 %rename("empty?") empty;
378 %rename("clear!") clear;
382 %rename("push!") push_back;
385 T pop() throw (std::out_of_range) {
386 if (self->size() == 0)
387 throw std::out_of_range("pop from empty vector");
392 T ref(int i) throw (std::out_of_range) {
393 int size = int(self->size());
397 throw std::out_of_range("vector index out of range");
399 void set(int i, T x) throw (std::out_of_range) {
400 int size = int(self->size());
404 throw std::out_of_range("vector index out of range");
410 specialize_std_vector(bool,SCHEME_BOOLP,SCHEME_TRUEP,\
412 specialize_std_vector(char,SCHEME_INTP,SCHEME_INT_VAL,\
413 scheme_make_integer_value);
414 specialize_std_vector(int,SCHEME_INTP,SCHEME_INT_VAL,\
415 scheme_make_integer_value);
416 specialize_std_vector(short,SCHEME_INTP,SCHEME_INT_VAL,\
417 scheme_make_integer_value);
418 specialize_std_vector(long,SCHEME_INTP,SCHEME_INT_VAL,\
419 scheme_make_integer_value);
420 specialize_std_vector(unsigned char,SCHEME_INTP,SCHEME_INT_VAL,\
421 scheme_make_integer_value);
422 specialize_std_vector(unsigned int,SCHEME_INTP,SCHEME_INT_VAL,\
423 scheme_make_integer_value);
424 specialize_std_vector(unsigned short,SCHEME_INTP,SCHEME_INT_VAL,\
425 scheme_make_integer_value);
426 specialize_std_vector(unsigned long,SCHEME_INTP,SCHEME_INT_VAL,\
427 scheme_make_integer_value);
428 specialize_std_vector(float,SCHEME_REALP,scheme_real_to_double,\
430 specialize_std_vector(double,SCHEME_REALP,scheme_real_to_double,\
432 specialize_std_vector(std::string,SCHEME_STRINGP,swig_scm_to_string,\