CObject.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. all_objects().remove(*this);
  24. stop_timer();
  25. if (m_parent)
  26. m_parent->remove_child(*this);
  27. auto children_to_delete = move(m_children);
  28. for (auto* child : children_to_delete)
  29. delete child;
  30. }
  31. void CObject::event(CEvent& event)
  32. {
  33. switch (event.type()) {
  34. case CEvent::Timer:
  35. return timer_event(static_cast<CTimerEvent&>(event));
  36. case CEvent::DeferredDestroy:
  37. delete this;
  38. break;
  39. case CEvent::ChildAdded:
  40. case CEvent::ChildRemoved:
  41. return child_event(static_cast<CChildEvent&>(event));
  42. case CEvent::Invalid:
  43. ASSERT_NOT_REACHED();
  44. break;
  45. case CEvent::Custom:
  46. return custom_event(static_cast<CCustomEvent&>(event));
  47. default:
  48. break;
  49. }
  50. }
  51. void CObject::add_child(CObject& object)
  52. {
  53. // FIXME: Should we support reparenting objects?
  54. ASSERT(!object.parent() || object.parent() == this);
  55. object.m_parent = this;
  56. m_children.append(&object);
  57. event(*make<CChildEvent>(CEvent::ChildAdded, object));
  58. }
  59. void CObject::remove_child(CObject& object)
  60. {
  61. for (ssize_t i = 0; i < m_children.size(); ++i) {
  62. if (m_children[i] == &object) {
  63. m_children.remove(i);
  64. event(*make<CChildEvent>(CEvent::ChildRemoved, object));
  65. return;
  66. }
  67. }
  68. }
  69. void CObject::timer_event(CTimerEvent&)
  70. {
  71. }
  72. void CObject::child_event(CChildEvent&)
  73. {
  74. }
  75. void CObject::custom_event(CCustomEvent&)
  76. {
  77. }
  78. void CObject::start_timer(int ms)
  79. {
  80. if (m_timer_id) {
  81. dbgprintf("CObject{%p} already has a timer!\n", this);
  82. ASSERT_NOT_REACHED();
  83. }
  84. m_timer_id = CEventLoop::register_timer(*this, ms, true);
  85. }
  86. void CObject::stop_timer()
  87. {
  88. if (!m_timer_id)
  89. return;
  90. bool success = CEventLoop::unregister_timer(m_timer_id);
  91. ASSERT(success);
  92. m_timer_id = 0;
  93. }
  94. void CObject::delete_later()
  95. {
  96. CEventLoop::current().post_event(*this, make<CEvent>(CEvent::DeferredDestroy));
  97. }
  98. void CObject::dump_tree(int indent)
  99. {
  100. for (int i = 0; i < indent; ++i) {
  101. printf(" ");
  102. }
  103. printf("%s{%p}\n", class_name(), this);
  104. for_each_child([&](auto& child) {
  105. child.dump_tree(indent + 2);
  106. return IterationDecision::Continue;
  107. });
  108. }
  109. void CObject::deferred_invoke(Function<void(CObject&)> invokee)
  110. {
  111. CEventLoop::current().post_event(*this, make<CDeferredInvocationEvent>(move(invokee)));
  112. }
  113. void CObject::save_to(JsonObject& json)
  114. {
  115. json.set("class_name", class_name());
  116. json.set("address", String::format("%p", this));
  117. json.set("name", name());
  118. json.set("parent", String::format("%p", parent()));
  119. }
  120. bool CObject::is_ancestor_of(const CObject& other) const
  121. {
  122. if (&other == this)
  123. return false;
  124. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  125. if (ancestor == this)
  126. return true;
  127. }
  128. return false;
  129. }
  130. void CObject::dispatch_event(CEvent& e, CObject* stay_within)
  131. {
  132. ASSERT(!stay_within || stay_within == this || stay_within->is_ancestor_of(*this));
  133. auto* target = this;
  134. do {
  135. target->event(e);
  136. target = target->parent();
  137. } while (target && target != stay_within && !e.is_accepted());
  138. }