-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.hpp
75 lines (62 loc) · 1.46 KB
/
state.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
#pragma once
#include <stdexcept>
#include "api.hpp"
#include "global.hpp"
#include "ref.hpp"
#include "cfunction.hpp"
namespace glua {
class state {
public:
state(bool openLibs = true) : l(api::open()) {
if(openLibs) api::openLibs(l);
}
// No copying
state(state&) = delete;
state& operator=(state&) = delete;
~state() {
api::close(l);
}
/**
* Access a global variable.
*/
inline global operator[](const char* key) {
return global(l, key);
}
/**
* Load and run a lua file.
*/
inline void load(const char* filename) {
api::loadFile(l, filename);
}
/**
* Load and run a lua file.
*/
inline void load(std::string filename) {
api::loadFile(l, filename);
}
/**
* Load and run a c-string as a lua chunk.
*/
inline void run(const char* chunk) {
api::loadString(l, chunk);
}
/**
* Load and run a string as a lua chunk.
*/
inline void run(std::string chunk) {
api::loadString(l, chunk);
}
template<typename FuncT, FuncT func>
void registerFunction(const char* name) {
lua_pushcclosure(&l, &cfunction<FuncT, func>::wrapper, 0);
lua_setglobal(&l, name);
}
template<typename Functor>
void registerFunction(const char* name, Functor f) {
cfunctor<Functor>::push(l, f);
lua_setglobal(&l, name);
}
private:
lua_State& l;
};
} // namespace glua