Object.cpp 6.8 KB

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