-
-
Notifications
You must be signed in to change notification settings - Fork 537
/
Copy pathstack_field.hpp
279 lines (252 loc) · 10.6 KB
/
stack_field.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// sol2
// The MIT License (MIT)
// Copyright (c) 2013-2021 Rapptz, ThePhD and contributors
// 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 SOL_STACK_FIELD_HPP
#define SOL_STACK_FIELD_HPP
#include <sol/stack_core.hpp>
#include <sol/stack_push.hpp>
#include <sol/stack_get.hpp>
#include <sol/stack_check_get.hpp>
namespace sol { namespace stack {
namespace stack_detail {
template <typename T, bool global, bool raw>
inline constexpr bool is_get_direct_tableless_v = (global && !raw && meta::is_c_str_v<T>);
template <typename T, bool global, bool raw>
inline constexpr bool is_get_direct_v = (is_get_direct_tableless_v<T, global, raw>) // cf-hack
|| (!global && !raw && (meta::is_c_str_v<T> || meta::is_string_of_v<T, char>)) // cf-hack
|| (!global && raw && (std::is_integral_v<T> && !std::is_same_v<T, bool>))
#if SOL_LUA_VERSION_I_ >= 503
|| (!global && !raw && (std::is_integral_v<T> && !std::is_same_v<T, bool>))
#endif // integer keys 5.3 or better
#if SOL_LUA_VERSION_I_ >= 502
|| (!global && raw && std::is_pointer_v<T> && std::is_void_v<std::remove_pointer_t<T>>)
#endif // void pointer keys 5.2 or better
;
template <typename T, bool global, bool raw>
inline constexpr bool is_set_direct_tableless_v = (global && !raw && meta::is_c_str_v<T>);
template <typename T, bool global, bool raw>
inline constexpr bool is_set_direct_v = (is_set_direct_tableless_v<T, global, raw>) // cf-hack
|| (!global && !raw && (meta::is_c_str_v<T> || meta::is_string_of_v<T, char>)) // cf-hack
|| (!global && raw && (std::is_integral_v<T> && !std::is_same_v<T, bool>)) // cf-hack
#if SOL_LUA_VERSION_I_ >= 503
|| (!global && !raw && (std::is_integral_v<T> && !std::is_same_v<T, bool>))
#endif // integer keys 5.3 or better
#if SOL_LUA_VERSION_I_ >= 502
|| (!global && raw && (std::is_pointer_v<T> && std::is_void_v<std::remove_pointer_t<T>>))
#endif // void pointer keys 5.2 or better
;
} // namespace stack_detail
template <typename T, bool global, bool raw, typename>
struct field_getter {
static inline constexpr int default_table_index
= meta::conditional_t<stack_detail::is_get_direct_v<T, global, raw>, std::integral_constant<int, -1>, std::integral_constant<int, -2>>::value;
template <typename Key>
void get(lua_State* L, Key&& key, int tableindex = default_table_index) {
if constexpr (std::is_same_v<T, update_if_empty_t> || std::is_same_v<T, override_value_t> || std::is_same_v<T, create_if_nil_t>) {
(void)L;
(void)key;
(void)tableindex;
}
else if constexpr (std::is_same_v<T, env_key_t>) {
(void)key;
#if SOL_LUA_VERSION_I_ < 502
// Use lua_setfenv
lua_getfenv(L, tableindex);
#else
// Use upvalues as explained in Lua 5.2 and beyond's manual
if (lua_getupvalue(L, tableindex, 1) == nullptr) {
push(L, lua_nil);
}
#endif
}
else if constexpr (std::is_same_v<T, metatable_key_t>) {
(void)key;
if (lua_getmetatable(L, tableindex) == 0)
push(L, lua_nil);
}
else if constexpr (raw) {
if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
lua_rawgeti(L, tableindex, static_cast<lua_Integer>(key));
}
#if SOL_LUA_VERSION_I_ >= 502
else if constexpr (std::is_pointer_v<T> && std::is_void_v<std::remove_pointer_t<T>>) {
lua_rawgetp(L, tableindex, key);
}
#endif // Lua 5.2.x+
else {
push(L, std::forward<Key>(key));
lua_rawget(L, tableindex);
}
}
else {
if constexpr (meta::is_c_str_v<T>) {
if constexpr (global) {
(void)tableindex;
lua_getglobal(L, &key[0]);
}
else {
lua_getfield(L, tableindex, &key[0]);
}
}
else if constexpr (std::is_same_v<T, meta_function>) {
const auto& real_key = to_string(key);
lua_getfield(L, tableindex, &real_key[0]);
}
#if SOL_LUA_VERSION_I_ >= 503
else if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
lua_geti(L, tableindex, static_cast<lua_Integer>(key));
}
#endif // Lua 5.3.x+
else {
push(L, std::forward<Key>(key));
lua_gettable(L, tableindex);
}
}
}
};
template <typename... Args, bool b, bool raw, typename C>
struct field_getter<std::tuple<Args...>, b, raw, C> {
template <std::size_t... I, typename Keys>
void apply(std::index_sequence<0, I...>, lua_State* L, Keys&& keys, int tableindex) {
get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)), tableindex);
void(detail::swallow { (get_field<false, raw>(L, std::get<I>(std::forward<Keys>(keys))), 0)... });
reference saved(L, -1);
lua_pop(L, static_cast<int>(sizeof...(I)));
saved.push();
}
template <typename Keys>
void get(lua_State* L, Keys&& keys) {
apply(std::make_index_sequence<sizeof...(Args)>(), L, std::forward<Keys>(keys), lua_absindex(L, -1));
}
template <typename Keys>
void get(lua_State* L, Keys&& keys, int tableindex) {
apply(std::make_index_sequence<sizeof...(Args)>(), L, std::forward<Keys>(keys), tableindex);
}
};
template <typename A, typename B, bool b, bool raw, typename C>
struct field_getter<std::pair<A, B>, b, raw, C> {
template <typename Keys>
void get(lua_State* L, Keys&& keys, int tableindex) {
get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)), tableindex);
get_field<false, raw>(L, std::get<1>(std::forward<Keys>(keys)));
reference saved(L, -1);
lua_pop(L, static_cast<int>(2));
saved.push();
}
template <typename Keys>
void get(lua_State* L, Keys&& keys) {
get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)));
get_field<false, raw>(L, std::get<1>(std::forward<Keys>(keys)));
reference saved(L, -1);
lua_pop(L, static_cast<int>(2));
saved.push();
}
};
template <typename T, bool global, bool raw, typename>
struct field_setter {
static constexpr int default_table_index
= meta::conditional_t<stack_detail::is_set_direct_v<T, global, raw>, std::integral_constant<int, -2>, std::integral_constant<int, -3>>::value;
template <typename Key, typename Value>
void set(lua_State* L, Key&& key, Value&& value, int tableindex = default_table_index) {
if constexpr (std::is_same_v<T, update_if_empty_t> || std::is_same_v<T, override_value_t>) {
(void)L;
(void)key;
(void)value;
(void)tableindex;
}
else if constexpr (std::is_same_v<T, metatable_key_t>) {
(void)key;
push(L, std::forward<Value>(value));
lua_setmetatable(L, tableindex);
}
else if constexpr (raw) {
if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
push(L, std::forward<Value>(value));
lua_rawseti(L, tableindex, static_cast<lua_Integer>(key));
}
#if SOL_LUA_VERSION_I_ >= 502
else if constexpr (std::is_pointer_v<T> && std::is_void_v<std::remove_pointer_t<T>>) {
push(L, std::forward<Value>(value));
lua_rawsetp(L, tableindex, std::forward<Key>(key));
}
#endif // Lua 5.2.x
else {
push(L, std::forward<Key>(key));
push(L, std::forward<Value>(value));
lua_rawset(L, tableindex);
}
}
else {
if constexpr (meta::is_c_str_v<T> || meta::is_string_of_v<T, char>) {
if constexpr (global) {
push(L, std::forward<Value>(value));
lua_setglobal(L, &key[0]);
(void)tableindex;
}
else {
push(L, std::forward<Value>(value));
lua_setfield(L, tableindex, &key[0]);
}
}
#if SOL_LUA_VERSION_I_ >= 503
else if constexpr (std::is_integral_v<T> && !std::is_same_v<bool, T>) {
push(L, std::forward<Value>(value));
lua_seti(L, tableindex, static_cast<lua_Integer>(key));
}
#endif // Lua 5.3.x
else {
push(L, std::forward<Key>(key));
push(L, std::forward<Value>(value));
lua_settable(L, tableindex);
}
}
}
};
template <typename... Args, bool b, bool raw, typename C>
struct field_setter<std::tuple<Args...>, b, raw, C> {
template <bool g, std::size_t I, typename Keys, typename Value>
void apply(std::index_sequence<I>, lua_State* L, Keys&& keys, Value&& value, int tableindex) {
I < 1 ? set_field<g, raw>(L, std::get<I>(std::forward<Keys>(keys)), std::forward<Value>(value), tableindex)
: set_field<g, raw>(L, std::get<I>(std::forward<Keys>(keys)), std::forward<Value>(value));
}
template <bool g, std::size_t I0, std::size_t I1, std::size_t... I, typename Keys, typename Value>
void apply(std::index_sequence<I0, I1, I...>, lua_State* L, Keys&& keys, Value&& value, int tableindex) {
I0 < 1 ? get_field<g, raw>(L, std::get<I0>(std::forward<Keys>(keys)), tableindex)
: get_field<g, raw>(L, std::get<I0>(std::forward<Keys>(keys)), -1);
apply<false>(std::index_sequence<I1, I...>(), L, std::forward<Keys>(keys), std::forward<Value>(value), -1);
}
template <bool g, std::size_t I0, std::size_t... I, typename Keys, typename Value>
void top_apply(std::index_sequence<I0, I...>, lua_State* L, Keys&& keys, Value&& value, int tableindex) {
apply<g>(std::index_sequence<I0, I...>(), L, std::forward<Keys>(keys), std::forward<Value>(value), tableindex);
lua_pop(L, static_cast<int>(sizeof...(I)));
}
template <typename Keys, typename Value>
void set(lua_State* L, Keys&& keys, Value&& value, int tableindex = -3) {
top_apply<b>(std::make_index_sequence<sizeof...(Args)>(), L, std::forward<Keys>(keys), std::forward<Value>(value), tableindex);
}
};
template <typename A, typename B, bool b, bool raw, typename C>
struct field_setter<std::pair<A, B>, b, raw, C> {
template <typename Keys, typename Value>
void set(lua_State* L, Keys&& keys, Value&& value, int tableindex = -1) {
get_field<b, raw>(L, std::get<0>(std::forward<Keys>(keys)), tableindex);
set_field<false, raw>(L, std::get<1>(std::forward<Keys>(keys)), std::forward<Value>(value), lua_gettop(L));
lua_pop(L, 1);
}
};
}} // namespace sol::stack
#endif // SOL_STACK_FIELD_HPP