TreeNode.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include <AK/Assertions.h>
  3. #include <AK/NonnullRefPtr.h>
  4. template<typename T>
  5. class TreeNode {
  6. public:
  7. void ref()
  8. {
  9. ASSERT(m_ref_count);
  10. ++m_ref_count;
  11. }
  12. void deref()
  13. {
  14. ASSERT(m_ref_count);
  15. if (!--m_ref_count)
  16. delete static_cast<T*>(this);
  17. }
  18. int ref_count() const { return m_ref_count; }
  19. T* parent() { return m_parent; }
  20. const T* parent() const { return m_parent; }
  21. bool has_children() const { return m_first_child; }
  22. T* next_sibling() { return m_next_sibling; }
  23. T* previous_sibling() { return m_previous_sibling; }
  24. T* first_child() { return m_first_child; }
  25. T* last_child() { return m_last_child; }
  26. const T* next_sibling() const { return m_next_sibling; }
  27. const T* previous_sibling() const { return m_previous_sibling; }
  28. const T* first_child() const { return m_first_child; }
  29. const T* last_child() const { return m_last_child; }
  30. void prepend_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
  31. void append_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
  32. void donate_all_children_to(T& node);
  33. protected:
  34. TreeNode() { }
  35. private:
  36. int m_ref_count { 1 };
  37. T* m_parent { nullptr };
  38. T* m_first_child { nullptr };
  39. T* m_last_child { nullptr };
  40. T* m_next_sibling { nullptr };
  41. T* m_previous_sibling { nullptr };
  42. };
  43. template<typename T>
  44. inline void TreeNode<T>::append_child(NonnullRefPtr<T> node, bool call_inserted_into)
  45. {
  46. ASSERT(!node->m_parent);
  47. if (m_last_child)
  48. m_last_child->m_next_sibling = node.ptr();
  49. node->m_previous_sibling = m_last_child;
  50. node->m_parent = static_cast<T*>(this);
  51. m_last_child = node.ptr();
  52. if (!m_first_child)
  53. m_first_child = m_last_child;
  54. if (call_inserted_into)
  55. node->inserted_into(static_cast<T&>(*this));
  56. (void)node.leak_ref();
  57. }
  58. template<typename T>
  59. inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node, bool call_inserted_into)
  60. {
  61. ASSERT(!node->m_parent);
  62. if (m_first_child)
  63. m_first_child->m_previous_sibling = node.ptr();
  64. node->m_next_sibling = m_first_child;
  65. node->m_parent = static_cast<T*>(this);
  66. m_first_child = node.ptr();
  67. if (!m_last_child)
  68. m_last_child = m_first_child;
  69. if (call_inserted_into)
  70. node->inserted_into(static_cast<T&>(*this));
  71. (void)node.leak_ref();
  72. }
  73. template<typename T>
  74. inline void TreeNode<T>::donate_all_children_to(T& node)
  75. {
  76. for (T* child = m_first_child; child != nullptr;) {
  77. T* next_child = child->m_next_sibling;
  78. child->m_parent = nullptr;
  79. child->m_next_sibling = nullptr;
  80. child->m_previous_sibling = nullptr;
  81. node.append_child(adopt(*child));
  82. child = next_child;
  83. }
  84. m_first_child = nullptr;
  85. m_last_child = nullptr;
  86. }