TreeNode.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #pragma once
  2. #include <AK/Assertions.h>
  3. #include <AK/NonnullRefPtr.h>
  4. #include <AK/Weakable.h>
  5. template<typename T>
  6. class TreeNode : public Weakable<T> {
  7. public:
  8. void ref()
  9. {
  10. ASSERT(m_ref_count);
  11. ++m_ref_count;
  12. }
  13. void deref()
  14. {
  15. ASSERT(m_ref_count);
  16. if (!--m_ref_count) {
  17. if (m_next_sibling)
  18. m_next_sibling->m_previous_sibling = m_previous_sibling;
  19. if (m_previous_sibling)
  20. m_previous_sibling->m_next_sibling = m_next_sibling;
  21. T* next_child;
  22. for (auto* child = m_first_child; child; child = next_child) {
  23. next_child = child->m_next_sibling;
  24. child->m_parent = nullptr;
  25. child->deref();
  26. }
  27. delete static_cast<T*>(this);
  28. }
  29. }
  30. int ref_count() const { return m_ref_count; }
  31. T* parent() { return m_parent; }
  32. const T* parent() const { return m_parent; }
  33. bool has_children() const { return m_first_child; }
  34. T* next_sibling() { return m_next_sibling; }
  35. T* previous_sibling() { return m_previous_sibling; }
  36. T* first_child() { return m_first_child; }
  37. T* last_child() { return m_last_child; }
  38. const T* next_sibling() const { return m_next_sibling; }
  39. const T* previous_sibling() const { return m_previous_sibling; }
  40. const T* first_child() const { return m_first_child; }
  41. const T* last_child() const { return m_last_child; }
  42. bool is_ancestor_of(const TreeNode&) const;
  43. void prepend_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
  44. void append_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
  45. void donate_all_children_to(T& node);
  46. bool is_child_allowed(const T&) const { return true; }
  47. protected:
  48. TreeNode() {}
  49. private:
  50. int m_ref_count { 1 };
  51. T* m_parent { nullptr };
  52. T* m_first_child { nullptr };
  53. T* m_last_child { nullptr };
  54. T* m_next_sibling { nullptr };
  55. T* m_previous_sibling { nullptr };
  56. };
  57. template<typename T>
  58. inline void TreeNode<T>::append_child(NonnullRefPtr<T> node, bool call_inserted_into)
  59. {
  60. ASSERT(!node->m_parent);
  61. if (!static_cast<T*>(this)->is_child_allowed(*node))
  62. return;
  63. if (m_last_child)
  64. m_last_child->m_next_sibling = node.ptr();
  65. node->m_previous_sibling = m_last_child;
  66. node->m_parent = static_cast<T*>(this);
  67. m_last_child = node.ptr();
  68. if (!m_first_child)
  69. m_first_child = m_last_child;
  70. if (call_inserted_into)
  71. node->inserted_into(static_cast<T&>(*this));
  72. (void)node.leak_ref();
  73. }
  74. template<typename T>
  75. inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node, bool call_inserted_into)
  76. {
  77. ASSERT(!node->m_parent);
  78. if (!static_cast<T*>(this)->is_child_allowed(*node))
  79. return;
  80. if (m_first_child)
  81. m_first_child->m_previous_sibling = node.ptr();
  82. node->m_next_sibling = m_first_child;
  83. node->m_parent = static_cast<T*>(this);
  84. m_first_child = node.ptr();
  85. if (!m_last_child)
  86. m_last_child = m_first_child;
  87. if (call_inserted_into)
  88. node->inserted_into(static_cast<T&>(*this));
  89. (void)node.leak_ref();
  90. }
  91. template<typename T>
  92. inline void TreeNode<T>::donate_all_children_to(T& node)
  93. {
  94. for (T* child = m_first_child; child != nullptr;) {
  95. T* next_child = child->m_next_sibling;
  96. child->m_parent = nullptr;
  97. child->m_next_sibling = nullptr;
  98. child->m_previous_sibling = nullptr;
  99. node.append_child(adopt(*child));
  100. child = next_child;
  101. }
  102. m_first_child = nullptr;
  103. m_last_child = nullptr;
  104. }
  105. template<typename T>
  106. inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
  107. {
  108. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  109. if (ancestor == this)
  110. return true;
  111. }
  112. return false;
  113. }