TreeNode.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Assertions.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/TypeCasts.h>
  10. #include <AK/Weakable.h>
  11. #include <LibWeb/Forward.h>
  12. namespace Web {
  13. template<typename T>
  14. class TreeNode : public Weakable<T> {
  15. public:
  16. void ref()
  17. {
  18. VERIFY(!m_in_removed_last_ref);
  19. VERIFY(m_ref_count);
  20. ++m_ref_count;
  21. }
  22. void unref()
  23. {
  24. VERIFY(!m_in_removed_last_ref);
  25. VERIFY(m_ref_count);
  26. if (!--m_ref_count) {
  27. if constexpr (IsBaseOf<DOM::Node, T>) {
  28. m_in_removed_last_ref = true;
  29. static_cast<T*>(this)->removed_last_ref();
  30. } else {
  31. delete static_cast<T*>(this);
  32. }
  33. return;
  34. }
  35. }
  36. int ref_count() const { return m_ref_count; }
  37. T* parent() { return m_parent; }
  38. const T* parent() const { return m_parent; }
  39. bool has_children() const { return m_first_child; }
  40. T* next_sibling() { return m_next_sibling; }
  41. T* previous_sibling() { return m_previous_sibling; }
  42. T* first_child() { return m_first_child; }
  43. T* last_child() { return m_last_child; }
  44. const T* next_sibling() const { return m_next_sibling; }
  45. const T* previous_sibling() const { return m_previous_sibling; }
  46. const T* first_child() const { return m_first_child; }
  47. const T* last_child() const { return m_last_child; }
  48. int child_count() const
  49. {
  50. int count = 0;
  51. for (auto* child = first_child(); child; child = child->next_sibling())
  52. ++count;
  53. return count;
  54. }
  55. T* child_at_index(int index)
  56. {
  57. int count = 0;
  58. for (auto* child = first_child(); child; child = child->next_sibling()) {
  59. if (count == index)
  60. return child;
  61. ++count;
  62. }
  63. return nullptr;
  64. }
  65. const T* child_at_index(int index) const
  66. {
  67. return const_cast<TreeNode*>(this)->child_at_index(index);
  68. }
  69. Optional<size_t> index_of_child(const T& search_child)
  70. {
  71. VERIFY(search_child.parent() == this);
  72. size_t index = 0;
  73. auto* child = first_child();
  74. VERIFY(child);
  75. do {
  76. if (child == &search_child)
  77. return index;
  78. index++;
  79. } while (child && (child = child->next_sibling()));
  80. return {};
  81. }
  82. template<typename ChildType>
  83. Optional<size_t> index_of_child(const T& search_child)
  84. {
  85. VERIFY(search_child.parent() == this);
  86. size_t index = 0;
  87. auto* child = first_child();
  88. VERIFY(child);
  89. do {
  90. if (!is<ChildType>(child))
  91. continue;
  92. if (child == &search_child)
  93. return index;
  94. index++;
  95. } while (child && (child = child->next_sibling()));
  96. return {};
  97. }
  98. bool is_ancestor_of(const TreeNode&) const;
  99. bool is_inclusive_ancestor_of(const TreeNode&) const;
  100. bool is_descendant_of(const TreeNode&) const;
  101. bool is_inclusive_descendant_of(const TreeNode&) const;
  102. void append_child(NonnullRefPtr<T> node);
  103. void prepend_child(NonnullRefPtr<T> node);
  104. void insert_before(NonnullRefPtr<T> node, RefPtr<T> child);
  105. void remove_child(NonnullRefPtr<T> node);
  106. bool is_child_allowed(const T&) const { return true; }
  107. T* next_in_pre_order()
  108. {
  109. if (first_child())
  110. return first_child();
  111. T* node;
  112. if (!(node = next_sibling())) {
  113. node = parent();
  114. while (node && !node->next_sibling())
  115. node = node->parent();
  116. if (node)
  117. node = node->next_sibling();
  118. }
  119. return node;
  120. }
  121. const T* next_in_pre_order() const
  122. {
  123. return const_cast<TreeNode*>(this)->next_in_pre_order();
  124. }
  125. bool is_before(const T& other) const
  126. {
  127. if (this == &other)
  128. return false;
  129. for (auto* node = this; node; node = node->next_in_pre_order()) {
  130. if (node == &other)
  131. return true;
  132. }
  133. return false;
  134. }
  135. template<typename Callback>
  136. IterationDecision for_each_in_inclusive_subtree(Callback callback) const
  137. {
  138. if (callback(static_cast<const 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 Callback>
  147. IterationDecision for_each_in_inclusive_subtree(Callback callback)
  148. {
  149. if (callback(static_cast<T&>(*this)) == IterationDecision::Break)
  150. return IterationDecision::Break;
  151. for (auto* child = first_child(); child; child = child->next_sibling()) {
  152. if (child->for_each_in_inclusive_subtree(callback) == IterationDecision::Break)
  153. return IterationDecision::Break;
  154. }
  155. return IterationDecision::Continue;
  156. }
  157. template<typename U, typename Callback>
  158. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback)
  159. {
  160. if (is<U>(static_cast<const T&>(*this))) {
  161. if (callback(static_cast<U&>(*this)) == IterationDecision::Break)
  162. return IterationDecision::Break;
  163. }
  164. for (auto* child = first_child(); child; child = child->next_sibling()) {
  165. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  166. return IterationDecision::Break;
  167. }
  168. return IterationDecision::Continue;
  169. }
  170. template<typename U, typename Callback>
  171. IterationDecision for_each_in_inclusive_subtree_of_type(Callback callback) const
  172. {
  173. if (is<U>(static_cast<const T&>(*this))) {
  174. if (callback(static_cast<const U&>(*this)) == IterationDecision::Break)
  175. return IterationDecision::Break;
  176. }
  177. for (auto* child = first_child(); child; child = child->next_sibling()) {
  178. if (child->template for_each_in_inclusive_subtree_of_type<U>(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) const
  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 Callback>
  193. IterationDecision for_each_in_subtree(Callback callback)
  194. {
  195. for (auto* child = first_child(); child; child = child->next_sibling()) {
  196. if (child->for_each_in_inclusive_subtree(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)
  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 U, typename Callback>
  211. IterationDecision for_each_in_subtree_of_type(Callback callback) const
  212. {
  213. for (auto* child = first_child(); child; child = child->next_sibling()) {
  214. if (child->template for_each_in_inclusive_subtree_of_type<U>(callback) == IterationDecision::Break)
  215. return IterationDecision::Break;
  216. }
  217. return IterationDecision::Continue;
  218. }
  219. template<typename Callback>
  220. void for_each_child(Callback callback) const
  221. {
  222. return const_cast<TreeNode*>(this)->template for_each_child(move(callback));
  223. }
  224. template<typename Callback>
  225. void for_each_child(Callback callback)
  226. {
  227. for (auto* node = first_child(); node; node = node->next_sibling())
  228. callback(*node);
  229. }
  230. template<typename U, typename Callback>
  231. void for_each_child_of_type(Callback callback)
  232. {
  233. for (auto* node = first_child(); node; node = node->next_sibling()) {
  234. if (is<U>(node))
  235. callback(verify_cast<U>(*node));
  236. }
  237. }
  238. template<typename U, typename Callback>
  239. void for_each_child_of_type(Callback callback) const
  240. {
  241. return const_cast<TreeNode*>(this)->template for_each_child_of_type<U>(move(callback));
  242. }
  243. template<typename U>
  244. const U* next_sibling_of_type() const
  245. {
  246. return const_cast<TreeNode*>(this)->template next_sibling_of_type<U>();
  247. }
  248. template<typename U>
  249. inline U* next_sibling_of_type()
  250. {
  251. for (auto* sibling = next_sibling(); sibling; sibling = sibling->next_sibling()) {
  252. if (is<U>(*sibling))
  253. return &verify_cast<U>(*sibling);
  254. }
  255. return nullptr;
  256. }
  257. template<typename U>
  258. const U* previous_sibling_of_type() const
  259. {
  260. return const_cast<TreeNode*>(this)->template previous_sibling_of_type<U>();
  261. }
  262. template<typename U>
  263. U* previous_sibling_of_type()
  264. {
  265. for (auto* sibling = previous_sibling(); sibling; sibling = sibling->previous_sibling()) {
  266. if (is<U>(*sibling))
  267. return &verify_cast<U>(*sibling);
  268. }
  269. return nullptr;
  270. }
  271. template<typename U>
  272. const U* first_child_of_type() const
  273. {
  274. return const_cast<TreeNode*>(this)->template first_child_of_type<U>();
  275. }
  276. template<typename U>
  277. const U* last_child_of_type() const
  278. {
  279. return const_cast<TreeNode*>(this)->template last_child_of_type<U>();
  280. }
  281. template<typename U>
  282. U* first_child_of_type()
  283. {
  284. for (auto* child = first_child(); child; child = child->next_sibling()) {
  285. if (is<U>(*child))
  286. return &verify_cast<U>(*child);
  287. }
  288. return nullptr;
  289. }
  290. template<typename U>
  291. U* last_child_of_type()
  292. {
  293. for (auto* child = last_child(); child; child = child->previous_sibling()) {
  294. if (is<U>(*child))
  295. return &verify_cast<U>(*child);
  296. }
  297. return nullptr;
  298. }
  299. template<typename U>
  300. bool has_child_of_type() const
  301. {
  302. return first_child_of_type<U>() != nullptr;
  303. }
  304. template<typename U>
  305. const U* first_ancestor_of_type() const
  306. {
  307. return const_cast<TreeNode*>(this)->template first_ancestor_of_type<U>();
  308. }
  309. template<typename U>
  310. U* first_ancestor_of_type()
  311. {
  312. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  313. if (is<U>(*ancestor))
  314. return &verify_cast<U>(*ancestor);
  315. }
  316. return nullptr;
  317. }
  318. ~TreeNode()
  319. {
  320. VERIFY(!m_parent);
  321. T* next_child = nullptr;
  322. for (auto* child = m_first_child; child; child = next_child) {
  323. next_child = child->m_next_sibling;
  324. child->m_parent = nullptr;
  325. child->unref();
  326. }
  327. }
  328. protected:
  329. TreeNode() { }
  330. bool m_deletion_has_begun { false };
  331. bool m_in_removed_last_ref { false };
  332. private:
  333. int m_ref_count { 1 };
  334. T* m_parent { nullptr };
  335. T* m_first_child { nullptr };
  336. T* m_last_child { nullptr };
  337. T* m_next_sibling { nullptr };
  338. T* m_previous_sibling { nullptr };
  339. };
  340. template<typename T>
  341. inline void TreeNode<T>::remove_child(NonnullRefPtr<T> node)
  342. {
  343. VERIFY(node->m_parent == this);
  344. if (m_first_child == node)
  345. m_first_child = node->m_next_sibling;
  346. if (m_last_child == node)
  347. m_last_child = node->m_previous_sibling;
  348. if (node->m_next_sibling)
  349. node->m_next_sibling->m_previous_sibling = node->m_previous_sibling;
  350. if (node->m_previous_sibling)
  351. node->m_previous_sibling->m_next_sibling = node->m_next_sibling;
  352. node->m_next_sibling = nullptr;
  353. node->m_previous_sibling = nullptr;
  354. node->m_parent = nullptr;
  355. node->unref();
  356. }
  357. template<typename T>
  358. inline void TreeNode<T>::append_child(NonnullRefPtr<T> node)
  359. {
  360. VERIFY(!node->m_parent);
  361. if (!static_cast<T*>(this)->is_child_allowed(*node))
  362. return;
  363. if (m_last_child)
  364. m_last_child->m_next_sibling = node.ptr();
  365. node->m_previous_sibling = m_last_child;
  366. node->m_parent = static_cast<T*>(this);
  367. m_last_child = node.ptr();
  368. if (!m_first_child)
  369. m_first_child = m_last_child;
  370. [[maybe_unused]] auto& rc = node.leak_ref();
  371. }
  372. template<typename T>
  373. inline void TreeNode<T>::insert_before(NonnullRefPtr<T> node, RefPtr<T> child)
  374. {
  375. if (!child)
  376. return append_child(move(node));
  377. VERIFY(!node->m_parent);
  378. VERIFY(child->parent() == this);
  379. node->m_previous_sibling = child->m_previous_sibling;
  380. node->m_next_sibling = child;
  381. if (child->m_previous_sibling)
  382. child->m_previous_sibling->m_next_sibling = node;
  383. if (m_first_child == child)
  384. m_first_child = node;
  385. child->m_previous_sibling = node;
  386. node->m_parent = static_cast<T*>(this);
  387. [[maybe_unused]] auto& rc = node.leak_ref();
  388. }
  389. template<typename T>
  390. inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node)
  391. {
  392. VERIFY(!node->m_parent);
  393. if (!static_cast<T*>(this)->is_child_allowed(*node))
  394. return;
  395. if (m_first_child)
  396. m_first_child->m_previous_sibling = node.ptr();
  397. node->m_next_sibling = m_first_child;
  398. node->m_parent = static_cast<T*>(this);
  399. m_first_child = node.ptr();
  400. if (!m_last_child)
  401. m_last_child = m_first_child;
  402. node->inserted_into(static_cast<T&>(*this));
  403. [[maybe_unused]] auto& rc = node.leak_ref();
  404. static_cast<T*>(this)->children_changed();
  405. }
  406. template<typename T>
  407. inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
  408. {
  409. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  410. if (ancestor == this)
  411. return true;
  412. }
  413. return false;
  414. }
  415. template<typename T>
  416. inline bool TreeNode<T>::is_inclusive_ancestor_of(const TreeNode<T>& other) const
  417. {
  418. return &other == this || is_ancestor_of(other);
  419. }
  420. template<typename T>
  421. inline bool TreeNode<T>::is_descendant_of(const TreeNode<T>& other) const
  422. {
  423. return other.is_ancestor_of(*this);
  424. }
  425. template<typename T>
  426. inline bool TreeNode<T>::is_inclusive_descendant_of(const TreeNode<T>& other) const
  427. {
  428. return other.is_inclusive_ancestor_of(*this);
  429. }
  430. }