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
// 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:
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
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
34
// ------------------------------------------------------------------------
5
%fragment("StdVectorTraits","header",fragment="StdSequenceTraits")
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);
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);
46
%mixin vector "Enumerable";
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);
53
for (unsigned int i=0; i<size; i++) {
54
VALUE o = RARRAY_PTR($input)[i];
56
SWIG_ConvertPtr(o, (void **) &x, $descriptor(T *), 1);
61
SWIG_ConvertPtr($input, &ptr, $&1_descriptor, 1);
62
$1 = *(($&1_type) ptr);
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);
70
for (unsigned int i=0; i<size; i++) {
71
VALUE o = RARRAY_PTR($input)[i];
73
SWIG_ConvertPtr(o, (void **) &x, $descriptor(T *), 1);
77
SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, 1);
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));
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);
94
/* an empty sequence can be of any type */
97
/* check the first element only */
99
VALUE o = RARRAY_PTR($input)[0];
100
if ((SWIG_ConvertPtr(o,(void **) &x,
101
$descriptor(T *),0)) != -1)
107
/* wrapped vector? */
109
if (SWIG_ConvertPtr($input,(void **) &v,
110
$&1_descriptor,0) != -1)
116
%typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
118
/* native sequence? */
119
if (rb_obj_is_kind_of($input,rb_cArray)) {
120
unsigned int size = RARRAY_LEN($input);
122
/* an empty sequence can be of any type */
125
/* check the first element only */
127
VALUE o = RARRAY_PTR($input)[0];
128
if ((SWIG_ConvertPtr(o,(void **) &x,
129
$descriptor(T *),0)) != -1)
135
/* wrapped vector? */
137
if (SWIG_ConvertPtr($input,(void **) &v,
138
$1_descriptor,0) != -1)
146
vector(unsigned int size);
147
vector(unsigned int size, const T& value);
148
vector(const vector<T> &);
150
%rename(__len__) size;
151
unsigned int size() const;
152
%rename("empty?") empty;
155
%rename(push) push_back;
156
void push_back(const T& x);
158
T pop() throw (std::out_of_range) {
159
if (self->size() == 0)
160
throw std::out_of_range("pop from empty vector");
165
T& __getitem__(int i) throw (std::out_of_range) {
166
int size = int(self->size());
171
throw std::out_of_range("vector index out of range");
173
void __setitem__(int i, const T& x) throw (std::out_of_range) {
174
int size = int(self->size());
179
throw std::out_of_range("vector index out of range");
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));
191
// Partial specialization for vectors of pointers. [ beazley ]
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];
202
SWIG_ConvertPtr(o, (void **) &x, $descriptor(T *), 1);
203
(($1_type &)$1)[i] = x;
207
SWIG_ConvertPtr($input, &ptr, $&1_descriptor, 1);
208
$1 = *(($&1_type) ptr);
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);
217
for (unsigned int i=0; i<size; i++) {
218
VALUE o = RARRAY_PTR($input)[i];
220
SWIG_ConvertPtr(o, (void **) &x, $descriptor(T *), 1);
224
SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, 1);
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));
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);
241
/* an empty sequence can be of any type */
244
/* check the first element only */
246
VALUE o = RARRAY_PTR($input)[0];
247
if ((SWIG_ConvertPtr(o,(void **) &x,
248
$descriptor(T *),0)) != -1)
254
/* wrapped vector? */
256
if (SWIG_ConvertPtr($input,(void **) &v,
257
$&1_descriptor,0) != -1)
263
%typecheck(SWIG_TYPECHECK_VECTOR) const vector<T*>&,
265
/* native sequence? */
266
if (rb_obj_is_kind_of($input,rb_cArray)) {
267
unsigned int size = RARRAY_LEN($input);
269
/* an empty sequence can be of any type */
272
/* check the first element only */
274
VALUE o = RARRAY_PTR($input)[0];
275
if ((SWIG_ConvertPtr(o,(void **) &x,
276
$descriptor(T *),0)) != -1)
282
/* wrapped vector? */
284
if (SWIG_ConvertPtr($input,(void **) &v,
285
$1_descriptor,0) != -1)
293
vector(unsigned int size);
294
vector(unsigned int size, T * &value);
295
vector(const vector<T*> &);
297
%rename(__len__) size;
298
unsigned int size() const;
299
%rename("empty?") empty;
302
%rename(push) push_back;
303
void push_back(T* x);
305
T* pop() throw (std::out_of_range) {
306
if (self->size() == 0)
307
throw std::out_of_range("pop from empty vector");
312
T* __getitem__(int i) throw (std::out_of_range) {
313
int size = int(self->size());
318
throw std::out_of_range("vector index out of range");
320
void __setitem__(int i, T* x) throw (std::out_of_range) {
321
int size = int(self->size());
326
throw std::out_of_range("vector index out of range");
329
for (unsigned int i=0; i<self->size(); i++) {
331
rb_yield(SWIG_NewPointerObj((void *) x,
332
$descriptor(T *), 0));
339
// specializations for built-ins
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];
351
(($1_type &)$1)[i] = (T)(CONVERT_FROM(o));
353
rb_raise(rb_eTypeError,
354
"wrong argument type"
355
" (expected vector<" #T ">)");
359
SWIG_ConvertPtr($input, &ptr, $&1_descriptor, 1);
360
$1 = *(($&1_type) ptr);
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);
369
for (unsigned int i=0; i<size; i++) {
370
VALUE o = RARRAY_PTR($input)[i];
372
temp[i] = (T)(CONVERT_FROM(o));
374
rb_raise(rb_eTypeError,
375
"wrong argument type"
376
" (expected vector<" #T ">)");
379
SWIG_ConvertPtr($input, (void **) &$1, $1_descriptor, 1);
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]));
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);
392
/* an empty sequence can be of any type */
395
/* check the first element only */
396
VALUE o = RARRAY_PTR($input)[0];
403
/* wrapped vector? */
405
if (SWIG_ConvertPtr($input,(void **) &v,
406
$&1_descriptor,0) != -1)
412
%typecheck(SWIG_TYPECHECK_VECTOR) const vector<T>&,
414
/* native sequence? */
415
if (rb_obj_is_kind_of($input,rb_cArray)) {
416
unsigned int size = RARRAY_LEN($input);
418
/* an empty sequence can be of any type */
421
/* check the first element only */
422
VALUE o = RARRAY_PTR($input)[0];
429
/* wrapped vector? */
431
if (SWIG_ConvertPtr($input,(void **) &v,
432
$1_descriptor,0) != -1)
440
vector(unsigned int size);
441
vector(unsigned int size, const T& value);
442
vector(const vector<T> &);
444
%rename(__len__) size;
445
unsigned int size() const;
446
%rename("empty?") empty;
449
%rename(push) push_back;
452
T pop() throw (std::out_of_range) {
453
if (self->size() == 0)
454
throw std::out_of_range("pop from empty vector");
459
T __getitem__(int i) throw (std::out_of_range) {
460
int size = int(self->size());
465
throw std::out_of_range("vector index out of range");
467
void __setitem__(int i, T x) throw (std::out_of_range) {
468
int size = int(self->size());
473
throw std::out_of_range("vector index out of range");
476
for (unsigned int i=0; i<self->size(); i++)
477
rb_yield(CONVERT_TO((*self)[i]));
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);
26
%define %swig_vector_methods(Type...)
27
%swig_sequence_methods(Type)
28
%swig_sequence_front_inserters(Type);
31
%define %swig_vector_methods_val(Type...)
32
%swig_sequence_methods_val(Type);
33
%swig_sequence_front_inserters(Type);
37
%mixin std::vector "Enumerable";
38
%ignore std::vector::push_back;
39
%ignore std::vector::pop_back;
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;
49
%alias std::vector::push "<<";
51
%include <std/std_vector.i>