From 6d4855d4ad1a07f083aae01bdc233364c2ba8e02 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 23 Dec 2023 17:52:20 -0600 Subject: [PATCH] [ox/std] Add Result::orVal (synced from 45ec39f77bf4c7f980cd66847798853988873964) --- src/ox/std/error.hpp | 48 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/ox/std/error.hpp b/src/ox/std/error.hpp index a3de89525..af36c1046 100644 --- a/src/ox/std/error.hpp +++ b/src/ox/std/error.hpp @@ -264,6 +264,54 @@ struct [[nodiscard]] Result { return f(std::move(value)); } + /** + * Returns parameter alt if Result contains an error. + * @param alt + * @return value of Result or alt + */ + constexpr T orVal(T &&alt) & noexcept { + if (error) { + return std::move(alt); + } + return value; + } + + /** + * Returns parameter alt if Result contains an error. + * @param alt + * @return value of Result or alt + */ + constexpr T orVal(T &&alt) && noexcept { + if (error) { + return std::move(alt); + } + return std::move(value); + } + + /** + * Returns parameter alt if Result contains an error. + * @param alt + * @return value of Result or alt + */ + constexpr T orVal(T const&alt) & noexcept { + if (error) { + return alt; + } + return value; + } + + /** + * Returns parameter alt if Result contains an error. + * @param alt + * @return value of Result or alt + */ + constexpr T orVal(T const&alt) && noexcept { + if (error) { + return alt; + } + return std::move(value); + } + }; namespace detail {