mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-07-30 04:39:06 +00:00
LibGC+Everywhere: Factor out a LibGC from LibJS
Resulting in a massive rename across almost everywhere! Alongside the namespace change, we now have the following names: * JS::NonnullGCPtr -> GC::Ref * JS::GCPtr -> GC::Ptr * JS::HeapFunction -> GC::Function * JS::CellImpl -> GC::Cell * JS::Handle -> GC::Root
This commit is contained in:
parent
ce23efc5f6
commit
f87041bf3a
Notes:
github-actions[bot]
2024-11-15 13:50:17 +00:00
Author: https://github.com/shannonbooth
Commit: f87041bf3a
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2345
1722 changed files with 9939 additions and 9906 deletions
241
Libraries/LibGC/Ptr.h
Normal file
241
Libraries/LibGC/Ptr.h
Normal file
|
@ -0,0 +1,241 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Andreas Kling <andreas@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Traits.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
namespace GC {
|
||||
|
||||
template<typename T>
|
||||
class Ptr;
|
||||
|
||||
template<typename T>
|
||||
class Ref {
|
||||
public:
|
||||
Ref() = delete;
|
||||
|
||||
Ref(T& ptr)
|
||||
: m_ptr(&ptr)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Ref(U& ptr)
|
||||
requires(IsConvertible<U*, T*>)
|
||||
: m_ptr(&static_cast<T&>(ptr))
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Ref(Ref<U> const& other)
|
||||
requires(IsConvertible<U*, T*>)
|
||||
: m_ptr(other.ptr())
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Ref& operator=(Ref<U> const& other)
|
||||
requires(IsConvertible<U*, T*>)
|
||||
{
|
||||
m_ptr = static_cast<T*>(other.ptr());
|
||||
return *this;
|
||||
}
|
||||
|
||||
Ref& operator=(T& other)
|
||||
{
|
||||
m_ptr = &other;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Ref& operator=(U& other)
|
||||
requires(IsConvertible<U*, T*>)
|
||||
{
|
||||
m_ptr = &static_cast<T&>(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
RETURNS_NONNULL T* operator->() const { return m_ptr; }
|
||||
|
||||
[[nodiscard]] T& operator*() const { return *m_ptr; }
|
||||
|
||||
RETURNS_NONNULL T* ptr() const { return m_ptr; }
|
||||
|
||||
RETURNS_NONNULL operator T*() const { return m_ptr; }
|
||||
|
||||
operator T&() const { return *m_ptr; }
|
||||
|
||||
private:
|
||||
T* m_ptr { nullptr };
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
class Ptr {
|
||||
public:
|
||||
constexpr Ptr() = default;
|
||||
|
||||
Ptr(T& ptr)
|
||||
: m_ptr(&ptr)
|
||||
{
|
||||
}
|
||||
|
||||
Ptr(T* ptr)
|
||||
: m_ptr(ptr)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Ptr(Ptr<U> const& other)
|
||||
requires(IsConvertible<U*, T*>)
|
||||
: m_ptr(other.ptr())
|
||||
{
|
||||
}
|
||||
|
||||
Ptr(Ref<T> const& other)
|
||||
: m_ptr(other.ptr())
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Ptr(Ref<U> const& other)
|
||||
requires(IsConvertible<U*, T*>)
|
||||
: m_ptr(other.ptr())
|
||||
{
|
||||
}
|
||||
|
||||
Ptr(nullptr_t)
|
||||
: m_ptr(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Ptr& operator=(Ptr<U> const& other)
|
||||
requires(IsConvertible<U*, T*>)
|
||||
{
|
||||
m_ptr = static_cast<T*>(other.ptr());
|
||||
return *this;
|
||||
}
|
||||
|
||||
Ptr& operator=(Ref<T> const& other)
|
||||
{
|
||||
m_ptr = other.ptr();
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Ptr& operator=(Ref<U> const& other)
|
||||
requires(IsConvertible<U*, T*>)
|
||||
{
|
||||
m_ptr = static_cast<T*>(other.ptr());
|
||||
return *this;
|
||||
}
|
||||
|
||||
Ptr& operator=(T& other)
|
||||
{
|
||||
m_ptr = &other;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Ptr& operator=(U& other)
|
||||
requires(IsConvertible<U*, T*>)
|
||||
{
|
||||
m_ptr = &static_cast<T&>(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
Ptr& operator=(T* other)
|
||||
{
|
||||
m_ptr = other;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
Ptr& operator=(U* other)
|
||||
requires(IsConvertible<U*, T*>)
|
||||
{
|
||||
m_ptr = static_cast<T*>(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
T* operator->() const
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
}
|
||||
|
||||
[[nodiscard]] T& operator*() const
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return *m_ptr;
|
||||
}
|
||||
|
||||
T* ptr() const { return m_ptr; }
|
||||
|
||||
explicit operator bool() const { return !!m_ptr; }
|
||||
bool operator!() const { return !m_ptr; }
|
||||
|
||||
operator T*() const { return m_ptr; }
|
||||
|
||||
private:
|
||||
T* m_ptr { nullptr };
|
||||
};
|
||||
|
||||
// Non-Owning GC::Ptr
|
||||
template<typename T>
|
||||
using RawPtr = Ptr<T>;
|
||||
|
||||
// Non-Owning Ref
|
||||
template<typename T>
|
||||
using RawRef = Ref<T>;
|
||||
|
||||
template<typename T, typename U>
|
||||
inline bool operator==(Ptr<T> const& a, Ptr<U> const& b)
|
||||
{
|
||||
return a.ptr() == b.ptr();
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
inline bool operator==(Ptr<T> const& a, Ref<U> const& b)
|
||||
{
|
||||
return a.ptr() == b.ptr();
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
inline bool operator==(Ref<T> const& a, Ref<U> const& b)
|
||||
{
|
||||
return a.ptr() == b.ptr();
|
||||
}
|
||||
|
||||
template<typename T, typename U>
|
||||
inline bool operator==(Ref<T> const& a, Ptr<U> const& b)
|
||||
{
|
||||
return a.ptr() == b.ptr();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<typename T>
|
||||
struct Traits<GC::Ptr<T>> : public DefaultTraits<GC::Ptr<T>> {
|
||||
static unsigned hash(GC::Ptr<T> const& value)
|
||||
{
|
||||
return Traits<T*>::hash(value.ptr());
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct Traits<GC::Ref<T>> : public DefaultTraits<GC::Ref<T>> {
|
||||
static unsigned hash(GC::Ref<T> const& value)
|
||||
{
|
||||
return Traits<T*>::hash(value.ptr());
|
||||
}
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue