// // SWIG typemaps for std::vector // Luigi Ballabio // Apr 8, 2002 // // Guile implementation %include std_common.i %include exception.i %exception std::vector::ref { try { $action } catch (std::out_of_range& e) { SWIG_exception(SWIG_IndexError,const_cast(e.what())); } } %exception std::vector::set { try { $action } catch (std::out_of_range& e) { SWIG_exception(SWIG_IndexError,const_cast(e.what())); } } %exception std::vector::pop { try { $action } catch (std::out_of_range& e) { SWIG_exception(SWIG_IndexError,const_cast(e.what())); } } // ------------------------------------------------------------------------ // std::vector // // The aim of all that follows would be to integrate std::vector with // Guile as much as possible, namely, to allow the user to pass and // be returned Guile 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 Guile 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 Guile vector of T:s // is returned which is most easily used in other Guile 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 (gh_vector_p($input)) { unsigned long size = gh_vector_length($input); $1 = std::vector(size); for (unsigned long i=0; i(); } else if (gh_pair_p($input)) { SCM head, tail; $1 = std::vector(); tail = $input; while (!gh_null_p(tail)) { head = gh_car(tail); tail = gh_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 (gh_vector_p($input)) { unsigned long size = gh_vector_length($input); temp = std::vector(size); $1 = &temp; for (unsigned long i=0; i(); $1 = &temp; } else if (gh_pair_p($input)) { temp = std::vector(); $1 = &temp; SCM head, tail; tail = $input; while (!gh_null_p(tail)) { head = gh_car(tail); tail = gh_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 = gh_make_vector(gh_long2scm($1.size()),SCM_UNSPECIFIED); for (unsigned int i=0; i<$1.size(); i++) { T* x = new T((($1_type &)$1)[i]); gh_vector_set_x($result,gh_long2scm(i), SWIG_NewPointerObj(x, $descriptor(T *), 1)); } } %typecheck(SWIG_TYPECHECK_VECTOR) vector { /* native sequence? */ if (gh_vector_p($input)) { unsigned int size = gh_vector_length($input); if (size == 0) { /* an empty sequence can be of any type */ $1 = 1; } else { /* check the first element only */ SCM o = gh_vector_ref($input,gh_ulong2scm(0)); T* x; if (SWIG_ConvertPtr(o,(void**) &x, $descriptor(T *), 0) != -1) $1 = 1; else $1 = 0; } } else if (gh_null_p($input)) { /* again, an empty sequence can be of any type */ $1 = 1; } else if (gh_pair_p($input)) { /* check the first element only */ T* x; SCM head = gh_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 (gh_vector_p($input)) { unsigned int size = gh_vector_length($input); if (size == 0) { /* an empty sequence can be of any type */ $1 = 1; } else { /* check the first element only */ T* x; SCM o = gh_vector_ref($input,gh_ulong2scm(0)); if (SWIG_ConvertPtr(o,(void**) &x, $descriptor(T *), 0) != -1) $1 = 1; else $1 = 0; } } else if (gh_null_p($input)) { /* again, an empty sequence can be of any type */ $1 = 1; } else if (gh_pair_p($input)) { /* check the first element only */ T* x; SCM head = gh_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() { 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) { int size = int(self->size()); if (i>=0 && isize()); if (i>=0 && i class vector { %typemap(in) vector { if (gh_vector_p($input)) { unsigned long size = gh_vector_length($input); $1 = std::vector(size); for (unsigned long i=0; i(); } else if (gh_pair_p($input)) { SCM v = gh_list_to_vector($input); unsigned long size = gh_vector_length(v); $1 = std::vector(size); for (unsigned long i=0; i& (std::vector temp), const vector* (std::vector temp) { if (gh_vector_p($input)) { unsigned long size = gh_vector_length($input); temp = std::vector(size); $1 = &temp; for (unsigned long i=0; i(); $1 = &temp; } else if (gh_pair_p($input)) { SCM v = gh_list_to_vector($input); unsigned long size = gh_vector_length(v); temp = std::vector(size); $1 = &temp; for (unsigned long i=0; i { $result = gh_make_vector(gh_long2scm($1.size()),SCM_UNSPECIFIED); for (unsigned int i=0; i<$1.size(); i++) { SCM x = CONVERT_TO((($1_type &)$1)[i]); gh_vector_set_x($result,gh_long2scm(i),x); } } %typecheck(SWIG_TYPECHECK_VECTOR) vector { /* native sequence? */ if (gh_vector_p($input)) { unsigned int size = gh_vector_length($input); if (size == 0) { /* an empty sequence can be of any type */ $1 = 1; } else { /* check the first element only */ T* x; SCM o = gh_vector_ref($input,gh_ulong2scm(0)); $1 = CHECK(o) ? 1 : 0; } } else if (gh_null_p($input)) { /* again, an empty sequence can be of any type */ $1 = 1; } else if (gh_pair_p($input)) { /* check the first element only */ T* x; SCM head = gh_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 (gh_vector_p($input)) { unsigned int size = gh_vector_length($input); if (size == 0) { /* an empty sequence can be of any type */ $1 = 1; } else { /* check the first element only */ T* x; SCM o = gh_vector_ref($input,gh_ulong2scm(0)); $1 = CHECK(o) ? 1 : 0; } } else if (gh_null_p($input)) { /* again, an empty sequence can be of any type */ $1 = 1; } else if (gh_pair_p($input)) { /* check the first element only */ T* x; SCM head = gh_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() { 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) { int size = int(self->size()); if (i>=0 && isize()); if (i>=0 && i