// 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 #include #include #include namespace sol { namespace stack { namespace stack_detail { template inline constexpr bool is_get_direct_tableless_v = (global && !raw && meta::is_c_str_v); template inline constexpr bool is_get_direct_v = (is_get_direct_tableless_v) // cf-hack || (!global && !raw && (meta::is_c_str_v || meta::is_string_of_v)) // cf-hack || (!global && raw && (std::is_integral_v && !std::is_same_v)) #if SOL_LUA_VERSION_I_ >= 503 || (!global && !raw && (std::is_integral_v && !std::is_same_v)) #endif // integer keys 5.3 or better #if SOL_LUA_VERSION_I_ >= 502 || (!global && raw && std::is_pointer_v && std::is_void_v>) #endif // void pointer keys 5.2 or better ; template inline constexpr bool is_set_direct_tableless_v = (global && !raw && meta::is_c_str_v); template inline constexpr bool is_set_direct_v = (is_set_direct_tableless_v) // cf-hack || (!global && !raw && (meta::is_c_str_v || meta::is_string_of_v)) // cf-hack || (!global && raw && (std::is_integral_v && !std::is_same_v)) // cf-hack #if SOL_LUA_VERSION_I_ >= 503 || (!global && !raw && (std::is_integral_v && !std::is_same_v)) #endif // integer keys 5.3 or better #if SOL_LUA_VERSION_I_ >= 502 || (!global && raw && (std::is_pointer_v && std::is_void_v>)) #endif // void pointer keys 5.2 or better ; } // namespace stack_detail template struct field_getter { static inline constexpr int default_table_index = meta::conditional_t, std::integral_constant, std::integral_constant>::value; template void get(lua_State* L, Key&& key, int tableindex = default_table_index) { if constexpr (std::is_same_v || std::is_same_v || std::is_same_v) { (void)L; (void)key; (void)tableindex; } else if constexpr (std::is_same_v) { (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) { (void)key; if (lua_getmetatable(L, tableindex) == 0) push(L, lua_nil); } else if constexpr (raw) { if constexpr (std::is_integral_v && !std::is_same_v) { lua_rawgeti(L, tableindex, static_cast(key)); } #if SOL_LUA_VERSION_I_ >= 502 else if constexpr (std::is_pointer_v && std::is_void_v>) { lua_rawgetp(L, tableindex, key); } #endif // Lua 5.2.x+ else { push(L, std::forward(key)); lua_rawget(L, tableindex); } } else { if constexpr (meta::is_c_str_v) { if constexpr (global) { (void)tableindex; lua_getglobal(L, &key[0]); } else { lua_getfield(L, tableindex, &key[0]); } } else if constexpr (std::is_same_v) { 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 && !std::is_same_v) { lua_geti(L, tableindex, static_cast(key)); } #endif // Lua 5.3.x+ else { push(L, std::forward(key)); lua_gettable(L, tableindex); } } } }; template struct field_getter, b, raw, C> { template void apply(std::index_sequence<0, I...>, lua_State* L, Keys&& keys, int tableindex) { get_field(L, std::get<0>(std::forward(keys)), tableindex); void(detail::swallow { (get_field(L, std::get(std::forward(keys))), 0)... }); reference saved(L, -1); lua_pop(L, static_cast(sizeof...(I))); saved.push(); } template void get(lua_State* L, Keys&& keys) { apply(std::make_index_sequence(), L, std::forward(keys), lua_absindex(L, -1)); } template void get(lua_State* L, Keys&& keys, int tableindex) { apply(std::make_index_sequence(), L, std::forward(keys), tableindex); } }; template struct field_getter, b, raw, C> { template void get(lua_State* L, Keys&& keys, int tableindex) { get_field(L, std::get<0>(std::forward(keys)), tableindex); get_field(L, std::get<1>(std::forward(keys))); reference saved(L, -1); lua_pop(L, static_cast(2)); saved.push(); } template void get(lua_State* L, Keys&& keys) { get_field(L, std::get<0>(std::forward(keys))); get_field(L, std::get<1>(std::forward(keys))); reference saved(L, -1); lua_pop(L, static_cast(2)); saved.push(); } }; template struct field_setter { static constexpr int default_table_index = meta::conditional_t, std::integral_constant, std::integral_constant>::value; template void set(lua_State* L, Key&& key, Value&& value, int tableindex = default_table_index) { if constexpr (std::is_same_v || std::is_same_v) { (void)L; (void)key; (void)value; (void)tableindex; } else if constexpr (std::is_same_v) { (void)key; push(L, std::forward(value)); lua_setmetatable(L, tableindex); } else if constexpr (raw) { if constexpr (std::is_integral_v && !std::is_same_v) { push(L, std::forward(value)); lua_rawseti(L, tableindex, static_cast(key)); } #if SOL_LUA_VERSION_I_ >= 502 else if constexpr (std::is_pointer_v && std::is_void_v>) { push(L, std::forward(value)); lua_rawsetp(L, tableindex, std::forward(key)); } #endif // Lua 5.2.x else { push(L, std::forward(key)); push(L, std::forward(value)); lua_rawset(L, tableindex); } } else { if constexpr (meta::is_c_str_v || meta::is_string_of_v) { if constexpr (global) { push(L, std::forward(value)); lua_setglobal(L, &key[0]); (void)tableindex; } else { push(L, std::forward(value)); lua_setfield(L, tableindex, &key[0]); } } #if SOL_LUA_VERSION_I_ >= 503 else if constexpr (std::is_integral_v && !std::is_same_v) { push(L, std::forward(value)); lua_seti(L, tableindex, static_cast(key)); } #endif // Lua 5.3.x else { push(L, std::forward(key)); push(L, std::forward(value)); lua_settable(L, tableindex); } } } }; template struct field_setter, b, raw, C> { template void apply(std::index_sequence, lua_State* L, Keys&& keys, Value&& value, int tableindex) { I < 1 ? set_field(L, std::get(std::forward(keys)), std::forward(value), tableindex) : set_field(L, std::get(std::forward(keys)), std::forward(value)); } template void apply(std::index_sequence, lua_State* L, Keys&& keys, Value&& value, int tableindex) { I0 < 1 ? get_field(L, std::get(std::forward(keys)), tableindex) : get_field(L, std::get(std::forward(keys)), -1); apply(std::index_sequence(), L, std::forward(keys), std::forward(value), -1); } template void top_apply(std::index_sequence, lua_State* L, Keys&& keys, Value&& value, int tableindex) { apply(std::index_sequence(), L, std::forward(keys), std::forward(value), tableindex); lua_pop(L, static_cast(sizeof...(I))); } template void set(lua_State* L, Keys&& keys, Value&& value, int tableindex = -3) { top_apply(std::make_index_sequence(), L, std::forward(keys), std::forward(value), tableindex); } }; template struct field_setter, b, raw, C> { template void set(lua_State* L, Keys&& keys, Value&& value, int tableindex = -1) { get_field(L, std::get<0>(std::forward(keys)), tableindex); set_field(L, std::get<1>(std::forward(keys)), std::forward(value), lua_gettop(L)); lua_pop(L, 1); } }; }} // namespace sol::stack #endif // SOL_STACK_FIELD_HPP