summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-07-24 07:26:02 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-07-24 07:26:02 +0200
commitadb7b1bc4cc1911d02e19d785222f86e21a37bf0 (patch)
tree4661465ccbad7d7894b715a40d4eca0a6ed337a2 /AK
parent442256b5f858740fc935988d502a25385e9e2875 (diff)
downloadserenity-adb7b1bc4cc1911d02e19d785222f86e21a37bf0.zip
AK: Add Optional::value_or(T).
This is like value() but with a fallback in case there's no value set.
Diffstat (limited to 'AK')
-rw-r--r--AK/Optional.h7
1 files changed, 7 insertions, 0 deletions
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 };