Object.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/Badge.h>
  8. #include <AK/JsonObject.h>
  9. #include <LibCore/Event.h>
  10. #include <LibCore/EventLoop.h>
  11. #include <LibCore/Object.h>
  12. #include <stdio.h>
  13. namespace Core {
  14. IntrusiveList<Object, RawPtr<Object>, &Object::m_all_objects_list_node>& Object::all_objects()
  15. {
  16. static IntrusiveList<Object, RawPtr<Object>, &Object::m_all_objects_list_node> objects;
  17. return objects;
  18. }
  19. Object::Object(Object* parent)
  20. : m_parent(parent)
  21. {
  22. all_objects().append(*this);
  23. if (m_parent)
  24. m_parent->add_child(*this);
  25. REGISTER_READONLY_STRING_PROPERTY("class_name", class_name);
  26. REGISTER_STRING_PROPERTY("name", name, set_name);
  27. register_property(
  28. "address", [this] { return FlatPtr(this); },
  29. [](auto&) { return false; });
  30. register_property(
  31. "parent", [this] { return FlatPtr(this->parent()); },
  32. [](auto&) { return false; });
  33. }
  34. Object::~Object()
  35. {
  36. // NOTE: We move our children out to a stack vector to prevent other
  37. // code from trying to iterate over them.
  38. auto children = move(m_children);
  39. // NOTE: We also unparent the children, so that they won't try to unparent
  40. // themselves in their own destructors.
  41. for (auto& child : children)
  42. child.m_parent = nullptr;
  43. all_objects().remove(*this);
  44. stop_timer();
  45. if (m_parent)
  46. m_parent->remove_child(*this);
  47. }
  48. void Object::event(Core::Event& event)
  49. {
  50. switch (event.type()) {
  51. case Core::Event::Timer:
  52. return timer_event(static_cast<TimerEvent&>(event));
  53. case Core::Event::ChildAdded:
  54. case Core::Event::ChildRemoved:
  55. return child_event(static_cast<ChildEvent&>(event));
  56. case Core::Event::Invalid:
  57. VERIFY_NOT_REACHED();
  58. break;
  59. case Core::Event::Custom:
  60. return custom_event(static_cast<CustomEvent&>(event));
  61. default:
  62. break;
  63. }
  64. }
  65. void Object::add_child(Object& object)
  66. {
  67. // FIXME: Should we support reparenting objects?
  68. VERIFY(!object.parent() || object.parent() == this);
  69. object.m_parent = this;
  70. m_children.append(object);
  71. Core::ChildEvent child_event(Core::Event::ChildAdded, object);
  72. event(child_event);
  73. }
  74. void Object::insert_child_before(Object& new_child, Object& before_child)
  75. {
  76. // FIXME: Should we support reparenting objects?
  77. VERIFY(!new_child.parent() || new_child.parent() == this);
  78. new_child.m_parent = this;
  79. m_children.insert_before_matching(new_child, [&](auto& existing_child) { return existing_child.ptr() == &before_child; });
  80. Core::ChildEvent child_event(Core::Event::ChildAdded, new_child, &before_child);
  81. event(child_event);
  82. }
  83. void Object::remove_child(Object& object)
  84. {
  85. for (size_t i = 0; i < m_children.size(); ++i) {
  86. if (m_children.ptr_at(i).ptr() == &object) {
  87. // NOTE: We protect the child so it survives the handling of ChildRemoved.
  88. NonnullRefPtr<Object> protector = object;
  89. object.m_parent = nullptr;
  90. m_children.remove(i);
  91. Core::ChildEvent child_event(Core::Event::ChildRemoved, object);
  92. event(child_event);
  93. return;
  94. }
  95. }
  96. VERIFY_NOT_REACHED();
  97. }
  98. void Object::remove_all_children()
  99. {
  100. while (!m_children.is_empty())
  101. m_children.first().remove_from_parent();
  102. }
  103. void Object::timer_event(Core::TimerEvent&)
  104. {
  105. }
  106. void Object::child_event(Core::ChildEvent&)
  107. {
  108. }
  109. void Object::custom_event(CustomEvent&)
  110. {
  111. }
  112. void Object::start_timer(int ms, TimerShouldFireWhenNotVisible fire_when_not_visible)
  113. {
  114. if (m_timer_id) {
  115. dbgln("{} {:p} already has a timer!", class_name(), this);
  116. VERIFY_NOT_REACHED();
  117. }
  118. m_timer_id = Core::EventLoop::register_timer(*this, ms, true, fire_when_not_visible);
  119. }
  120. void Object::stop_timer()
  121. {
  122. if (!m_timer_id)
  123. return;
  124. bool success = Core::EventLoop::unregister_timer(m_timer_id);
  125. VERIFY(success);
  126. m_timer_id = 0;
  127. }
  128. void Object::dump_tree(int indent)
  129. {
  130. for (int i = 0; i < indent; ++i) {
  131. out(" ");
  132. }
  133. out("{}{{{:p}}}", class_name(), this);
  134. if (!name().is_null())
  135. out(" {}", name());
  136. outln();
  137. for_each_child([&](auto& child) {
  138. child.dump_tree(indent + 2);
  139. return IterationDecision::Continue;
  140. });
  141. }
  142. void Object::deferred_invoke(Function<void(Object&)> invokee)
  143. {
  144. deferred_invoke([invokee = move(invokee), this] { invokee(*this); });
  145. }
  146. void Object::deferred_invoke(Function<void()> invokee)
  147. {
  148. Core::deferred_invoke([invokee = move(invokee), strong_this = NonnullRefPtr(*this)] { invokee(); });
  149. }
  150. void Object::save_to(JsonObject& json)
  151. {
  152. for (auto& it : m_properties) {
  153. auto& property = it.value;
  154. json.set(property->name(), property->get());
  155. }
  156. }
  157. JsonValue Object::property(String const& name) const
  158. {
  159. auto it = m_properties.find(name);
  160. if (it == m_properties.end())
  161. return JsonValue();
  162. return it->value->get();
  163. }
  164. bool Object::set_property(String const& name, JsonValue const& value)
  165. {
  166. auto it = m_properties.find(name);
  167. if (it == m_properties.end())
  168. return false;
  169. return it->value->set(value);
  170. }
  171. bool Object::is_ancestor_of(const Object& other) const
  172. {
  173. if (&other == this)
  174. return false;
  175. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  176. if (ancestor == this)
  177. return true;
  178. }
  179. return false;
  180. }
  181. void Object::dispatch_event(Core::Event& e, Object* stay_within)
  182. {
  183. VERIFY(!stay_within || stay_within == this || stay_within->is_ancestor_of(*this));
  184. auto* target = this;
  185. do {
  186. // If there's an event filter on this target, ask if it wants to swallow this event.
  187. if (target->m_event_filter && !target->m_event_filter(e))
  188. return;
  189. target->event(e);
  190. target = target->parent();
  191. if (target == stay_within) {
  192. // Prevent the event from bubbling any further.
  193. return;
  194. }
  195. } while (target && !e.is_accepted());
  196. }
  197. bool Object::is_visible_for_timer_purposes() const
  198. {
  199. if (parent())
  200. return parent()->is_visible_for_timer_purposes();
  201. return true;
  202. }
  203. void Object::increment_inspector_count(Badge<InspectorServerConnection>)
  204. {
  205. ++m_inspector_count;
  206. if (m_inspector_count == 1)
  207. did_begin_inspection();
  208. }
  209. void Object::decrement_inspector_count(Badge<InspectorServerConnection>)
  210. {
  211. --m_inspector_count;
  212. if (!m_inspector_count)
  213. did_end_inspection();
  214. }
  215. void Object::register_property(const String& name, Function<JsonValue()> getter, Function<bool(const JsonValue&)> setter)
  216. {
  217. m_properties.set(name, make<Property>(name, move(getter), move(setter)));
  218. }
  219. void Object::set_event_filter(Function<bool(Core::Event&)> filter)
  220. {
  221. m_event_filter = move(filter);
  222. }
  223. static HashMap<StringView, ObjectClassRegistration*>& object_classes()
  224. {
  225. static HashMap<StringView, ObjectClassRegistration*>* map;
  226. if (!map)
  227. map = new HashMap<StringView, ObjectClassRegistration*>;
  228. return *map;
  229. }
  230. ObjectClassRegistration::ObjectClassRegistration(StringView class_name, Function<NonnullRefPtr<Object>()> factory, ObjectClassRegistration* parent_class)
  231. : m_class_name(class_name)
  232. , m_factory(move(factory))
  233. , m_parent_class(parent_class)
  234. {
  235. object_classes().set(class_name, this);
  236. }
  237. ObjectClassRegistration::~ObjectClassRegistration()
  238. {
  239. }
  240. bool ObjectClassRegistration::is_derived_from(const ObjectClassRegistration& base_class) const
  241. {
  242. if (&base_class == this)
  243. return true;
  244. if (!m_parent_class)
  245. return false;
  246. return m_parent_class->is_derived_from(base_class);
  247. }
  248. void ObjectClassRegistration::for_each(Function<void(const ObjectClassRegistration&)> callback)
  249. {
  250. for (auto& it : object_classes()) {
  251. callback(*it.value);
  252. }
  253. }
  254. const ObjectClassRegistration* ObjectClassRegistration::find(StringView class_name)
  255. {
  256. return object_classes().get(class_name).value_or(nullptr);
  257. }
  258. }