Object.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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::timer_event(Core::TimerEvent&)
  120. {
  121. }
  122. void Object::child_event(Core::ChildEvent&)
  123. {
  124. }
  125. void Object::custom_event(CustomEvent&)
  126. {
  127. }
  128. void Object::start_timer(int ms, TimerShouldFireWhenNotVisible fire_when_not_visible)
  129. {
  130. if (m_timer_id) {
  131. dbgprintf("Object{%p} already has a timer!\n", this);
  132. ASSERT_NOT_REACHED();
  133. }
  134. m_timer_id = Core::EventLoop::register_timer(*this, ms, true, fire_when_not_visible);
  135. }
  136. void Object::stop_timer()
  137. {
  138. if (!m_timer_id)
  139. return;
  140. bool success = Core::EventLoop::unregister_timer(m_timer_id);
  141. ASSERT(success);
  142. m_timer_id = 0;
  143. }
  144. void Object::dump_tree(int indent)
  145. {
  146. for (int i = 0; i < indent; ++i) {
  147. printf(" ");
  148. }
  149. printf("%s{%p}", class_name(), this);
  150. if (!name().is_null())
  151. printf(" %s", name().characters());
  152. printf("\n");
  153. for_each_child([&](auto& child) {
  154. child.dump_tree(indent + 2);
  155. return IterationDecision::Continue;
  156. });
  157. }
  158. void Object::deferred_invoke(Function<void(Object&)> invokee)
  159. {
  160. Core::EventLoop::current().post_event(*this, make<Core::DeferredInvocationEvent>(move(invokee)));
  161. }
  162. void Object::save_to(JsonObject& json)
  163. {
  164. for (auto& it : m_properties) {
  165. auto& property = it.value;
  166. json.set(property->name(), property->get());
  167. }
  168. }
  169. JsonValue Object::property(const StringView& name)
  170. {
  171. auto it = m_properties.find(name);
  172. if (it == m_properties.end())
  173. return JsonValue();
  174. return it->value->get();
  175. }
  176. bool Object::set_property(const StringView& name, const JsonValue& value)
  177. {
  178. auto it = m_properties.find(name);
  179. if (it == m_properties.end())
  180. return false;
  181. return it->value->set(value);
  182. }
  183. bool Object::is_ancestor_of(const Object& other) const
  184. {
  185. if (&other == this)
  186. return false;
  187. for (auto* ancestor = other.parent(); ancestor; ancestor = ancestor->parent()) {
  188. if (ancestor == this)
  189. return true;
  190. }
  191. return false;
  192. }
  193. void Object::dispatch_event(Core::Event& e, Object* stay_within)
  194. {
  195. ASSERT(!stay_within || stay_within == this || stay_within->is_ancestor_of(*this));
  196. auto* target = this;
  197. do {
  198. target->event(e);
  199. target = target->parent();
  200. if (target == stay_within) {
  201. // Prevent the event from bubbling any further.
  202. e.accept();
  203. break;
  204. }
  205. } while (target && !e.is_accepted());
  206. }
  207. bool Object::is_visible_for_timer_purposes() const
  208. {
  209. if (parent())
  210. return parent()->is_visible_for_timer_purposes();
  211. return true;
  212. }
  213. void Object::increment_inspector_count(Badge<RPCClient>)
  214. {
  215. ++m_inspector_count;
  216. if (m_inspector_count == 1)
  217. did_begin_inspection();
  218. }
  219. void Object::decrement_inspector_count(Badge<RPCClient>)
  220. {
  221. --m_inspector_count;
  222. if (!m_inspector_count)
  223. did_end_inspection();
  224. }
  225. void Object::register_property(const String& name, Function<JsonValue()> getter, Function<bool(const JsonValue&)> setter)
  226. {
  227. m_properties.set(name, make<Property>(name, move(getter), move(setter)));
  228. }
  229. const LogStream& operator<<(const LogStream& stream, const Object& object)
  230. {
  231. return stream << object.class_name() << '{' << &object << '}';
  232. }
  233. }