TreeNode.h 13 KB

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