/* ----------------------------------------------------------------------------- * std_vector.i * * SWIG typemaps for std::vector * ----------------------------------------------------------------------------- */ %include // ------------------------------------------------------------------------ // 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), f(const std::vector&), f(const std::vector*): // the parameter being read-only, either a MzScheme sequence or a // previously wrapped std::vector can be passed. // -- f(std::vector&), f(std::vector*): // the parameter must be modified; therefore, only a wrapped std::vector // can be passed. // -- std::vector 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& f(), std::vector* f(), const std::vector& f(), // const std::vector* f(): // the vector is returned by reference; therefore, a wrapped std::vector // is returned // ------------------------------------------------------------------------ %{ #include #include #include %} // exported class namespace std { template class vector { %typemap(in) vector { if (SCHEME_VECTORP($input)) { unsigned int size = SCHEME_VEC_SIZE($input); $1 = std::vector(size); Scheme_Object** items = SCHEME_VEC_ELS($input); for (unsigned int i=0; i(); } else if (SCHEME_PAIRP($input)) { Scheme_Object *head, *tail; $1 = std::vector(); 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& (std::vector temp), const vector* (std::vector temp) { if (SCHEME_VECTORP($input)) { unsigned int size = SCHEME_VEC_SIZE($input); temp = std::vector(size); $1 = &temp; Scheme_Object** items = SCHEME_VEC_ELS($input); for (unsigned int i=0; i(); $1 = &temp; } else if (SCHEME_PAIRP($input)) { temp = std::vector(); $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 { $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 { /* 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* v; if (SWIG_ConvertPtr($input,(void **) &v, $&1_descriptor, 0) != -1) $1 = 1; else $1 = 0; } } %typecheck(SWIG_TYPECHECK_VECTOR) const vector&, const vector* { /* 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* 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&); %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 && isize()); if (i>=0 && i class vector { %typemap(in) vector { if (SCHEME_VECTORP($input)) { unsigned int size = SCHEME_VEC_SIZE($input); $1 = std::vector(size); Scheme_Object** items = SCHEME_VEC_ELS($input); for (unsigned int i=0; i", $argnum - 1, argc, argv); } } else if (SCHEME_NULLP($input)) { $1 = std::vector(); } else if (SCHEME_PAIRP($input)) { Scheme_Object *head, *tail; $1 = std::vector(); 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& (std::vector temp), const vector* (std::vector temp) { if (SCHEME_VECTORP($input)) { unsigned int size = SCHEME_VEC_SIZE($input); temp = std::vector(size); $1 = &temp; Scheme_Object** items = SCHEME_VEC_ELS($input); for (unsigned int i=0; i", $argnum - 1, argc, argv); } } else if (SCHEME_NULLP($input)) { temp = std::vector(); $1 = &temp; } else if (SCHEME_PAIRP($input)) { temp = std::vector(); $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 { $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 { /* 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* v; $1 = (SWIG_ConvertPtr($input,(void **) &v, $&1_descriptor, 0) != -1) ? 1 : 0; } } %typecheck(SWIG_TYPECHECK_VECTOR) const vector&, const vector* { /* 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* 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&); %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 && isize()); if (i>=0 && i