-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLuaWrapper.h
200 lines (155 loc) · 5.79 KB
/
LuaWrapper.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
#ifndef LUA_WRAPPER_H
#define LUA_WRAPPER_H
#include <string>
#include <vector>
#include <iostream>
#include <functional>
#include <map>
#include <memory>
#include <stdio.h>
#include "LuaEnvironment.h"
class LuaWrapper
{
public:
~LuaWrapper();
static LuaWrapper& GetInstance()
{
static LuaWrapper instance;
return instance;
}
void CleanStack()
{
LuaCallDispatcher::GetInstance().CleanStack(m_luaState);
}
void LoadScript(const std::string& environmentName, const std::string& filename);
std::vector<int> GetIntVector(const std::string& name);
std::vector<std::string> GetTableKeys(const std::string& name);
template <typename Ret, typename... Args>
void RegisterGlobalFunction(const std::string& envName, const std::string &name, std::function<Ret(Args...)> function)
{
if (!FindLuaEnvironment(envName))
return;
m_environmentMap[envName]->RegisterGlobalFunction<Ret>(m_luaState, name, function);
CleanStack();
}
template <typename Class, typename Ret, typename... Args, class = typename std::enable_if<!(std::is_void<Ret>::value), int>::type>
void RegisterMemberFunction(const std::string& envName, Class *pClass, const std::string& metatableName, const std::string& name, Ret(Class::*function)(Args...), const std::string& funcName)
{
if (!FindLuaEnvironment(envName))
return;
m_environmentMap[envName]->RegisterMemberFunction(m_luaState, pClass, metatableName, name, function, funcName);
CleanStack();
}
template <typename Class, typename Ret, typename... Args, class = typename std::enable_if<(std::is_void<Ret>::value), int>::type>
void RegisterMemberFunction(const std::string& envName, Class *pClass, const std::string& metatableName, const std::string& name, void(Class::*function)(Args...), const std::string& funcName)
{
if (!FindLuaEnvironment(envName))
return;
m_environmentMap[envName]->RegisterMemberFunction<Class,Ret>(m_luaState, pClass, metatableName, name, function, funcName);
CleanStack();
}
template <typename Ret, typename... Args, class = typename std::enable_if<!(std::is_void<Ret>::value), int>::type>
Ret RunFunction(const std::string& envName,const FuncNameInfo& nameInfo, Args... args)
{
if (!FindLuaEnvironment(envName))
return Ret();
Ret result = m_environmentMap[envName]->RunFunction<Ret>(m_luaState, nameInfo, args...);
CleanStack();
return result;
}
template <typename Ret, typename... Args, class = typename std::enable_if<(std::is_void<Ret>::value), int>::type>
void RunFunction(const std::string& envName,const FuncNameInfo& nameInfo, Args... args)
{
if (!GetInstance().FindLuaEnvironment(envName))
return;
m_environmentMap[envName]->RunFunction<void>(m_luaState, nameInfo, args...);
CleanStack();
}
template <typename Ret>
Ret GetGlobalValue(const std::string& envName, const std::string& name)
{
if (!FindLuaEnvironment(envName))
return Ret();
auto result = m_environmentMap[envName]->GetGlobalValue<Ret>(m_luaState, name);
CleanStack();
return result;
}
std::shared_ptr<LuaObject> GetLuaObject(const std::string& envName, const std::string& name, int index = -1, const std::string& globalName = "")
{
if (!FindLuaEnvironment(envName))
return nullptr;
auto result = m_environmentMap[envName]->GetLuaObject(m_luaState, name, index, globalName);
CleanStack();
return result;
}
void DeleteLuaObject(const std::string& envName, const std::string& name)
{
if (!FindLuaEnvironment(envName))
return;
m_environmentMap[envName]->DeleteLuaObject(m_luaState, name);
CleanStack();
}
template <typename Ret>
std::vector<Ret> TableToVector(const std::string& envName, const std::string& tableName, const std::string& globalName = "")
{
if (!FindLuaEnvironment(envName))
return std::vector<Ret>();
auto result = m_environmentMap[envName]->TableToVector<Ret>(m_luaState, tableName, globalName);
CleanStack();
return result;
}
template <typename Ret1, typename Ret2>
std::vector<std::pair<Ret1, Ret2>> TableToVector(const std::string& envName, const std::string& tableName, const std::string& globalName = "")
{
if (!FindLuaEnvironment(envName))
return std::vector<std::pair<Ret1, Ret2>>();
auto result = m_environmentMap[envName]->TableToVector<Ret1, Ret2>(m_luaState, tableName, globalName);
CleanStack();
return result;
}
static lua_State* GetState()
{
return m_luaState;
}
void Clear()
{
m_environmentMap.clear();
//THIS IS WRONG
lua_close(m_luaState);
m_luaState = luaL_newstate();
luaL_openlibs(m_luaState);
}
static void StackDump()
{
int i;
int top = lua_gettop(m_luaState);
for (i = 1; i <= top; i++) { /* repeat for each level */
int t = lua_type(m_luaState, i);
switch (t) {
case LUA_TSTRING: /* strings */
printf("`%s'", lua_tostring(m_luaState, i));
break;
case LUA_TBOOLEAN: /* booleans */
printf(lua_toboolean(m_luaState, i) ? "true" : "false");
break;
case LUA_TNUMBER: /* numbers */
printf("%g", lua_tonumber(m_luaState, i));
break;
default: /* other values */
printf("%s", lua_typename(m_luaState, t));
break;
}
printf(" "); /* put a separator */
}
printf("\n"); /* end the listing */
}
private:
void CreateLuaEnvironment(const std::string& name);
bool FindLuaEnvironment(const std::string& name) const;
LuaWrapper();
void PrintError(const std::string& variableName, const std::string& reason);
static lua_State* m_luaState;
int level;
std::map<std::string, std::unique_ptr<LuaEnvironment>> m_environmentMap;
};
#endif