Object.cpp 7.7 KB

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