-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLuaCallDispatcher.h
396 lines (317 loc) · 9.76 KB
/
LuaCallDispatcher.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#ifndef LUA_CALL_DISPATCHER_H
#define LUA_CALL_DISPATCHER_H
#define MIN_RETURN = 0
#include <tuple>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <vector>
#include<lua.hpp>
#include <type_traits>
class LuaCallDispatcher
{
public:
struct LuaUserData
{
LuaUserData(const void* value = 0)
{
m_value = value;
}
const void* m_value;
};
static LuaCallDispatcher& GetInstance()
{
static LuaCallDispatcher dispatch;
return dispatch;
}
void CleanStack(lua_State* L)
{
int stackSize = lua_gettop(L);
lua_pop(L, stackSize);
}
inline void Push() {};
inline void Push(lua_State* L, bool value) { lua_pushboolean(L, value); }
inline void Push(lua_State* L, char value) { lua_pushnumber(L, value); }
inline void Push(lua_State* L, unsigned char value) { lua_pushnumber(L, value); }
inline void Push(lua_State* L, short value) { lua_pushnumber(L, value); }
inline void Push(lua_State* L, unsigned short value) { lua_pushnumber(L, value); }
inline void Push(lua_State* L, int value) { lua_pushnumber(L, value); }
inline void Push(lua_State* L, unsigned int value) { lua_pushnumber(L, value); }
inline void Push(lua_State* L, long value) { lua_pushnumber(L, value); }
inline void Push(lua_State* L, unsigned long value) { lua_pushnumber(L, value); }
inline void Push(lua_State* L, double value) { lua_pushnumber(L, (lua_Number)value); }
inline void Push(lua_State* L, float value) { lua_pushnumber(L, (lua_Number)value); }
inline void Push(lua_State* L, const char* value) { lua_pushstring(L, value); }
inline void Push(lua_State* L, std::string value) { lua_pushstring(L, value.c_str()); }
inline void Push(lua_State* L, const char* value, int len) { lua_pushlstring(L, value, len); }
inline void Push(lua_State* L) { }
inline void Push(lua_State* L, lua_CFunction value) { lua_pushcclosure(L, value, 0); }
inline void Push(lua_State* L, const void* value) { lua_pushlightuserdata(L, (void*)value); }
inline void Push(lua_State* L, const LuaUserData& value){ *(void **)(lua_newuserdata(L, sizeof(void *))) = (void*)value.m_value; }
template <typename T, typename... Ts>
inline void Push(lua_State* L, const T value, const Ts... values)
{
Push(L, value);
Push(L, values...);
}
template<typename RT>
inline RT Read(lua_State* L, int index) const;
template <size_t, typename... Ts>
struct _pop
{
public:
typedef std::tuple<Ts...> type;
static type apply(lua_State* L)
{
auto result = worker<Ts...>(L, 1);
lua_pop(L, sizeof...(Ts));
return result;
}
private:
template <typename T>
static std::tuple<T> worker(lua_State* L, const int index)
{
return std::make_tuple(GetInstance().Read<T>(L, index));
}
template <typename T1, typename T2, typename... Rest>
static std::tuple<T1, T2, Rest...> worker(lua_State* L, const int index)
{
std::tuple<T1> head = std::make_tuple(GetInstance().Read<T1>(index));
return std::tuple_cat(head, worker<T2, Rest...>(L, index + 1));
}
};
template <typename T>
struct _pop<1, T>
{
typedef T type;
static type apply(lua_State* L)
{
T result = GetInstance().Read<T>(L, -1);
lua_pop(L, 1);
return result;
}
};
template <typename... T>
typename _pop<sizeof...(T), T...>::type Pop(lua_State* L)
{
return _pop<sizeof...(T), T...>::apply(L);
}
/*template <typename Ret, typename ...OtherRet, typename... Args, class = typename std::enable_if<(sizeof...(OtherRet) != 0 && std::is_void<Ret>::value), int>::type>
typename _pop<1 + sizeof... (OtherRet), Ret, OtherRet...>::type CallGlobal(lua_State* L, const std::string &fun, const Args&... args)
{
lua_getfield(L, -1,fun.c_str());
const int num_args = sizeof...(Args);
const int num_ret = sizeof...(OtherRet)+1;
GetInstance().Push(L, args...);
lua_call(L, num_args, num_ret);
return GetInstance().Pop<Ret, OtherRet...>(L);
}
template <typename Ret, typename ...OtherRet, typename... Args, class = typename std::enable_if<(sizeof...(OtherRet) == 0 && std::is_void<Ret>::value), int>::type>
void CallGlobal(lua_State* L, const std::string &fun, const Args&... args)
{
lua_getfield(L, -1,fun.c_str());
const int num_args = sizeof...(Args);
GetInstance().Push(L, args...);
lua_call(L, num_args, 0);
}*/
template <typename Ret, typename... Args, class = typename std::enable_if<(!std::is_void<Ret>::value), int>::type>
Ret CallGlobal(lua_State* L, const std::string &fun, const Args&... args)
{
lua_getfield(L, -1, fun.c_str());
const int num_args = sizeof...(Args);
GetInstance().Push(L, args...);
lua_call(L, num_args, 1);
return GetInstance().Pop<Ret>(L);
}
template <typename Ret, typename... Args, class = typename std::enable_if<(std::is_void<Ret>::value), int>::type>
void CallGlobal(lua_State* L, const std::string &fun, const Args&... args)
{
lua_getfield(L, -1, fun.c_str());
const int num_args = sizeof...(Args);
GetInstance().Push(L, args...);
lua_call(L, num_args, 0);
}
template <typename Ret,typename... Args, class = typename std::enable_if<(!std::is_void<Ret>::value), int>::type>
Ret CallMemberFunction(lua_State* L, const std::string &objName, const std::string &funcName, const Args&... args)
{
lua_getfield(L, -1, objName.c_str());
lua_getfield(L, -1, funcName.c_str());
const int num_args = sizeof...(Args) + 1;
lua_pushvalue(L, -2);
GetInstance().Push(L, args...);
lua_call(L, num_args, 1);
return GetInstance().Pop<Ret>(L);
}
template <typename Ret, typename ...OtherRet, typename... Args, class = typename std::enable_if<(std::is_void<Ret>::value), int>::type>
void CallMemberFunction(lua_State* L, const std::string &objName, const std::string &funcName, const Args&... args)
{
lua_getfield(L, -1, objName.c_str());
lua_getfield(L, -1, funcName.c_str());
const int num_args = sizeof...(Args) + 1;
lua_pushvalue(L, -2);
GetInstance().Push(L, args...);
//stackDump(L);
lua_call(L, num_args, 0);
}
void CreateGlobalObject(lua_State* L, const std::string& objName, const std::string& metatableName = "", void* userData = 0)
{
if (!metatableName.empty())
{
lua_getfield(L, -1, metatableName.c_str());
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
}
else
{
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
}
if (userData != 0)
{
lua_pushlightuserdata(L, userData);
lua_setfield(L, -2, "__object");
}
lua_setfield(L, -2, objName.c_str());
}
template <typename Ret>
Ret GetGlobal(lua_State* L, std::string name)
{
lua_getfield(L, -1, name.c_str());
return GetInstance().Pop<Ret>(L);
}
template <typename Ret>
std::vector<Ret> TableToVector(lua_State* L, const std::string& tableName)
{
std::vector<Ret> result;
lua_getfield(L, -1, tableName.c_str());
int size = luaL_len(L, -1);
lua_pushnil(L);
for (int i = 1; i <= size; ++i)
{
lua_next(L, -2);
result.push_back(GetInstance().Pop<Ret>(L));
}
lua_pop(L, -1);
return result;
}
template <typename Ret1, typename Ret2 >
std::vector<std::pair<Ret1, Ret2>> TableToVector(lua_State* L, const std::string& tableName)
{
std::vector<std::pair<Ret1, Ret2>> result;
lua_getfield(L, -1, tableName.c_str());
int size = (int)luaL_len(L, -1);
lua_pushnil(L);
std::pair<Ret1, Ret2> p;
for (int i = 1; i <= size; ++i)
{
lua_next(L, -2);
lua_pushnil(L);
lua_next(L, -2);
p.first = GetInstance().Pop<Ret1>(L);
lua_next(L, -2);
p.second = GetInstance().Pop<Ret2>(L);
result.push_back(p);
lua_pop(L, -4);
}
lua_pop(L, -1);
return result;
}
template <typename Ret1, typename Ret2 >
std::vector<std::pair<Ret1, Ret2>> TableToVectorFromTop(lua_State* L)
{
std::vector<std::pair<Ret1, Ret2>> result;
int size = (int)luaL_len(L, -1);
lua_pushnil(L);
std::pair<Ret1, Ret2> p;
for (int i = 1; i <= size; ++i)
{
lua_next(L, -2);
lua_pushnil(L);
lua_next(L, -2);
p.first = GetInstance().Pop<Ret1>(L);
lua_next(L, -2);
p.second = GetInstance().Pop<Ret2>(L);
result.push_back(p);
lua_pop(L, 2);
}
lua_pop(L, -1);
return result;
}
static void stackDump(lua_State *L)
{
int i;
int top = lua_gettop(L);
for (i = 1; i <= top; i++) { /* repeat for each level */
int t = lua_type(L, i);
switch (t) {
case LUA_TSTRING: /* strings */
printf("`%s'", lua_tostring(L, i));
break;
case LUA_TBOOLEAN: /* booleans */
printf(lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER: /* numbers */
printf("%g", lua_tonumber(L, i));
break;
default: /* other values */
printf("%s", lua_typename(L, t));
break;
}
printf(" "); /* put a separator */
}
printf("\n"); /* end the listing */
}
private:
LuaCallDispatcher() {};
};
template <>
struct LuaCallDispatcher::_pop<1, void>
{
typedef void type;
static void apply(lua_State* L)
{
return void();
}
};
template<>
inline int LuaCallDispatcher::Read(lua_State* L, int index) const
{
return (int)lua_tointeger(L, index);
}
template<>
inline float LuaCallDispatcher::Read(lua_State* L, int index) const
{
return (float)lua_tonumber(L, index);
}
template<>
inline double LuaCallDispatcher::Read(lua_State* L, int index) const
{
return lua_tonumber(L, index);
}
template<>
inline bool LuaCallDispatcher::Read(lua_State* L, int index) const
{
return lua_toboolean(L, index) != 0;
}
template<>
inline const char* LuaCallDispatcher::Read(lua_State* L, int index) const
{
return lua_tostring(L, index);
}
template<>
inline std::string LuaCallDispatcher::Read(lua_State* L, int index) const
{
return lua_tostring(L, index);
}
template<>
inline lua_CFunction LuaCallDispatcher::Read(lua_State* L, int index) const
{
return lua_tocfunction(L, index);
}
template<>
inline LuaCallDispatcher::LuaUserData LuaCallDispatcher::Read(lua_State* L, int index) const
{
return lua_touserdata(L, index);
}
#endif