diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-10-09 09:43:24 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-10-09 09:43:24 +0200 |
commit | ea290e120ee0069e0c4a983d49fb69fc942dc3f2 (patch) | |
tree | d98f519b0a39f0f195b91f78ae62e040b5c59a79 | |
parent | 08ceea621246a5085d75d698af5e7968262f4b3a (diff) | |
download | lanes-ea290e120ee0069e0c4a983d49fb69fc942dc3f2.tar.gz lanes-ea290e120ee0069e0c4a983d49fb69fc942dc3f2.tar.bz2 lanes-ea290e120ee0069e0c4a983d49fb69fc942dc3f2.zip |
Improved Unique<> implementation, moved StackIndex into a separate header
-rw-r--r-- | src/compat.h | 5 | ||||
-rw-r--r-- | src/luaerrors.h | 2 | ||||
-rw-r--r-- | src/stackindex.hpp | 11 | ||||
-rw-r--r-- | src/unique.hpp | 33 |
4 files changed, 34 insertions, 17 deletions
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 @@ | |||
1 | #pragma once | 1 | #pragma once |
2 | 2 | ||
3 | #include "debug.h" | 3 | #include "debug.h" |
4 | #include "stackindex.hpp" | ||
4 | 5 | ||
5 | // try to detect if we are building against LuaJIT or MoonJIT | 6 | // try to detect if we are building against LuaJIT or MoonJIT |
6 | #if defined(LUA_JITLIBNAME) | 7 | #if defined(LUA_JITLIBNAME) |
@@ -32,10 +33,6 @@ | |||
32 | 33 | ||
33 | // ################################################################################################# | 34 | // ################################################################################################# |
34 | 35 | ||
35 | static constexpr StackIndex kIdxRegistry{ LUA_REGISTRYINDEX }; | ||
36 | |||
37 | // ################################################################################################# | ||
38 | |||
39 | // a strong-typed wrapper over lua types to see them easier in a debugger | 36 | // a strong-typed wrapper over lua types to see them easier in a debugger |
40 | enum class LuaType | 37 | enum class LuaType |
41 | { | 38 | { |
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 @@ | |||
1 | #pragma once | 1 | #pragma once |
2 | 2 | ||
3 | #include "unique.hpp" | 3 | #include "stackindex.hpp" |
4 | 4 | ||
5 | // ################################################################################################# | 5 | // ################################################################################################# |
6 | 6 | ||
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 @@ | |||
1 | #pragma once | ||
2 | |||
3 | #include "unique.hpp" | ||
4 | |||
5 | DECLARE_UNIQUE_TYPE(StackIndex, int); | ||
6 | static_assert(std::is_trivial_v<StackIndex>); | ||
7 | |||
8 | // ################################################################################################# | ||
9 | |||
10 | static constexpr StackIndex kIdxRegistry{ LUA_REGISTRYINDEX }; | ||
11 | 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 <typename T, typename TAG, typename specialization = void> | |||
7 | class Unique | 7 | class Unique |
8 | { | 8 | { |
9 | private: | 9 | private: |
10 | T val; | 10 | T val; // no default initialization so that std::is_trivial_v<Unique<T>> == true |
11 | 11 | ||
12 | public: | 12 | public: |
13 | using type = T; | 13 | using type = T; |
14 | constexpr Unique() = default; | 14 | |
15 | operator T() const { return val; } | 15 | ~Unique() = default; |
16 | Unique& operator=(T const&) = delete; | 16 | constexpr explicit Unique(T b_) |
17 | Unique& operator=(T&&) = delete; | ||
18 | explicit constexpr Unique(T b_) | ||
19 | : val{ b_ } | 17 | : val{ b_ } |
20 | { | 18 | { |
21 | } | 19 | } |
20 | |||
21 | // rule of 5 | ||
22 | constexpr Unique() = default; | ||
23 | constexpr Unique(Unique const&) = default; | ||
24 | constexpr Unique(Unique&&) = default; | ||
25 | constexpr Unique& operator=(Unique const&) = default; | ||
26 | constexpr Unique& operator=(Unique&&) = default; | ||
27 | |||
28 | // can't implicitly affect from base type | ||
29 | Unique& operator=(T const&) = delete; | ||
30 | constexpr Unique& operator=(T&&) = delete; | ||
31 | |||
32 | // cast | ||
33 | constexpr operator T() const noexcept { return val; } | ||
34 | |||
22 | // pre-increment | 35 | // pre-increment |
23 | auto& operator++() | 36 | auto& operator++() noexcept |
24 | { | 37 | { |
25 | ++val; | 38 | ++val; |
26 | return *this; | 39 | return *this; |
27 | } | 40 | } |
28 | // post-increment | 41 | // post-increment |
29 | auto operator++(int) | 42 | auto operator++(int) noexcept |
30 | { | 43 | { |
31 | return Unique<T, TAG>{ std::exchange(val, val + 1) }; | 44 | return Unique<T, TAG>{ std::exchange(val, val + 1) }; |
32 | } | 45 | } |
@@ -46,7 +59,3 @@ class Unique<T, TAG, std::enable_if_t<!std::is_scalar_v<T>>> | |||
46 | }; | 59 | }; |
47 | 60 | ||
48 | #define DECLARE_UNIQUE_TYPE(_name, _type) using _name = Unique<_type, class Unique_##_name##_Tag> | 61 | #define DECLARE_UNIQUE_TYPE(_name, _type) using _name = Unique<_type, class Unique_##_name##_Tag> |
49 | |||
50 | // putting this here to break a header circular dependency until I find a better place | ||
51 | DECLARE_UNIQUE_TYPE(StackIndex, int); | ||
52 | static constexpr StackIndex kIdxTop{ -1 }; | ||