TreeNode.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/TypeCasts.h>
  30. #include <AK/Weakable.h>
  31. #include <LibWeb/Forward.h>
  32. namespace Web {
  33. template<typename T>
  34. class TreeNode : public Weakable<T> {
  35. public:
  36. void ref()
  37. {
  38. ASSERT(m_ref_count);
  39. ++m_ref_count;
  40. }
  41. void unref()
  42. {
  43. ASSERT(m_ref_count);
  44. if (!--m_ref_count) {
  45. if constexpr (IsBaseOf<DOM::Node, T>::value)
  46. static_cast<T*>(this)->removed_last_ref();
  47. else
  48. delete static_cast<T*>(this);
  49. return;
  50. }
  51. }
  52. int ref_count() const { return m_ref_count; }
  53. T* parent() { return m_parent; }
  54. const T* parent() const { return m_parent; }
  55. bool has_children() const { return m_first_child; }
  56. T* next_sibling() { return m_next_sibling; }
  57. T* previous_sibling() { return m_previous_sibling; }
  58. T* first_child() { return m_first_child; }
  59. T* last_child() { return m_last_child; }
  60. const T* next_sibling() const { return m_next_sibling; }
  61. const T* previous_sibling() const { return m_previous_sibling; }
  62. const T* first_child() const { return m_first_child; }
  63. const T* last_child() const { return m_last_child; }
  64. int child_count() const
  65. {
  66. int count = 0;
  67. for (auto* child = first_child(); child; child = child->next_sibling())
  68. ++count;
  69. return count;
  70. }
  71. T* child_at_index(int index)
  72. {
  73. int count = 0;
  74. for (auto* child = first_child(); child; child = child->next_sibling()) {
  75. if (count == index)
  76. return child;
  77. ++count;
  78. }
  79. return nullptr;
  80. }
  81. const T* child_at_index(int index) const
  82. {
  83. return const_cast<TreeNode*>(this)->child_at_index(index);
  84. }
  85. bool is_ancestor_of(const TreeNode&) const;
  86. void prepend_child(NonnullRefPtr<T> node);
  87. void append_child(NonnullRefPtr<T> node, bool notify = true);
  88. void insert_before(NonnullRefPtr<T> node, RefPtr<T> child, bool notify = true);
  89. NonnullRefPtr<T> remove_child(NonnullRefPtr<T> node);
  90. bool is_child_allowed(const T&) const { return true; }
  91. T* next_in_pre_order()
  92. {
  93. if (first_child())
  94. return first_child();
  95. T* node;
  96. if (!(node = next_sibling())) {
  97. node = parent();
  98. while (node && !node->next_sibling())
  99. node = node->parent();
  100. if (node)
  101. node = node->next_sibling();
  102. }
  103. return node;
  104. }
  105. const T* next_in_pre_order() const
  106. {
  107. return const_cast<TreeNode*>(this)->next_in_pre_order();
  108. }
  109. bool is_before(const T& other) const
  110. {
  111. if (this == &other)
  112. return false;
  113. for (auto* node = this; node; node = node->next_in_pre_order()) {
  114. if (node == &other)
  115. return true;
  116. }
  117. return false;
  118. }
  119. template<typename Callback>
  120. IterationDecision for_each_in_subtree(Callback callback) const
  121. {
  122. if (callback(static_cast<const T&>(*this)) == IterationDecision::Break)
  123. return IterationDecision::Break;
  124. for (auto* child = first_child(); child; child = child->next_sibling()) {
  125. if (child->for_each_in_subtree(callback) == IterationDecision::Break)
  126. return IterationDecision::Break;
  127. }
  128. return IterationDecision::Continue;
  129. }
  130. template<typename Callback>
  131. IterationDecision for_each_in_subtree(Callback callback)
  132. {
  133. if (callback(static_cast<T&>(*this)) == IterationDecision::Break)
  134. return IterationDecision::Break;
  135. for (auto* child = first_child(); child; child = child->next_sibling()) {
  136. if (child->for_each_in_subtree(callback) == IterationDecision::Break)
  137. return IterationDecision::Break;
  138. }
  139. return IterationDecision::Continue;
  140. }
  141. template<typename U, typename Callback>
  142. IterationDecision for_each_in_subtree_of_type(Callback callback)
  143. {
  144. if (is<U>(static_cast<const T&>(*this))) {
  145. if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
  146. return IterationDecision::Break;
  147. }
  148. for (auto* child = first_child(); child; child = child->next_sibling()) {
  149. if (child->template for_each_in_subtree_of_type<U>(callback) == IterationDecision::Break)
  150. return IterationDecision::Break;
  151. }
  152. return IterationDecision::Continue;
  153. }
  154. template<typename U, typename Callback>
  155. IterationDecision for_each_in_subtree_of_type(Callback callback) const
  156. {
  157. if (is<U>(static_cast<const T&>(*this))) {
  158. if (callback(static_cast<const U&>(*this)) == IterationDecision::Break)
  159. return IterationDecision::Break;
  160. }
  161. for (auto* child = first_child(); child; child = child->next_sibling()) {
  162. if (child->template for_each_in_subtree_of_type<U>(callback) == IterationDecision::Break)
  163. return IterationDecision::Break;
  164. }
  165. return IterationDecision::Continue;
  166. }
  167. template<typename Callback>
  168. void for_each_child(Callback callback) const
  169. {
  170. return const_cast<TreeNode*>(this)->template for_each_child(move(callback));
  171. }
  172. template<typename Callback>
  173. void for_each_child(Callback callback)
  174. {
  175. for (auto* node = first_child(); node; node = node->next_sibling())
  176. callback(*node);
  177. }
  178. template<typename U, typename Callback>
  179. void for_each_child_of_type(Callback callback)
  180. {
  181. for (auto* node = first_child(); node; node = node->next_sibling()) {
  182. if (is<U>(node))
  183. callback(downcast<U>(*node));
  184. }
  185. }
  186. template<typename U, typename Callback>
  187. void for_each_child_of_type(Callback callback) const
  188. {
  189. return const_cast<TreeNode*>(this)->template for_each_child_of_type<U>(move(callback));
  190. }
  191. template<typename U>
  192. const U* next_sibling_of_type() const
  193. {
  194. return const_cast<TreeNode*>(this)->template next_sibling_of_type<U>();
  195. }
  196. template<typename U>
  197. inline U* next_sibling_of_type()
  198. {
  199. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  200. if (is<U>(*sibling))
  201. return &downcast<U>(*sibling);
  202. }
  203. return nullptr;
  204. }
  205. template<typename U>
  206. const U* previous_sibling_of_type() const
  207. {
  208. return const_cast<TreeNode*>(this)->template previous_sibling_of_type<U>();
  209. }
  210. template<typename U>
  211. U* previous_sibling_of_type()
  212. {
  213. for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
  214. if (is<U>(*sibling))
  215. return &downcast<U>(*sibling);
  216. }
  217. return nullptr;
  218. }
  219. template<typename U>
  220. const U* first_child_of_type() const
  221. {
  222. return const_cast<TreeNode*>(this)->template first_child_of_type<U>();
  223. }
  224. template<typename U>
  225. U* first_child_of_type()
  226. {
  227. for (auto* child = first_child(); child; child = child->next_sibling()) {
  228. if (is<U>(*child))
  229. return &downcast<U>(*child);
  230. }
  231. return nullptr;
  232. }
  233. template<typename U>
  234. const U* first_ancestor_of_type() const
  235. {
  236. return const_cast<TreeNode*>(this)->template first_ancestor_of_type<U>();
  237. }
  238. template<typename U>
  239. U* first_ancestor_of_type()
  240. {
  241. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  242. if (is<U>(*ancestor))
  243. return &downcast<U>(*ancestor);
  244. }
  245. return nullptr;
  246. }
  247. protected:
  248. TreeNode() { }
  249. private:
  250. int m_ref_count { 1 };
  251. T* m_parent { nullptr };
  252. T* m_first_child { nullptr };
  253. T* m_last_child { nullptr };
  254. T* m_next_sibling { nullptr };
  255. T* m_previous_sibling { nullptr };
  256. };
  257. template<typename T>
  258. inline NonnullRefPtr<T> TreeNode<T>::remove_child(NonnullRefPtr<T> node)
  259. {
  260. ASSERT(node->m_parent == this);
  261. if (m_first_child == node)
  262. m_first_child = node->m_next_sibling;
  263. if (m_last_child == node)
  264. m_last_child = node->m_previous_sibling;
  265. if (node->m_next_sibling)
  266. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  267. if (node->m_previous_sibling)
  268. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  269. node->m_next_sibling = nullptr;
  270. node->m_previous_sibling = nullptr;
  271. node->m_parent = nullptr;
  272. node->removed_from(static_cast<T&>(*this));
  273. node->unref();
  274. static_cast<T*>(this)->children_changed();
  275. return node;
  276. }
  277. template<typename T>
  278. inline void TreeNode<T>::append_child(NonnullRefPtr<T> node, bool notify)
  279. {
  280. ASSERT(!node->m_parent);
  281. if (!static_cast<T*>(this)->is_child_allowed(*node))
  282. return;
  283. if (m_last_child)
  284. m_last_child->m_next_sibling = node.ptr();
  285. node->m_previous_sibling = m_last_child;
  286. node->m_parent = static_cast<T*>(this);
  287. m_last_child = node.ptr();
  288. if (!m_first_child)
  289. m_first_child = m_last_child;
  290. if (notify)
  291. node->inserted_into(static_cast<T&>(*this));
  292. (void)node.leak_ref();
  293. if (notify)
  294. static_cast<T*>(this)->children_changed();
  295. }
  296. template<typename T>
  297. inline void TreeNode<T>::insert_before(NonnullRefPtr<T> node, RefPtr<T> child, bool notify)
  298. {
  299. if (!child)
  300. return append_child(move(node), notify);
  301. ASSERT(!node->m_parent);
  302. ASSERT(child->parent() == this);
  303. if (!static_cast<T*>(this)->is_child_allowed(*node))
  304. return;
  305. node->m_previous_sibling = child->m_previous_sibling;
  306. node->m_next_sibling = child;
  307. if (m_first_child == child)
  308. m_first_child = node;
  309. node->m_parent = static_cast<T*>(this);
  310. if (notify)
  311. node->inserted_into(static_cast<T&>(*this));
  312. (void)node.leak_ref();
  313. if (notify)
  314. static_cast<T*>(this)->children_changed();
  315. }
  316. template<typename T>
  317. inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node)
  318. {
  319. ASSERT(!node->m_parent);
  320. if (!static_cast<T*>(this)->is_child_allowed(*node))
  321. return;
  322. if (m_first_child)
  323. m_first_child->m_previous_sibling = node.ptr();
  324. node->m_next_sibling = m_first_child;
  325. node->m_parent = static_cast<T*>(this);
  326. m_first_child = node.ptr();
  327. if (!m_last_child)
  328. m_last_child = m_first_child;
  329. node->inserted_into(static_cast<T&>(*this));
  330. (void)node.leak_ref();
  331. static_cast<T*>(this)->children_changed();
  332. }
  333. template<typename T>
  334. inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
  335. {
  336. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  337. if (ancestor == this)
  338. return true;
  339. }
  340. return false;
  341. }
  342. }