CObject.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include <AK/Assertions.h>
  2. #include <AK/JsonObject.h>
  3. #include <AK/kstdio.h>
  4. #include <LibCore/CEvent.h>
  5. #include <LibCore/CEventLoop.h>
  6. #include <LibCore/CObject.h>
  7. #include <stdio.h>
  8. IntrusiveList<CObject, &CObject::m_all_objects_list_node>& CObject::all_objects()
  9. {
  10. static IntrusiveList<CObject, &CObject::m_all_objects_list_node> objects;
  11. return objects;
  12. }
  13. CObject::CObject(CObject* parent, bool is_widget)
  14. : m_parent(parent)
  15. , m_widget(is_widget)
  16. {
  17. all_objects().append(*this);
  18. if (m_parent)
  19. m_parent->add_child(*this);
  20. }
  21. CObject::~CObject()
  22. {
  23. // NOTE: We move our children out to a stack vector to prevent other
  24. // code from trying to iterate over them.
  25. auto children = move(m_children);
  26. // NOTE: We also unparent the children, so that they won't try to unparent
  27. // themselves in their own destructors.
  28. for (auto& child : children)
  29. child.m_parent = nullptr;
  30. all_objects().remove(*this);
  31. stop_timer();
  32. if (m_parent)
  33. m_parent->remove_child(*this);
  34. }
  35. void CObject::event(CEvent& event)
  36. {
  37. switch (event.type()) {
  38. case CEvent::Timer:
  39. return timer_event(static_cast<CTimerEvent&>(event));
  40. case CEvent::ChildAdded:
  41. case CEvent::ChildRemoved:
  42. return child_event(static_cast<CChildEvent&>(event));
  43. case CEvent::Invalid:
  44. ASSERT_NOT_REACHED();
  45. break;
  46. case CEvent::Custom:
  47. return custom_event(static_cast<CCustomEvent&>(event));
  48. default:
  49. break;
  50. }
  51. }
  52. void CObject::add_child(CObject& object)
  53. {
  54. // FIXME: Should we support reparenting objects?
  55. ASSERT(!object.parent() || object.parent() == this);
  56. object.m_parent = this;
  57. m_children.append(object);
  58. event(*make<CChildEvent>(CEvent::ChildAdded, object));
  59. }
  60. void CObject::remove_child(CObject& object)
  61. {
  62. for (int i = 0; i < m_children.size(); ++i) {
  63. if (m_children.ptr_at(i).ptr() == &object) {
  64. // NOTE: We protect the child so it survives the handling of ChildRemoved.
  65. NonnullRefPtr<CObject> protector = object;
  66. object.m_parent = nullptr;
  67. m_children.remove(i);
  68. event(*make<CChildEvent>(CEvent::ChildRemoved, object));
  69. return;
  70. }
  71. }
  72. ASSERT_NOT_REACHED();
  73. }
  74. void CObject::timer_event(CTimerEvent&)
  75. {
  76. }
  77. void CObject::child_event(CChildEvent&)
  78. {
  79. }
  80. void CObject::custom_event(CCustomEvent&)
  81. {
  82. }
  83. void CObject::start_timer(int ms)
  84. {
  85. if (m_timer_id) {
  86. dbgprintf("CObject{%p} already has a timer!\n", this);
  87. ASSERT_NOT_REACHED();
  88. }
  89. m_timer_id = CEventLoop::register_timer(*this, ms, true);
  90. }
  91. void CObject::stop_timer()
  92. {
  93. if (!m_timer_id)
  94. return;
  95. bool success = CEventLoop::unregister_timer(m_timer_id);
  96. ASSERT(success);
  97. m_timer_id = 0;
  98. }
  99. void CObject::dump_tree(int indent)
  100. {
  101. for (int i = 0; i < indent; ++i) {
  102. printf(" ");
  103. }
  104. printf("%s{%p}\n", class_name(), this);
  105. for_each_child([&](auto& child) {
  106. child.dump_tree(indent + 2);
  107. return IterationDecision::Continue;
  108. });
  109. }
  110. void CObject::deferred_invoke(Function<void(CObject&)> invokee)
  111. {
  112. CEventLoop::current().post_event(*this, make<CDeferredInvocationEvent>(move(invokee)));
  113. }
  114. void CObject::save_to(JsonObject& json)
  115. {
  116. json.set("class_name", class_name());
  117. json.set("address", String::format("%p", this));
  118. json.set("name", name());
  119. json.set("parent", String::format("%p", parent()));
  120. }
  121. bool CObject::is_ancestor_of(const CObject& other) const
  122. {
  123. if (&other == this)
  124. return false;
  125. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  126. if (ancestor == this)
  127. return true;
  128. }
  129. return false;
  130. }
  131. void CObject::dispatch_event(CEvent& e, CObject* stay_within)
  132. {
  133. ASSERT(!stay_within || stay_within == this || stay_within->is_ancestor_of(*this));
  134. auto* target = this;
  135. do {
  136. target->event(e);
  137. target = target->parent();
  138. } while (target && target != stay_within && !e.is_accepted());
  139. }