From a7c014125fcf82be8c3cb39398e439babe21c99f Mon Sep 17 00:00:00 2001 From: asynts Date: Wed, 30 Dec 2020 21:16:37 +0100 Subject: [PATCH] AK: Add operator* and operator-> overloads in Optional. --- AK/Optional.h | 6 ++++++ AK/Tests/TestOptional.cpp | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/AK/Optional.h b/AK/Optional.h index 092cdc6e9f..f2fd929fb3 100644 --- a/AK/Optional.h +++ b/AK/Optional.h @@ -155,6 +155,12 @@ public: return fallback; } + const T& operator*() const { return value(); } + T& operator*() { return value(); } + + const T* operator->() const { return &value(); } + T* operator->() { return &value(); } + private: // Call when we don't want to alter the consume state ALWAYS_INLINE const T& value_without_consume_state() const diff --git a/AK/Tests/TestOptional.cpp b/AK/Tests/TestOptional.cpp index 76b3bf2676..15ac04c25f 100644 --- a/AK/Tests/TestOptional.cpp +++ b/AK/Tests/TestOptional.cpp @@ -66,4 +66,12 @@ TEST_CASE(optional_leak_1) EXPECT_EQ(vec[0].str.value(), "foo"); } +TEST_CASE(short_notation) +{ + Optional value = "foo"; + + EXPECT_EQ(value->length(), 3u); + EXPECT_EQ(*value, "foo"); +} + TEST_MAIN(Optional)