From ea290e120ee0069e0c4a983d49fb69fc942dc3f2 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Wed, 9 Oct 2024 09:43:24 +0200 Subject: Improved Unique<> implementation, moved StackIndex into a separate header --- src/compat.h | 5 +---- src/luaerrors.h | 2 +- src/stackindex.hpp | 11 +++++++++++ src/unique.hpp | 33 +++++++++++++++++++++------------ 4 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 src/stackindex.hpp (limited to 'src') diff --git a/src/compat.h b/src/compat.h index 90e72a3..081e4c7 100644 --- a/src/compat.h +++ b/src/compat.h @@ -1,6 +1,7 @@ #pragma once #include "debug.h" +#include "stackindex.hpp" // try to detect if we are building against LuaJIT or MoonJIT #if defined(LUA_JITLIBNAME) @@ -32,10 +33,6 @@ // ################################################################################################# -static constexpr StackIndex kIdxRegistry{ LUA_REGISTRYINDEX }; - -// ################################################################################################# - // a strong-typed wrapper over lua types to see them easier in a debugger enum class LuaType { diff --git a/src/luaerrors.h b/src/luaerrors.h index cf04f6c..ef32c60 100644 --- a/src/luaerrors.h +++ b/src/luaerrors.h @@ -1,6 +1,6 @@ #pragma once -#include "unique.hpp" +#include "stackindex.hpp" // ################################################################################################# diff --git a/src/stackindex.hpp b/src/stackindex.hpp new file mode 100644 index 0000000..7c2c17a --- /dev/null +++ b/src/stackindex.hpp @@ -0,0 +1,11 @@ +#pragma once + +#include "unique.hpp" + +DECLARE_UNIQUE_TYPE(StackIndex, int); +static_assert(std::is_trivial_v); + +// ################################################################################################# + +static constexpr StackIndex kIdxRegistry{ LUA_REGISTRYINDEX }; +static constexpr StackIndex kIdxTop{ -1 }; diff --git a/src/unique.hpp b/src/unique.hpp index 846708e..829fa49 100644 --- a/src/unique.hpp +++ b/src/unique.hpp @@ -7,26 +7,39 @@ template class Unique { private: - T val; + T val; // no default initialization so that std::is_trivial_v> == true public: using type = T; - constexpr Unique() = default; - operator T() const { return val; } - Unique& operator=(T const&) = delete; - Unique& operator=(T&&) = delete; - explicit constexpr Unique(T b_) + + ~Unique() = default; + constexpr explicit Unique(T b_) : val{ b_ } { } + + // rule of 5 + constexpr Unique() = default; + constexpr Unique(Unique const&) = default; + constexpr Unique(Unique&&) = default; + constexpr Unique& operator=(Unique const&) = default; + constexpr Unique& operator=(Unique&&) = default; + + // can't implicitly affect from base type + Unique& operator=(T const&) = delete; + constexpr Unique& operator=(T&&) = delete; + + // cast + constexpr operator T() const noexcept { return val; } + // pre-increment - auto& operator++() + auto& operator++() noexcept { ++val; return *this; } // post-increment - auto operator++(int) + auto operator++(int) noexcept { return Unique{ std::exchange(val, val + 1) }; } @@ -46,7 +59,3 @@ class Unique>> }; #define DECLARE_UNIQUE_TYPE(_name, _type) using _name = Unique<_type, class Unique_##_name##_Tag> - -// putting this here to break a header circular dependency until I find a better place -DECLARE_UNIQUE_TYPE(StackIndex, int); -static constexpr StackIndex kIdxTop{ -1 }; -- cgit v1.2.3-55-g6feb