summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/TreeNode.h
diff options
context:
space:
mode:
Diffstat (limited to 'Userland/Libraries/LibWeb/TreeNode.h')
-rw-r--r--Userland/Libraries/LibWeb/TreeNode.h112
1 files changed, 38 insertions, 74 deletions
diff --git a/Userland/Libraries/LibWeb/TreeNode.h b/Userland/Libraries/LibWeb/TreeNode.h
index 1972c4fd05..74d7a94d40 100644
--- a/Userland/Libraries/LibWeb/TreeNode.h
+++ b/Userland/Libraries/LibWeb/TreeNode.h
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@@ -7,54 +7,27 @@
#pragma once
#include <AK/Assertions.h>
-#include <AK/NonnullRefPtr.h>
#include <AK/TypeCasts.h>
-#include <AK/Weakable.h>
+#include <LibJS/Heap/GCPtr.h>
#include <LibWeb/Forward.h>
namespace Web {
template<typename T>
-class TreeNode : public Weakable<T> {
+class TreeNode {
public:
- void ref()
- {
- VERIFY(!m_in_removed_last_ref);
- if constexpr (!IsBaseOf<DOM::Node, T>) {
- // NOTE: DOM::Document is allowed to survive with 0 ref count, if one of its descendant nodes are alive.
- VERIFY(m_ref_count);
- }
- ++m_ref_count;
- }
-
- void unref()
- {
- VERIFY(!m_in_removed_last_ref);
- VERIFY(m_ref_count);
- if (!--m_ref_count) {
- if constexpr (IsBaseOf<DOM::Node, T>) {
- m_in_removed_last_ref = true;
- static_cast<T*>(this)->removed_last_ref();
- } else {
- delete static_cast<T*>(this);
- }
- return;
- }
- }
- int ref_count() const { return m_ref_count; }
-
T* parent() { return m_parent; }
- const T* parent() const { return m_parent; }
+ T const* parent() const { return m_parent; }
bool has_children() const { return m_first_child; }
T* next_sibling() { return m_next_sibling; }
T* previous_sibling() { return m_previous_sibling; }
T* first_child() { return m_first_child; }
T* last_child() { return m_last_child; }
- const T* next_sibling() const { return m_next_sibling; }
- const T* previous_sibling() const { return m_previous_sibling; }
- const T* first_child() const { return m_first_child; }
- const T* last_child() const { return m_last_child; }
+ T const* next_sibling() const { return m_next_sibling; }
+ T const* previous_sibling() const { return m_previous_sibling; }
+ T const* first_child() const { return m_first_child; }
+ T const* last_child() const { return m_last_child; }
size_t child_count() const
{
@@ -75,7 +48,7 @@ public:
return nullptr;
}
- const T* child_at_index(int index) const
+ T const* child_at_index(int index) const
{
return const_cast<TreeNode*>(this)->child_at_index(index);
}
@@ -90,7 +63,7 @@ public:
return index;
}
- Optional<size_t> index_of_child(const T& search_child)
+ Optional<size_t> index_of_child(T const& search_child)
{
VERIFY(search_child.parent() == this);
size_t index = 0;
@@ -106,7 +79,7 @@ public:
}
template<typename ChildType>
- Optional<size_t> index_of_child(const T& search_child)
+ Optional<size_t> index_of_child(T const& search_child)
{
VERIFY(search_child.parent() == this);
size_t index = 0;
@@ -130,12 +103,12 @@ public:
bool is_following(TreeNode const&) const;
- void append_child(NonnullRefPtr<T> node);
- void prepend_child(NonnullRefPtr<T> node);
- void insert_before(NonnullRefPtr<T> node, RefPtr<T> child);
- void remove_child(NonnullRefPtr<T> node);
+ void append_child(JS::NonnullGCPtr<T> node);
+ void prepend_child(JS::NonnullGCPtr<T> node);
+ void insert_before(JS::NonnullGCPtr<T> node, JS::GCPtr<T> child);
+ void remove_child(JS::NonnullGCPtr<T> node);
- bool is_child_allowed(const T&) const { return true; }
+ bool is_child_allowed(T const&) const { return true; }
T* next_in_pre_order()
{
@@ -230,7 +203,7 @@ public:
template<typename Callback>
IterationDecision for_each_in_inclusive_subtree(Callback callback) const
{
- if (callback(static_cast<const T&>(*this)) == IterationDecision::Break)
+ if (callback(static_cast<T const&>(*this)) == IterationDecision::Break)
return IterationDecision::Break;
for (auto* child = first_child(); child; child = child->next_sibling()) {
if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
@@ -254,7 +227,7 @@ public:
template<typename U, typename Callback>
IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback)
{
- if (is<U>(static_cast<const T&>(*this))) {
+ if (is<U>(static_cast<T const&>(*this))) {
if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
return IterationDecision::Break;
}
@@ -268,8 +241,8 @@ public:
template<typename U, typename Callback>
IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
{
- if (is<U>(static_cast<const T&>(*this))) {
- if (callback(static_cast<const U&>(*this)) == IterationDecision::Break)
+ if (is<U>(static_cast<T const&>(*this))) {
+ if (callback(static_cast<U const&>(*this)) == IterationDecision::Break)
return IterationDecision::Break;
}
for (auto* child = first_child(); child; child = child->next_sibling()) {
@@ -348,7 +321,7 @@ public:
}
template<typename U>
- const U* next_sibling_of_type() const
+ U const* next_sibling_of_type() const
{
return const_cast<TreeNode*>(this)->template next_sibling_of_type<U>();
}
@@ -364,7 +337,7 @@ public:
}
template<typename U>
- const U* previous_sibling_of_type() const
+ U const* previous_sibling_of_type() const
{
return const_cast<TreeNode*>(this)->template previous_sibling_of_type<U>();
}
@@ -380,13 +353,13 @@ public:
}
template<typename U>
- const U* first_child_of_type() const
+ U const* first_child_of_type() const
{
return const_cast<TreeNode*>(this)->template first_child_of_type<U>();
}
template<typename U>
- const U* last_child_of_type() const
+ U const* last_child_of_type() const
{
return const_cast<TreeNode*>(this)->template last_child_of_type<U>();
}
@@ -418,7 +391,7 @@ public:
}
template<typename U>
- const U* first_ancestor_of_type() const
+ U const* first_ancestor_of_type() const
{
return const_cast<TreeNode*>(this)->template first_ancestor_of_type<U>();
}
@@ -442,25 +415,21 @@ public:
return false;
}
- ~TreeNode()
- {
- VERIFY(!m_parent);
- T* next_child = nullptr;
- for (auto* child = m_first_child; child; child = next_child) {
- next_child = child->m_next_sibling;
- child->m_parent = nullptr;
- child->unref();
- }
- }
+ ~TreeNode() = default;
protected:
TreeNode() = default;
- bool m_deletion_has_begun { false };
- bool m_in_removed_last_ref { false };
+ void visit_edges(JS::Cell::Visitor& visitor)
+ {
+ visitor.visit(m_parent);
+ visitor.visit(m_first_child);
+ visitor.visit(m_last_child);
+ visitor.visit(m_next_sibling);
+ visitor.visit(m_previous_sibling);
+ }
private:
- int m_ref_count { 1 };
T* m_parent { nullptr };
T* m_first_child { nullptr };
T* m_last_child { nullptr };
@@ -469,7 +438,7 @@ private:
};
template<typename T>
-inline void TreeNode<T>::remove_child(NonnullRefPtr<T> node)
+inline void TreeNode<T>::remove_child(JS::NonnullGCPtr<T> node)
{
VERIFY(node->m_parent == this);
@@ -488,12 +457,10 @@ inline void TreeNode<T>::remove_child(NonnullRefPtr<T> node)
node->m_next_sibling = nullptr;
node->m_previous_sibling = nullptr;
node->m_parent = nullptr;
-
- node->unref();
}
template<typename T>
-inline void TreeNode<T>::append_child(NonnullRefPtr<T> node)
+inline void TreeNode<T>::append_child(JS::NonnullGCPtr<T> node)
{
VERIFY(!node->m_parent);
@@ -507,11 +474,10 @@ inline void TreeNode<T>::append_child(NonnullRefPtr<T> node)
m_last_child = node.ptr();
if (!m_first_child)
m_first_child = m_last_child;
- [[maybe_unused]] auto& rc = node.leak_ref();
}
template<typename T>
-inline void TreeNode<T>::insert_before(NonnullRefPtr<T> node, RefPtr<T> child)
+inline void TreeNode<T>::insert_before(JS::NonnullGCPtr<T> node, JS::GCPtr<T> child)
{
if (!child)
return append_child(move(node));
@@ -531,11 +497,10 @@ inline void TreeNode<T>::insert_before(NonnullRefPtr<T> node, RefPtr<T> child)
child->m_previous_sibling = node;
node->m_parent = static_cast<T*>(this);
- [[maybe_unused]] auto& rc = node.leak_ref();
}
template<typename T>
-inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node)
+inline void TreeNode<T>::prepend_child(JS::NonnullGCPtr<T> node)
{
VERIFY(!node->m_parent);
@@ -550,7 +515,6 @@ inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node)
if (!m_last_child)
m_last_child = m_first_child;
node->inserted_into(static_cast<T&>(*this));
- [[maybe_unused]] auto& rc = node.leak_ref();
static_cast<T*>(this)->children_changed();
}