LibWeb/CSS: Extract ValueComparingRefPtr types into their own header

I originally wanted to move this to prevent a circular dependency, in a
commit that I'm probably not submitting any more. But also, there's
nothing about this type that is CSS or LibWeb-specific, so let's put
this in AK.
This commit is contained in:
Sam Atkins 2026-01-13 14:06:36 +00:00 committed by Jelle Raaijmakers
parent 54c2ecedca
commit 0f04c6dd3e
Notes: github-actions[bot] 2026-02-06 11:12:06 +00:00
2 changed files with 66 additions and 52 deletions

65
AK/ValueComparingRefPtr.h Normal file
View file

@ -0,0 +1,65 @@
/*
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefPtr.h>
namespace Web::CSS {
template<typename T>
struct ValueComparingNonnullRefPtr : public NonnullRefPtr<T> {
using NonnullRefPtr<T>::NonnullRefPtr;
ValueComparingNonnullRefPtr(NonnullRefPtr<T> const& other)
: NonnullRefPtr<T>(other)
{
}
ValueComparingNonnullRefPtr(NonnullRefPtr<T>&& other)
: NonnullRefPtr<T>(move(other))
{
}
bool operator==(ValueComparingNonnullRefPtr const& other) const
{
return this->ptr() == other.ptr() || this->ptr()->equals(*other);
}
private:
using NonnullRefPtr<T>::operator==;
};
template<typename T>
struct ValueComparingRefPtr : public RefPtr<T> {
using RefPtr<T>::RefPtr;
ValueComparingRefPtr(RefPtr<T> const& other)
: RefPtr<T>(other)
{
}
ValueComparingRefPtr(RefPtr<T>&& other)
: RefPtr<T>(move(other))
{
}
template<typename U>
bool operator==(ValueComparingNonnullRefPtr<U> const& other) const
{
return this->ptr() == other.ptr() || (this->ptr() && this->ptr()->equals(*other));
}
bool operator==(ValueComparingRefPtr const& other) const
{
return this->ptr() == other.ptr() || (this->ptr() && other.ptr() && this->ptr()->equals(*other));
}
private:
using RefPtr<T>::operator==;
};
}

View file

@ -16,6 +16,7 @@
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <AK/ValueComparingRefPtr.h>
#include <AK/Vector.h>
#include <AK/WeakPtr.h>
#include <LibGfx/Color.h>
@ -99,58 +100,6 @@ namespace Web::CSS {
__ENUMERATE_CSS_STYLE_VALUE_TYPE(ValueList, value_list, StyleValueList) \
__ENUMERATE_CSS_STYLE_VALUE_TYPE(ViewFunction, view_function, ViewFunctionStyleValue)
template<typename T>
struct ValueComparingNonnullRefPtr : public NonnullRefPtr<T> {
using NonnullRefPtr<T>::NonnullRefPtr;
ValueComparingNonnullRefPtr(NonnullRefPtr<T> const& other)
: NonnullRefPtr<T>(other)
{
}
ValueComparingNonnullRefPtr(NonnullRefPtr<T>&& other)
: NonnullRefPtr<T>(move(other))
{
}
bool operator==(ValueComparingNonnullRefPtr const& other) const
{
return this->ptr() == other.ptr() || this->ptr()->equals(*other);
}
private:
using NonnullRefPtr<T>::operator==;
};
template<typename T>
struct ValueComparingRefPtr : public RefPtr<T> {
using RefPtr<T>::RefPtr;
ValueComparingRefPtr(RefPtr<T> const& other)
: RefPtr<T>(other)
{
}
ValueComparingRefPtr(RefPtr<T>&& other)
: RefPtr<T>(move(other))
{
}
template<typename U>
bool operator==(ValueComparingNonnullRefPtr<U> const& other) const
{
return this->ptr() == other.ptr() || (this->ptr() && this->ptr()->equals(*other));
}
bool operator==(ValueComparingRefPtr const& other) const
{
return this->ptr() == other.ptr() || (this->ptr() && other.ptr() && this->ptr()->equals(*other));
}
private:
using RefPtr<T>::operator==;
};
struct ColorResolutionContext {
Optional<PreferredColorScheme> color_scheme;
Optional<Color> current_color;