-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLuaFunction.hpp
207 lines (188 loc) · 7.67 KB
/
LuaFunction.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
/*
* Copyright (c) 2015-2023 JlnWntr ([email protected])
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#ifndef LUA_FUNCTION_H
#define LUA_FUNCTION_H
#ifndef LUA_ADAPTER_H
#include "LuaAdapter.hpp"
#endif
static int LUA_ADAPTER_NULL{};
typedef int (*Lua_callback_function)(lua_State *L);
class LuaFunction {
public:
/**
* Default constructor
*/
LuaFunction() : Lua{nullptr} {}
/**
* Constructor
* @param lua uses an existing LuaState
*/
LuaFunction(lua_State *const lua) : Lua{std::make_shared<LuaState>(lua)} {}
LuaFunction(LuaAdapter &lua) : Lua{lua.GetLuaState()} {}
LuaFunction(const LuaFunction &) = delete;
/**
* Destructor
*/
~LuaFunction() {}
/**
* Calls a Lua-function
* @param f name of the Lua-function
* @param c number of arguments passed to the Lua-function
* @param a function arguments
* @param r new value returned from the called Lua-function
* @return true on success, false on error
*/
template <typename A, typename R>
bool Call(const std::string f, const auto c, const A *const a, R &r) {
if (not this->Lua.get() or not this->Lua.get()->Lua() or
not(lua_getglobal(this->Lua.get()->Lua(), f.c_str()) == LUA_TFUNCTION))
return false;
for (auto i = 0; i < c; i++) {
if (not(a + i)) break;
if constexpr (std::is_same_v<double, A> or std::is_same_v<A, float>)
lua_pushnumber(this->Lua.get()->Lua(), a[i]);
else if constexpr (std::is_same_v<A, int>)
lua_pushinteger(this->Lua.get()->Lua(), a[i]);
else if constexpr (std::is_same_v<A, std::string>)
lua_pushlstring(this->Lua.get()->Lua(), a[i].c_str(), a[i].length());
else if constexpr (std::is_same_v<A, const char*>)
lua_pushlstring(this->Lua.get()->Lua(), a[i], strlen(a[i]));
else if constexpr (std::is_same_v<A, bool>)
lua_pushboolean(this->Lua.get()->Lua(), a[i]);
}
if (this->Pcall(c, 1, 0) == false){
return false;
}
if constexpr (std::is_same_v<double, R> or std::is_same_v<R, float>) {
if (lua_isnumber(this->Lua.get()->Lua(), -1))
r = lua_tonumber(this->Lua.get()->Lua(), -1);
} else if constexpr (std::is_same_v<R, int>) {
if (lua_isinteger(this->Lua.get()->Lua(), -1))
r = lua_tointeger(this->Lua.get()->Lua(), -1);
} else if constexpr (std::is_same_v<R, bool>) {
if (lua_isboolean(this->Lua.get()->Lua(), -1))
r = lua_toboolean(this->Lua.get()->Lua(), -1);
} else if constexpr (std::is_same_v<R, std::string>) {
if (lua_isstring(this->Lua.get()->Lua(), -1))
r = lua_tostring(this->Lua.get()->Lua(), -1);
}
lua_pop(this->Lua.get()->Lua(), 1);
return true;
}
/**
* Calls a Lua-function
* @param f name of the Lua-function
* @param c number of arguments passed to the Lua-function
* @param a function arguments
* @return true on success, false on error
*/
template <typename A>
bool Test(const std::string f, const auto c, const A *const a) {
if (not this->Lua.get() or not this->Lua.get()->Lua() or
not(lua_getglobal(this->Lua.get()->Lua(), f.c_str()) == LUA_TFUNCTION))
return false;
for (auto i = 0; i < c; i++) {
if (not(a + i)) break;
if constexpr (std::is_same_v<double, A> or std::is_same_v<A, float>)
lua_pushnumber(this->Lua.get()->Lua(), a[i]);
else if constexpr (std::is_same_v<A, int>)
lua_pushinteger(this->Lua.get()->Lua(), a[i]);
else if constexpr (std::is_same_v<A, std::string>)
lua_pushlstring(this->Lua.get()->Lua(), a[i].c_str(), a[i].length());
else if constexpr (std::is_same_v<A, const char*>)
lua_pushlstring(this->Lua.get()->Lua(), a[i], strlen(a[i]));
else if constexpr (std::is_same_v<A, bool>)
lua_pushboolean(this->Lua.get()->Lua(), a[i]);
}
return this->Pcall(c, 0, 0);
}
/**
* Calls a Lua-function
* @param f name of the Lua-function
* @param a function argument
* @param r new value returned from the called Lua-function
* @return true on success, false on error
*/
template <typename A, typename R>
bool Call(const std::string f, const A a, R &r = LUA_ADAPTER_NULL) {
return this->Call(f.c_str(), 1, &a, r);
}
/**
* Calls a Lua-function
* @param f name of the Lua-function
* @param a function argument
* @return true on success, false on error
*/
template <typename A>
bool Call(const std::string f, const A a) {
return this->Call(f.c_str(), a, LUA_ADAPTER_NULL);
}
/**
* Calls a Lua-function
* @param f name of the Lua-function
* @return true on success, false on error
*/
bool Call(const std::string f) {
if (not this->Lua.get() or not this->Lua.get()->Lua() or
not(lua_getglobal(this->Lua.get()->Lua(), f.c_str()) == LUA_TFUNCTION))
return false;
return this->Pcall(0, 0, 0);
}
/**
* Makes a C-/C++-function available for Lua
* (It's called pushFunction(), but you're not 'incrementing' the stack)
* @param function C-/C++-function
* @param name of the function
* @return true on success, false on error
*/
bool Push(Lua_callback_function function, const std::string name) {
if (not this->Lua.get() or not this->Lua.get()->Lua())
return false;
lua_pushcfunction(this->Lua.get()->Lua(), function);
lua_setglobal(this->Lua.get()->Lua(), name.c_str());
return true;
}
private:
std::shared_ptr<LuaState> Lua;
/**
* Calls Lua's pcall and outputs errors.
* @param argc number of arguments
* @param results number of results
* @param msgh message handler (see
* https://www.lua.org/manual/5.3/manual.html#lua_pcall)
* @return true on success, false on error
*/
bool Pcall(int argc, int results, int msgh) {
if (not this->Lua.get() or not this->Lua.get()->Lua()) return false;
const int call{lua_pcall(this->Lua.get()->Lua(), argc, results, msgh)};
if (call == LUA_OK) return true;
#ifdef LUA_ADAPTER_DEBUG
std::cerr << LUA_ADAPTER_PREFIX << "Error: pcall failed. Code: ";
std::cerr << call;
std::cerr << ", '" << lua_tostring(this->Lua.get()->Lua(), -1) << "'\n";
#endif
lua_pop(this->Lua.get()->Lua(), 1);
return false;
}
};
#endif