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.
7 * std::vector typemaps for LUA
8 * ----------------------------------------------------------------------------- */
13 %include <std_except.i> // the general exepctions
15 A really cut down version of the vector class.
17 Note: this does not match the true std::vector class
18 but 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)
21 And no support for iterators & insert/erase
23 It would be useful to have a vector<->Lua table conversion routine
33 vector(const vector&);
34 vector(unsigned int,T);
35 unsigned int size() const;
36 unsigned int max_size() const;
39 void push_back(T val);
41 T front()const; // only read front & back
42 T back()const; // not write to them
43 // operator [] given later:
45 %extend // this is a extra bit of SWIG code
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)
51 if (idx>=self->size())
52 throw std::out_of_range("in vector::__getitem__()");
55 void __setitem__(unsigned int idx,T val) throw (std::out_of_range)
57 if (idx>=self->size())
58 throw std::out_of_range("in vector::__setitem__()");
68 These look a bit like the array<->LuaTable fns
69 but are templated, not %defined
70 (you must have template support for STL)
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
80 std::vector<T>* SWIG_read_number_vector(lua_State* L,int index)
83 std::vector<T>* vec=new std::vector<T>();
86 lua_rawgeti(L,index,i+1);
92 if (!lua_isnumber(L,-1))
98 vec->push_back((T)lua_tonumber(L,-1));
104 // writes a vector of numbers out as a lua table
106 int SWIG_write_number_vector(lua_State* L,std::vector<T> *vec)
109 for(int i=0;i<vec->size();++i)
111 lua_pushnumber(L,(double)((*vec)[i]));
112 lua_rawseti(L,-2,i+1);// -1 is the number, -2 is the table
119 %define SWIG_TYPEMAP_NUM_VECTOR(T)
122 %typemap(in) std::vector<T> *INPUT
123 %{ $1 = SWIG_read_number_vector<T>(L,$input);
124 if (!$1) SWIG_fail;%}
126 %typemap(freearg) std::vector<T> *INPUT
130 %typemap(argout) std::vector<T> *OUTPUT
131 %{ SWIG_write_number_vector(L,$1); SWIG_arg++; %}