TreeNode.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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_inclusive_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_inclusive_subtree(callback) == IterationDecision::Break)
  131. return IterationDecision::Break;
  132. }
  133. return IterationDecision::Continue;
  134. }
  135. template<typename Callback>
  136. IterationDecision for_each_in_inclusive_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_inclusive_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_inclusive_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_inclusive_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_inclusive_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_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  168. return IterationDecision::Break;
  169. }
  170. return IterationDecision::Continue;
  171. }
  172. template<typename Callback>
  173. IterationDecision for_each_in_subtree(Callback callback) const
  174. {
  175. for (auto* child = first_child(); child; child = child->next_sibling()) {
  176. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  177. return IterationDecision::Break;
  178. }
  179. return IterationDecision::Continue;
  180. }
  181. template<typename Callback>
  182. IterationDecision for_each_in_subtree(Callback callback)
  183. {
  184. for (auto* child = first_child(); child; child = child->next_sibling()) {
  185. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  186. return IterationDecision::Break;
  187. }
  188. return IterationDecision::Continue;
  189. }
  190. template<typename U, typename Callback>
  191. IterationDecision for_each_in_subtree_of_type(Callback callback)
  192. {
  193. for (auto* child = first_child(); child; child = child->next_sibling()) {
  194. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  195. return IterationDecision::Break;
  196. }
  197. return IterationDecision::Continue;
  198. }
  199. template<typename U, typename Callback>
  200. IterationDecision for_each_in_subtree_of_type(Callback callback) const
  201. {
  202. for (auto* child = first_child(); child; child = child->next_sibling()) {
  203. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  204. return IterationDecision::Break;
  205. }
  206. return IterationDecision::Continue;
  207. }
  208. template<typename Callback>
  209. void for_each_child(Callback callback) const
  210. {
  211. return const_cast<TreeNode*>(this)->template for_each_child(move(callback));
  212. }
  213. template<typename Callback>
  214. void for_each_child(Callback callback)
  215. {
  216. for (auto* node = first_child(); node; node = node->next_sibling())
  217. callback(*node);
  218. }
  219. template<typename U, typename Callback>
  220. void for_each_child_of_type(Callback callback)
  221. {
  222. for (auto* node = first_child(); node; node = node->next_sibling()) {
  223. if (is<U>(node))
  224. callback(downcast<U>(*node));
  225. }
  226. }
  227. template<typename U, typename Callback>
  228. void for_each_child_of_type(Callback callback) const
  229. {
  230. return const_cast<TreeNode*>(this)->template for_each_child_of_type<U>(move(callback));
  231. }
  232. template<typename U>
  233. const U* next_sibling_of_type() const
  234. {
  235. return const_cast<TreeNode*>(this)->template next_sibling_of_type<U>();
  236. }
  237. template<typename U>
  238. inline U* next_sibling_of_type()
  239. {
  240. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  241. if (is<U>(*sibling))
  242. return &downcast<U>(*sibling);
  243. }
  244. return nullptr;
  245. }
  246. template<typename U>
  247. const U* previous_sibling_of_type() const
  248. {
  249. return const_cast<TreeNode*>(this)->template previous_sibling_of_type<U>();
  250. }
  251. template<typename U>
  252. U* previous_sibling_of_type()
  253. {
  254. for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
  255. if (is<U>(*sibling))
  256. return &downcast<U>(*sibling);
  257. }
  258. return nullptr;
  259. }
  260. template<typename U>
  261. const U* first_child_of_type() const
  262. {
  263. return const_cast<TreeNode*>(this)->template first_child_of_type<U>();
  264. }
  265. template<typename U>
  266. const U* last_child_of_type() const
  267. {
  268. return const_cast<TreeNode*>(this)->template last_child_of_type<U>();
  269. }
  270. template<typename U>
  271. U* first_child_of_type()
  272. {
  273. for (auto* child = first_child(); child; child = child->next_sibling()) {
  274. if (is<U>(*child))
  275. return &downcast<U>(*child);
  276. }
  277. return nullptr;
  278. }
  279. template<typename U>
  280. U* last_child_of_type()
  281. {
  282. for (auto* child = last_child(); child; child = child->previous_sibling()) {
  283. if (is<U>(*child))
  284. return &downcast<U>(*child);
  285. }
  286. return nullptr;
  287. }
  288. template<typename U>
  289. const U* first_ancestor_of_type() const
  290. {
  291. return const_cast<TreeNode*>(this)->template first_ancestor_of_type<U>();
  292. }
  293. template<typename U>
  294. U* first_ancestor_of_type()
  295. {
  296. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  297. if (is<U>(*ancestor))
  298. return &downcast<U>(*ancestor);
  299. }
  300. return nullptr;
  301. }
  302. ~TreeNode()
  303. {
  304. VERIFY(!m_parent);
  305. T* next_child = nullptr;
  306. for (auto* child = m_first_child; child; child = next_child) {
  307. next_child = child->m_next_sibling;
  308. child->m_parent = nullptr;
  309. child->unref();
  310. }
  311. }
  312. protected:
  313. TreeNode() { }
  314. bool m_deletion_has_begun { false };
  315. bool m_in_removed_last_ref { false };
  316. private:
  317. int m_ref_count { 1 };
  318. T* m_parent { nullptr };
  319. T* m_first_child { nullptr };
  320. T* m_last_child { nullptr };
  321. T* m_next_sibling { nullptr };
  322. T* m_previous_sibling { nullptr };
  323. };
  324. template<typename T>
  325. inline void TreeNode<T>::remove_all_children()
  326. {
  327. while (RefPtr<T> child = first_child())
  328. remove_child(child.release_nonnull());
  329. }
  330. template<typename T>
  331. inline void TreeNode<T>::remove_child(NonnullRefPtr<T> node)
  332. {
  333. VERIFY(node->m_parent == this);
  334. if (m_first_child == node)
  335. m_first_child = node->m_next_sibling;
  336. if (m_last_child == node)
  337. m_last_child = node->m_previous_sibling;
  338. if (node->m_next_sibling)
  339. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  340. if (node->m_previous_sibling)
  341. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  342. node->m_next_sibling = nullptr;
  343. node->m_previous_sibling = nullptr;
  344. node->m_parent = nullptr;
  345. node->unref();
  346. }
  347. template<typename T>
  348. inline void TreeNode<T>::append_child(NonnullRefPtr<T> node)
  349. {
  350. VERIFY(!node->m_parent);
  351. if (!static_cast<T*>(this)->is_child_allowed(*node))
  352. return;
  353. if (m_last_child)
  354. m_last_child->m_next_sibling = node.ptr();
  355. node->m_previous_sibling = m_last_child;
  356. node->m_parent = static_cast<T*>(this);
  357. m_last_child = node.ptr();
  358. if (!m_first_child)
  359. m_first_child = m_last_child;
  360. [[maybe_unused]] auto& rc = node.leak_ref();
  361. }
  362. template<typename T>
  363. inline void TreeNode<T>::insert_before(NonnullRefPtr<T> node, RefPtr<T> child)
  364. {
  365. if (!child)
  366. return append_child(move(node));
  367. VERIFY(!node->m_parent);
  368. VERIFY(child->parent() == this);
  369. node->m_previous_sibling = child->m_previous_sibling;
  370. node->m_next_sibling = child;
  371. if (child->m_previous_sibling)
  372. child->m_previous_sibling->m_next_sibling = node;
  373. if (m_first_child == child)
  374. m_first_child = node;
  375. child->m_previous_sibling = node;
  376. node->m_parent = static_cast<T*>(this);
  377. [[maybe_unused]] auto& rc = node.leak_ref();
  378. }
  379. template<typename T>
  380. inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node)
  381. {
  382. VERIFY(!node->m_parent);
  383. if (!static_cast<T*>(this)->is_child_allowed(*node))
  384. return;
  385. if (m_first_child)
  386. m_first_child->m_previous_sibling = node.ptr();
  387. node->m_next_sibling = m_first_child;
  388. node->m_parent = static_cast<T*>(this);
  389. m_first_child = node.ptr();
  390. if (!m_last_child)
  391. m_last_child = m_first_child;
  392. node->inserted_into(static_cast<T&>(*this));
  393. [[maybe_unused]] auto& rc = node.leak_ref();
  394. static_cast<T*>(this)->children_changed();
  395. }
  396. template<typename T>
  397. inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
  398. {
  399. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  400. if (ancestor == this)
  401. return true;
  402. }
  403. return false;
  404. }
  405. }