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