// // SWIG typemaps for std::vector types // Luigi Ballabio // May 7, 2002 // // PHP implementation %include exception.i // containers // methods which can raise are caused to throw an IndexError %exception std::vector::get { 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 // PHP as much as possible, namely, to allow the user to pass and // be returned PHP 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 PHP 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 PHP sequence of T:s // is returned which is most easily used in other PHP 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 { // add generic typemaps here public: vector(unsigned int size = 0); unsigned int size() const; bool empty() const; void clear(); %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& get(int i) { int size = int(self->size()); if (i>=0 && isize()); if (i>=0 && i class vector { // add specialized typemaps here public: vector(unsigned int size = 0); unsigned int size() const; bool empty() const; void clear(); %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 get(int i) { int size = int(self->size()); if (i>=0 && isize()); if (i>=0 && i