TreeNode.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #pragma once
  2. #include <AK/Assertions.h>
  3. #include <AK/NonnullRefPtr.h>
  4. #include <AK/Weakable.h>
  5. // FIXME: I wish I didn't have to forward declare these, but I can't seem to avoid
  6. // it if I still want to have for_each_in_subtree_of_type<U> inline here.
  7. class Node;
  8. class LayoutNode;
  9. template<typename T>
  10. bool is(const Node&);
  11. template<typename T>
  12. bool is(const LayoutNode&);
  13. template<typename T>
  14. class TreeNode : public Weakable<T> {
  15. public:
  16. void ref()
  17. {
  18. ASSERT(m_ref_count);
  19. ++m_ref_count;
  20. }
  21. void deref()
  22. {
  23. ASSERT(m_ref_count);
  24. if (!--m_ref_count) {
  25. if (m_next_sibling)
  26. m_next_sibling->m_previous_sibling = m_previous_sibling;
  27. if (m_previous_sibling)
  28. m_previous_sibling->m_next_sibling = m_next_sibling;
  29. T* next_child;
  30. for (auto* child = m_first_child; child; child = next_child) {
  31. next_child = child->m_next_sibling;
  32. child->m_parent = nullptr;
  33. child->deref();
  34. }
  35. delete static_cast<T*>(this);
  36. }
  37. }
  38. int ref_count() const { return m_ref_count; }
  39. T* parent() { return m_parent; }
  40. const T* parent() const { return m_parent; }
  41. bool has_children() const { return m_first_child; }
  42. T* next_sibling() { return m_next_sibling; }
  43. T* previous_sibling() { return m_previous_sibling; }
  44. T* first_child() { return m_first_child; }
  45. T* last_child() { return m_last_child; }
  46. const T* next_sibling() const { return m_next_sibling; }
  47. const T* previous_sibling() const { return m_previous_sibling; }
  48. const T* first_child() const { return m_first_child; }
  49. const T* last_child() const { return m_last_child; }
  50. int child_count() const
  51. {
  52. int count = 0;
  53. for (auto* child = first_child(); child; child = child->next_sibling())
  54. ++count;
  55. return count;
  56. }
  57. T* child_at_index(int index)
  58. {
  59. int count = 0;
  60. for (auto* child = first_child(); child; child = child->next_sibling()) {
  61. if (count == index)
  62. return child;
  63. ++count;
  64. }
  65. return nullptr;
  66. }
  67. const T* child_at_index(int index) const
  68. {
  69. return const_cast<TreeNode*>(this)->child_at_index(index);
  70. }
  71. bool is_ancestor_of(const TreeNode&) const;
  72. void prepend_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
  73. void append_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
  74. NonnullRefPtr<T> remove_child(NonnullRefPtr<T> node, bool call_removed_from = true);
  75. void donate_all_children_to(T& node);
  76. bool is_child_allowed(const T&) const { return true; }
  77. T* next_in_pre_order()
  78. {
  79. if (first_child())
  80. return first_child();
  81. T* node;
  82. if (!(node = next_sibling())) {
  83. node = parent();
  84. while (node && !node->next_sibling())
  85. node = node->parent();
  86. if (node)
  87. node = node->next_sibling();
  88. }
  89. return node;
  90. }
  91. const T* next_in_pre_order() const
  92. {
  93. return const_cast<TreeNode*>(this)->next_in_pre_order();
  94. }
  95. template<typename Callback>
  96. IterationDecision for_each_in_subtree(Callback callback) const
  97. {
  98. if (callback(static_cast<const T&>(*this)) == IterationDecision::Break)
  99. return IterationDecision::Break;
  100. for (auto* child = first_child(); child; child = child->next_sibling()) {
  101. if (child->for_each_in_subtree(callback) == IterationDecision::Break)
  102. return IterationDecision::Break;
  103. }
  104. return IterationDecision::Continue;
  105. }
  106. template<typename Callback>
  107. IterationDecision for_each_in_subtree(Callback callback)
  108. {
  109. if (callback(static_cast<T&>(*this)) == IterationDecision::Break)
  110. return IterationDecision::Break;
  111. for (auto* child = first_child(); child; child = child->next_sibling()) {
  112. if (child->for_each_in_subtree(callback) == IterationDecision::Break)
  113. return IterationDecision::Break;
  114. }
  115. return IterationDecision::Continue;
  116. }
  117. template<typename U, typename Callback>
  118. IterationDecision for_each_in_subtree_of_type(Callback callback)
  119. {
  120. if (is<U>(static_cast<const T&>(*this))) {
  121. if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
  122. return IterationDecision::Break;
  123. }
  124. for (auto* child = first_child(); child; child = child->next_sibling()) {
  125. if (child->template for_each_in_subtree_of_type<U>(callback) == IterationDecision::Break)
  126. return IterationDecision::Break;
  127. }
  128. return IterationDecision::Continue;
  129. }
  130. template<typename U, typename Callback>
  131. IterationDecision for_each_in_subtree_of_type(Callback callback) const
  132. {
  133. if (is<U>(static_cast<const T&>(*this))) {
  134. if (callback(static_cast<const U&>(*this)) == IterationDecision::Break)
  135. return IterationDecision::Break;
  136. }
  137. for (auto* child = first_child(); child; child = child->next_sibling()) {
  138. if (child->template for_each_in_subtree_of_type<U>(callback) == IterationDecision::Break)
  139. return IterationDecision::Break;
  140. }
  141. return IterationDecision::Continue;
  142. }
  143. protected:
  144. TreeNode() {}
  145. private:
  146. int m_ref_count { 1 };
  147. T* m_parent { nullptr };
  148. T* m_first_child { nullptr };
  149. T* m_last_child { nullptr };
  150. T* m_next_sibling { nullptr };
  151. T* m_previous_sibling { nullptr };
  152. };
  153. template<typename T>
  154. inline NonnullRefPtr<T> TreeNode<T>::remove_child(NonnullRefPtr<T> node, bool call_removed_from)
  155. {
  156. ASSERT(node->m_parent == this);
  157. if (m_first_child == node)
  158. m_first_child = node->m_next_sibling;
  159. if (m_last_child == node)
  160. m_last_child = node->m_previous_sibling;
  161. if (node->m_next_sibling)
  162. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  163. if (node->m_previous_sibling)
  164. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  165. node->m_next_sibling = nullptr;
  166. node->m_previous_sibling = nullptr;
  167. node->m_parent = nullptr;
  168. if (call_removed_from)
  169. node->removed_from(static_cast<T&>(*this));
  170. node->deref();
  171. return node;
  172. }
  173. template<typename T>
  174. inline void TreeNode<T>::append_child(NonnullRefPtr<T> node, bool call_inserted_into)
  175. {
  176. ASSERT(!node->m_parent);
  177. if (!static_cast<T*>(this)->is_child_allowed(*node))
  178. return;
  179. if (m_last_child)
  180. m_last_child->m_next_sibling = node.ptr();
  181. node->m_previous_sibling = m_last_child;
  182. node->m_parent = static_cast<T*>(this);
  183. m_last_child = node.ptr();
  184. if (!m_first_child)
  185. m_first_child = m_last_child;
  186. if (call_inserted_into)
  187. node->inserted_into(static_cast<T&>(*this));
  188. (void)node.leak_ref();
  189. }
  190. template<typename T>
  191. inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node, bool call_inserted_into)
  192. {
  193. ASSERT(!node->m_parent);
  194. if (!static_cast<T*>(this)->is_child_allowed(*node))
  195. return;
  196. if (m_first_child)
  197. m_first_child->m_previous_sibling = node.ptr();
  198. node->m_next_sibling = m_first_child;
  199. node->m_parent = static_cast<T*>(this);
  200. m_first_child = node.ptr();
  201. if (!m_last_child)
  202. m_last_child = m_first_child;
  203. if (call_inserted_into)
  204. node->inserted_into(static_cast<T&>(*this));
  205. (void)node.leak_ref();
  206. }
  207. template<typename T>
  208. inline void TreeNode<T>::donate_all_children_to(T& node)
  209. {
  210. for (T* child = m_first_child; child != nullptr;) {
  211. T* next_child = child->m_next_sibling;
  212. child->m_parent = nullptr;
  213. child->m_next_sibling = nullptr;
  214. child->m_previous_sibling = nullptr;
  215. node.append_child(adopt(*child));
  216. child = next_child;
  217. }
  218. m_first_child = nullptr;
  219. m_last_child = nullptr;
  220. }
  221. template<typename T>
  222. inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
  223. {
  224. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  225. if (ancestor == this)
  226. return true;
  227. }
  228. return false;
  229. }