2022-08-09 06:32:27 -04:00
|
|
|
/*
|
2024-10-04 07:19:50 -04:00
|
|
|
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
2022-08-09 06:32:27 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-02-26 18:06:27 -05:00
|
|
|
#include <AK/Traits.h>
|
2022-08-09 06:32:27 -04:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
2024-11-14 10:01:23 -05:00
|
|
|
namespace GC {
|
2022-08-09 06:32:27 -04:00
|
|
|
|
|
|
|
template<typename T>
|
2024-11-14 10:01:23 -05:00
|
|
|
class Ptr;
|
2022-08-09 06:32:27 -04:00
|
|
|
|
|
|
|
template<typename T>
|
2024-11-14 10:01:23 -05:00
|
|
|
class Ref {
|
2022-08-09 06:32:27 -04:00
|
|
|
public:
|
2024-11-14 10:01:23 -05:00
|
|
|
Ref() = delete;
|
2022-08-09 06:32:27 -04:00
|
|
|
|
2024-11-14 10:01:23 -05:00
|
|
|
Ref(T& ptr)
|
2022-08-09 06:32:27 -04:00
|
|
|
: m_ptr(&ptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
Ref(U& ptr)
|
2022-10-16 18:06:11 -04:00
|
|
|
requires(IsConvertible<U*, T*>)
|
2022-08-09 06:32:27 -04:00
|
|
|
: m_ptr(&static_cast<T&>(ptr))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
Ref(Ref<U> const& other)
|
2022-10-16 18:06:11 -04:00
|
|
|
requires(IsConvertible<U*, T*>)
|
2023-02-25 12:46:54 -05:00
|
|
|
: m_ptr(other.ptr())
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
Ref& operator=(Ref<U> const& other)
|
2022-10-16 18:06:11 -04:00
|
|
|
requires(IsConvertible<U*, T*>)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
2023-02-25 12:46:54 -05:00
|
|
|
m_ptr = static_cast<T*>(other.ptr());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-11-14 10:01:23 -05:00
|
|
|
Ref& operator=(T& other)
|
2023-02-25 12:46:54 -05:00
|
|
|
{
|
|
|
|
m_ptr = &other;
|
2022-08-09 06:32:27 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
Ref& operator=(U& other)
|
2022-10-16 18:06:11 -04:00
|
|
|
requires(IsConvertible<U*, T*>)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
2023-02-25 12:46:54 -05:00
|
|
|
m_ptr = &static_cast<T&>(other);
|
2022-08-09 06:32:27 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-07-26 13:56:09 -04:00
|
|
|
RETURNS_NONNULL T* operator->() const { return m_ptr; }
|
2022-08-09 06:32:27 -04:00
|
|
|
|
2024-10-24 16:36:28 -04:00
|
|
|
[[nodiscard]] T& operator*() const { return *m_ptr; }
|
2022-08-09 06:32:27 -04:00
|
|
|
|
2023-07-26 13:56:09 -04:00
|
|
|
RETURNS_NONNULL T* ptr() const { return m_ptr; }
|
2022-08-09 06:32:27 -04:00
|
|
|
|
2023-07-26 13:56:09 -04:00
|
|
|
RETURNS_NONNULL operator T*() const { return m_ptr; }
|
2022-08-09 06:32:27 -04:00
|
|
|
|
2022-11-18 20:08:51 -05:00
|
|
|
operator T&() const { return *m_ptr; }
|
2022-08-09 06:32:27 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
T* m_ptr { nullptr };
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2024-11-14 10:01:23 -05:00
|
|
|
class Ptr {
|
2022-08-09 06:32:27 -04:00
|
|
|
public:
|
2024-11-14 10:01:23 -05:00
|
|
|
constexpr Ptr() = default;
|
2022-08-09 06:32:27 -04:00
|
|
|
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr(T& ptr)
|
2022-08-09 06:32:27 -04:00
|
|
|
: m_ptr(&ptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr(T* ptr)
|
2022-08-09 06:32:27 -04:00
|
|
|
: m_ptr(ptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-26 18:07:20 -05:00
|
|
|
template<typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr(Ptr<U> const& other)
|
2023-02-26 18:07:20 -05:00
|
|
|
requires(IsConvertible<U*, T*>)
|
|
|
|
: m_ptr(other.ptr())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr(Ref<T> const& other)
|
2023-02-25 12:46:54 -05:00
|
|
|
: m_ptr(other.ptr())
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr(Ref<U> const& other)
|
2022-10-16 18:06:11 -04:00
|
|
|
requires(IsConvertible<U*, T*>)
|
2023-02-25 12:46:54 -05:00
|
|
|
: m_ptr(other.ptr())
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr(nullptr_t)
|
2022-08-09 06:32:27 -04:00
|
|
|
: m_ptr(nullptr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr& operator=(Ptr<U> const& other)
|
2022-10-16 18:06:11 -04:00
|
|
|
requires(IsConvertible<U*, T*>)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
2023-02-25 12:46:54 -05:00
|
|
|
m_ptr = static_cast<T*>(other.ptr());
|
2022-08-09 06:32:27 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr& operator=(Ref<T> const& other)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
2023-02-25 12:46:54 -05:00
|
|
|
m_ptr = other.ptr();
|
2022-08-09 06:32:27 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr& operator=(Ref<U> const& other)
|
2022-10-16 18:06:11 -04:00
|
|
|
requires(IsConvertible<U*, T*>)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
2023-02-25 12:46:54 -05:00
|
|
|
m_ptr = static_cast<T*>(other.ptr());
|
2022-08-09 06:32:27 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr& operator=(T& other)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
2023-02-25 12:46:54 -05:00
|
|
|
m_ptr = &other;
|
2022-08-09 06:32:27 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr& operator=(U& other)
|
2022-10-16 18:06:11 -04:00
|
|
|
requires(IsConvertible<U*, T*>)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
2023-02-25 12:46:54 -05:00
|
|
|
m_ptr = &static_cast<T&>(other);
|
2022-08-09 06:32:27 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr& operator=(T* other)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
2023-02-25 12:46:54 -05:00
|
|
|
m_ptr = other;
|
2022-08-09 06:32:27 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
Ptr& operator=(U* other)
|
2022-10-16 18:06:11 -04:00
|
|
|
requires(IsConvertible<U*, T*>)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
2023-02-25 12:46:54 -05:00
|
|
|
m_ptr = static_cast<T*>(other);
|
2022-08-09 06:32:27 -04:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-13 18:39:25 -05:00
|
|
|
T* operator->() const
|
|
|
|
{
|
2024-07-09 09:04:25 -04:00
|
|
|
ASSERT(m_ptr);
|
2022-12-13 18:39:25 -05:00
|
|
|
return m_ptr;
|
|
|
|
}
|
|
|
|
|
2024-10-24 16:36:28 -04:00
|
|
|
[[nodiscard]] T& operator*() const
|
2022-12-13 18:39:25 -05:00
|
|
|
{
|
2024-07-09 09:04:25 -04:00
|
|
|
ASSERT(m_ptr);
|
2022-12-13 18:39:25 -05:00
|
|
|
return *m_ptr;
|
|
|
|
}
|
|
|
|
|
2022-11-18 20:08:51 -05:00
|
|
|
T* ptr() const { return m_ptr; }
|
2022-08-09 06:32:27 -04:00
|
|
|
|
2024-04-02 01:43:24 -04:00
|
|
|
explicit operator bool() const { return !!m_ptr; }
|
2022-08-09 06:32:27 -04:00
|
|
|
bool operator!() const { return !m_ptr; }
|
|
|
|
|
2022-11-18 20:08:51 -05:00
|
|
|
operator T*() const { return m_ptr; }
|
2022-08-09 06:32:27 -04:00
|
|
|
|
|
|
|
private:
|
|
|
|
T* m_ptr { nullptr };
|
|
|
|
};
|
|
|
|
|
2024-11-14 10:01:23 -05:00
|
|
|
// Non-Owning GC::Ptr
|
2024-04-05 13:04:37 -04:00
|
|
|
template<typename T>
|
2024-11-14 10:01:23 -05:00
|
|
|
using RawPtr = Ptr<T>;
|
2024-04-05 13:04:37 -04:00
|
|
|
|
2024-11-14 10:01:23 -05:00
|
|
|
// Non-Owning Ref
|
2024-11-11 09:51:30 -05:00
|
|
|
template<typename T>
|
2024-11-14 10:01:23 -05:00
|
|
|
using RawRef = Ref<T>;
|
2024-11-11 09:51:30 -05:00
|
|
|
|
2022-08-09 06:32:27 -04:00
|
|
|
template<typename T, typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
inline bool operator==(Ptr<T> const& a, Ptr<U> const& b)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
|
|
|
return a.ptr() == b.ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
inline bool operator==(Ptr<T> const& a, Ref<U> const& b)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
|
|
|
return a.ptr() == b.ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
inline bool operator==(Ref<T> const& a, Ref<U> const& b)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
|
|
|
return a.ptr() == b.ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T, typename U>
|
2024-11-14 10:01:23 -05:00
|
|
|
inline bool operator==(Ref<T> const& a, Ptr<U> const& b)
|
2022-08-09 06:32:27 -04:00
|
|
|
{
|
|
|
|
return a.ptr() == b.ptr();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-02-26 18:06:27 -05:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
2024-11-14 10:01:23 -05:00
|
|
|
struct Traits<GC::Ptr<T>> : public DefaultTraits<GC::Ptr<T>> {
|
|
|
|
static unsigned hash(GC::Ptr<T> const& value)
|
2023-02-26 18:06:27 -05:00
|
|
|
{
|
|
|
|
return Traits<T*>::hash(value.ptr());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2024-11-14 10:01:23 -05:00
|
|
|
struct Traits<GC::Ref<T>> : public DefaultTraits<GC::Ref<T>> {
|
|
|
|
static unsigned hash(GC::Ref<T> const& value)
|
2023-02-26 18:06:27 -05:00
|
|
|
{
|
|
|
|
return Traits<T*>::hash(value.ptr());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|