From 80b18cc979fcb1e772e5b7c7d45aa72602d461d7 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Fri, 20 Dec 2024 15:35:55 +0100 Subject: More work on option --- asl/option.hpp | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) (limited to 'asl/option.hpp') diff --git a/asl/option.hpp b/asl/option.hpp index 4793830..6aa5ebb 100644 --- a/asl/option.hpp +++ b/asl/option.hpp @@ -19,10 +19,85 @@ public: constexpr option() = default; constexpr option(nullopt_t) {} // NOLINT(*-explicit-conversions) + constexpr option(const option& other) requires copy_constructible + { + if (other.m_has_value) + { + m_payload.init_unsafe(other.m_payload.as_init_unsafe()); + m_has_value = true; + } + } + + constexpr option(option&& other) requires move_constructible + { + if (other.m_has_value) + { + m_payload.init_unsafe(ASL_MOVE(other.m_payload.as_init_unsafe())); + m_has_value = true; + } + } + + constexpr option& operator=(const option& other) & requires copy_assignable && copy_constructible + { + if (&other == this) { return *this; } + + if (other.m_has_value) + { + if (m_has_value) + { + m_payload.as_init_unsafe() = other.m_payload.as_init_unsafe(); + } + else + { + m_payload.init_unsafe(other.m_payload.as_init_unsafe()); + m_has_value = true; + } + } + else if (m_has_value) + { + reset(); + } + + return *this; + } + + constexpr option& operator=(option&& other) & requires move_assignable && move_constructible + { + if (&other == this) { return *this; } + + if (other.m_has_value) + { + if (m_has_value) + { + m_payload.as_init_unsafe() = ASL_MOVE(other.m_payload.as_init_unsafe()); + } + else + { + m_payload.init_unsafe(ASL_MOVE(other.m_payload.as_init_unsafe())); + m_has_value = true; + } + } + else if (m_has_value) + { + reset(); + } + + return *this; + } + constexpr ~option() = default; constexpr ~option() requires (!trivially_destructible) { - if (m_has_value) { m_payload.uninit_unsafe(); } + reset(); + } + + constexpr void reset() + { + if (m_has_value) + { + m_payload.uninit_unsafe(); + m_has_value = false; + } } }; -- cgit