From adb7b1bc4cc1911d02e19d785222f86e21a37bf0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 24 Jul 2019 07:26:02 +0200 Subject: [PATCH] AK: Add Optional::value_or(T). This is like value() but with a fallback in case there's no value set. --- AK/Optional.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/AK/Optional.h b/AK/Optional.h index 7399b2ee4b..9dbef306fb 100644 --- a/AK/Optional.h +++ b/AK/Optional.h @@ -96,6 +96,13 @@ public: return released_value; } + T value_or(const T& fallback) const + { + if (has_value()) + return value(); + return fallback; + } + private: char m_storage[sizeof(T)] __attribute__((aligned(sizeof(T)))); bool m_has_value { false };