1/* ----------------------------------------------------------------------------- 2 * See the LICENSE file for information on copyright, usage and redistribution 3 * of SWIG, and the README file for authors - http://www.swig.org/release.html. 4 * 5 * _std_common.i 6 * 7 * std::helpers for LUA 8 * ----------------------------------------------------------------------------- */ 9 10%include <std_except.i> // the general exepctions 11 12/* 13The basic idea here, is instead of trying to feed SWIG all the 14horribly templated STL code, to give it a neatened version. 15 16These %defines cover some of the more common methods 17so the class declarations become just a set of %defines 18 19*/ 20 21/* #define for basic container features 22note: I allow front(), back() & pop_back() to throw execptions 23upon empty containers, rather than coredump 24(as we have'nt defined the methods, we can use %extend to add with 25new features) 26 27*/ 28%define %STD_CONTAINER_METHODS(CLASS,T) 29public: 30 CLASS(); 31 CLASS(const CLASS&); 32 unsigned int size() const; 33 unsigned int max_size() const; 34 bool empty() const; 35 void clear(); 36 %extend { // the extra stuff which must be checked 37 T front()const throw (std::out_of_range){ // only read front & back 38 if (self->empty()) 39 throw std::out_of_range("in "#CLASS"::front()"); 40 return self->front(); 41 } 42 T back()const throw (std::out_of_range){ // not write to them 43 if (self->empty()) 44 throw std::out_of_range("in "#CLASS"::back()"); 45 return self->back(); 46 } 47 } 48%enddef 49 50/* push/pop for front/back 51also note: front & back are read only methods, not used for writing 52*/ 53%define %STD_FRONT_ACCESS_METHODS(CLASS,T) 54public: 55 void push_front(const T& val); 56 %extend { // must check this 57 void pop_front() throw (std::out_of_range){ 58 if (self->empty()) 59 throw std::out_of_range("in "#CLASS"::pop_front()"); 60 self->pop_back(); 61 } 62 } 63%enddef 64 65%define %STD_BACK_ACCESS_METHODS(CLASS,T) 66public: 67 void push_back(const T& val); 68 %extend { // must check this 69 void pop_back() throw (std::out_of_range){ 70 if (self->empty()) 71 throw std::out_of_range("in "#CLASS"::pop_back()"); 72 self->pop_back(); 73 } 74 } 75%enddef 76 77/* 78Random access methods 79*/ 80%define %STD_RANDOM_ACCESS_METHODS(CLASS,T) 81 %extend // this is a extra bit of SWIG code 82 { 83 // [] is replaced by __getitem__ & __setitem__ 84 // simply throws a string, which causes a lua error 85 T __getitem__(unsigned int idx) throw (std::out_of_range){ 86 if (idx>=self->size()) 87 throw std::out_of_range("in "#CLASS"::__getitem__()"); 88 return (*self)[idx]; 89 } 90 void __setitem__(unsigned int idx,const T& val) throw (std::out_of_range){ 91 if (idx>=self->size()) 92 throw std::out_of_range("in "#CLASS"::__setitem__()"); 93 (*self)[idx]=val; 94 } 95 }; 96%enddef 97