-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.hpp
605 lines (540 loc) · 14.4 KB
/
api.hpp
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#pragma once
#include <stdexcept>
#include <tuple>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "util.hpp"
/**
* api.hpp
* Contains free functions in the glua::api (and glua::api::detail)
* namespace which provide a simple wrapper for the lua c api. Other
* than the use of a *reference* to lua_State instead of a pointer,
* only a few functions have been modified, such as push; the version
* of push here takes a variable number of arguments of arbitrary types
* and pushes them all, in order onto the stack. All functions are
* inline, since most simply forward their arguments onto the
* corresponding call in the lua c api.
*/
namespace glua {
namespace api {
/**
* Create a new lua userdata of, allocating space for a specific
* type of object and return a reference. NOTE: This does NOT call
* the constructor for the object, so if you need to construct
* the object, you must use the placement new operator
*/
template<typename T>
inline T& newUserdata(lua_State& l) {
T* t = static_cast<T*>(lua_newuserdata(&l, sizeof(T)));
if(t == nullptr) throw std::runtime_error("Error: lua_newuserdata returned null pointer");
luaL_newmetatable(&l, ::glua::detail::type_traits<T>::name);
lua_setmetatable(&l, -2);
return *t;
}
template<typename T>
inline T& newUserdata(lua_State& l, const char* name) {
T* t = static_cast<T*>(lua_newuserdata(&l, sizeof(T)));
if(t == nullptr) throw std::runtime_error("Error: lua_newuserdata returned null pointer");
luaL_newmetatable(&l, name);
lua_setmetatable(&l, -2);
return *t;
}
/**
* If the object at index index is userdata, return a reference
* to it. Otherwise throw an error.
*/
template<typename T>
inline T& getUserdata(lua_State& l, int index) {
T* t = static_cast<T*>(luaL_checkudata(&l, index, ::glua::detail::type_traits<T>::name));
if(t == nullptr) throw std::runtime_error("Error: trying to get non-userdata as userdata");
else return *t;
}
template<typename T>
inline T& getUserdata(lua_State& l, int index, const char* name) {
T* t = static_cast<T*>(luaL_checkudata(&l, index, name));
if(t == nullptr) throw std::runtime_error("Error: trying to get non-userdata as userdata");
else return *t;
}
namespace detail {
/**
* Template struct containing a single method
* which pushes an arbitrary value onto the lua stack.
* Default method of doing this is to create a new
* userdata and copy the value into the userdata.
*
* This set of methods is implemented using a struct
* because partial specialization is required
*/
template<typename T>
struct _push_impl {
inline static void push(lua_State& l, T val) {
T* data = &newUserdata<T>(l);
new (data) T(val);
}
};
/**
* Push implementaiton for arbitrary reference types.
* Creates a new userdata of T* and assigns it the
* value of &ref
*/
/*
template<typename T>
struct _push_impl<T&> {
inline static void push(lua_State& l, T& ref) {
_push_impl<T*>::push(l, &ref);
}
};
*/
/**
* Push implementaiton for arbitrary pointer types.
* Creates a new userdata of T* and assigns it the
* value of ptr
*/
template<typename T>
struct _push_impl<T*> {
inline static void push(lua_State& l, T* ptr) {
newUserdata<T*>(l) = ptr;
}
};
/**
* Push implementaiton for bool
*/
template<>
struct _push_impl<bool> {
inline static void push(lua_State& l, bool val) {
lua_pushboolean(&l, val);
}
};
/**
* Push implementaiton for integers
*/
template<>
struct _push_impl<lua_Integer> {
inline static void push(lua_State& l, lua_Integer val) {
lua_pushinteger(&l, val);
}
};
/**
* Push implementaiton for unsigned integers
*/
template<>
struct _push_impl<lua_Unsigned> {
inline static void push(lua_State& l, lua_Unsigned val) {
lua_pushunsigned(&l, val);
}
};
/**
* Push implementaiton for the lua_Number type (usually double)
*/
template<>
struct _push_impl<lua_Number> {
inline static void push(lua_State& l, lua_Number val) {
lua_pushnumber(&l, val);
}
};
/**
* Push implementaiton for c-style strings
*/
template<>
struct _push_impl<const char*> {
inline static void push(lua_State& l, const char* val) {
lua_pushstring(&l, val);
}
};
/**
* Push implementaiton for strings
*/
template<>
struct _push_impl<std::string> {
inline static void push(lua_State& l, std::string val) {
lua_pushlstring(&l, val.c_str(), val.length());
}
};
/**
* Push implementaiton for the nullptr type, which pushes nil
* to the lua stack.
*/
template<>
struct _push_impl<std::nullptr_t> {
inline static void push(lua_State& l, std::nullptr_t) {
lua_pushnil(&l);
}
};
/**
* Template struct supplying an implementaiton to
* push an arbitrary number of arbitrary values onto
* the lua stack.
*
* This method is implemented using a struct because
* partial specialization is required.
*/
template<typename... T>
struct _push_n_impl {};
/**
* For more than one argument, push the first, then
* push the rest (instantiating a new copy of this struct)
*/
template<typename T1, typename... T>
struct _push_n_impl<T1, T...> {
inline static void push(lua_State& l, T1 val1, T... vals) {
_push_impl<T1>::push(l, val1);
_push_n_impl<T...>::push(l, vals...);
}
};
/**
* Specialization for pushing a single value.
*/
template<typename T>
struct _push_n_impl<T> {
inline static void push(lua_State& l, T val) {
_push_impl<T>::push(l, val);
}
};
} // namespace detail
/**
* Push any number of arbitrary values onto the lua stack.
*/
template<typename... T>
inline void push(lua_State& l, T... vals) {
detail::_push_n_impl<T...>::push(l, vals...);
}
namespace detail {
/**
* Template struct supplying implementaiton of setTable.
*/
template<typename Key, typename Value>
struct _set_table_impl {
inline static void set_table(lua_State& l, Key key, Value value) {
push(l, key, value);
lua_settable(&l, -3);
}
};
/**
* Sepcialization for c-string key type using the more efficient lua_setfield funciton.
*/
template<typename Value>
struct _set_table_impl<const char*, Value> {
inline static void set_table(lua_State& l, const char* key, Value value) {
push(l, value);
lua_setfield(&l, -2, key);
}
};
/**
* Sepcialization for string key type using the more efficient lua_setfield funciton.
*/
template<typename Value>
struct _set_table_impl<std::string, Value> {
inline static void set_table(lua_State& l, std::string key, Value value) {
_set_table_impl<const char*, Value>::set_table(l, key.c_str(), value);
}
};
} // namespace detail
/**
* Sets t[key] = value,
* where t is the table at the top of the stack.
*/
template<typename Key, typename Value>
inline void setTable(lua_State& l, Key key, Value value) {
detail::_set_table_impl<Key, Value>::set_table(l, key, value);
}
/**
* Sets the global named by key to the specified value
*/
template<typename Value>
inline void setGlobal(lua_State& l, const char* key, Value value) {
push(l, value);
lua_setglobal(&l, key);
}
/**
* Pushes the global named by key onto the stack.
*/
inline void getGlobal(lua_State& l, const char* key) {
lua_getglobal(&l, key);
}
/**
* Pushes the global named by key onto the stack.
*/
inline void getGlobal(lua_State& l, std::string key) {
lua_getglobal(&l, key.c_str());
}
/**
* Pushes the value t[key] onto the stack, where
* t is the table at the top of the stack.
*/
template<typename Key>
inline void getTable(lua_State& l, Key key) {
push(l, key);
lua_gettable(&l, -2);
}
/**
* Specialization of getTable for string key type.
* Implemented using the more efficient lua_getfield funciton.
*/
template<>
inline void getTable(lua_State& l, std::string key) {
lua_getfield(&l, -1, key.c_str());
}
/**
* Specialization of getTable for c-string key type.
* Implemented using the more efficient lua_getfield funciton.
*/
template<>
inline void getTable(lua_State& l, const char* key) {
lua_getfield(&l, -1, key);
//luaL_getsubtable(&l, -1, key);
}
namespace detail {
/**
* Template struct supplying implementation of checkGet.
*
* Default implementation for arbitrary type T is to treat the
* value on the stack as userdata of type T and return a copy
* of the value.
*
* This method is implemented using a struct because partial
* specialization is requied.
*/
template<typename T>
struct _check_get_impl {
/*
inline static auto get(lua_State& l, int index)
-> decltype(T::get(l, index))
{
return T::get(l, index);
}
*/
inline static T get(lua_State& l, int index)
{
return getUserdata<T>(l, index);
}
};
/**
* Partial specialization for reference types.
* Treats the value on the top of the stack as
* userdata of a pointer to type T and returns
* a reference, as long as the pointer is not null.
* If the pointer is null, throws an exception.
*/
/*
template<typename T>
struct _check_get_impl<T&> {
inline static T& get(lua_State& l, int index)
{
T* ret = getUserdata<T*>(l, index);
if(ret == nullptr) throw std::runtime_error("Lua returned null reference");
return *ret;
}
};
*/
/**
* Partial specialization for pointer types.
* Treats the value on the top of the stack as
* userdata of pointer to type T and returns it.
*/
template<typename T>
struct _check_get_impl<T*> {
inline static T* get(lua_State& l, int index)
{
return getUserdata<T*>(l, index);
}
};
/**
* Partial specialization for booleans.
*/
template<>
struct _check_get_impl<bool> {
inline static bool get(lua_State& l, int index) {
return lua_toboolean(&l, index);
}
};
/**
* Partial specialization for integers.
*/
template<>
struct _check_get_impl<lua_Integer> {
inline static lua_Integer get(lua_State& l, int index) {
return luaL_checkinteger(&l, index);
}
};
/**
* Partial specialization for unsigned integers.
*/
template<>
struct _check_get_impl<lua_Unsigned> {
inline static lua_Unsigned get(lua_State& l, int index) {
return luaL_checkunsigned(&l, index);
}
};
/**
* Partial specialization for the lua_Number type (usually double).
*/
template<>
struct _check_get_impl<lua_Number> {
inline static lua_Number get(lua_State& l, int index) {
return luaL_checknumber(&l, index);
}
};
/**
* Partial specialization for strings.
*/
template<>
struct _check_get_impl<std::string> {
inline static std::string get(lua_State& l, int index) {
size_t len = 0;
const char* str = luaL_checklstring(&l, index, &len);
return std::string(str, len);
}
};
/**
* Partial specialization for c-strings.
*/
template<>
struct _check_get_impl<const char*> {
inline static const char* get(lua_State& l, int index) {
return luaL_checkstring(&l, index);
}
};
/**
* Template struct providing an implementation for getting
* an arbitrary number of arbitrary values from the lua stack.
*
* This method is implemented using a struct because it requires
* partial specialization.
*/
template<typename T, int N = 1>
struct _check_get_n_impl {
inline static auto get(lua_State& l, int index)
-> decltype(_check_get_impl<T>::get(l, -(N-index)))
{
return _check_get_impl<T>::get(l, -(N-index));
}
inline static auto get(lua_State& l)
-> decltype( _check_get_impl<T>::get(l, -N))
{
return _check_get_impl<T>::get(l, -N);
}
};
/**
* Partial specialization for tuple type, allowing the
* retrieval of multiple values.
*/
template<typename... T>
struct _check_get_n_impl<std::tuple<T...>> {
/**
* Helper function which takes an _index_list to provide the
* _check_get_n_impl instances with the required index parameter
*/
template<int... I>
inline static auto work(lua_State& l, ::glua::detail::_index_list<I...>)
-> decltype(make<std::tuple>(_check_get_n_impl<T,sizeof...(I)>::get(l, I)...))
{
return make<std::tuple>(_check_get_n_impl<T,sizeof...(I)>::get(l, I)...);
}
inline static auto get(lua_State& l)
-> decltype(work(l, typename ::glua::detail::_build_index_list<sizeof...(T)>::build()))
{
// Call the helper function to do all the work.
return work(l, typename ::glua::detail::_build_index_list<sizeof...(T)>::build());
}
};
} // namespace detail
/**
* Get a value from the lua stack.
* to get multiple values from the stack
* use checkGet<std::tuple<...>>
*
* NOTE: does not pop any values from the stack.
*/
template<typename T>
inline auto checkGet(lua_State& l)
#if __cplusplus <= 201103L
-> decltype(detail::_check_get_n_impl<T>::get(l))
#endif
{
return detail::_check_get_n_impl<T>::get(l);
}
/**
* Clear the stack.
*/
inline void clearStack(lua_State& l) {
lua_settop(&l, 0);
}
/**
* Create a new lua_State.
*/
inline lua_State& open() {
lua_State* l = luaL_newstate();
if(l == nullptr) throw std::runtime_error("Couldn't create new lua state!");
return *l;
}
/**
* Open the core lua libraries.
*/
inline void openLibs(lua_State& l) {
luaL_openlibs(&l);
}
/**
* Close a lus_State.
*/
inline void close(lua_State& l) {
lua_close(&l);
}
/**
* Execute a lua object on the stack.
*/
inline void call(lua_State& l, int nargs, int nret) {
lua_call(&l, nargs, nret);
}
/**
* Load a lua file and run it.
*/
inline void loadFile(lua_State& l, const char* filename) {
luaL_dofile(&l, filename);
}
/**
* Load a lua file and run it.
*/
inline void loadFile(lua_State& l, std::string filename) {
luaL_dofile(&l, filename.c_str());
}
/**
* Load and run a string as a lua chunk.
*/
inline void loadString(lua_State& l, const char* chunk) {
luaL_dostring(&l, chunk);
}
/**
* Load and run a string as a lua chunk.
*/
inline void loadString(lua_State& l, std::string chunk) {
luaL_dostring(&l, chunk.c_str());
}
/**
* Create a reference to the lua object on the top of
* the stack.
*/
inline int ref(lua_State& l) {
return luaL_ref(&l, LUA_REGISTRYINDEX);
}
/**
* Delete a reference.
*/
inline void unref(lua_State& l, int ref) {
luaL_unref(&l, LUA_REGISTRYINDEX, ref);
}
/**
* Push the value associated with a particular
* reference onto the stack.
*/
inline void getRef(lua_State& l, int ref) {
lua_rawgeti(&l, LUA_REGISTRYINDEX, ref);
}
/**
* Get an upvalue index.
*/
inline int upvalueIndex(int index) {
return lua_upvalueindex(index);
};
} // namespace api
} // namespace glua