summaryrefslogtreecommitdiff
path: root/AK/Optional.h
diff options
context:
space:
mode:
authorMax Wipfli <mail@maxwipfli.ch>2021-06-01 10:12:53 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-01 11:38:17 +0200
commitf3fda59abd44fc34bbb07a6d6dc18cd23ab02c22 (patch)
treea32aae6ba8fe65ff04237db7a85f7c1752a601c6 /AK/Optional.h
parent12a42edd13f6bd2def0ff045389ab289fbcb0b83 (diff)
downloadserenity-f3fda59abd44fc34bbb07a6d6dc18cd23ab02c22.zip
AK: Enable direct comparsion of Optional<T> and T
This patch introduces a new operator== to compare an Optional to its contained type directly. If the Optional does not contain a value, the comparison will always return false. This also adds a test case for the new behavior as well as comparison between Optional objects themselves.
Diffstat (limited to 'AK/Optional.h')
-rw-r--r--AK/Optional.h6
1 files changed, 6 insertions, 0 deletions
diff --git a/AK/Optional.h b/AK/Optional.h
index 9a1b786da8..6b8a987006 100644
--- a/AK/Optional.h
+++ b/AK/Optional.h
@@ -100,6 +100,12 @@ public:
return has_value() == other.has_value() && (!has_value() || value() == other.value());
}
+ template<typename O>
+ ALWAYS_INLINE bool operator==(O const& other) const
+ {
+ return has_value() && value() == other;
+ }
+
ALWAYS_INLINE ~Optional()
{
clear();