mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 15:10:19 +00:00
cc4b3cbacc
Some checks are pending
CI / Lagom (false, FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, macos-14, macOS, Clang) (push) Waiting to run
CI / Lagom (false, NO_FUZZ, ubuntu-24.04, Linux, GNU) (push) Waiting to run
CI / Lagom (true, NO_FUZZ, ubuntu-24.04, Linux, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (macos-14, macOS, macOS-universal2) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (ubuntu-24.04, Linux, Linux-x86_64) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Push notes / build (push) Waiting to run
92 lines
1.5 KiB
C++
92 lines
1.5 KiB
C++
/*
|
|
* Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/Assertions.h>
|
|
#include <AK/RefCounted.h>
|
|
#include <AK/RefPtr.h>
|
|
#include <AK/StdLibExtras.h>
|
|
|
|
namespace AK {
|
|
|
|
template<typename T>
|
|
class Weakable;
|
|
template<typename T>
|
|
class WeakPtr;
|
|
|
|
class WeakLink : public RefCounted<WeakLink> {
|
|
template<typename T>
|
|
friend class Weakable;
|
|
template<typename T>
|
|
friend class WeakPtr;
|
|
|
|
public:
|
|
template<typename T>
|
|
RefPtr<T> strong_ref() const
|
|
requires(IsBaseOf<RefCountedBase, T>)
|
|
{
|
|
return static_cast<T*>(m_ptr);
|
|
}
|
|
|
|
template<typename T>
|
|
T* unsafe_ptr() const
|
|
{
|
|
return static_cast<T*>(m_ptr);
|
|
}
|
|
|
|
bool is_null() const { return m_ptr == nullptr; }
|
|
|
|
void revoke() { m_ptr = nullptr; }
|
|
|
|
private:
|
|
template<typename T>
|
|
explicit WeakLink(T& weakable)
|
|
: m_ptr(&weakable)
|
|
{
|
|
}
|
|
|
|
mutable void* m_ptr { nullptr };
|
|
};
|
|
|
|
template<typename T>
|
|
class Weakable {
|
|
private:
|
|
class Link;
|
|
|
|
public:
|
|
template<typename U = T>
|
|
WeakPtr<U> make_weak_ptr() const
|
|
{
|
|
return MUST(try_make_weak_ptr<U>());
|
|
}
|
|
|
|
template<typename U = T>
|
|
ErrorOr<WeakPtr<U>> try_make_weak_ptr() const;
|
|
|
|
protected:
|
|
Weakable() = default;
|
|
|
|
~Weakable()
|
|
{
|
|
revoke_weak_ptrs();
|
|
}
|
|
|
|
void revoke_weak_ptrs()
|
|
{
|
|
if (auto link = move(m_link))
|
|
link->revoke();
|
|
}
|
|
|
|
private:
|
|
mutable RefPtr<WeakLink> m_link;
|
|
};
|
|
|
|
}
|
|
|
|
#if USING_AK_GLOBALLY
|
|
using AK::Weakable;
|
|
#endif
|