Object.cpp 8.0 KB

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