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_vector.i 6 * 7 * std::vector typemaps for LUA 8 * ----------------------------------------------------------------------------- */ 9 10%{ 11#include <vector> 12%} 13%include <std_except.i> // the general exepctions 14/* 15A really cut down version of the vector class. 16 17Note: this does not match the true std::vector class 18but instead is an approximate, so that SWIG knows how to wrapper it. 19(Eg, all access is by value, not ref, as SWIG turns refs to pointers) 20 21And no support for iterators & insert/erase 22 23It would be useful to have a vector<->Lua table conversion routine 24 25*/ 26namespace std { 27 28 template<class T> 29 class vector { 30 public: 31 vector(); 32 vector(unsigned int); 33 vector(const vector&); 34 vector(unsigned int,T); 35 unsigned int size() const; 36 unsigned int max_size() const; 37 bool empty() const; 38 void clear(); 39 void push_back(T val); 40 void pop_back(); 41 T front()const; // only read front & back 42 T back()const; // not write to them 43 // operator [] given later: 44 45 %extend // this is a extra bit of SWIG code 46 { 47 // [] is replaced by __getitem__ & __setitem__ 48 // simply throws a string, which causes a lua error 49 T __getitem__(unsigned int idx) throw (std::out_of_range) 50 { 51 if (idx>=self->size()) 52 throw std::out_of_range("in vector::__getitem__()"); 53 return (*self)[idx]; 54 } 55 void __setitem__(unsigned int idx,T val) throw (std::out_of_range) 56 { 57 if (idx>=self->size()) 58 throw std::out_of_range("in vector::__setitem__()"); 59 (*self)[idx]=val; 60 } 61 }; 62 }; 63 64} 65 66/* 67Vector<->LuaTable fns 68These look a bit like the array<->LuaTable fns 69but are templated, not %defined 70(you must have template support for STL) 71 72*/ 73/* 74%{ 75// reads a table into a vector of numbers 76// lua numbers will be cast into the type required (rounding may occur) 77// return 0 if non numbers found in the table 78// returns new'ed ptr if ok 79template<class T> 80std::vector<T>* SWIG_read_number_vector(lua_State* L,int index) 81{ 82 int i=0; 83 std::vector<T>* vec=new std::vector<T>(); 84 while(1) 85 { 86 lua_rawgeti(L,index,i+1); 87 if (!lua_isnil(L,-1)) 88 { 89 lua_pop(L,1); 90 break; // finished 91 } 92 if (!lua_isnumber(L,-1)) 93 { 94 lua_pop(L,1); 95 delete vec; 96 return 0; // error 97 } 98 vec->push_back((T)lua_tonumber(L,-1)); 99 lua_pop(L,1); 100 ++i; 101 } 102 return vec; // ok 103} 104// writes a vector of numbers out as a lua table 105template<class T> 106int SWIG_write_number_vector(lua_State* L,std::vector<T> *vec) 107{ 108 lua_newtable(L); 109 for(int i=0;i<vec->size();++i) 110 { 111 lua_pushnumber(L,(double)((*vec)[i])); 112 lua_rawseti(L,-2,i+1);// -1 is the number, -2 is the table 113 } 114} 115%} 116 117// then the typemaps 118 119%define SWIG_TYPEMAP_NUM_VECTOR(T) 120 121// in 122%typemap(in) std::vector<T> *INPUT 123%{ $1 = SWIG_read_number_vector<T>(L,$input); 124 if (!$1) SWIG_fail;%} 125 126%typemap(freearg) std::vector<T> *INPUT 127%{ delete $1;%} 128 129// out 130%typemap(argout) std::vector<T> *OUTPUT 131%{ SWIG_write_number_vector(L,$1); SWIG_arg++; %} 132 133%enddef 134*/ 135