summaryrefslogtreecommitdiff
path: root/asl/option.hpp
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2024-12-20 15:35:55 +0100
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2024-12-20 15:35:55 +0100
commit80b18cc979fcb1e772e5b7c7d45aa72602d461d7 (patch)
tree266cf3a6b23ac8f4d903a238f063b87354e231ea /asl/option.hpp
parent0f2d186fbd4cc637a359b1d62370174aebd2b80f (diff)
More work on option
Diffstat (limited to 'asl/option.hpp')
-rw-r--r--asl/option.hpp77
1 files changed, 76 insertions, 1 deletions
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<T>
+ {
+ 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<T>
+ {
+ 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<T> && copy_constructible<T>
+ {
+ 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<T> && move_constructible<T>
+ {
+ 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<T>)
{
- if (m_has_value) { m_payload.uninit_unsafe(); }
+ reset();
+ }
+
+ constexpr void reset()
+ {
+ if (m_has_value)
+ {
+ m_payload.uninit_unsafe();
+ m_has_value = false;
+ }
}
};