Skip to content

Commit

Permalink
AK: Merge implementations of operator== for Optional
Browse files Browse the repository at this point in the history
Instead of having a overload of the operator in each specialization of
Optional, use a free function as a common implementation.
  • Loading branch information
LucasChollet authored and awesomekling committed Jan 3, 2025
1 parent a4abb35 commit d6abd44
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 74 deletions.
12 changes: 0 additions & 12 deletions AK/FlyString.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,18 +155,6 @@ class Optional<FlyString> : public OptionalBase<FlyString> {
return *this;
}

template<typename O>
ALWAYS_INLINE bool operator==(Optional<O> const& other) const
{
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;
}

void clear()
{
m_value = FlyString(nullptr);
Expand Down
51 changes: 13 additions & 38 deletions AK/Optional.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,6 @@ requires(!IsLvalueReference<Self>) class [[nodiscard]] OptionalBase {
return TRY(callback());
}

template<typename O>
ALWAYS_INLINE bool operator==(Optional<O> const& other) const
{
return static_cast<Self const&>(*this).has_value() == (other).has_value()
&& (!static_cast<Self const&>(*this).has_value() || static_cast<Self const&>(*this).value() == (other).value());
}

template<typename O>
requires(!Detail::IsBaseOf<OptionalBase<T, Self>, O>)
ALWAYS_INLINE bool operator==(O const& other) const
{
return static_cast<Self const&>(*this).has_value() && static_cast<Self const&>(*this).value() == other;
}

[[nodiscard]] ALWAYS_INLINE T const& operator*() const { return static_cast<Self const&>(*this).value(); }
[[nodiscard]] ALWAYS_INLINE T& operator*() { return static_cast<Self&>(*this).value(); }

Expand Down Expand Up @@ -262,18 +248,6 @@ requires(!IsLvalueReference<T>) class [[nodiscard]] Optional<T> : public Optiona
return *this;
}

template<typename O>
ALWAYS_INLINE bool operator==(Optional<O> const& other) const
{
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()
requires(!IsTriviallyDestructible<T> && IsDestructible<T>)
{
Expand Down Expand Up @@ -488,18 +462,6 @@ requires(IsLvalueReference<T>) class [[nodiscard]] Optional<T> {
return *exchange(m_pointer, nullptr);
}

template<typename U>
ALWAYS_INLINE bool operator==(Optional<U> const& other) const
{
return has_value() == other.has_value() && (!has_value() || value() == other.value());
}

template<typename U>
ALWAYS_INLINE bool operator==(U const& other) const
{
return has_value() && value() == other;
}

ALWAYS_INLINE AddConstToReferencedType<T> operator*() const { return value(); }
ALWAYS_INLINE T operator*() { return value(); }

Expand Down Expand Up @@ -595,6 +557,19 @@ requires(IsLvalueReference<T>) class [[nodiscard]] Optional<T> {
RemoveReference<T>* m_pointer { nullptr };
};

template<typename T1, typename T2>
ALWAYS_INLINE bool operator==(Optional<T1> const& first, Optional<T2> const& second)
{
return first.has_value() == second.has_value()
&& (!first.has_value() || first.value() == second.value());
}

template<typename T1, typename T2>
ALWAYS_INLINE bool operator==(Optional<T1> const& first, T2 const& second)
{
return first.has_value() && first.value() == second;
}

}

#if USING_AK_GLOBALLY
Expand Down
12 changes: 0 additions & 12 deletions AK/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,18 +283,6 @@ class Optional<String> : public OptionalBase<String> {
return *this;
}

template<typename O>
ALWAYS_INLINE bool operator==(Optional<O> const& other) const
{
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;
}

void clear()
{
m_value = String(nullptr);
Expand Down
12 changes: 0 additions & 12 deletions Libraries/LibJS/Runtime/Value.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,18 +593,6 @@ class Optional<JS::Value> : public OptionalBase<JS::Value> {
return *this;
}

template<typename O>
ALWAYS_INLINE bool operator==(Optional<O> const& other) const
{
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;
}

void clear()
{
m_value = {};
Expand Down

0 comments on commit d6abd44

Please sign in to comment.