Files
ladybird/AK/ValueComparingRefPtr.h

71 lines
1.5 KiB
C++

/*
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefPtr.h>
namespace AK {
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));
}
bool operator==(nullptr_t) const
{
return this->is_null();
}
private:
using RefPtr<T>::operator==;
};
}