~ubuntu-branches/ubuntu/oneiric/swig1.3/oneiric

« back to all changes in this revision

Viewing changes to Lib/ruby/std_vector.i

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-12-06 10:27:08 UTC
  • mfrom: (1.2.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20071206102708-t37t62i45n595w0e
Tags: 1.3.33-2ubuntu1
* Merge with Debian; remaining changes:
  - Drop support for pike.
  - Use python2.5 instead of python2.4.
  - Clean Runtime/ as well.
  - Force a few environment variables.
* debian/Rules (clean): Remove Lib/ocaml/swigp4.ml.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.
4
 
 *
5
 
 * std_vector.i
6
 
 *
7
 
 * SWIG typemaps for std::vector
8
 
 * ----------------------------------------------------------------------------- */
9
 
 
10
 
%include <std_common.i>
11
 
 
12
 
// ------------------------------------------------------------------------
13
 
// std::vector
14
 
// 
15
 
// The aim of all that follows would be to integrate std::vector with 
16
 
// Ruby as much as possible, namely, to allow the user to pass and 
17
 
// be returned Ruby arrays
18
 
// const declarations are used to guess the intent of the function being
19
 
// exported; therefore, the following rationale is applied:
20
 
// 
21
 
//   -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*):
22
 
//      the parameter being read-only, either a Ruby array 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
26
 
//      can be passed.
27
 
//   -- std::vector<T> f():
28
 
//      the vector is returned by copy; therefore, a Ruby array of T:s 
29
 
//      is returned which is most easily used in other Ruby 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
33
 
//      is returned
34
 
// ------------------------------------------------------------------------
35
 
 
 
1
/*
 
2
  Vectors
 
3
*/
 
4
 
 
5
%fragment("StdVectorTraits","header",fragment="StdSequenceTraits")
36
6
%{
37
 
#include <vector>
38
 
#include <algorithm>
39
 
#include <stdexcept>
 
7
  namespace swig {
 
8
    template <class T>
 
9
    struct traits_asptr<std::vector<T> >  {
 
10
      static int asptr(VALUE obj, std::vector<T> **vec) {
 
11
        return traits_asptr_stdseq<std::vector<T> >::asptr(obj, vec);
 
12
      }
 
13
    };
 
14
    
 
15
    template <class T>
 
16
    struct traits_from<std::vector<T> > {
 
17
      static VALUE from(const std::vector<T>& vec) {
 
18
        return traits_from_stdseq<std::vector<T> >::from(vec);
 
19
      }
 
20
    };
 
21
  }
40
22
%}
41
23
 
42
 
// exported class
43
 
 
44
 
namespace std {
45
 
 
46
 
    %mixin vector "Enumerable";
47
 
 
48
 
    template<class T> class vector {
49
 
        %typemap(in) vector<T> {
50
 
            if (rb_obj_is_kind_of($input,rb_cArray)) {
51
 
                unsigned int size = RARRAY_LEN($input);
52
 
                $1;
53
 
                for (unsigned int i=0; i<size; i++) {
54
 
                    VALUE o = RARRAY_PTR($input)[i];
55
 
                    T* x;
56
 
                    SWIG_ConvertPtr(o, (void **) &x, $descriptor(T *), 1);
57
 
                    $1.push_back(*x);
58
 
                }
59
 
            } else {
60
 
                void *ptr;
61
 
                SWIG_ConvertPtr($input, &ptr, $&1_descriptor, 1);
62
 
                $1 = *(($&1_type) ptr);
63
 
            }
64
 
        }
65
 
        %typemap(in) const vector<T>& (std::vector<T> temp),
66
 
                     const vector<T>* (std::vector<T> temp) {
67
 
            if (rb_obj_is_kind_of($input,rb_cArray)) {
68
 
                unsigned int size = RARRAY_LEN($input);
69
 
                $1 = &temp;
70
 
                for (unsigned int i=0; i<size; i++) {
71
 
                    VALUE o = RARRAY_PTR($input)[i];
72
 
                    T* x;
73
 
                    SWIG_ConvertPtr(o, (void **) &x, $descriptor(T *), 1);
74
 
                    temp.push_back(*x);
75
 
                }
76
 
            } else {
77
 
                SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, 1);
78
 
            }
79
 
        }
80
 
        %typemap(out) vector<T> {
81
 
            $result = rb_ary_new2($1.size());
82
 
            for (unsigned int i=0; i<$1.size(); i++) {
83
 
                T* x = new T((($1_type &)$1)[i]);
84
 
                rb_ary_store($result,i,
85
 
                             SWIG_NewPointerObj((void *) x, 
86
 
                                                $descriptor(T *), 1));
87
 
            }
88
 
        }
89
 
        %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
90
 
            /* native sequence? */
91
 
            if (rb_obj_is_kind_of($input,rb_cArray)) {
92
 
                unsigned int size = RARRAY_LEN($input);
93
 
                if (size == 0) {
94
 
                    /* an empty sequence can be of any type */
95
 
                    $1 = 1;
96
 
                } else {
97
 
                    /* check the first element only */
98
 
                    T* x;
99
 
                    VALUE o = RARRAY_PTR($input)[0];
100
 
                    if ((SWIG_ConvertPtr(o,(void **) &x, 
101
 
                                         $descriptor(T *),0)) != -1)
102
 
                        $1 = 1;
103
 
                    else
104
 
                        $1 = 0;
105
 
                }
106
 
            } else {
107
 
                /* wrapped vector? */
108
 
                std::vector<T >* v;
109
 
                if (SWIG_ConvertPtr($input,(void **) &v, 
110
 
                                    $&1_descriptor,0) != -1)
111
 
                    $1 = 1;
112
 
                else
113
 
                    $1 = 0;
114
 
            }
115
 
        }
116
 
        %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
117
 
                                          const vector<T>* {
118
 
            /* native sequence? */
119
 
            if (rb_obj_is_kind_of($input,rb_cArray)) {
120
 
                unsigned int size = RARRAY_LEN($input);
121
 
                if (size == 0) {
122
 
                    /* an empty sequence can be of any type */
123
 
                    $1 = 1;
124
 
                } else {
125
 
                    /* check the first element only */
126
 
                    T* x;
127
 
                    VALUE o = RARRAY_PTR($input)[0];
128
 
                    if ((SWIG_ConvertPtr(o,(void **) &x, 
129
 
                                         $descriptor(T *),0)) != -1)
130
 
                        $1 = 1;
131
 
                    else
132
 
                        $1 = 0;
133
 
                }
134
 
            } else {
135
 
                /* wrapped vector? */
136
 
                std::vector<T >* v;
137
 
                if (SWIG_ConvertPtr($input,(void **) &v, 
138
 
                                    $1_descriptor,0) != -1)
139
 
                    $1 = 1;
140
 
                else
141
 
                    $1 = 0;
142
 
            }
143
 
        }
144
 
      public:
145
 
        vector();
146
 
        vector(unsigned int size);
147
 
        vector(unsigned int size, const T& value);
148
 
        vector(const vector<T> &);
149
 
 
150
 
        %rename(__len__) size;
151
 
        unsigned int size() const;
152
 
        %rename("empty?") empty;
153
 
        bool empty() const;
154
 
        void clear();
155
 
        %rename(push) push_back;
156
 
        void push_back(const T& x);
157
 
        %extend {
158
 
            T pop() throw (std::out_of_range) {
159
 
                if (self->size() == 0)
160
 
                    throw std::out_of_range("pop from empty vector");
161
 
                T x = self->back();
162
 
                self->pop_back();
163
 
                return x;
164
 
            }
165
 
            T& __getitem__(int i) throw (std::out_of_range) {
166
 
                int size = int(self->size());
167
 
                if (i<0) i += size;
168
 
                if (i>=0 && i<size)
169
 
                    return (*self)[i];
170
 
                else
171
 
                    throw std::out_of_range("vector index out of range");
172
 
            }
173
 
            void __setitem__(int i, const T& x) throw (std::out_of_range) {
174
 
                int size = int(self->size());
175
 
                if (i<0) i+= size;
176
 
                if (i>=0 && i<size)
177
 
                    (*self)[i] = x;
178
 
                else
179
 
                    throw std::out_of_range("vector index out of range");
180
 
            }
181
 
            void each() {
182
 
                for (unsigned int i=0; i<self->size(); i++) {
183
 
                    T* x = &((*self)[i]);
184
 
                    rb_yield(SWIG_NewPointerObj((void *) x, 
185
 
                                                $descriptor(T *), 0));
186
 
                }
187
 
            }
188
 
        }
189
 
    };
190
 
 
191
 
    // Partial specialization for vectors of pointers.  [ beazley ]
192
 
 
193
 
    %mixin vector<T*> "Enumerable";
194
 
    template<class T> class vector<T*> {
195
 
        %typemap(in) vector<T*> {
196
 
            if (rb_obj_is_kind_of($input,rb_cArray)) {
197
 
                unsigned int size = RARRAY_LEN($input);
198
 
                $1 = std::vector<T* >(size);
199
 
                for (unsigned int i=0; i<size; i++) {
200
 
                    VALUE o = RARRAY_PTR($input)[i];
201
 
                    T* x;
202
 
                    SWIG_ConvertPtr(o, (void **) &x, $descriptor(T *), 1);
203
 
                    (($1_type &)$1)[i] = x;
204
 
                }
205
 
            } else {
206
 
                void *ptr;
207
 
                SWIG_ConvertPtr($input, &ptr, $&1_descriptor, 1);
208
 
                $1 = *(($&1_type) ptr);
209
 
            }
210
 
        }
211
 
        %typemap(in) const vector<T*>& (std::vector<T*> temp),
212
 
                     const vector<T*>* (std::vector<T*> temp) {
213
 
            if (rb_obj_is_kind_of($input,rb_cArray)) {
214
 
                unsigned int size = RARRAY_LEN($input);
215
 
                temp = std::vector<T* >(size);
216
 
                $1 = &temp;
217
 
                for (unsigned int i=0; i<size; i++) {
218
 
                    VALUE o = RARRAY_PTR($input)[i];
219
 
                    T* x;
220
 
                    SWIG_ConvertPtr(o, (void **) &x, $descriptor(T *), 1);
221
 
                    temp[i] = x;
222
 
                }
223
 
            } else {
224
 
                SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, 1);
225
 
            }
226
 
        }
