annotate lib/swig/swigwin-2.0.11/Lib/lua/std_vector.i @ 2467:43faa2865d57

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