1 /* ----------------------------------------------------------------------------- 2 * std_vector.i 3 * ----------------------------------------------------------------------------- */ 4 5 %{ 6 #include <vector> 7 #include <stdexcept> 8 %} 9 10 namespace std { 11 12 template<class T> class vector { 13 public: 14 typedef size_t size_type; 15 typedef T value_type; 16 typedef const value_type& const_reference; 17 vector(); 18 vector(size_type n); 19 size_type size() const; 20 size_type capacity() const; 21 void reserve(size_type n); 22 %rename(isEmpty) empty; 23 bool empty() const; 24 void clear(); 25 %rename(add) push_back; 26 void push_back(const value_type& x); 27 %extend { 28 const_reference get(int i) throw (std::out_of_range) { 29 int size = int(self->size()); 30 if (i>=0 && i<size) 31 return (*self)[i]; 32 else 33 throw std::out_of_range("vector index out of range"); 34 } 35 void set(int i, const value_type& val) throw (std::out_of_range) { 36 int size = int(self->size()); 37 if (i>=0 && i<size) 38 (*self)[i] = val; 39 else 40 throw std::out_of_range("vector index out of range"); 41 } 42 } 43 }; 44 45 // bool specialization 46 template<> class vector<bool> { 47 public: 48 typedef size_t size_type; 49 typedef bool value_type; 50 typedef bool const_reference; 51 vector(); 52 vector(size_type n); 53 size_type size() const; 54 size_type capacity() const; 55 void reserve(size_type n); 56 %rename(isEmpty) empty; 57 bool empty() const; 58 void clear(); 59 %rename(add) push_back; 60 void push_back(const value_type& x); 61 %extend { 62 const_reference get(int i) throw (std::out_of_range) { 63 int size = int(self->size()); 64 if (i>=0 && i<size) 65 return (*self)[i]; 66 else 67 throw std::out_of_range("vector index out of range"); 68 } 69 void set(int i, const value_type& val) throw (std::out_of_range) { 70 int size = int(self->size()); 71 if (i>=0 && i<size) 72 (*self)[i] = val; 73 else 74 throw std::out_of_range("vector index out of range"); 75 } 76 } 77 }; 78 } 79