1 /* ----------------------------------------------------------------------------- 2 * std_vector.i 3 * ----------------------------------------------------------------------------- */ 4 5 %include <std_common.i> 6 7 %{ 8 #include <vector> 9 #include <stdexcept> 10 %} 11 12 namespace std { 13 14 template<class T> class vector { 15 public: 16 typedef size_t size_type; 17 typedef T value_type; 18 typedef const value_type& const_reference; 19 vector(); 20 vector(size_type n); 21 size_type size() const; 22 size_type capacity() const; 23 void reserve(size_type n); 24 void clear(); 25 %rename(push) push_back; 26 void push_back(const value_type& x); 27 %extend { 28 bool is_empty() const { 29 return $self->empty(); 30 } 31 T pop() throw (std::out_of_range) { 32 if (self->size() == 0) 33 throw std::out_of_range("pop from empty vector"); 34 T x = self->back(); 35 self->pop_back(); 36 return x; 37 } 38 const_reference get(int i) throw (std::out_of_range) { 39 int size = int(self->size()); 40 if (i>=0 && i<size) 41 return (*self)[i]; 42 else 43 throw std::out_of_range("vector index out of range"); 44 } 45 void set(int i, const value_type& val) throw (std::out_of_range) { 46 int size = int(self->size()); 47 if (i>=0 && i<size) 48 (*self)[i] = val; 49 else 50 throw std::out_of_range("vector index out of range"); 51 } 52 } 53 }; 54 55 // bool specialization 56 template<> class vector<bool> { 57 public: 58 typedef size_t size_type; 59 typedef bool value_type; 60 typedef bool const_reference; 61 vector(); 62 vector(size_type n); 63 size_type size() const; 64 size_type capacity() const; 65 void reserve(size_type n); 66 void clear(); 67 %rename(push) push_back; 68 void push_back(const value_type& x); 69 %extend { 70 bool is_empty() const { 71 return $self->empty(); 72 } 73 bool pop() throw (std::out_of_range) { 74 if (self->size() == 0) 75 throw std::out_of_range("pop from empty vector"); 76 bool x = self->back(); 77 self->pop_back(); 78 return x; 79 } 80 const_reference get(int i) throw (std::out_of_range) { 81 int size = int(self->size()); 82 if (i>=0 && i<size) 83 return (*self)[i]; 84 else 85 throw std::out_of_range("vector index out of range"); 86 } 87 void set(int i, const value_type& val) throw (std::out_of_range) { 88 int size = int(self->size()); 89 if (i>=0 && i<size) 90 (*self)[i] = val; 91 else 92 throw std::out_of_range("vector index out of range"); 93 } 94 } 95 }; 96 } 97 98 %define specialize_std_vector(T) 99 #warning "specialize_std_vector - specialization for type T no longer needed" 100 %enddef 101 102 103