From 8745a54f88f31cd51dc86c96039ebe0b3e98f5ea Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 24 Oct 2024 10:33:02 +0200 Subject: Added strong types UserValueCount and UnusedInt --- deep_test/deep_test.cpp | 4 ++-- src/compat.cpp | 4 ++-- src/compat.h | 2 +- src/deep.cpp | 4 ++-- src/deep.h | 4 ++-- src/intercopycontext.cpp | 6 +++--- src/lanes.cpp | 3 ++- src/linda.cpp | 4 ++-- src/linda.h | 4 ++-- src/stackindex.hpp | 6 ++++++ src/unique.hpp | 12 ++++++++++++ 11 files changed, 36 insertions(+), 17 deletions(-) diff --git a/deep_test/deep_test.cpp b/deep_test/deep_test.cpp index 39352f3..a6c6296 100644 --- a/deep_test/deep_test.cpp +++ b/deep_test/deep_test.cpp @@ -129,9 +129,9 @@ static luaL_Reg const deep_mt[] = { int luaD_new_deep(lua_State* L) { - int const nuv{ static_cast(luaL_optinteger(L, 1, 0)) }; + UserValueCount const _nuv{ static_cast(luaL_optinteger(L, 1, 0)) }; lua_settop(L, 0); - MyDeepFactory::Instance.pushDeepUserdata(DestState{ L }, nuv); + MyDeepFactory::Instance.pushDeepUserdata(DestState{ L }, _nuv); return 1; } diff --git a/src/compat.cpp b/src/compat.cpp index 6bdeecc..1be910f 100644 --- a/src/compat.cpp +++ b/src/compat.cpp @@ -5,11 +5,11 @@ // ################################################################################################# -int luaG_getalluservalues(lua_State* const L_, StackIndex const idx_) +UserValueCount luaG_getalluservalues(lua_State* const L_, StackIndex const idx_) { STACK_CHECK_START_REL(L_, 0); StackIndex const _idx{ luaG_absindex(L_, idx_) }; - int _nuv{ 0 }; + UserValueCount _nuv{ 0 }; do { // we don't know how many uservalues we are going to extract, there might be a lot... STACK_GROW(L_, 1); diff --git a/src/compat.h b/src/compat.h index 081e4c7..af014b1 100644 --- a/src/compat.h +++ b/src/compat.h @@ -172,7 +172,7 @@ static inline int luaG_dump(lua_State* const L_, lua_Writer const writer_, void* // ################################################################################################# -int luaG_getalluservalues(lua_State* L_, StackIndex idx_); +UserValueCount luaG_getalluservalues(lua_State* L_, StackIndex idx_); // ################################################################################################# diff --git a/src/deep.cpp b/src/deep.cpp index a244f8b..0d801a3 100644 --- a/src/deep.cpp +++ b/src/deep.cpp @@ -175,7 +175,7 @@ DeepFactory* DeepFactory::LookupFactory(lua_State* const L_, StackIndex const in * Initializes necessary structures if it's the first time 'factory' is being used in * this Lua state (metatable, registring it). Otherwise, increments the reference count. */ -void DeepFactory::PushDeepProxy(DestState const L_, DeepPrelude* const prelude_, int const nuv_, LookupMode const mode_, lua_State* const errL_) +void DeepFactory::PushDeepProxy(DestState const L_, DeepPrelude* const prelude_, UserValueCount const nuv_, LookupMode const mode_, lua_State* const errL_) { STACK_CHECK_START_REL(L_, 0); kDeepProxyCacheRegKey.getSubTableMode(L_, "v"); // L_: DPC @@ -302,7 +302,7 @@ void DeepFactory::PushDeepProxy(DestState const L_, DeepPrelude* const prelude_, * * Returns: 'proxy' userdata for accessing the deep data via 'DeepFactory::toDeep()' */ -void DeepFactory::pushDeepUserdata(DestState const L_, int const nuv_) const +void DeepFactory::pushDeepUserdata(DestState const L_, UserValueCount const nuv_) const { STACK_GROW(L_, 1); STACK_CHECK_START_REL(L_, 0); diff --git a/src/deep.h b/src/deep.h index 8896698..0ea2712 100644 --- a/src/deep.h +++ b/src/deep.h @@ -68,8 +68,8 @@ class DeepFactory static void DeleteDeepObject(lua_State* L_, DeepPrelude* o_); [[nodiscard]] static bool IsDeepUserdata(lua_State* const L_, StackIndex const idx_); [[nodiscard]] static DeepFactory* LookupFactory(lua_State* L_, StackIndex index_, LookupMode mode_); - static void PushDeepProxy(DestState L_, DeepPrelude* o_, int nuv_, LookupMode mode_, lua_State* errL_); - void pushDeepUserdata(DestState L_, int nuv_) const; + static void PushDeepProxy(DestState L_, DeepPrelude* o_, UserValueCount nuv_, LookupMode mode_, lua_State* errL_); + void pushDeepUserdata(DestState L_, UserValueCount nuv_) const; [[nodiscard]] DeepPrelude* toDeep(lua_State* L_, StackIndex index_) const; }; diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 473ebfd..f54c152 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp @@ -708,7 +708,7 @@ LuaType InterCopyContext::processConversion() const StackIndex const _mt{ luaG_absindex(L1, StackIndex{ -2 }) }; // L1: ... mt __lanesclone size_t const userdata_size{ lua_rawlen(L1, _L1_i) }; // extract all the uservalues, but don't transfer them yet - int const _nuv{ luaG_getalluservalues(L1, _L1_i) }; // L1: ... mt __lanesclone [uv]* + UserValueCount const _nuv{ luaG_getalluservalues(L1, _L1_i) }; // L1: ... mt __lanesclone [uv]* // create the clone userdata with the required number of uservalue slots void* const _clone{ lua_newuserdatauv(L2, userdata_size, _nuv) }; // L2: ... u // copy the metatable in the target state, and give it to the clone we put there @@ -785,7 +785,7 @@ LuaType InterCopyContext::processConversion() const STACK_CHECK_START_REL(L2, 0); // extract all uservalues of the source. unfortunately, the only way to know their count is to iterate until we fail - int const _nuv{ luaG_getalluservalues(L1, L1_i) }; // L1: ... deep ... [uv]* + UserValueCount const _nuv{ luaG_getalluservalues(L1, L1_i) }; // L1: ... deep ... [uv]* STACK_CHECK(L1, _nuv); DeepPrelude* const _deep{ *luaG_tofulluserdata(L1, L1_i) }; @@ -867,7 +867,7 @@ LuaType InterCopyContext::processConversion() const size_t const _userdata_size{ lua_rawlen(L1, kIdxTop) }; { // extract uservalues (don't transfer them yet) - int const _nuv{ luaG_getalluservalues(L1, source_i) }; // L1: ... u [uv]* + UserValueCount const _nuv{ luaG_getalluservalues(L1, source_i) }; // L1: ... u [uv]* STACK_CHECK(L1, _nuv + 1); // create the clone userdata with the required number of uservalue slots _clone = lua_newuserdatauv(L2, _userdata_size, _nuv); // L2: ... mt u diff --git a/src/lanes.cpp b/src/lanes.cpp index 4ebe20c..572d8f9 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -718,7 +718,8 @@ LUAG_FUNC(configure) } STACK_CHECK(L_, 2); - DeepFactory::PushDeepProxy(DestState{ L_ }, _U->timerLinda, 0, LookupMode::LaneBody, L_); // L_: settings M timerLinda + UserValueCount const _nuv{ 0 }; // no uservalue in the linda + DeepFactory::PushDeepProxy(DestState{ L_ }, _U->timerLinda, _nuv, LookupMode::LaneBody, L_); // L_: settings M timerLinda lua_setfield(L_, -2, "timerLinda"); // L_: settings M STACK_CHECK(L_, 2); diff --git a/src/linda.cpp b/src/linda.cpp index bd40f5e..6655ae4 100644 --- a/src/linda.cpp +++ b/src/linda.cpp @@ -981,7 +981,7 @@ LUAG_FUNC(linda) lua_replace(L_, 3); // L_: name group close_handler // if we have a __close handler, we need a uservalue slot to store it - int const _nuv{ _closeHandlerIdx ? 1 : 0 }; + UserValueCount const _nuv{ _closeHandlerIdx ? 1 : 0 }; LindaFactory::Instance.pushDeepUserdata(DestState{ L_ }, _nuv); // L_: name group close_handler linda if (_closeHandlerIdx != 0) { lua_replace(L_, 2); // L_: name linda close_handler @@ -996,7 +996,7 @@ LUAG_FUNC(linda) if (_nameIdx > _groupIdx) { lua_insert(L_, 1); // L_: name group } - LindaFactory::Instance.pushDeepUserdata(DestState{ L_ }, 0); // L_: name group linda + LindaFactory::Instance.pushDeepUserdata(DestState{ L_ }, UserValueCount{ 0 }); // L_: name group linda return 1; } diff --git a/src/linda.h b/src/linda.h index ea660d9..c05fb14 100644 --- a/src/linda.h +++ b/src/linda.h @@ -30,13 +30,13 @@ class Linda : linda{ linda_ } , L{ L_ } { - [[maybe_unused]] int const _prev{ linda.keeperOperationCount.fetch_add(1, std::memory_order_seq_cst) }; + [[maybe_unused]] UnusedInt const _prev{ linda.keeperOperationCount.fetch_add(1, std::memory_order_seq_cst) }; } public: ~KeeperOperationInProgress() { - [[maybe_unused]] int const _prev{ linda.keeperOperationCount.fetch_sub(1, std::memory_order_seq_cst) }; + [[maybe_unused]] UnusedInt const _prev{ linda.keeperOperationCount.fetch_sub(1, std::memory_order_seq_cst) }; } }; diff --git a/src/stackindex.hpp b/src/stackindex.hpp index 7c2c17a..1c2ce8c 100644 --- a/src/stackindex.hpp +++ b/src/stackindex.hpp @@ -5,6 +5,12 @@ DECLARE_UNIQUE_TYPE(StackIndex, int); static_assert(std::is_trivial_v); +DECLARE_UNIQUE_TYPE(UserValueCount, int); +static_assert(std::is_trivial_v); + +DECLARE_UNIQUE_TYPE(UnusedInt, int); +static_assert(std::is_trivial_v); + // ################################################################################################# static constexpr StackIndex kIdxRegistry{ LUA_REGISTRYINDEX }; diff --git a/src/unique.hpp b/src/unique.hpp index 829fa49..98a9d48 100644 --- a/src/unique.hpp +++ b/src/unique.hpp @@ -43,6 +43,18 @@ class Unique { return Unique{ std::exchange(val, val + 1) }; } + + // pre-decrement + auto& operator--() noexcept + { + --val; + return *this; + } + // post-decrement + auto operator--(int) noexcept + { + return Unique{ std::exchange(val, val - 1) }; + } }; template -- cgit v1.2.3-55-g6feb