TreeNode.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <AK/Assertions.h>
  28. #include <AK/NonnullRefPtr.h>
  29. #include <AK/Weakable.h>
  30. // FIXME: I wish I didn't have to forward declare these, but I can't seem to avoid
  31. // it if I still want to have for_each_in_subtree_of_type<U> inline here.
  32. class Node;
  33. class LayoutNode;
  34. template<typename T>
  35. bool is(const Node&);
  36. template<typename T>
  37. bool is(const LayoutNode&);
  38. template<typename T>
  39. class TreeNode : public Weakable<T> {
  40. public:
  41. void ref()
  42. {
  43. ASSERT(m_ref_count);
  44. ++m_ref_count;
  45. }
  46. void unref()
  47. {
  48. ASSERT(m_ref_count);
  49. if (!--m_ref_count) {
  50. if (m_next_sibling)
  51. m_next_sibling->m_previous_sibling = m_previous_sibling;
  52. if (m_previous_sibling)
  53. m_previous_sibling->m_next_sibling = m_next_sibling;
  54. T* next_child;
  55. for (auto* child = m_first_child; child; child = next_child) {
  56. next_child = child->m_next_sibling;
  57. child->m_parent = nullptr;
  58. child->unref();
  59. }
  60. delete static_cast<T*>(this);
  61. }
  62. }
  63. int ref_count() const { return m_ref_count; }
  64. T* parent() { return m_parent; }
  65. const T* parent() const { return m_parent; }
  66. bool has_children() const { return m_first_child; }
  67. T* next_sibling() { return m_next_sibling; }
  68. T* previous_sibling() { return m_previous_sibling; }
  69. T* first_child() { return m_first_child; }
  70. T* last_child() { return m_last_child; }
  71. const T* next_sibling() const { return m_next_sibling; }
  72. const T* previous_sibling() const { return m_previous_sibling; }
  73. const T* first_child() const { return m_first_child; }
  74. const T* last_child() const { return m_last_child; }
  75. int child_count() const
  76. {
  77. int count = 0;
  78. for (auto* child = first_child(); child; child = child->next_sibling())
  79. ++count;
  80. return count;
  81. }
  82. T* child_at_index(int index)
  83. {
  84. int count = 0;
  85. for (auto* child = first_child(); child; child = child->next_sibling()) {
  86. if (count == index)
  87. return child;
  88. ++count;
  89. }
  90. return nullptr;
  91. }
  92. const T* child_at_index(int index) const
  93. {
  94. return const_cast<TreeNode*>(this)->child_at_index(index);
  95. }
  96. bool is_ancestor_of(const TreeNode&) const;
  97. void prepend_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
  98. void append_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
  99. NonnullRefPtr<T> remove_child(NonnullRefPtr<T> node, bool call_removed_from = true);
  100. void donate_all_children_to(T& node);
  101. bool is_child_allowed(const T&) const { return true; }
  102. T* next_in_pre_order()
  103. {
  104. if (first_child())
  105. return first_child();
  106. T* node;
  107. if (!(node = next_sibling())) {
  108. node = parent();
  109. while (node && !node->next_sibling())
  110. node = node->parent();
  111. if (node)
  112. node = node->next_sibling();
  113. }
  114. return node;
  115. }
  116. const T* next_in_pre_order() const
  117. {
  118. return const_cast<TreeNode*>(this)->next_in_pre_order();
  119. }
  120. template<typename Callback>
  121. IterationDecision for_each_in_subtree(Callback callback) const
  122. {
  123. if (callback(static_cast<const T&>(*this)) == IterationDecision::Break)
  124. return IterationDecision::Break;
  125. for (auto* child = first_child(); child; child = child->next_sibling()) {
  126. if (child->for_each_in_subtree(callback) == IterationDecision::Break)
  127. return IterationDecision::Break;
  128. }
  129. return IterationDecision::Continue;
  130. }
  131. template<typename Callback>
  132. IterationDecision for_each_in_subtree(Callback callback)
  133. {
  134. if (callback(static_cast<T&>(*this)) == IterationDecision::Break)
  135. return IterationDecision::Break;
  136. for (auto* child = first_child(); child; child = child->next_sibling()) {
  137. if (child->for_each_in_subtree(callback) == IterationDecision::Break)
  138. return IterationDecision::Break;
  139. }
  140. return IterationDecision::Continue;
  141. }
  142. template<typename U, typename Callback>
  143. IterationDecision for_each_in_subtree_of_type(Callback callback)
  144. {
  145. if (is<U>(static_cast<const T&>(*this))) {
  146. if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
  147. return IterationDecision::Break;
  148. }
  149. for (auto* child = first_child(); child; child = child->next_sibling()) {
  150. if (child->template for_each_in_subtree_of_type<U>(callback) == IterationDecision::Break)
  151. return IterationDecision::Break;
  152. }
  153. return IterationDecision::Continue;
  154. }
  155. template<typename U, typename Callback>
  156. IterationDecision for_each_in_subtree_of_type(Callback callback) const
  157. {
  158. if (is<U>(static_cast<const T&>(*this))) {
  159. if (callback(static_cast<const U&>(*this)) == IterationDecision::Break)
  160. return IterationDecision::Break;
  161. }
  162. for (auto* child = first_child(); child; child = child->next_sibling()) {
  163. if (child->template for_each_in_subtree_of_type<U>(callback) == IterationDecision::Break)
  164. return IterationDecision::Break;
  165. }
  166. return IterationDecision::Continue;
  167. }
  168. protected:
  169. TreeNode() {}
  170. private:
  171. int m_ref_count { 1 };
  172. T* m_parent { nullptr };
  173. T* m_first_child { nullptr };
  174. T* m_last_child { nullptr };
  175. T* m_next_sibling { nullptr };
  176. T* m_previous_sibling { nullptr };
  177. };
  178. template<typename T>
  179. inline NonnullRefPtr<T> TreeNode<T>::remove_child(NonnullRefPtr<T> node, bool call_removed_from)
  180. {
  181. ASSERT(node->m_parent == this);
  182. if (m_first_child == node)
  183. m_first_child = node->m_next_sibling;
  184. if (m_last_child == node)
  185. m_last_child = node->m_previous_sibling;
  186. if (node->m_next_sibling)
  187. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  188. if (node->m_previous_sibling)
  189. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  190. node->m_next_sibling = nullptr;
  191. node->m_previous_sibling = nullptr;
  192. node->m_parent = nullptr;
  193. if (call_removed_from)
  194. node->removed_from(static_cast<T&>(*this));
  195. node->unref();
  196. return node;
  197. }
  198. template<typename T>
  199. inline void TreeNode<T>::append_child(NonnullRefPtr<T> node, bool call_inserted_into)
  200. {
  201. ASSERT(!node->m_parent);
  202. if (!static_cast<T*>(this)->is_child_allowed(*node))
  203. return;
  204. if (m_last_child)
  205. m_last_child->m_next_sibling = node.ptr();
  206. node->m_previous_sibling = m_last_child;
  207. node->m_parent = static_cast<T*>(this);
  208. m_last_child = node.ptr();
  209. if (!m_first_child)
  210. m_first_child = m_last_child;
  211. if (call_inserted_into)
  212. node->inserted_into(static_cast<T&>(*this));
  213. (void)node.leak_ref();
  214. }
  215. template<typename T>
  216. inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node, bool call_inserted_into)
  217. {
  218. ASSERT(!node->m_parent);
  219. if (!static_cast<T*>(this)->is_child_allowed(*node))
  220. return;
  221. if (m_first_child)
  222. m_first_child->m_previous_sibling = node.ptr();
  223. node->m_next_sibling = m_first_child;
  224. node->m_parent = static_cast<T*>(this);
  225. m_first_child = node.ptr();
  226. if (!m_last_child)
  227. m_last_child = m_first_child;
  228. if (call_inserted_into)
  229. node->inserted_into(static_cast<T&>(*this));
  230. (void)node.leak_ref();
  231. }
  232. template<typename T>
  233. inline void TreeNode<T>::donate_all_children_to(T& node)
  234. {
  235. for (T* child = m_first_child; child != nullptr;) {
  236. T* next_child = child->m_next_sibling;
  237. child->m_parent = nullptr;
  238. child->m_next_sibling = nullptr;
  239. child->m_previous_sibling = nullptr;
  240. node.append_child(adopt(*child));
  241. child = next_child;
  242. }
  243. m_first_child = nullptr;
  244. m_last_child = nullptr;
  245. }
  246. template<typename T>
  247. inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
  248. {
  249. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  250. if (ancestor == this)
  251. return true;
  252. }
  253. return false;
  254. }