Coverage Report

Created: 2025-12-18 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/AK/Weakable.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <AK/Assertions.h>
10
#include <AK/RefCounted.h>
11
#include <AK/RefPtr.h>
12
#include <AK/StdLibExtras.h>
13
14
namespace AK {
15
16
template<typename T>
17
class Weakable;
18
template<typename T>
19
class WeakPtr;
20
21
class WeakLink : public RefCounted<WeakLink> {
22
    template<typename T>
23
    friend class Weakable;
24
    template<typename T>
25
    friend class WeakPtr;
26
27
public:
28
    template<typename T>
29
    RefPtr<T> strong_ref() const
30
    requires(IsBaseOf<RefCountedBase, T>)
31
    {
32
        return static_cast<T*>(m_ptr);
33
    }
34
35
    template<typename T>
36
    T* unsafe_ptr() const
37
6
    {
38
6
        return static_cast<T*>(m_ptr);
39
6
    }
Unexecuted instantiation: Core::EventReceiver* AK::WeakLink::unsafe_ptr<Core::EventReceiver>() const
Unexecuted instantiation: JS::Shape* AK::WeakLink::unsafe_ptr<JS::Shape>() const
Unexecuted instantiation: JS::PrototypeChainValidity* AK::WeakLink::unsafe_ptr<JS::PrototypeChainValidity>() const
Unexecuted instantiation: JS::Object* AK::WeakLink::unsafe_ptr<JS::Object>() const
PDF::Document* AK::WeakLink::unsafe_ptr<PDF::Document>() const
Line
Count
Source
37
6
    {
38
6
        return static_cast<T*>(m_ptr);
39
6
    }
Unexecuted instantiation: Web::HTML::BrowsingContext* AK::WeakLink::unsafe_ptr<Web::HTML::BrowsingContext>() const
Unexecuted instantiation: Web::DOM::Node* AK::WeakLink::unsafe_ptr<Web::DOM::Node>() const
Unexecuted instantiation: Web::HTML::HTMLFormElement* AK::WeakLink::unsafe_ptr<Web::HTML::HTMLFormElement>() const
Unexecuted instantiation: Web::CSS::FontLoader const* AK::WeakLink::unsafe_ptr<Web::CSS::FontLoader const>() const
Unexecuted instantiation: Web::DOM::Element* AK::WeakLink::unsafe_ptr<Web::DOM::Element>() const
Unexecuted instantiation: Web::DOM::Document* AK::WeakLink::unsafe_ptr<Web::DOM::Document>() const
Unexecuted instantiation: Web::CSS::MediaQueryList* AK::WeakLink::unsafe_ptr<Web::CSS::MediaQueryList>() const
Unexecuted instantiation: Web::HTML::Navigable* AK::WeakLink::unsafe_ptr<Web::HTML::Navigable>() const
Unexecuted instantiation: JS::Cell* AK::WeakLink::unsafe_ptr<JS::Cell>() const
Unexecuted instantiation: Web::ResourceClient* AK::WeakLink::unsafe_ptr<Web::ResourceClient>() const
Unexecuted instantiation: Web::DOM::EventTarget* AK::WeakLink::unsafe_ptr<Web::DOM::EventTarget>() const
Unexecuted instantiation: Web::HTML::HTMLElement* AK::WeakLink::unsafe_ptr<Web::HTML::HTMLElement>() const
Unexecuted instantiation: Wasm::Module const* AK::WeakLink::unsafe_ptr<Wasm::Module const>() const
Unexecuted instantiation: Web::WebSockets::WebSocket* AK::WeakLink::unsafe_ptr<Web::WebSockets::WebSocket>() const
40
41
0
    bool is_null() const { return m_ptr == nullptr; }
42
43
11
    void revoke() { m_ptr = nullptr; }
44
45
private:
46
    template<typename T>
47
    explicit WeakLink(T& weakable)
48
11
        : m_ptr(&weakable)
49
11
    {
50
11
    }
Unexecuted instantiation: AK::WeakLink::WeakLink<Core::EventReceiver>(Core::EventReceiver&)
Unexecuted instantiation: AK::WeakLink::WeakLink<JS::Cell>(JS::Cell&)
AK::WeakLink::WeakLink<PDF::Document>(PDF::Document&)
Line
Count
Source
48
11
        : m_ptr(&weakable)
49
11
    {
50
11
    }
Unexecuted instantiation: AK::WeakLink::WeakLink<PDF::OutlineItem>(PDF::OutlineItem&)
Unexecuted instantiation: AK::WeakLink::WeakLink<Web::ResourceClient>(Web::ResourceClient&)
Unexecuted instantiation: AK::WeakLink::WeakLink<Web::CSS::ImageStyleValue>(Web::CSS::ImageStyleValue&)
Unexecuted instantiation: AK::WeakLink::WeakLink<Wasm::Module>(Wasm::Module&)
51
52
    mutable void* m_ptr { nullptr };
53
};
54
55
template<typename T>
56
class Weakable {
57
private:
58
    class Link;
59
60
public:
61
    template<typename U = T>
62
    WeakPtr<U> make_weak_ptr() const
63
11
    {
64
11
        return MUST(try_make_weak_ptr<U>());
65
11
    }
Unexecuted instantiation: AK::WeakPtr<Core::EventReceiver> AK::Weakable<Core::EventReceiver>::make_weak_ptr<Core::EventReceiver>() const
Unexecuted instantiation: AK::WeakPtr<JS::Shape> AK::Weakable<JS::Cell>::make_weak_ptr<JS::Shape>() const
Unexecuted instantiation: AK::WeakPtr<JS::Object> AK::Weakable<JS::Cell>::make_weak_ptr<JS::Object>() const
Unexecuted instantiation: AK::WeakPtr<JS::PrototypeChainValidity> AK::Weakable<JS::Cell>::make_weak_ptr<JS::PrototypeChainValidity>() const
AK::WeakPtr<PDF::Document> AK::Weakable<PDF::Document>::make_weak_ptr<PDF::Document>() const
Line
Count
Source
63
11
    {
64
11
        return MUST(try_make_weak_ptr<U>());
65
11
    }
Unexecuted instantiation: AK::WeakPtr<PDF::OutlineItem> AK::Weakable<PDF::OutlineItem>::make_weak_ptr<PDF::OutlineItem>() const
Unexecuted instantiation: AK::WeakPtr<Web::DOM::Element> AK::Weakable<JS::Cell>::make_weak_ptr<Web::DOM::Element>() const
Unexecuted instantiation: AK::WeakPtr<Web::CSS::FontLoader> AK::Weakable<Web::ResourceClient>::make_weak_ptr<Web::CSS::FontLoader>() const
Unexecuted instantiation: AK::WeakPtr<Web::DOM::Document> AK::Weakable<JS::Cell>::make_weak_ptr<Web::DOM::Document>() const
Unexecuted instantiation: AK::WeakPtr<Web::CSS::ImageStyleValue> AK::Weakable<Web::CSS::ImageStyleValue>::make_weak_ptr<Web::CSS::ImageStyleValue>() const
Unexecuted instantiation: AK::WeakPtr<Web::CSS::MediaQueryList> AK::Weakable<JS::Cell>::make_weak_ptr<Web::CSS::MediaQueryList>() const
Unexecuted instantiation: AK::WeakPtr<Web::HTML::BrowsingContext> AK::Weakable<JS::Cell>::make_weak_ptr<Web::HTML::BrowsingContext>() const
Unexecuted instantiation: AK::WeakPtr<Web::HTML::Navigable> AK::Weakable<JS::Cell>::make_weak_ptr<Web::HTML::Navigable>() const
Unexecuted instantiation: AK::WeakPtr<Web::DOM::Node> AK::Weakable<JS::Cell>::make_weak_ptr<Web::DOM::Node>() const
Unexecuted instantiation: AK::WeakPtr<Web::HTML::HTMLFormElement> AK::Weakable<JS::Cell>::make_weak_ptr<Web::HTML::HTMLFormElement>() const
Unexecuted instantiation: AK::WeakPtr<Web::HTML::HTMLInputElement> AK::Weakable<JS::Cell>::make_weak_ptr<Web::HTML::HTMLInputElement>() const
Unexecuted instantiation: AK::WeakPtr<Web::HTML::HTMLSelectElement> AK::Weakable<JS::Cell>::make_weak_ptr<Web::HTML::HTMLSelectElement>() const
Unexecuted instantiation: AK::WeakPtr<JS::Cell> AK::Weakable<JS::Cell>::make_weak_ptr<JS::Cell>() const
Unexecuted instantiation: AK::WeakPtr<Web::ResourceClient> AK::Weakable<Web::ResourceClient>::make_weak_ptr<Web::ResourceClient>() const
Unexecuted instantiation: AK::WeakPtr<Wasm::Module> AK::Weakable<Wasm::Module>::make_weak_ptr<Wasm::Module>() const
Unexecuted instantiation: AK::WeakPtr<Web::WebSockets::WebSocket> AK::Weakable<JS::Cell>::make_weak_ptr<Web::WebSockets::WebSocket>() const
66
67
    template<typename U = T>
68
    ErrorOr<WeakPtr<U>> try_make_weak_ptr() const;
69
70
protected:
71
8.72k
    Weakable() = default;
Unexecuted instantiation: AK::Weakable<Core::EventReceiver>::Weakable()
AK::Weakable<Wasm::Module>::Weakable()
Line
Count
Source
71
8.68k
    Weakable() = default;
Unexecuted instantiation: AK::Weakable<JS::Cell>::Weakable()
AK::Weakable<PDF::Document>::Weakable()
Line
Count
Source
71
11
    Weakable() = default;
Unexecuted instantiation: AK::Weakable<PDF::OutlineItem>::Weakable()
Unexecuted instantiation: AK::Weakable<Web::ResourceClient>::Weakable()
Unexecuted instantiation: AK::Weakable<Web::CSS::ImageStyleValue>::Weakable()
AK::Weakable<Threading::Thread>::Weakable()
Line
Count
Source
71
33
    Weakable() = default;
72
73
    ~Weakable()
74
8.72k
    {
75
8.72k
        revoke_weak_ptrs();
76
8.72k
    }
Unexecuted instantiation: AK::Weakable<Core::EventReceiver>::~Weakable()
AK::Weakable<Wasm::Module>::~Weakable()
Line
Count
Source
74
8.68k
    {
75
8.68k
        revoke_weak_ptrs();
76
8.68k
    }
Unexecuted instantiation: AK::Weakable<JS::Cell>::~Weakable()
Unexecuted instantiation: AK::Weakable<PDF::OutlineItem>::~Weakable()
AK::Weakable<PDF::Document>::~Weakable()
Line
Count
Source
74
11
    {
75
11
        revoke_weak_ptrs();
76
11
    }
Unexecuted instantiation: AK::Weakable<Web::CSS::ImageStyleValue>::~Weakable()
Unexecuted instantiation: AK::Weakable<Web::ResourceClient>::~Weakable()
AK::Weakable<Threading::Thread>::~Weakable()
Line
Count
Source
74
33
    {
75
33
        revoke_weak_ptrs();
76
33
    }
77
78
    void revoke_weak_ptrs()
79
8.72k
    {
80
8.72k
        if (auto link = move(m_link))
81
11
            link->revoke();
82
8.72k
    }
Unexecuted instantiation: AK::Weakable<Core::EventReceiver>::revoke_weak_ptrs()
AK::Weakable<Wasm::Module>::revoke_weak_ptrs()
Line
Count
Source
79
8.68k
    {
80
8.68k
        if (auto link = move(m_link))
81
0
            link->revoke();
82
8.68k
    }
Unexecuted instantiation: AK::Weakable<JS::Cell>::revoke_weak_ptrs()
Unexecuted instantiation: AK::Weakable<PDF::OutlineItem>::revoke_weak_ptrs()
AK::Weakable<PDF::Document>::revoke_weak_ptrs()
Line
Count
Source
79
11
    {
80
11
        if (auto link = move(m_link))
81
11
            link->revoke();
82
11
    }
Unexecuted instantiation: AK::Weakable<Web::ResourceClient>::revoke_weak_ptrs()
Unexecuted instantiation: AK::Weakable<Web::CSS::ImageStyleValue>::revoke_weak_ptrs()
AK::Weakable<Threading::Thread>::revoke_weak_ptrs()
Line
Count
Source
79
33
    {
80
33
        if (auto link = move(m_link))
81
0
            link->revoke();
82
33
    }
83
84
private:
85
    mutable RefPtr<WeakLink> m_link;
86
};
87
88
}
89
90
#if USING_AK_GLOBALLY
91
using AK::Weakable;
92
#endif