From b869f490c33cd97f79fbb667e68411491e1d4309 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Mon, 25 Dec 2023 00:51:17 -0600 Subject: [PATCH] [ox/std] Add or_value to Optional, Result --- deps/ox/src/ox/std/error.hpp | 8 +++--- deps/ox/src/ox/std/optional.hpp | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/deps/ox/src/ox/std/error.hpp b/deps/ox/src/ox/std/error.hpp index af36c104..4b54906f 100644 --- a/deps/ox/src/ox/std/error.hpp +++ b/deps/ox/src/ox/std/error.hpp @@ -269,7 +269,7 @@ struct [[nodiscard]] Result { * @param alt * @return value of Result or alt */ - constexpr T orVal(T &&alt) & noexcept { + constexpr T or_value(T &&alt) const& noexcept { if (error) { return std::move(alt); } @@ -281,7 +281,7 @@ struct [[nodiscard]] Result { * @param alt * @return value of Result or alt */ - constexpr T orVal(T &&alt) && noexcept { + constexpr T or_value(T &&alt) && noexcept { if (error) { return std::move(alt); } @@ -293,7 +293,7 @@ struct [[nodiscard]] Result { * @param alt * @return value of Result or alt */ - constexpr T orVal(T const&alt) & noexcept { + constexpr T or_value(T const&alt) const& noexcept { if (error) { return alt; } @@ -305,7 +305,7 @@ struct [[nodiscard]] Result { * @param alt * @return value of Result or alt */ - constexpr T orVal(T const&alt) && noexcept { + constexpr T or_value(T const&alt) && noexcept { if (error) { return alt; } diff --git a/deps/ox/src/ox/std/optional.hpp b/deps/ox/src/ox/std/optional.hpp index 1a8fb3b2..687fda4c 100644 --- a/deps/ox/src/ox/std/optional.hpp +++ b/deps/ox/src/ox/std/optional.hpp @@ -64,6 +64,54 @@ class Optional { return *m_ptr; } + /** + * Returns parameter alt if Result contains an error. + * @param alt + * @return value of Result or alt + */ + constexpr T or_value(T &&alt) const& noexcept { + if (!m_ptr) { + return std::move(alt); + } + return *m_ptr; + } + + /** + * Returns parameter alt if Result contains an error. + * @param alt + * @return value of Result or alt + */ + constexpr T or_value(T &&alt) && noexcept { + if (!m_ptr) { + return std::move(alt); + } + return std::move(*m_ptr); + } + + /** + * Returns parameter alt if Result contains an error. + * @param alt + * @return value of Result or alt + */ + constexpr T or_value(T const&alt) const& noexcept { + if (!m_ptr) { + return alt; + } + return *m_ptr; + } + + /** + * Returns parameter alt if Result contains an error. + * @param alt + * @return value of Result or alt + */ + constexpr T or_value(T const&alt) && noexcept { + if (!m_ptr) { + return alt; + } + return std::move(*m_ptr); + } + constexpr T &operator*() & noexcept { return *m_ptr; }