TreeNode.h 3.8 KB

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