/tags/rel-1-3-25/SWIG/Lib/php4/std_vector.i
Swig | 158 lines | 141 code | 17 blank | 0 comment | 0 complexity | ae5c217a0b98f8ba23d2512d11ea7271 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
1// 2// SWIG typemaps for std::vector types 3// Luigi Ballabio 4// May 7, 2002 5// 6// PHP implementation 7 8%include exception.i 9 10// containers 11 12// methods which can raise are caused to throw an IndexError 13%exception std::vector::get { 14 try { 15 $action 16 } catch (std::out_of_range& e) { 17 SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); 18 } 19} 20 21%exception std::vector::set { 22 try { 23 $action 24 } catch (std::out_of_range& e) { 25 SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); 26 } 27} 28 29%exception std::vector::pop { 30 try { 31 $action 32 } catch (std::out_of_range& e) { 33 SWIG_exception(SWIG_IndexError,const_cast<char*>(e.what())); 34 } 35} 36 37 38// ------------------------------------------------------------------------ 39// std::vector 40// 41// The aim of all that follows would be to integrate std::vector with 42// PHP as much as possible, namely, to allow the user to pass and 43// be returned PHP lists. 44// const declarations are used to guess the intent of the function being 45// exported; therefore, the following rationale is applied: 46// 47// -- f(std::vector<T>), f(const std::vector<T>&), f(const std::vector<T>*): 48// the parameter being read-only, either a PHP sequence or a 49// previously wrapped std::vector<T> can be passed. 50// -- f(std::vector<T>&), f(std::vector<T>*): 51// the parameter must be modified; therefore, only a wrapped std::vector 52// can be passed. 53// -- std::vector<T> f(): 54// the vector is returned by copy; therefore, a PHP sequence of T:s 55// is returned which is most easily used in other PHP functions 56// -- std::vector<T>& f(), std::vector<T>* f(), const std::vector<T>& f(), 57// const std::vector<T>* f(): 58// the vector is returned by reference; therefore, a wrapped std::vector 59// is returned 60// ------------------------------------------------------------------------ 61 62%{ 63#include <vector> 64#include <algorithm> 65#include <stdexcept> 66%} 67 68// exported class 69 70namespace std { 71 72 template<class T> class vector { 73 // add generic typemaps here 74 public: 75 vector(unsigned int size = 0); 76 unsigned int size() const; 77 bool empty() const; 78 void clear(); 79 %rename(push) push_back; 80 void push_back(const T& x); 81 %extend { 82 T pop() { 83 if (self->size() == 0) 84 throw std::out_of_range("pop from empty vector"); 85 T x = self->back(); 86 self->pop_back(); 87 return x; 88 } 89 T& get(int i) { 90 int size = int(self->size()); 91 if (i>=0 && i<size) 92 return (*self)[i]; 93 else 94 throw std::out_of_range("vector index out of range"); 95 } 96 void set(int i, const T& x) { 97 int size = int(self->size()); 98 if (i>=0 && i<size) 99 (*self)[i] = x; 100 else 101 throw std::out_of_range("vector index out of range"); 102 } 103 } 104 }; 105 106 107 // specializations for built-ins 108 109 %define specialize_std_vector(T) 110 template<> class vector<T> { 111 // add specialized typemaps here 112 public: 113 vector(unsigned int size = 0); 114 unsigned int size() const; 115 bool empty() const; 116 void clear(); 117 %rename(push) push_back; 118 void push_back(T x); 119 %extend { 120 T pop() { 121 if (self->size() == 0) 122 throw std::out_of_range("pop from empty vector"); 123 T x = self->back(); 124 self->pop_back(); 125 return x; 126 } 127 T get(int i) { 128 int size = int(self->size()); 129 if (i>=0 && i<size) 130 return (*self)[i]; 131 else 132 throw std::out_of_range("vector index out of range"); 133 } 134 void set(int i, T x) { 135 int size = int(self->size()); 136 if (i>=0 && i<size) 137 (*self)[i] = x; 138 else 139 throw std::out_of_range("vector index out of range"); 140 } 141 } 142 }; 143 %enddef 144 145 specialize_std_vector(bool); 146 specialize_std_vector(char); 147 specialize_std_vector(int); 148 specialize_std_vector(short); 149 specialize_std_vector(long); 150 specialize_std_vector(unsigned char); 151 specialize_std_vector(unsigned int); 152 specialize_std_vector(unsigned short); 153 specialize_std_vector(unsigned long); 154 specialize_std_vector(float); 155 specialize_std_vector(double); 156 157} 158