diff --git a/AK/Optional.h b/AK/Optional.h index d214b17fb69..ac777e21c1f 100644 --- a/AK/Optional.h +++ b/AK/Optional.h @@ -22,11 +22,16 @@ namespace AK { // Make sure you didn't accidentally make your destructor private before // you start bug hunting. :^) +template +class Optional; + template -class [[nodiscard]] Optional { +requires(!IsLvalueReference) class [[nodiscard]] Optional { template friend class Optional; + static_assert(!IsLvalueReference && !IsRvalueReference); + public: using ValueType = T; @@ -202,6 +207,161 @@ private: alignas(T) u8 m_storage[sizeof(T)]; bool m_has_value { false }; }; + +template +requires(IsLvalueReference) class [[nodiscard]] Optional { + template + friend class Optional; + + template + constexpr static bool CanBePlacedInOptional = IsSame, RemoveReference>> && (IsBaseOf, RemoveCVReference> || IsSame, RemoveCVReference>); + +public: + using ValueType = T; + + ALWAYS_INLINE Optional() = default; + + template + ALWAYS_INLINE Optional(U& value) requires(CanBePlacedInOptional) + : m_pointer(&value) + { + } + + ALWAYS_INLINE Optional(RemoveReference& value) + : m_pointer(&value) + { + } + + ALWAYS_INLINE Optional(Optional const& other) + : m_pointer(other.m_pointer) + { + } + + ALWAYS_INLINE Optional(Optional&& other) + : m_pointer(other.m_pointer) + { + other.m_pointer = nullptr; + } + + template + ALWAYS_INLINE Optional(Optional const& other) requires(CanBePlacedInOptional) + : m_pointer(other.m_pointer) + { + } + + template + ALWAYS_INLINE Optional(Optional&& other) requires(CanBePlacedInOptional) + : m_pointer(other.m_pointer) + { + other.m_pointer = nullptr; + } + + ALWAYS_INLINE Optional& operator=(Optional const& other) + { + m_pointer = other.m_pointer; + return *this; + } + + ALWAYS_INLINE Optional& operator=(Optional&& other) + { + m_pointer = other.m_pointer; + other.m_pointer = nullptr; + return *this; + } + + template + ALWAYS_INLINE Optional& operator=(Optional const& other) requires(CanBePlacedInOptional) + { + m_pointer = other.m_pointer; + return *this; + } + + template + ALWAYS_INLINE Optional& operator=(Optional&& other) requires(CanBePlacedInOptional) + { + m_pointer = other.m_pointer; + other.m_pointer = nullptr; + return *this; + } + + // Note: Disallows assignment from a temporary as this does not do any lifetime extension. + template + ALWAYS_INLINE Optional& operator=(U&& value) requires(CanBePlacedInOptional&& IsLvalueReference) + { + m_pointer = &value; + return *this; + } + + ALWAYS_INLINE void clear() + { + m_pointer = nullptr; + } + + [[nodiscard]] ALWAYS_INLINE bool has_value() const { return m_pointer != nullptr; } + + [[nodiscard]] ALWAYS_INLINE T value() + { + VERIFY(m_pointer); + return *m_pointer; + } + + [[nodiscard]] ALWAYS_INLINE AddConstToReferencedType value() const + { + VERIFY(m_pointer); + return *m_pointer; + } + + template + requires(IsBaseOf, U>) [[nodiscard]] ALWAYS_INLINE AddConstToReferencedType value_or(U& fallback) const + { + if (m_pointer) + return value(); + return fallback; + } + + // Note that this ends up copying the value. + [[nodiscard]] ALWAYS_INLINE RemoveCVReference value_or(RemoveCVReference fallback) const + { + if (m_pointer) + return value(); + return fallback; + } + + [[nodiscard]] ALWAYS_INLINE T release_value() + { + return *exchange(m_pointer, nullptr); + } + + template + ALWAYS_INLINE bool operator==(Optional const& other) const + { + return has_value() == other.has_value() && (!has_value() || value() == other.value()); + } + + template + ALWAYS_INLINE bool operator==(U const& other) const + { + return has_value() && value() == other; + } + + ALWAYS_INLINE AddConstToReferencedType operator*() const { return value(); } + ALWAYS_INLINE T operator*() { return value(); } + + ALWAYS_INLINE RawPtr>> operator->() const { return &value(); } + ALWAYS_INLINE RawPtr> operator->() { return &value(); } + + // Conversion operators from Optional -> Optional + ALWAYS_INLINE operator Optional>() const + { + if (has_value()) + return Optional>(value()); + return {}; + } + +private: + RemoveReference* m_pointer { nullptr }; +}; + } using AK::Optional; diff --git a/AK/StdLibExtraDetails.h b/AK/StdLibExtraDetails.h index 1b6bc6b9448..5d0006a2056 100644 --- a/AK/StdLibExtraDetails.h +++ b/AK/StdLibExtraDetails.h @@ -25,6 +25,24 @@ using TrueType = IntegralConstant; template using AddConst = const T; +template +struct __AddConstToReferencedType { + using Type = T; +}; + +template +struct __AddConstToReferencedType { + using Type = AddConst&; +}; + +template +struct __AddConstToReferencedType { + using Type = AddConst&&; +}; + +template +using AddConstToReferencedType = typename __AddConstToReferencedType::Type; + template struct __RemoveConst { using Type = T; @@ -577,6 +595,7 @@ inline constexpr bool IsOneOf = (IsSame || ...); } using AK::Detail::AddConst; +using AK::Detail::AddConstToReferencedType; using AK::Detail::AddLvalueReference; using AK::Detail::AddRvalueReference; using AK::Detail::AssertSize; diff --git a/Tests/AK/TestOptional.cpp b/Tests/AK/TestOptional.cpp index e96c12a23c9..723d3dd74b3 100644 --- a/Tests/AK/TestOptional.cpp +++ b/Tests/AK/TestOptional.cpp @@ -211,3 +211,61 @@ TEST_CASE(test_copy_ctor_and_dtor_called) static_assert(!IsDestructible>); #endif } + +TEST_CASE(basic_optional_reference) +{ + Optional x; + EXPECT_EQ(x.has_value(), false); + int a = 3; + x = a; + EXPECT_EQ(x.has_value(), true); + EXPECT_EQ(x.value(), 3); + EXPECT_EQ(&x.value(), &a); + + Optional y; + EXPECT_EQ(y.has_value(), false); + int b = 3; + y = b; + EXPECT_EQ(y.has_value(), true); + EXPECT_EQ(y.value(), 3); + EXPECT_EQ(&y.value(), &b); + static_assert(IsConst>); +} + +TEST_CASE(move_optional_reference) +{ + Optional x; + EXPECT_EQ(x.has_value(), false); + int b = 3; + x = b; + EXPECT_EQ(x.has_value(), true); + EXPECT_EQ(x.value(), 3); + + Optional y; + y = move(x); + EXPECT_EQ(y.has_value(), true); + EXPECT_EQ(y.value(), 3); + EXPECT_EQ(x.has_value(), false); +} + +TEST_CASE(short_notation_reference) +{ + StringView test = "foo"; + Optional value = test; + + EXPECT_EQ(value->length(), 3u); + EXPECT_EQ(*value, "foo"); +} + +TEST_CASE(comparison_reference) +{ + StringView test = "foo"; + Optional opt0; + Optional opt1 = test; + Optional opt2 = "foo"; + Optional opt3 = "bar"; + + EXPECT_NE(opt0, opt1); + EXPECT_EQ(opt1, opt2); + EXPECT_NE(opt1, opt3); +}