-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLua.hh
More file actions
363 lines (307 loc) · 10.1 KB
/
Lua.hh
File metadata and controls
363 lines (307 loc) · 10.1 KB
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
#pragma once
#include <string>
#include <vector>
#include <functional>
#include <tuple>
#include <type_traits>
#include <iostream>
extern "C" {
#include <lua.h>
};
namespace util {
class LuaClass {
private:
unsigned int references;
bool track_references;
protected:
public:
LuaClass():
references(0),
track_references(false)
{}
void enable_tracking() {
track_references = true;
}
void reference() {
references++;
}
void collect() {
if (track_references && !--references)
delete this;
}
};
class Lua {
protected:
template <typename T>
int ret(const T r) {
static_assert(std::is_convertible<T, LuaClass*>::value,
"LuaClass * required!");
object((LuaClass *)r, std::remove_pointer<T>::type::class_name());
return 1;
}
template <class T>
T arg(const int i) {
if (std::is_base_of<util::LuaClass, T>::value) {
return *(T *)object(i);
} else {
return *(T *)userdata(i);
}
}
template <class T>
T* argp(const int i) {
if (std::is_base_of<util::LuaClass, T>::value) {
return (T *)object(i);
} else {
return (T *)userdata(i);
}
}
template <typename T, typename T1, typename... Args>
std::tuple<T, T1, Args...> args(const int i = 1) {
return std::tuple_cat(arg<T>(i), args<T1, Args...>(i + 1));
}
template <typename T>
std::tuple<T> args(const int i = 1) {
return std::tuple<T>(arg<T>(i));
}
template <int N> struct apply_method {
template <class T, typename R, typename... MethodArgs,
typename... TupleArgs, typename... Args>
static R apply(T* o, R (T::*method)(MethodArgs...),
std::tuple<TupleArgs...>& t, Args... args) {
return apply_method<N-1>::
apply(o, method, t, std::get<N-1>(t), args...);
}
};
template <int N> struct apply_function {
template <typename R, typename... FunctionArgs, typename... TupleArgs,
typename... Args>
static R apply(R (*function)(FunctionArgs...),
std::tuple<TupleArgs...>& t, Args... args) {
return apply_function<N-1>::
apply(function, t, std::get<N-1>(t), args...);
}
};
template <int N, class T> struct apply_constructor {
template <typename... TupleArgs, typename... Args>
static T * apply(std::tuple<TupleArgs...>& t, Args... args) {
return apply_constructor<N-1, T>::
apply(t, std::get<N-1>(t), args...);
}
};
private:
bool del;
lua_State * vm;
std::vector<std::function<int(Lua&)> *> lambdas;
static int call(lua_State *vm);
public:
Lua(lua_State *vm);
Lua();
~Lua();
void lambda(std::function<int(Lua&)> *function, const std::string& name);
void load(const std::string& name, const int i = -1);
void pop(const int i = 1);
void remove(const int i);
void table();
void metatable(const int i = -2);
void closure(int (*)(lua_State *), const int i = 1);
void copy(const int i = -1);
void save(const std::string& name, const int i = -2);
void file(const std::string& name);
bool is_nil(const int i = -1);
void object(const LuaClass *, const std::string& name);
LuaClass * object(const int i = -1);
void userdata(const void *);
void * userdata(const int i = -1);
void number(const lua_Number);
lua_Number tonumber(const int i = -1);
void string(const std::string&);
std::string string(const int i = -1);
void boolean(const bool);
bool boolean(const int i = -1);
class LuaObject : public LuaClass {
private:
protected:
public:
static void export_class(Lua& vm);
static void export_me(Lua& vm);
static const std::string class_name();
};
/*
* class LuaObject: public LuaClass {};
* class P : public LuaClass {};
* class T : public LuaClass {};
*
* -> T::export_me(;
* -> export_class<T, P>();
* -> T::class_name();
* -> P::class_name();
* -> P::export_me(;
* -> export_class<P>();
* -> P::class_name();
* -> LuaObject::class_name();
* -> LuaObject::export_me(;
* -> export_class<LuaObject>();
* -> LuaObject::class_name();
* -> LuaObject::class_name();
* -> LuaObject::export_class(;
* -> P::export_class(;
* -> T::export_class(;
*/
template<class T, class P = LuaObject>
void export_class() {
static_assert(std::is_base_of<util::LuaClass, T>::value,
"LuaClass implementation expected!");
auto name = T::class_name();
auto parent_name = P::class_name();
bool parent = name != parent_name;
if (parent) {
P::export_me(*this);
}
load(name);
if (!is_nil()) {
pop();
return;
}
pop();
table();
table();
copy(-2);
save("__index");
save("mtab");
if (parent) {
load(parent_name);
load("mtab");
metatable(-3);
pop();
}
T::export_class(*this);
save(name);
}
template <typename R, class T, typename... Args>
void export_method(const std::string& name,
R (T::*method)(Args...)) {
auto function = new std::function<int(Lua&)>([method] (Lua& vm) -> int {
auto tuple = vm.args<Args...>(2);
return vm.ret(
apply_method<sizeof...(Args)>
::apply(vm.argp<T>(1), method, tuple));
});
lambda(function, name);
}
template <class T, typename... Args>
void export_method(const std::string& name,
void (T::*method)(Args...)) {
auto function = new std::function<int(Lua&)>([method] (Lua& vm) -> int {
auto tuple = vm.args<Args...>(2);
apply_method<sizeof...(Args)>::apply(vm.argp<T>(1), method, tuple);
return 0;
});
lambda(function, name);
}
template <typename R, class T>
void export_method(const std::string& name, R (T::*method)()) {
auto function = new std::function<int(Lua&)>([method] (Lua& vm) -> int {
return vm.ret((vm.argp<T>(1)->*method)());
});
lambda(function, name);
}
template <class T>
void export_method(const std::string& name, void (T::*method)()) {
auto function = new std::function<int(Lua&)>([method] (Lua& vm) -> int {
(vm.argp<T>(1)->*method)();
return 0;
});
lambda(function, name);
}
template <typename R, typename... Args>
void export_function(const std::string& name,
R (*callback)(Args...)) {
auto function = new std::function<int(Lua&)>([callback] (Lua& vm) -> int {
auto tuple = vm.args<Args...>();
return vm.ret(
apply_function<sizeof...(Args)>::apply(callback, tuple));
});
lambda(function, name);
}
template <typename... Args>
void export_function(const std::string& name,
void (*callback)(Args...)) {
auto function = new std::function<int(Lua&)>([callback] (Lua& vm) -> int {
auto tuple = vm.args<Args...>();
apply_function<sizeof...(Args)>
::apply(callback, tuple);
return 0;
});
lambda(function, name);
}
template <typename R>
void export_function(const std::string& name, R (*callback)()) {
auto function = new std::function<int(Lua&)>([callback] (Lua& vm) -> int {
return vm.ret((*callback)());
});
lambda(function, name);
}
void export_function(const std::string& name, void (*callback)());
template <class T, typename Arg1, typename... Args>
void export_constructor() {
auto function = new std::function<int(Lua&)>([] (Lua& vm) -> int {
auto tuple = vm.args<Arg1, Args...>(1);
T *object =
apply_constructor<sizeof...(Args), T>::apply(tuple);
static_cast<LuaClass *>(object)->enable_tracking();
return vm.ret(object);
});
lambda(function, "new");
}
template <class T>
void export_constructor() {
auto function = new std::function<int(Lua&)>([] (Lua& vm) -> int {
T *object = new T();
static_cast<LuaClass *>(object)->enable_tracking();
return vm.ret(object);
});
lambda(function, "new");
}
};
template <>
int Lua::ret<lua_Number>(const lua_Number r);
template <>
int Lua::ret<std::string>(const std::string r);
template <>
int Lua::ret<bool>(const bool r);
template <>
int Lua::ret<int>(const int r);
template <>
std::string Lua::arg<std::string>(const int i);
template <>
lua_Number Lua::arg<lua_Number>(const int i);
template <>
lua_Integer Lua::arg<lua_Integer>(const int i);
template <>
int Lua::arg<int>(const int i);
template <>
bool Lua::arg<bool>(const int i);
template <> struct Lua::apply_method<0> {
template <class T, typename R, typename... MethodArgs,
typename... TupleArgs, typename... Args>
static R apply(T* o, R (T::*method)(MethodArgs...),
std::tuple<TupleArgs...>& t, Args... args) {
return (o->*method)(args...);
}
};
template <> struct Lua::apply_function<0> {
template <typename R, typename... FunctionArgs, typename... TupleArgs,
typename... Args>
static R apply(R (*function)(FunctionArgs...),
std::tuple<TupleArgs...>& t, Args... args) {
return
(*function)(args...);
}
};
template <class T> struct Lua::apply_constructor<0, T> {
template <typename... TupleArgs, typename... Args>
static T * apply(std::tuple<TupleArgs...>& t, Args... args) {
return new T(args...);
}
};
} // namespace util;