From 5cc7cbc3704ec255eb5d0ac53b2cc0fcb1221d63 Mon Sep 17 00:00:00 2001 From: Dominick Allen Date: Wed, 23 Oct 2024 13:21:10 -0500 Subject: String conversion and parsing format spec. --- include/fud_option.hpp | 45 +++++++++++++++++++++++++-------------------- 1 file changed, 25 insertions(+), 20 deletions(-) (limited to 'include/fud_option.hpp') diff --git a/include/fud_option.hpp b/include/fud_option.hpp index ca3954f..931ef82 100644 --- a/include/fud_option.hpp +++ b/include/fud_option.hpp @@ -103,16 +103,16 @@ class Option { } } - constexpr Option(const Option& rhs) noexcept : m_engaged(rhs.m_engaged), m_data(rhs.m_data) + constexpr Option(const Option& rhs) noexcept : m_data(rhs.m_data), m_engaged(rhs.m_engaged) { } - constexpr Option(Option&& rhs) noexcept : m_engaged(rhs.m_engaged), m_data(std::move(rhs.m_data)) + constexpr Option(Option&& rhs) noexcept : m_data(std::move(rhs.m_data)), m_engaged(rhs.m_engaged) { rhs.cleanup(); } - ~Option() noexcept + constexpr ~Option() noexcept { destroy(); } @@ -142,6 +142,11 @@ class Option { return m_engaged; } + [[nodiscard]] bool isNone() const + { + return !m_engaged; + } + operator bool() const { return hasValue(); } @@ -170,7 +175,23 @@ class Option { { fudAssert(m_engaged); static_assert(!IsRef); - return *reinterpret_cast(m_data.data()); + return std::move(*reinterpret_cast(m_data.data())); + } + + [[nodiscard]] constexpr const ValueType& valueOr(const ValueType& alternative) const& + { + if (m_engaged) { + return value(); + } + return alternative; + } + + [[nodiscard]] constexpr ValueType&& valueOr(ValueType&& alternative) const&& + { + if (m_engaged) { + return value(); + } + return std::move(alternative); } template @@ -203,27 +224,11 @@ class Option { m_data.clear(); } - // alignas(maxAlign) Array priv_m_data; - alignas(alignof(T)) option_detail::DataArray m_data{}; bool m_engaged; }; -namespace test { - -void testOption() -{ - Option intOpt; - static_cast(intOpt); - Option intRefNull; - static_cast(intRefNull); - int value; - Option intRefValue{value}; -} - -} // namespace test - } // namespace fud #endif -- cgit v1.2.3