227
 
        %typemap(out) vector<T*> {
228
 
            $result = rb_ary_new2($1.size());
229
 
            for (unsigned int i=0; i<$1.size(); i++) {
230
 
                T* x = (($1_type &)$1)[i];
231
 
                rb_ary_store($result,i,
232
 
                             SWIG_NewPointerObj((void *) x, 
233
 
                                                $descriptor(T *), 0));
234
 
            }
235
 
        }
236
 
        %typecheck(SWIG_TYPECHECK_VECTOR) vector<T*> {
237
 
            /* native sequence? */
238
 
            if (rb_obj_is_kind_of($input,rb_cArray)) {
239
 
                unsigned int size = RARRAY_LEN($input);
240
 
                if (size == 0) {
241
 
                    /* an empty sequence can be of any type */
242
 
                    $1 = 1;
243
 
                } else {
244
 
                    /* check the first element only */
245
 
                    T* x;
246
 
                    VALUE o = RARRAY_PTR($input)[0];
247
 
                    if ((SWIG_ConvertPtr(o,(void **) &x, 
248
 
                                         $descriptor(T *),0)) != -1)
249
 
                        $1 = 1;
250
 
                    else
251
 
                        $1 = 0;
252
 
                }
253
 
            } else {
254
 
                /* wrapped vector? */
255
 
                std::vector<T* >* v;
256
 
                if (SWIG_ConvertPtr($input,(void **) &v, 
257
 
                                    $&1_descriptor,0) != -1)
258
 
                    $1 = 1;
259
 
                else
260
 
                    $1 = 0;
261
 
            }
262
 
        }
