diff options
author | Dominick Allen <djallen@librehumanitas.org> | 2024-10-23 13:21:10 -0500 |
---|---|---|
committer | Dominick Allen <djallen@librehumanitas.org> | 2024-10-23 13:21:10 -0500 |
commit | 5cc7cbc3704ec255eb5d0ac53b2cc0fcb1221d63 (patch) | |
tree | 169d4d2d8dffe014851712e31a55036deb0c7c0c /include/fud_option.hpp | |
parent | b2dbcb55e2832c373fecb4033a3ed77e5dbc77aa (diff) |
String conversion and parsing format spec.
Diffstat (limited to 'include/fud_option.hpp')
-rw-r--r-- | include/fud_option.hpp | 45 |
1 files changed, 25 insertions, 20 deletions
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<const ValueType*>(m_data.data()); + return std::move(*reinterpret_cast<const ValueType*>(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 <typename F> @@ -203,27 +224,11 @@ class Option { m_data.clear(); } - // alignas(maxAlign) Array<uint8_t, maxSize> priv_m_data; - alignas(alignof(T)) option_detail::DataArray<Size> m_data{}; bool m_engaged; }; -namespace test { - -void testOption() -{ - Option<int> intOpt; - static_cast<void>(intOpt); - Option<int&> intRefNull; - static_cast<void>(intRefNull); - int value; - Option<int&> intRefValue{value}; -} - -} // namespace test - } // namespace fud #endif |