From cb6e1c2f3b93d5de02de4a8c8f13891e33674a36 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 28 Oct 2024 17:49:53 +0100 Subject: Renamed uniquekey.h → uniquekey.hpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cancel.h | 2 +- src/deep.hpp | 2 +- src/keeper.h | 2 +- src/lane.h | 2 +- src/tools.h | 2 +- src/uniquekey.h | 134 ------------------------------------------------------ src/uniquekey.hpp | 134 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/universe.h | 2 +- 8 files changed, 140 insertions(+), 140 deletions(-) delete mode 100644 src/uniquekey.h create mode 100644 src/uniquekey.hpp (limited to 'src') diff --git a/src/cancel.h b/src/cancel.h index 97661f1..7230169 100644 --- a/src/cancel.h +++ b/src/cancel.h @@ -1,7 +1,7 @@ #pragma once #include "macros_and_utils.hpp" -#include "uniquekey.h" +#include "uniquekey.hpp" // ################################################################################################# diff --git a/src/deep.hpp b/src/deep.hpp index 0ea2712..3956f71 100644 --- a/src/deep.hpp +++ b/src/deep.hpp @@ -6,7 +6,7 @@ */ #include "lanesconf.h" -#include "uniquekey.h" +#include "uniquekey.hpp" // forwards enum class LookupMode; diff --git a/src/keeper.h b/src/keeper.h index 7e3e1fa..2354c2e 100644 --- a/src/keeper.h +++ b/src/keeper.h @@ -1,6 +1,6 @@ #pragma once -#include "uniquekey.h" +#include "uniquekey.hpp" // forwards class Linda; diff --git a/src/lane.h b/src/lane.h index 7f3cea0..ead8379 100644 --- a/src/lane.h +++ b/src/lane.h @@ -1,7 +1,7 @@ #pragma once #include "cancel.h" -#include "uniquekey.h" +#include "uniquekey.hpp" #include "universe.h" // ################################################################################################# diff --git a/src/tools.h b/src/tools.h index 34cbb8f..c587500 100644 --- a/src/tools.h +++ b/src/tools.h @@ -1,6 +1,6 @@ #pragma once -#include "uniquekey.h" +#include "uniquekey.hpp" class Universe; diff --git a/src/uniquekey.h b/src/uniquekey.h deleted file mode 100644 index 9041363..0000000 --- a/src/uniquekey.h +++ /dev/null @@ -1,134 +0,0 @@ -#pragma once - -#include "compat.hpp" -#include "macros_and_utils.hpp" - -// ################################################################################################# - -class UniqueKey -{ - public: - uintptr_t const storage{ 0 }; - std::string_view debugName{}; - - // --------------------------------------------------------------------------------------------- - constexpr explicit UniqueKey(uint64_t val_, std::string_view const& debugName_ = {}) -#if LUAJIT_FLAVOR() == 64 // building against LuaJIT headers for 64 bits, light userdata is restricted to 47 significant bits, because LuaJIT uses the other bits for internal optimizations - : storage{ static_cast(val_ & 0x7FFFFFFFFFFFull) } -#else // LUAJIT_FLAVOR() - : storage{ static_cast(val_) } -#endif // LUAJIT_FLAVOR() - , debugName{ debugName_ } - { - } - // --------------------------------------------------------------------------------------------- - // rule of 5 - UniqueKey() = delete; - constexpr UniqueKey(UniqueKey const&) = default; - UniqueKey(UniqueKey&&) = delete; - UniqueKey& operator=(UniqueKey const&) = delete; - UniqueKey& operator=(UniqueKey&&) = delete; - // debugName is irrelevant in comparisons - inline constexpr std::weak_ordering operator<=>(UniqueKey const& rhs_) const { return storage <=> rhs_.storage; } - inline constexpr auto operator==(UniqueKey const& rhs_) const { return storage == rhs_.storage; } - // --------------------------------------------------------------------------------------------- - bool equals(lua_State* const L_, StackIndex const i_) const - { - return lua_touserdata(L_, i_) == std::bit_cast(storage); - } - // --------------------------------------------------------------------------------------------- - void pushKey(lua_State* const L_) const - { - lua_pushlightuserdata(L_, std::bit_cast(storage)); - } -}; - -// ################################################################################################# - -DECLARE_UNIQUE_TYPE(NArr, int); -DECLARE_UNIQUE_TYPE(NRec, int); - -class RegistryUniqueKey -: public UniqueKey -{ - public: - using UniqueKey::UniqueKey; - - // --------------------------------------------------------------------------------------------- - void pushValue(lua_State* const L_) const - { - STACK_CHECK_START_REL(L_, 0); - pushKey(L_); // L_: ... key - lua_rawget(L_, kIdxRegistry); // L_: ... value - STACK_CHECK(L_, 1); - } - // --------------------------------------------------------------------------------------------- - template - void setValue(lua_State* const L_, OP operation_) const - { - // Note we can't check stack consistency because operation is not always a push (could be insert, replace, whatever) - pushKey(L_); // L_: ... key - operation_(L_); // L_: ... key value - lua_rawset(L_, kIdxRegistry); // L_: ... - } - // --------------------------------------------------------------------------------------------- - template - [[nodiscard]] T* readLightUserDataValue(lua_State* const L_) const - { - STACK_GROW(L_, 1); - STACK_CHECK_START_REL(L_, 0); - pushValue(L_); // L_: ... {}|nil - T* const value{ luaG_tolightuserdata(L_, kIdxTop) }; - lua_pop(L_, 1); // L_: ... - STACK_CHECK(L_, 0); - return value; - } - // --------------------------------------------------------------------------------------------- - [[nodiscard]] bool readBoolValue(lua_State* const L_) const - { - STACK_GROW(L_, 1); - STACK_CHECK_START_REL(L_, 0); - pushValue(L_); // L_: ... bool|nil - bool const value{ lua_toboolean(L_, -1) ? true : false }; - lua_pop(L_, 1); // L_: ... - STACK_CHECK(L_, 0); - return value; - } - // --------------------------------------------------------------------------------------------- - // equivalent to luaL_getsubtable - [[nodiscard]] bool getSubTable(lua_State* const L_, NArr const narr_, NRec const nrec_) const - { - STACK_CHECK_START_REL(L_, 0); - pushValue(L_); // L_: {}|nil - if (!lua_isnil(L_, -1)) { - LUA_ASSERT(L_, lua_istable(L_, -1)); - STACK_CHECK(L_, 1); - return true; // table already exists - } - lua_pop(L_, 1); // L_: - // store a newly created table in the registry, but leave it on the stack too - lua_createtable(L_, narr_, nrec_); // L_: {} - setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); }); // L_: {} - STACK_CHECK(L_, 1); - return false; - } - // --------------------------------------------------------------------------------------------- - void getSubTableMode(lua_State* const L_, std::string_view const& mode_) const - { - STACK_CHECK_START_REL(L_, 0); - if (!getSubTable(L_, NArr{ 0 }, NRec{ 0 })) { // L_: {} - // Set its metatable if requested - if (!mode_.empty()) { - STACK_GROW(L_, 3); - lua_createtable(L_, 0, 1); // L_: {} mt - luaG_pushstring(L_, "__mode"); // L_: {} mt "__mode" - luaG_pushstring(L_, mode_); // L_: {} mt "__mode" mode - lua_rawset(L_, -3); // L_: {} mt - lua_setmetatable(L_, -2); // L_: {} - } - } - STACK_CHECK(L_, 1); - } -}; - -// ################################################################################################# diff --git a/src/uniquekey.hpp b/src/uniquekey.hpp new file mode 100644 index 0000000..9041363 --- /dev/null +++ b/src/uniquekey.hpp @@ -0,0 +1,134 @@ +#pragma once + +#include "compat.hpp" +#include "macros_and_utils.hpp" + +// ################################################################################################# + +class UniqueKey +{ + public: + uintptr_t const storage{ 0 }; + std::string_view debugName{}; + + // --------------------------------------------------------------------------------------------- + constexpr explicit UniqueKey(uint64_t val_, std::string_view const& debugName_ = {}) +#if LUAJIT_FLAVOR() == 64 // building against LuaJIT headers for 64 bits, light userdata is restricted to 47 significant bits, because LuaJIT uses the other bits for internal optimizations + : storage{ static_cast(val_ & 0x7FFFFFFFFFFFull) } +#else // LUAJIT_FLAVOR() + : storage{ static_cast(val_) } +#endif // LUAJIT_FLAVOR() + , debugName{ debugName_ } + { + } + // --------------------------------------------------------------------------------------------- + // rule of 5 + UniqueKey() = delete; + constexpr UniqueKey(UniqueKey const&) = default; + UniqueKey(UniqueKey&&) = delete; + UniqueKey& operator=(UniqueKey const&) = delete; + UniqueKey& operator=(UniqueKey&&) = delete; + // debugName is irrelevant in comparisons + inline constexpr std::weak_ordering operator<=>(UniqueKey const& rhs_) const { return storage <=> rhs_.storage; } + inline constexpr auto operator==(UniqueKey const& rhs_) const { return storage == rhs_.storage; } + // --------------------------------------------------------------------------------------------- + bool equals(lua_State* const L_, StackIndex const i_) const + { + return lua_touserdata(L_, i_) == std::bit_cast(storage); + } + // --------------------------------------------------------------------------------------------- + void pushKey(lua_State* const L_) const + { + lua_pushlightuserdata(L_, std::bit_cast(storage)); + } +}; + +// ################################################################################################# + +DECLARE_UNIQUE_TYPE(NArr, int); +DECLARE_UNIQUE_TYPE(NRec, int); + +class RegistryUniqueKey +: public UniqueKey +{ + public: + using UniqueKey::UniqueKey; + + // --------------------------------------------------------------------------------------------- + void pushValue(lua_State* const L_) const + { + STACK_CHECK_START_REL(L_, 0); + pushKey(L_); // L_: ... key + lua_rawget(L_, kIdxRegistry); // L_: ... value + STACK_CHECK(L_, 1); + } + // --------------------------------------------------------------------------------------------- + template + void setValue(lua_State* const L_, OP operation_) const + { + // Note we can't check stack consistency because operation is not always a push (could be insert, replace, whatever) + pushKey(L_); // L_: ... key + operation_(L_); // L_: ... key value + lua_rawset(L_, kIdxRegistry); // L_: ... + } + // --------------------------------------------------------------------------------------------- + template + [[nodiscard]] T* readLightUserDataValue(lua_State* const L_) const + { + STACK_GROW(L_, 1); + STACK_CHECK_START_REL(L_, 0); + pushValue(L_); // L_: ... {}|nil + T* const value{ luaG_tolightuserdata(L_, kIdxTop) }; + lua_pop(L_, 1); // L_: ... + STACK_CHECK(L_, 0); + return value; + } + // --------------------------------------------------------------------------------------------- + [[nodiscard]] bool readBoolValue(lua_State* const L_) const + { + STACK_GROW(L_, 1); + STACK_CHECK_START_REL(L_, 0); + pushValue(L_); // L_: ... bool|nil + bool const value{ lua_toboolean(L_, -1) ? true : false }; + lua_pop(L_, 1); // L_: ... + STACK_CHECK(L_, 0); + return value; + } + // --------------------------------------------------------------------------------------------- + // equivalent to luaL_getsubtable + [[nodiscard]] bool getSubTable(lua_State* const L_, NArr const narr_, NRec const nrec_) const + { + STACK_CHECK_START_REL(L_, 0); + pushValue(L_); // L_: {}|nil + if (!lua_isnil(L_, -1)) { + LUA_ASSERT(L_, lua_istable(L_, -1)); + STACK_CHECK(L_, 1); + return true; // table already exists + } + lua_pop(L_, 1); // L_: + // store a newly created table in the registry, but leave it on the stack too + lua_createtable(L_, narr_, nrec_); // L_: {} + setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); }); // L_: {} + STACK_CHECK(L_, 1); + return false; + } + // --------------------------------------------------------------------------------------------- + void getSubTableMode(lua_State* const L_, std::string_view const& mode_) const + { + STACK_CHECK_START_REL(L_, 0); + if (!getSubTable(L_, NArr{ 0 }, NRec{ 0 })) { // L_: {} + // Set its metatable if requested + if (!mode_.empty()) { + STACK_GROW(L_, 3); + lua_createtable(L_, 0, 1); // L_: {} mt + luaG_pushstring(L_, "__mode"); // L_: {} mt "__mode" + luaG_pushstring(L_, mode_); // L_: {} mt "__mode" mode + lua_rawset(L_, -3); // L_: {} mt + lua_setmetatable(L_, -2); // L_: {} + } + } + STACK_CHECK(L_, 1); + } +}; + +// ################################################################################################# diff --git a/src/universe.h b/src/universe.h index 75e2198..8a2a471 100644 --- a/src/universe.h +++ b/src/universe.h @@ -4,7 +4,7 @@ #include "keeper.h" #include "lanesconf.h" #include "tracker.h" -#include "uniquekey.h" +#include "uniquekey.hpp" // ################################################################################################# -- cgit v1.2.3-55-g6feb