diff options
Diffstat (limited to 'include/fud_option.hpp')
-rw-r--r-- | include/fud_option.hpp | 39 |
1 files changed, 24 insertions, 15 deletions
diff --git a/include/fud_option.hpp b/include/fud_option.hpp index 9d3068c..5a5611f 100644 --- a/include/fud_option.hpp +++ b/include/fud_option.hpp @@ -89,7 +89,16 @@ class Option { static_cast<void>(nullOpt); } - constexpr Option(T value) noexcept : m_engaged{true} + constexpr Option(const Option& rhs) noexcept : m_data(rhs.m_data), m_engaged(rhs.m_engaged) + { + } + + constexpr Option(Option&& rhs) noexcept : m_data(std::move(rhs.m_data)), m_engaged(rhs.m_engaged) + { + rhs.cleanup(); + } + + constexpr Option(const T& value) noexcept : m_engaged{true} { if constexpr (IsRef) { new (m_data.data()) std::reference_wrapper<ValueType>(std::ref(value)); @@ -98,30 +107,30 @@ class Option { } } - constexpr static Option take(T&& value) noexcept + constexpr Option(T&& value) noexcept requires (not IsRef) : m_engaged{true} { - Option option{}; - option.m_engaged = true; if constexpr (IsRef) { - new (option.m_data.data()) std::reference_wrapper<ValueType>(std::ref(value)); + new (m_data.data()) std::reference_wrapper<ValueType>(std::ref(value)); } else { - new (option.m_data.data()) ValueType(std::move(value)); + new (m_data.data()) ValueType(std::move(value)); } - return option; } - constexpr Option(const Option& rhs) noexcept : m_data(rhs.m_data), m_engaged(rhs.m_engaged) - { - } - - constexpr Option(Option&& rhs) noexcept : m_data(std::move(rhs.m_data)), m_engaged(rhs.m_engaged) + constexpr ~Option() noexcept { - rhs.cleanup(); + destroy(); } - constexpr ~Option() noexcept + constexpr static Option take(T&& value) noexcept { - destroy(); + Option option{}; + option.m_engaged = true; + if constexpr (IsRef) { + new (option.m_data.data()) std::reference_wrapper<ValueType>(std::ref(value)); + } else { + new (option.m_data.data()) ValueType(std::move(value)); + } + return option; } Option& operator=(const Option& rhs) noexcept |