From 62003d96f3045038a7c9b72f6b0976cd2d919117 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Fri, 12 Jul 2024 11:22:10 +0300 Subject: [PATCH] BAN: Implement Optional::value_or This will return value specified in the argument if optional is not storing any value. --- BAN/include/BAN/Optional.h | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/BAN/include/BAN/Optional.h b/BAN/include/BAN/Optional.h index 946ccc05c2..611dff9d00 100644 --- a/BAN/include/BAN/Optional.h +++ b/BAN/include/BAN/Optional.h @@ -40,6 +40,8 @@ namespace BAN constexpr T release_value(); constexpr T& value(); constexpr const T& value() const; + constexpr T& value_or(T&); + constexpr const T& value_or(const T&) const; constexpr void clear(); @@ -112,7 +114,7 @@ namespace BAN { clear(); m_has_value = other.has_value(); - if (other.has_value) + if (other.has_value()) new (m_storage) T(other.value()); return *this; } @@ -185,6 +187,22 @@ namespace BAN return (const T&)m_storage; } + template + constexpr T& Optional::value_or(T& empty) + { + if (!has_value()) + return empty; + return (T&)m_storage; + } + + template + constexpr const T& Optional::value_or(const T& empty) const + { + if (!has_value()) + return empty; + return (const T&)m_storage; + } + template constexpr void Optional::clear() {