-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_utils.h
236 lines (198 loc) · 6.94 KB
/
lua_utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#ifndef LUA_UTILS_H
#define LUA_UTILS_H
#include <lua.hpp>
#include <string>
#include <tuple>
#include <list>
#include <map>
#include <unordered_map>
#include <vector>
#include <type_traits>
namespace LuaUtils {
struct popper {
popper(lua_State* L, int size = 1) : m_L(L), m_size(size) {}
~popper() { if (m_size > 0) lua_pop(m_L, m_size); }
private:
lua_State* m_L;
int m_size;
};
template<typename T>
struct LuaValue;
template<class T>
struct LuaInteger {
static_assert( std::is_integral<T>::value, "type must be integral" );
static T getStackValue(lua_State* L, int pos) {
return luaL_checkinteger(L, pos);
}
static void pushValue(lua_State* L, T value) {
lua_pushinteger(L, value);
}
static int size() { return 1; }
};
template<> struct LuaValue<int> : public LuaInteger<int> {};
template<> struct LuaValue<char> : public LuaInteger<char> {};
template<> struct LuaValue<short> : public LuaInteger<short> {};
template<> struct LuaValue<long> : public LuaInteger<long> {};
template<> struct LuaValue<long long> : public LuaInteger<long long> {};
template<> struct LuaValue<unsigned int> : public LuaInteger<unsigned int> {};
template<> struct LuaValue<unsigned char> : public LuaInteger<unsigned char> {};
template<> struct LuaValue<unsigned short> : public LuaInteger<unsigned short> {};
template<> struct LuaValue<unsigned long> : public LuaInteger<unsigned long> {};
template<> struct LuaValue<unsigned long long> : public LuaInteger<unsigned long long> {};
template<class T> struct LuaFloatingPoint {
static_assert( std::is_floating_point<T>::value, "Type must be floating point number");
static T getStackValue(lua_State* L, int pos) {
return luaL_checknumber(L, pos);
}
static void pushValue(lua_State* L, T value) {
lua_pushnumber(L, value);
}
static int size() { return 1; }
};
template<> struct LuaValue<double> : public LuaFloatingPoint<double> {};
template<> struct LuaValue<long double> : public LuaFloatingPoint<long double> {};
template<> struct LuaValue<float> : public LuaFloatingPoint<float> {};
template<> struct LuaValue<bool> {
static bool getStackValue(lua_State* L, int pos) {
luaL_checktype(L, pos, LUA_TBOOLEAN);
return lua_toboolean(L, pos);
}
static void pushValue(lua_State* L, bool value) {
lua_pushboolean(L, value);
}
static int size() { return 1; }
};
template<> struct LuaValue<std::string> {
static std::string getStackValue(lua_State* L, int pos) {
return luaL_checkstring(L, pos);
}
static void pushValue(lua_State* L, const std::string& value) {
lua_pushstring(L, value.c_str());
}
static int size() { return 1; }
};
template<> struct LuaValue<const char*> {
static void pushValue(lua_State* L, const char* value) {
lua_pushstring(L, value);
}
static int size() { return 1; }
};
struct LuaNil {};
template<> struct LuaValue<LuaNil> {
static LuaNil getStackValue(lua_State* L, int pos) {
luaL_checktype(L, pos, LUA_TNIL);
return LuaNil();
}
static void pushValue(lua_State* L, LuaNil) {
lua_pushnil(L);
}
static int size() { return 1; }
};
template<> struct LuaValue<void> {
static void getStackValue(lua_State* L, int pos) {}
static int size() { return 0; }
};
template<class... T> struct LuaValue<std::tuple<T...> > {
template<class Tuple, int I, int N>
struct helper {
static void pushValue(lua_State* L, const Tuple& tuple) {
LuaValue< typename std::tuple_element< I, Tuple >::type >::pushValue(L, std::get<I>(tuple));
helper<Tuple, I+1, N>::pushValue(L, tuple);
}
static void getValue(lua_State* L, Tuple& tuple, int pos) {
std::get<I>(tuple) = LuaValue< typename std::tuple_element<I, Tuple>::type>::getStackValue(L, pos);
helper<Tuple, I+1, N>::getValue(L, tuple, ++pos);
}
};
template<class Tuple, int N>
struct helper<Tuple, N, N> {
static void pushValue(lua_State*, const Tuple&) {}
static void getValue(lua_State*, Tuple&, int) {}
};
typedef std::tuple<T...> TType;
static TType getStackValue(lua_State* L, int pos) {
TType ret;
helper<TType, 0, std::tuple_size<TType>::value>::getValue(L, ret, pos-size()+1);
return ret;
}
static void pushValue(lua_State* L, const TType& tuple) {
helper<TType, 0, std::tuple_size<TType>::value>::pushValue(L, tuple);
}
static int size() { return std::tuple_size<TType>::value; }
};
template<class C> struct LuaCollection {
typedef typename C::value_type value_type;
static C getStackValue(lua_State* L, int pos) {
C ret;
luaL_checktype(L, pos, LUA_TTABLE);
const int size = lua_objlen(L, pos);
for (int i = 1; i <= size; ++i) {
lua_rawgeti(L, pos, i);
ret.emplace_back(LuaValue<value_type>::getStackValue(L, -1));
lua_pop(L, 1);
}
return std::move(ret);
}
static void pushValue(lua_State* L, const C& list) {
lua_createtable(L, list.size(), 0);
int i = 1;
for (const value_type& el: list) {
LuaValue<value_type>::pushValue(L, el);
lua_rawseti(L, -2, i++);
}
}
static int size() { return 1; }
};
template<class T> struct LuaValue<std::list<T>> : public LuaCollection<std::list<T>> {};
template<class T> struct LuaValue<std::vector<T>> : public LuaCollection<std::vector<T>> {};
template<class M> struct LuaAssociativeArray {
typedef typename M::key_type key_type;
typedef typename M::mapped_type mapped_type;
static M getStackValue(lua_State* L, int pos) {
const int ppos = (pos < 0) ? lua_gettop(L) + pos + 1 : pos;
luaL_checktype(L, ppos, LUA_TTABLE);
M ret;
lua_pushnil(L);
while (lua_next(L, ppos) != 0) {
ret[LuaValue<key_type>::getStackValue(L, -2)]
= LuaValue<mapped_type>::getStackValue(L, -1);
lua_pop(L, 1);
}
return std::move(ret);
}
static void pushValue(lua_State* L, const M& map) {
lua_createtable(L, 0, map.size());
for (auto el: map) {
LuaValue<key_type>::pushValue(L, el.first);
LuaValue<mapped_type>::pushValue(L, el.second);
lua_settable(L, -3);
}
}
static int size() { return 1; }
};
template<class K, class V> struct LuaValue<std::map<K,V>> : public LuaAssociativeArray<std::map<K,V>> {};
template<class K, class V> struct LuaValue<std::unordered_map<K,V>> : public LuaAssociativeArray<std::unordered_map<K,V>> {};
int pushArgs(lua_State* L) {
return 0;
}
template<class H, class... T>
int pushArgs(lua_State* L, const H& h, const T&... t) {
int size = LuaValue<H>::size();
LuaValue<H>::pushValue(L, h);
return size + pushArgs(L, t...);
}
template<class R, class... Args>
auto callFunc(lua_State* L, const std::string& name, const Args... args)
-> decltype(LuaValue<R>::getStackValue(L, -1))
{
lua_getglobal(L, name.c_str());
luaL_checktype(L, -1, LUA_TFUNCTION);
const int size = pushArgs(L, args...);
if (lua_pcall(L, size, LuaValue<R>::size(), 0) != 0) {
luaL_error(L, "error running funtion %1", name.c_str());
}
popper p(L, LuaValue<R>::size());
return LuaValue<R>::getStackValue(L, -1);
}
}
#endif /* LUA_UTILS_H */