TreeNode.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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. bool is_inclusive_ancestor_of(const TreeNode&) const;
  91. bool is_descendant_of(const TreeNode&) const;
  92. bool is_inclusive_descendant_of(const TreeNode&) const;
  93. void append_child(NonnullRefPtr<T> node);
  94. void prepend_child(NonnullRefPtr<T> node);
  95. void insert_before(NonnullRefPtr<T> node, RefPtr<T> child);
  96. void remove_child(NonnullRefPtr<T> node);
  97. bool is_child_allowed(const T&) const { return true; }
  98. T* next_in_pre_order()
  99. {
  100. if (first_child())
  101. return first_child();
  102. T* node;
  103. if (!(node = next_sibling())) {
  104. node = parent();
  105. while (node && !node->next_sibling())
  106. node = node->parent();
  107. if (node)
  108. node = node->next_sibling();
  109. }
  110. return node;
  111. }
  112. const T* next_in_pre_order() const
  113. {
  114. return const_cast<TreeNode*>(this)->next_in_pre_order();
  115. }
  116. bool is_before(const T& other) const
  117. {
  118. if (this == &other)
  119. return false;
  120. for (auto* node = this; node; node = node->next_in_pre_order()) {
  121. if (node == &other)
  122. return true;
  123. }
  124. return false;
  125. }
  126. template<typename Callback>
  127. IterationDecision for_each_in_inclusive_subtree(Callback callback) const
  128. {
  129. if (callback(static_cast<const T&>(*this)) == IterationDecision::Break)
  130. return IterationDecision::Break;
  131. for (auto* child = first_child(); child; child = child->next_sibling()) {
  132. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  133. return IterationDecision::Break;
  134. }
  135. return IterationDecision::Continue;
  136. }
  137. template<typename Callback>
  138. IterationDecision for_each_in_inclusive_subtree(Callback callback)
  139. {
  140. if (callback(static_cast<T&>(*this)) == IterationDecision::Break)
  141. return IterationDecision::Break;
  142. for (auto* child = first_child(); child; child = child->next_sibling()) {
  143. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  144. return IterationDecision::Break;
  145. }
  146. return IterationDecision::Continue;
  147. }
  148. template<typename U, typename Callback>
  149. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback)
  150. {
  151. if (is<U>(static_cast<const T&>(*this))) {
  152. if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
  153. return IterationDecision::Break;
  154. }
  155. for (auto* child = first_child(); child; child = child->next_sibling()) {
  156. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  157. return IterationDecision::Break;
  158. }
  159. return IterationDecision::Continue;
  160. }
  161. template<typename U, typename Callback>
  162. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
  163. {
  164. if (is<U>(static_cast<const T&>(*this))) {
  165. if (callback(static_cast<const U&>(*this)) == IterationDecision::Break)
  166. return IterationDecision::Break;
  167. }
  168. for (auto* child = first_child(); child; child = child->next_sibling()) {
  169. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  170. return IterationDecision::Break;
  171. }
  172. return IterationDecision::Continue;
  173. }
  174. template<typename Callback>
  175. IterationDecision for_each_in_subtree(Callback callback) const
  176. {
  177. for (auto* child = first_child(); child; child = child->next_sibling()) {
  178. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  179. return IterationDecision::Break;
  180. }
  181. return IterationDecision::Continue;
  182. }
  183. template<typename Callback>
  184. IterationDecision for_each_in_subtree(Callback callback)
  185. {
  186. for (auto* child = first_child(); child; child = child->next_sibling()) {
  187. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  188. return IterationDecision::Break;
  189. }
  190. return IterationDecision::Continue;
  191. }
  192. template<typename U, typename Callback>
  193. IterationDecision for_each_in_subtree_of_type(Callback callback)
  194. {
  195. for (auto* child = first_child(); child; child = child->next_sibling()) {
  196. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  197. return IterationDecision::Break;
  198. }
  199. return IterationDecision::Continue;
  200. }
  201. template<typename U, typename Callback>
  202. IterationDecision for_each_in_subtree_of_type(Callback callback) const
  203. {
  204. for (auto* child = first_child(); child; child = child->next_sibling()) {
  205. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  206. return IterationDecision::Break;
  207. }
  208. return IterationDecision::Continue;
  209. }
  210. template<typename Callback>
  211. void for_each_child(Callback callback) const
  212. {
  213. return const_cast<TreeNode*>(this)->template for_each_child(move(callback));
  214. }
  215. template<typename Callback>
  216. void for_each_child(Callback callback)
  217. {
  218. for (auto* node = first_child(); node; node = node->next_sibling())
  219. callback(*node);
  220. }
  221. template<typename U, typename Callback>
  222. void for_each_child_of_type(Callback callback)
  223. {
  224. for (auto* node = first_child(); node; node = node->next_sibling()) {
  225. if (is<U>(node))
  226. callback(downcast<U>(*node));
  227. }
  228. }
  229. template<typename U, typename Callback>
  230. void for_each_child_of_type(Callback callback) const
  231. {
  232. return const_cast<TreeNode*>(this)->template for_each_child_of_type<U>(move(callback));
  233. }
  234. template<typename U>
  235. const U* next_sibling_of_type() const
  236. {
  237. return const_cast<TreeNode*>(this)->template next_sibling_of_type<U>();
  238. }
  239. template<typename U>
  240. inline U* next_sibling_of_type()
  241. {
  242. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  243. if (is<U>(*sibling))
  244. return &downcast<U>(*sibling);
  245. }
  246. return nullptr;
  247. }
  248. template<typename U>
  249. const U* previous_sibling_of_type() const
  250. {
  251. return const_cast<TreeNode*>(this)->template previous_sibling_of_type<U>();
  252. }
  253. template<typename U>
  254. U* previous_sibling_of_type()
  255. {
  256. for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
  257. if (is<U>(*sibling))
  258. return &downcast<U>(*sibling);
  259. }
  260. return nullptr;
  261. }
  262. template<typename U>
  263. const U* first_child_of_type() const
  264. {
  265. return const_cast<TreeNode*>(this)->template first_child_of_type<U>();
  266. }
  267. template<typename U>
  268. const U* last_child_of_type() const
  269. {
  270. return const_cast<TreeNode*>(this)->template last_child_of_type<U>();
  271. }
  272. template<typename U>
  273. U* first_child_of_type()
  274. {
  275. for (auto* child = first_child(); child; child = child->next_sibling()) {
  276. if (is<U>(*child))
  277. return &downcast<U>(*child);
  278. }
  279. return nullptr;
  280. }
  281. template<typename U>
  282. U* last_child_of_type()
  283. {
  284. for (auto* child = last_child(); child; child = child->previous_sibling()) {
  285. if (is<U>(*child))
  286. return &downcast<U>(*child);
  287. }
  288. return nullptr;
  289. }
  290. template<typename U>
  291. bool has_child_of_type() const
  292. {
  293. return first_child_of_type<U>() != nullptr;
  294. }
  295. template<typename U>
  296. const U* first_ancestor_of_type() const
  297. {
  298. return const_cast<TreeNode*>(this)->template first_ancestor_of_type<U>();
  299. }
  300. template<typename U>
  301. U* first_ancestor_of_type()
  302. {
  303. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  304. if (is<U>(*ancestor))
  305. return &downcast<U>(*ancestor);
  306. }
  307. return nullptr;
  308. }
  309. ~TreeNode()
  310. {
  311. VERIFY(!m_parent);
  312. T* next_child = nullptr;
  313. for (auto* child = m_first_child; child; child = next_child) {
  314. next_child = child->m_next_sibling;
  315. child->m_parent = nullptr;
  316. child->unref();
  317. }
  318. }
  319. protected:
  320. TreeNode() { }
  321. bool m_deletion_has_begun { false };
  322. bool m_in_removed_last_ref { false };
  323. private:
  324. int m_ref_count { 1 };
  325. T* m_parent { nullptr };
  326. T* m_first_child { nullptr };
  327. T* m_last_child { nullptr };
  328. T* m_next_sibling { nullptr };
  329. T* m_previous_sibling { nullptr };
  330. };
  331. template<typename T>
  332. inline void TreeNode<T>::remove_child(NonnullRefPtr<T> node)
  333. {
  334. VERIFY(node->m_parent == this);
  335. if (m_first_child == node)
  336. m_first_child = node->m_next_sibling;
  337. if (m_last_child == node)
  338. m_last_child = node->m_previous_sibling;
  339. if (node->m_next_sibling)
  340. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  341. if (node->m_previous_sibling)
  342. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  343. node->m_next_sibling = nullptr;
  344. node->m_previous_sibling = nullptr;
  345. node->m_parent = nullptr;
  346. node->unref();
  347. }
  348. template<typename T>
  349. inline void TreeNode<T>::append_child(NonnullRefPtr<T> node)
  350. {
  351. VERIFY(!node->m_parent);
  352. if (!static_cast<T*>(this)->is_child_allowed(*node))
  353. return;
  354. if (m_last_child)
  355. m_last_child->m_next_sibling = node.ptr();
  356. node->m_previous_sibling = m_last_child;
  357. node->m_parent = static_cast<T*>(this);
  358. m_last_child = node.ptr();
  359. if (!m_first_child)
  360. m_first_child = m_last_child;
  361. [[maybe_unused]] auto& rc = node.leak_ref();
  362. }
  363. template<typename T>
  364. inline void TreeNode<T>::insert_before(NonnullRefPtr<T> node, RefPtr<T> child)
  365. {
  366. if (!child)
  367. return append_child(move(node));
  368. VERIFY(!node->m_parent);
  369. VERIFY(child->parent() == this);
  370. node->m_previous_sibling = child->m_previous_sibling;
  371. node->m_next_sibling = child;
  372. if (child->m_previous_sibling)
  373. child->m_previous_sibling->m_next_sibling = node;
  374. if (m_first_child == child)
  375. m_first_child = node;
  376. child->m_previous_sibling = node;
  377. node->m_parent = static_cast<T*>(this);
  378. [[maybe_unused]] auto& rc = node.leak_ref();
  379. }
  380. template<typename T>
  381. inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node)
  382. {
  383. VERIFY(!node->m_parent);
  384. if (!static_cast<T*>(this)->is_child_allowed(*node))
  385. return;
  386. if (m_first_child)
  387. m_first_child->m_previous_sibling = node.ptr();
  388. node->m_next_sibling = m_first_child;
  389. node->m_parent = static_cast<T*>(this);
  390. m_first_child = node.ptr();
  391. if (!m_last_child)
  392. m_last_child = m_first_child;
  393. node->inserted_into(static_cast<T&>(*this));
  394. [[maybe_unused]] auto& rc = node.leak_ref();
  395. static_cast<T*>(this)->children_changed();
  396. }
  397. template<typename T>
  398. inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
  399. {
  400. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  401. if (ancestor == this)
  402. return true;
  403. }
  404. return false;
  405. }
  406. template<typename T>
  407. inline bool TreeNode<T>::is_inclusive_ancestor_of(const TreeNode<T>& other) const
  408. {
  409. return &other == this || is_ancestor_of(other);
  410. }
  411. template<typename T>
  412. inline bool TreeNode<T>::is_descendant_of(const TreeNode<T>& other) const
  413. {
  414. return other.is_ancestor_of(*this);
  415. }
  416. template<typename T>
  417. inline bool TreeNode<T>::is_inclusive_descendant_of(const TreeNode<T>& other) const
  418. {
  419. return other.is_inclusive_ancestor_of(*this);
  420. }
  421. }