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