forked from Vantskruv/wejoy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLuaScript.h
154 lines (129 loc) · 3.42 KB
/
LuaScript.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
#ifndef LUASCRIPT_H
#define LUASCRIPT_H
#include <string>
#include <vector>
#include <iostream>
extern "C" {
# include <lua5.2/lua.h>
# include <lua5.2/lauxlib.h>
# include <lua5.2/lualib.h>
}
class LuaScript {
public:
LuaScript(const std::string&);
~LuaScript();
void printError(const std::string& variableName, const std::string& reason);
std::vector<int> getIntVector(const std::string& name);
std::vector<std::string> getStringVector(const std::string& name);
std::vector<std::string> getTableKeys(const std::string& name);
//void call_device_function(const device_function&, int);
void call_device_function(const std::string&, int);
void pushcfunction(int (*)(lua_State*), const std::string&);
bool isOpen(){return L;};
inline void clean() {
int n = lua_gettop(L);
lua_pop(L, n);
}
template<typename T>
T get(const std::string& variableName, bool& _noerr)
{
_noerr = true;
if(!L)
{
printError(variableName, "Script is not loaded");
_noerr = false;
return lua_getdefault<T>();
}
T result;
if(lua_gettostack(variableName))
{ // variable succesfully on top of stack
result = lua_get<T>(variableName);
}
else
{
_noerr = false;
result = lua_getdefault<T>();
}
clean();
return result;
}
bool lua_gettostack(const std::string& variableName) {
level = 0;
std::string var = "";
for(unsigned int i = 0; i < variableName.size(); i++) {
if(variableName.at(i) == '.') {
if(level == 0) {
lua_getglobal(L, var.c_str());
} else {
lua_getfield(L, -1, var.c_str());
}
if(lua_isnil(L, -1)) {
//printError(variableName, "1:At level " + std::to_string(level) + ", " + var + " is not defined");
return false;
} else {
var = "";
level++;
}
} else {
var += variableName.at(i);
}
}
if(level == 0) {
lua_getglobal(L, var.c_str());
} else {
lua_getfield(L, -1, var.c_str());
}
if(lua_isnil(L, -1)) {
//printError(variableName, "2:At level " + std::to_string(level) + ", " + var + " is not defined");
return false;
}
return true;
}
// Generic get
template<typename T>
T lua_get(const std::string& variableName) {
return 0;
}
template<typename T>
T lua_getdefault() {
return 0;
}
private:
lua_State* L;
int level;
void _set_global_keys();
};
// Specializations
template <>
inline bool LuaScript::lua_get<bool>(const std::string& variableName) {
return (bool)lua_toboolean(L, -1);
}
template <>
inline float LuaScript::lua_get<float>(const std::string& variableName) {
if(!lua_isnumber(L, -1)) {
printError(variableName, "Not a number");
}
return (float)lua_tonumber(L, -1);
}
template <>
inline int LuaScript::lua_get<int>(const std::string& variableName) {
if(!lua_isnumber(L, -1)) {
printError(variableName, "Not a number");
}
return (int)lua_tonumber(L, -1);
}
template <>
inline std::string LuaScript::lua_get<std::string>(const std::string& variableName) {
std::string s = "null";
if(lua_isstring(L, -1)) {
s = std::string(lua_tostring(L, -1));
} else {
printError(variableName, "Not a string");
}
return s;
}
template<>
inline std::string LuaScript::lua_getdefault<std::string>() {
return "null";
}
#endif