Object.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #include <AK/Assertions.h>
  27. #include <AK/JsonObject.h>
  28. #include <LibCore/Event.h>
  29. #include <LibCore/EventLoop.h>
  30. #include <LibCore/Object.h>
  31. #include <stdio.h>
  32. namespace Core {
  33. IntrusiveList<Object, &Object::m_all_objects_list_node>& Object::all_objects()
  34. {
  35. static IntrusiveList<Object, &Object::m_all_objects_list_node> objects;
  36. return objects;
  37. }
  38. Object::Object(Object* parent, bool is_widget)
  39. : m_parent(parent)
  40. , m_widget(is_widget)
  41. {
  42. all_objects().append(*this);
  43. if (m_parent)
  44. m_parent->add_child(*this);
  45. }
  46. Object::~Object()
  47. {
  48. // NOTE: We move our children out to a stack vector to prevent other
  49. // code from trying to iterate over them.
  50. auto children = move(m_children);
  51. // NOTE: We also unparent the children, so that they won't try to unparent
  52. // themselves in their own destructors.
  53. for (auto& child : children)
  54. child.m_parent = nullptr;
  55. all_objects().remove(*this);
  56. stop_timer();
  57. if (m_parent)
  58. m_parent->remove_child(*this);
  59. }
  60. void Object::event(Core::Event& event)
  61. {
  62. switch (event.type()) {
  63. case Core::Event::Timer:
  64. return timer_event(static_cast<TimerEvent&>(event));
  65. case Core::Event::ChildAdded:
  66. case Core::Event::ChildRemoved:
  67. return child_event(static_cast<ChildEvent&>(event));
  68. case Core::Event::Invalid:
  69. ASSERT_NOT_REACHED();
  70. break;
  71. case Core::Event::Custom:
  72. return custom_event(static_cast<CustomEvent&>(event));
  73. default:
  74. break;
  75. }
  76. }
  77. void Object::add_child(Object& object)
  78. {
  79. // FIXME: Should we support reparenting objects?
  80. ASSERT(!object.parent() || object.parent() == this);
  81. object.m_parent = this;
  82. m_children.append(object);
  83. event(*make<Core::ChildEvent>(Core::Event::ChildAdded, object));
  84. }
  85. void Object::insert_child_before(Object& new_child, Object& before_child)
  86. {
  87. // FIXME: Should we support reparenting objects?
  88. ASSERT(!new_child.parent() || new_child.parent() == this);
  89. new_child.m_parent = this;
  90. m_children.insert_before_matching(new_child, [&](auto& existing_child) { return existing_child.ptr() == &before_child; });
  91. event(*make<Core::ChildEvent>(Core::Event::ChildAdded, new_child, &before_child));
  92. }
  93. void Object::remove_child(Object& object)
  94. {
  95. for (size_t i = 0; i < m_children.size(); ++i) {
  96. if (m_children.ptr_at(i).ptr() == &object) {
  97. // NOTE: We protect the child so it survives the handling of ChildRemoved.
  98. NonnullRefPtr<Object> protector = object;
  99. object.m_parent = nullptr;
  100. m_children.remove(i);
  101. event(*make<Core::ChildEvent>(Core::Event::ChildRemoved, object));
  102. return;
  103. }
  104. }
  105. ASSERT_NOT_REACHED();
  106. }
  107. void Object::timer_event(Core::TimerEvent&)
  108. {
  109. }
  110. void Object::child_event(Core::ChildEvent&)
  111. {
  112. }
  113. void Object::custom_event(CustomEvent&)
  114. {
  115. }
  116. void Object::start_timer(int ms, TimerShouldFireWhenNotVisible fire_when_not_visible)
  117. {
  118. if (m_timer_id) {
  119. dbgprintf("Object{%p} already has a timer!\n", this);
  120. ASSERT_NOT_REACHED();
  121. }
  122. m_timer_id = Core::EventLoop::register_timer(*this, ms, true, fire_when_not_visible);
  123. }
  124. void Object::stop_timer()
  125. {
  126. if (!m_timer_id)
  127. return;
  128. bool success = Core::EventLoop::unregister_timer(m_timer_id);
  129. ASSERT(success);
  130. m_timer_id = 0;
  131. }
  132. void Object::dump_tree(int indent)
  133. {
  134. for (int i = 0; i < indent; ++i) {
  135. printf(" ");
  136. }
  137. printf("%s{%p}\n", class_name(), this);
  138. for_each_child([&](auto& child) {
  139. child.dump_tree(indent + 2);
  140. return IterationDecision::Continue;
  141. });
  142. }
  143. void Object::deferred_invoke(Function<void(Object&)> invokee)
  144. {
  145. Core::EventLoop::current().post_event(*this, make<Core::DeferredInvocationEvent>(move(invokee)));
  146. }
  147. void Object::save_to(JsonObject& json)
  148. {
  149. json.set("class_name", class_name());
  150. json.set("address", String::format("%p", this));
  151. json.set("name", name());
  152. json.set("parent", String::format("%p", parent()));
  153. }
  154. bool Object::is_ancestor_of(const Object& other) const
  155. {
  156. if (&other == this)
  157. return false;
  158. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  159. if (ancestor == this)
  160. return true;
  161. }
  162. return false;
  163. }
  164. void Object::dispatch_event(Core::Event& e, Object* stay_within)
  165. {
  166. ASSERT(!stay_within || stay_within == this || stay_within->is_ancestor_of(*this));
  167. auto* target = this;
  168. do {
  169. target->event(e);
  170. target = target->parent();
  171. if (target == stay_within) {
  172. // Prevent the event from bubbling any further.
  173. e.accept();
  174. break;
  175. }
  176. } while (target && !e.is_accepted());
  177. }
  178. bool Object::is_visible_for_timer_purposes() const
  179. {
  180. if (parent())
  181. return parent()->is_visible_for_timer_purposes();
  182. return true;
  183. }
  184. const LogStream& operator<<(const LogStream& stream, const Object& object)
  185. {
  186. return stream << object.class_name() << '{' << &object << '}';
  187. }
  188. }