Object.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. Core::ChildEvent child_event(Core::Event::ChildAdded, object);
  85. event(child_event);
  86. }
  87. void Object::insert_child_before(Object& new_child, Object& before_child)
  88. {
  89. // FIXME: Should we support reparenting objects?
  90. ASSERT(!new_child.parent() || new_child.parent() == this);
  91. new_child.m_parent = this;
  92. m_children.insert_before_matching(new_child, [&](auto& existing_child) { return existing_child.ptr() == &before_child; });
  93. Core::ChildEvent child_event(Core::Event::ChildAdded, new_child, &before_child);
  94. event(child_event);
  95. }
  96. void Object::remove_child(Object& object)
  97. {
  98. for (size_t i = 0; i < m_children.size(); ++i) {
  99. if (m_children.ptr_at(i).ptr() == &object) {
  100. // NOTE: We protect the child so it survives the handling of ChildRemoved.
  101. NonnullRefPtr<Object> protector = object;
  102. object.m_parent = nullptr;
  103. m_children.remove(i);
  104. Core::ChildEvent child_event(Core::Event::ChildRemoved, object);
  105. event(child_event);
  106. return;
  107. }
  108. }
  109. ASSERT_NOT_REACHED();
  110. }
  111. void Object::timer_event(Core::TimerEvent&)
  112. {
  113. }
  114. void Object::child_event(Core::ChildEvent&)
  115. {
  116. }
  117. void Object::custom_event(CustomEvent&)
  118. {
  119. }
  120. void Object::start_timer(int ms, TimerShouldFireWhenNotVisible fire_when_not_visible)
  121. {
  122. if (m_timer_id) {
  123. dbgprintf("Object{%p} already has a timer!\n", this);
  124. ASSERT_NOT_REACHED();
  125. }
  126. m_timer_id = Core::EventLoop::register_timer(*this, ms, true, fire_when_not_visible);
  127. }
  128. void Object::stop_timer()
  129. {
  130. if (!m_timer_id)
  131. return;
  132. bool success = Core::EventLoop::unregister_timer(m_timer_id);
  133. ASSERT(success);
  134. m_timer_id = 0;
  135. }
  136. void Object::dump_tree(int indent)
  137. {
  138. for (int i = 0; i < indent; ++i) {
  139. printf(" ");
  140. }
  141. printf("%s{%p}", class_name(), this);
  142. if (!name().is_null())
  143. printf(" %s", name().characters());
  144. printf("\n");
  145. for_each_child([&](auto& child) {
  146. child.dump_tree(indent + 2);
  147. return IterationDecision::Continue;
  148. });
  149. }
  150. void Object::deferred_invoke(Function<void(Object&)> invokee)
  151. {
  152. Core::EventLoop::current().post_event(*this, make<Core::DeferredInvocationEvent>(move(invokee)));
  153. }
  154. void Object::save_to(JsonObject& json)
  155. {
  156. json.set("class_name", class_name());
  157. json.set("address", (FlatPtr)this);
  158. json.set("name", name());
  159. json.set("parent", (FlatPtr)parent());
  160. }
  161. bool Object::set_property(const StringView& name, const JsonValue& value)
  162. {
  163. if (name == "name") {
  164. set_name(value.to_string());
  165. return true;
  166. }
  167. return false;
  168. }
  169. bool Object::is_ancestor_of(const Object& other) const
  170. {
  171. if (&other == this)
  172. return false;
  173. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  174. if (ancestor == this)
  175. return true;
  176. }
  177. return false;
  178. }
  179. void Object::dispatch_event(Core::Event& e, Object* stay_within)
  180. {
  181. ASSERT(!stay_within || stay_within == this || stay_within->is_ancestor_of(*this));
  182. auto* target = this;
  183. do {
  184. target->event(e);
  185. target = target->parent();
  186. if (target == stay_within) {
  187. // Prevent the event from bubbling any further.
  188. e.accept();
  189. break;
  190. }
  191. } while (target && !e.is_accepted());
  192. }
  193. bool Object::is_visible_for_timer_purposes() const
  194. {
  195. if (parent())
  196. return parent()->is_visible_for_timer_purposes();
  197. return true;
  198. }
  199. void Object::increment_inspector_count(Badge<RPCClient>)
  200. {
  201. ++m_inspector_count;
  202. if (m_inspector_count == 1)
  203. did_begin_inspection();
  204. }
  205. void Object::decrement_inspector_count(Badge<RPCClient>)
  206. {
  207. --m_inspector_count;
  208. if (!m_inspector_count)
  209. did_end_inspection();
  210. }
  211. const LogStream& operator<<(const LogStream& stream, const Object& object)
  212. {
  213. return stream << object.class_name() << '{' << &object << '}';
  214. }
  215. }