263
 
        %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T*>&,
264
 
                                          const vector<T*>* {
265
 
            /* native sequence? */
266
 
            if (rb_obj_is_kind_of($input,rb_cArray)) {
267
 
                unsigned int size = RARRAY_LEN($input);
268
 
                if (size == 0) {
269
 
                    /* an empty sequence can be of any type */
270
 
                    $1 = 1;
271
 
                } else {
272
 
                    /* check the first element only */
273
 
                    T* x;
274
 
                    VALUE o = RARRAY_PTR($input)[0];
275
 
                    if ((SWIG_ConvertPtr(o,(void **) &x, 
276
 
                                         $descriptor(T *),0)) != -1)
277
 
                        $1 = 1;
278
 
                    else
279
 
                        $1 = 0;
280
 
                }
281
 
            } else {
282
 
                /* wrapped vector? */
283
 
                std::vector<T* >* v;
284
 
                if (SWIG_ConvertPtr($input,(void **) &v, 
285
 
                                    $1_descriptor,0) != -1)
286
 
                    $1 = 1;
287
 
                else
288
 
                    $1 = 0;
289
 
            }
290
 
        }
291
 
      public:
292
 
        vector();
293
 
        vector(unsigned int size);
294
 
        vector(unsigned int size, T * &value);
295
 
        vector(const vector<T*> &);
296
 
 
297
 
        %rename(__len__) size;
298
 
        unsigned int size() const;
299
 
        %rename("empty?") empty;
300
 
        bool empty() const;
301
 
        void clear();
302
 
        %rename(push) push_back;
303
 
        void push_back(T* x);
304
 
        %extend {
305
 
            T* pop() throw (std::out_of_range) {
306
 
                if (self->size() == 0)
307
 
                    throw std::out_of_range("pop from empty vector");
308
 
                T* x = self->back();
309
 
                self->pop_back();
310
 
                return x;
311
 
            }
312
 
            T* __getitem__(int i) throw (std::out_of_range) {
313
 
                int size = int(self->size());
314
 
                if (i<0) i += size;
315
 
                if (i>=0 && i<size)
316
 
                    return (*self)[i];
317
 
                else
318
 
                    throw std::out_of_range("vector index out of range");
319
 
            }
320
 
            void __setitem__(int i, T* x) throw (std::out_of_range) {
321
 
                int size = int(self->size());
322
 
                if (i<0) i+= size;
323
 
                if (i>=0 && i<size)
324
 
                    (*self)[i] = x;
325
 
                else
326
 
                    throw std::out_of_range("vector index out of range");
327
 
            }
328
 
            void each() {
329
 
                for (unsigned int i=0; i<self->size(); i++) {
330
 
                    T* x = (*self)[i];
331
 
                    rb_yield(SWIG_NewPointerObj((void *) x, 
332
 
                                                $descriptor(T *), 0));
333
 
                }
334
 
            }
335
 
        }
336
 
    };
337
 
        
338
 
 
339
 
    // specializations for built-ins
340
 
 
341
 
    %define specialize_std_vector(T,CHECK,CONVERT_FROM,CONVERT_TO)
342
 
    %mixin vector<T> "Enumerable";
