-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLuaObject.h
163 lines (131 loc) · 4.64 KB
/
LuaObject.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
#ifndef Lua_OBJECT_H
#define Lua_OBJECT_H
#include "LuaFunction.h"
#include <memory>
class ILuaObject
{
public:
virtual ~ILuaObject(){};
virtual void AddFunction(ILuaFunction* function, std::string funcName) = 0;
//virtual int Apply(lua_State* L) = 0;
};
class LuaObject : public ILuaObject
{
public:
LuaObject(lua_State* L, const std::string& metatableName, const std::string& name, ILuaFunction* function , const std::string& functionName)
{
m_luaState = L;
m_functions.push_back(std::move(std::unique_ptr<ILuaFunction>{function}));
m_name = name;
lua_setglobal(m_luaState, name.c_str());
}
LuaObject(lua_State* L, const std::string& name, int ref)
{
m_luaState = L;
m_name = name;
m_luaRef = ref;
}
LuaObject(const LuaObject& other) = delete;
LuaObject(LuaObject&& other)
:
m_name(other.m_name),
m_luaState(other.m_luaState)
{
other.m_luaState = nullptr;
m_functions = std::move(other.m_functions);
}
~LuaObject()
{
m_functions.clear();
//lua_getglobal(m_luaState, m_name.c_str());
//lua_pushnil(m_luaState);
//lua_setfield(m_luaState, -2, "__object");
//lua_pushnil(m_luaState);
//lua_setglobal(m_luaState, m_name.c_str());
luaL_unref(m_luaState, LUA_REGISTRYINDEX, m_luaRef);
}
void AddFunction(ILuaFunction* function, std::string funcName)
{
m_functions.push_back(std::move(std::unique_ptr<ILuaFunction>{function}));
};
template <typename Ret, typename ...Args, class = typename std::enable_if<!(std::is_void<Ret>::value), int>::type>
Ret CallFunction(std::string funcName, Args... args)
{
lua_rawgeti(m_luaState, LUA_REGISTRYINDEX, m_luaRef);
//LuaCallDispatcher::GetInstance().stackDump(m_luaState);
lua_getfield(m_luaState, -1, funcName.c_str());
const int num_args = sizeof...(Args)+1;
const int num_ret = 1;
lua_pushvalue(m_luaState, -2);
LuaCallDispatcher::GetInstance().Push(m_luaState, args...);
lua_call(m_luaState, num_args, num_ret);
Ret result = LuaCallDispatcher::GetInstance().Pop<Ret>(m_luaState);
CleanStack();
return result;
}
template <typename Ret, typename ...Args, class = typename std::enable_if<(std::is_void<Ret>::value), int>::type>
void CallFunction(std::string funcName, Args... args)
{
lua_rawgeti(m_luaState, LUA_REGISTRYINDEX, m_luaRef);
lua_getfield(m_luaState, -1, funcName.c_str());
const int num_args = sizeof...(Args)+1;
lua_pushvalue(m_luaState, -2);
LuaCallDispatcher::GetInstance().Push(m_luaState, args...);
try
{
LuaCallDispatcher::GetInstance().stackDump(m_luaState);
lua_call(m_luaState, num_args, 0);
}
catch(std::exception& e)
{
LuaCallDispatcher::GetInstance().stackDump(m_luaState);
std::cout << e.what() << std::endl;
}
CleanStack();
}
template <typename Ret, typename ...Args>
std::vector<Ret> CallFunctionVec(std::string funcName, Args... args)
{
lua_rawgeti(m_luaState, LUA_REGISTRYINDEX, m_luaRef);
lua_getfield(m_luaState, -1, funcName.c_str());
const int num_args = sizeof...(Args)+1;
const int num_ret = 1;
lua_pushvalue(m_luaState, -2);
LuaCallDispatcher::GetInstance().Push(m_luaState, args...);
lua_call(m_luaState, num_args, num_ret);
return LuaCallDispatcher::GetInstance().TableToVectorFromTop<Ret>(m_luaState);
//CleanStack();
//return result;
}
template <typename Ret1, typename Ret2, typename ...Args>
std::vector<std::pair<Ret1, Ret2>> CallFunctionVec(std::string funcName, Args... args)
{
lua_rawgeti(m_luaState, LUA_REGISTRYINDEX, m_luaRef);
lua_getfield(m_luaState, -1, funcName.c_str());
const int num_args = sizeof...(Args)+1;
const int num_ret = 1;
lua_pushvalue(m_luaState, -2);
LuaCallDispatcher::GetInstance().Push(m_luaState, args...);
lua_call(m_luaState, num_args, num_ret);
std::vector<std::pair<Ret1, Ret2>> result = LuaCallDispatcher::GetInstance().TableToVectorFromTop<Ret1, Ret2>(m_luaState);
CleanStack();
return result;
}
const std::string& GetName() const
{
return m_name;
}
void CleanStack()
{
//if(!m_luaState)
// return;
int stackSize = lua_gettop(m_luaState);
lua_pop(m_luaState, stackSize);
}
private:
lua_State* m_luaState;
std::string m_name;
std::vector<std::unique_ptr<ILuaFunction>> m_functions;
int m_luaRef;
};
#endif