TreeNode.h 9.5 KB

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