343
 
    template<> class vector<T> {
344
 
        %typemap(in) vector<T> {
345
 
            if (rb_obj_is_kind_of($input,rb_cArray)) {
346
 
                unsigned int size = RARRAY_LEN($input);
347
 
                $1 = std::vector<T >(size);
348
 
                for (unsigned int i=0; i<size; i++) {
349
 
                    VALUE o = RARRAY_PTR($input)[i];
350
 
                    if (CHECK(o))
351
 
                        (($1_type &)$1)[i] = (T)(CONVERT_FROM(o));
352
 
                    else
353
 
                        rb_raise(rb_eTypeError,
354
 
                                 "wrong argument type"
355
 
                                 " (expected vector<" #T ">)");
356
 
                }
357
 
            } else {
358
 
                void *ptr;
359
 
                SWIG_ConvertPtr($input, &ptr, $&1_descriptor, 1);
360
 
                $1 = *(($&1_type) ptr);
361
 
            }
362
 
        }
363
 
        %typemap(in) const vector<T>& (std::vector<T> temp),
364
 
                     const vector<T>* (std::vector<T> temp) {
365
 
            if (rb_obj_is_kind_of($input,rb_cArray)) {
366
 
                unsigned int size = RARRAY_LEN($input);
367
 
                temp = std::vector<T >(size);
368
 
                $1 = &temp;
369
 
                for (unsigned int i=0; i<size; i++) {
370
 
                    VALUE o = RARRAY_PTR($input)[i];
371
 
                    if (CHECK(o))
372
 
                        temp[i] = (T)(CONVERT_FROM(o));
373
 
                    else
374
 
                        rb_raise(rb_eTypeError,
375
 
                                 "wrong argument type"
376
 
                                 " (expected vector<" #T ">)");
377
 
                }
378
 
            } else {
379
 
                SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, 1);
380
 
            }
381
 
        }
382
 
        %typemap(out) vector<T> {
383
 
            $result = rb_ary_new2($1.size());
384
 
            for (unsigned int i=0; i<$1.size(); i++)
385
 
                rb_ary_store($result,i,CONVERT_TO((($1_type &)$1)[i]));
386
 
        }
387
 
        %typecheck(SWIG_TYPECHECK_VECTOR) vector<T> {
388
 
            /* native sequence? */
389
 
            if (rb_obj_is_kind_of($input,rb_cArray)) {
390
 
                unsigned int size = RARRAY_LEN($input);
391
 
                if (size == 0) {
392
 
                    /* an empty sequence can be of any type */
393
 
                    $1 = 1;
394
 
                } else {
395
 
                    /* check the first element only */
396
 
                    VALUE o = RARRAY_PTR($input)[0];
397
 
                    if (CHECK(o))
398
 
                        $1 = 1;
399
 
                    else
400
 
                        $1 = 0;
401
 
                }
402
 
            } else {
403
 
                /* wrapped vector? */
404
 
                std::vector<T >* v;
405
 
                if (SWIG_ConvertPtr($input,(void **) &v, 
406
 
                                    $&1_descriptor,0) != -1)
407
 
                    $1 = 1;
408
 
                else
409
 
                    $1 = 0;
410
 
            }
411
 
        }
412
 
        %typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
413
 
                                          const vector<T>* {
414
 
            /* native sequence? */
415
 
            if (rb_obj_is_kind_of($input,rb_cArray)) {
416
 
                unsigned int size = RARRAY_LEN($input);
417
 
                if (size == 0) {
418
 
                    /* an empty sequence can be of any type */
419
 
                    $1 = 1;
420
 
                } else {
421
 
                    /* check the first element only */
422
 
                    VALUE o = RARRAY_PTR($input)[0];
423
 
                    if (CHECK(o))
424
 
                        $1 = 1;
425
 
                    else
426
 
                        $1 = 0;
427
 
                }
428
 
            } else {
429
 
                /* wrapped vector? */
430
 
                std::vector<T >* v;
431
 
                if (SWIG_ConvertPtr($input,(void **) &v, 
432
 
                                    $1_descriptor,0) != -1)
433
 
                    $1 = 1;
434
 
                else
435
 
                    $1 = 0;
436
 
            }
437
 
        }
438
 
      public:
439
 
        vector();
440
 
        vector(unsigned int size);
441
 
        vector(unsigned int size, const T& value);
442
 
        vector(const vector<T> &);
443
 
 
444
 
        %rename(__len__) size;
445
 
        unsigned int size() const;
446
 
        %rename("empty?") empty;
447
 
        bool empty() const;
448
 
        void clear();
449
 
        %rename(push) push_back;
450
 
        void push_back(T x);
451
 
        %extend {
452
 
            T pop() throw (std::out_of_range) {
453
 
                if (self->size() == 0)
454
 
                    throw std::out_of_range("pop from empty vector");
455
 
                T x = self->back();
456
 
                self->pop_back();
457
 
                return x;
458
 
            }
459
 
            T __getitem__(int i) throw (std::out_of_range) {
460
 
                int size = int(self->size());
461
 
                if (i<0) i += size;
462
 
                if (i>=0 && i<size)
463
 
                    return (*self)[i];
464
 
                else
465
 
                    throw std::out_of_range("vector index out of range");
466
 
            }
467
 
            void __setitem__(int i, T x) throw (std::out_of_range) {
468
 
                int size = int(self->size());
469
 
                if (i<0) i+= size;
470
 
                if (i>=0 && i<size)
471
 
                    (*self)[i] = x;
472
 
                else
473
 
                    throw std::out_of_range("vector index out of range");
474
 
            }
475
 
            void each() {
476
 
                for (unsigned int i=0; i<self->size(); i++)
477
 
                    rb_yield(CONVERT_TO((*self)[i]));
478
 
            }
479
 
        }
480
 
    };
481
 
    %enddef
482
 
 
483
 
    specialize_std_vector(bool,SWIG_BOOL_P,SWIG_RB2BOOL,SWIG_BOOL2RB);
484
 
    specialize_std_vector(char,FIXNUM_P,FIX2INT,INT2NUM);
485
 
    specialize_std_vector(int,FIXNUM_P,FIX2INT,INT2NUM);
486
 
    specialize_std_vector(short,FIXNUM_P,FIX2INT,INT2NUM);
487
 
    specialize_std_vector(long,FIXNUM_P,FIX2INT,INT2NUM);
488
 
    specialize_std_vector(unsigned char,FIXNUM_P,FIX2INT,INT2NUM);
489
 
    specialize_std_vector(unsigned int,FIXNUM_P,FIX2INT,INT2NUM);
490
 
    specialize_std_vector(unsigned short,FIXNUM_P,FIX2INT,INT2NUM);
491
 
    specialize_std_vector(unsigned long,FIXNUM_P,FIX2INT,INT2NUM);
492
 
    specialize_std_vector(double,SWIG_FLOAT_P,SWIG_NUM2DBL,rb_float_new);
493
 
    specialize_std_vector(float,SWIG_FLOAT_P,SWIG_NUM2DBL,rb_float_new);
494
 
    specialize_std_vector(std::string,SWIG_STRING_P,SWIG_RB2STR,SWIG_STR2RB);
495
 
 
496
 
}
 
24
 
 
25
 
 
26
%define %swig_vector_methods(Type...) 
 
27
  %swig_sequence_methods(Type)
 
28
  %swig_sequence_front_inserters(Type);
 
29
%enddef
 
30
 
 
31
%define %swig_vector_methods_val(Type...) 
 
32
  %swig_sequence_methods_val(Type);
 
33
  %swig_sequence_front_inserters(Type);
 
34
%enddef
 
35
 
 
36
 
 
37
%mixin std::vector "Enumerable";
 
38
%ignore std::vector::push_back;
 
39
%ignore std::vector::pop_back;
 
40
 
 
41
 
 
42
%rename("delete")     std::vector::__delete__;
 
43
%rename("reject!")    std::vector::reject_bang;
 
44
%rename("map!")       std::vector::map_bang;
 
45
%rename("empty?")     std::vector::empty;
 
46
%rename("include?" )  std::vector::__contains__ const;
 
47
%rename("has_key?" )  std::vector::has_key const;
 
48
 
 
49
%alias  std::vector::push          "<<";
 
50
 
 
51
%include <std/std_vector.i>
497
52