Object.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (c) 2018-2021, 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. #pragma once
  27. #include <AK/Forward.h>
  28. #include <AK/HashMap.h>
  29. #include <AK/IntrusiveList.h>
  30. #include <AK/Noncopyable.h>
  31. #include <AK/NonnullRefPtrVector.h>
  32. #include <AK/String.h>
  33. #include <AK/TypeCasts.h>
  34. #include <AK/Weakable.h>
  35. #include <LibCore/Forward.h>
  36. #include <LibCore/Property.h>
  37. namespace Core {
  38. class RPCClient;
  39. enum class TimerShouldFireWhenNotVisible {
  40. No = 0,
  41. Yes
  42. };
  43. #define C_OBJECT(klass) \
  44. public: \
  45. virtual const char* class_name() const override { return #klass; } \
  46. template<class... Args> \
  47. static inline NonnullRefPtr<klass> construct(Args&&... args) \
  48. { \
  49. return adopt(*new klass(forward<Args>(args)...)); \
  50. }
  51. #define C_OBJECT_ABSTRACT(klass) \
  52. public: \
  53. virtual const char* class_name() const override { return #klass; }
  54. class Object
  55. : public RefCounted<Object>
  56. , public Weakable<Object> {
  57. // NOTE: No C_OBJECT macro for Core::Object itself.
  58. AK_MAKE_NONCOPYABLE(Object);
  59. AK_MAKE_NONMOVABLE(Object);
  60. IntrusiveListNode m_all_objects_list_node;
  61. public:
  62. virtual ~Object();
  63. virtual const char* class_name() const = 0;
  64. const String& name() const { return m_name; }
  65. void set_name(const StringView& name) { m_name = name; }
  66. NonnullRefPtrVector<Object>& children() { return m_children; }
  67. const NonnullRefPtrVector<Object>& children() const { return m_children; }
  68. template<typename Callback>
  69. void for_each_child(Callback callback)
  70. {
  71. for (auto& child : m_children) {
  72. if (callback(child) == IterationDecision::Break)
  73. return;
  74. }
  75. }
  76. template<typename T, typename Callback>
  77. void for_each_child_of_type(Callback callback) requires IsBaseOf<Object, T>::value;
  78. template<typename T>
  79. T* find_child_of_type_named(const String&) requires IsBaseOf<Object, T>::value;
  80. template<typename T>
  81. T* find_descendant_of_type_named(const String&) requires IsBaseOf<Object, T>::value;
  82. bool is_ancestor_of(const Object&) const;
  83. Object* parent() { return m_parent; }
  84. const Object* parent() const { return m_parent; }
  85. void start_timer(int ms, TimerShouldFireWhenNotVisible = TimerShouldFireWhenNotVisible::No);
  86. void stop_timer();
  87. bool has_timer() const { return m_timer_id; }
  88. void add_child(Object&);
  89. void insert_child_before(Object& new_child, Object& before_child);
  90. void remove_child(Object&);
  91. void remove_all_children();
  92. void set_event_filter(Function<bool(Core::Event&)>);
  93. void dump_tree(int indent = 0);
  94. void deferred_invoke(Function<void(Object&)>);
  95. void save_to(JsonObject&);
  96. bool set_property(const StringView& name, const JsonValue& value);
  97. JsonValue property(const StringView& name) const;
  98. const HashMap<String, NonnullOwnPtr<Property>>& properties() const { return m_properties; }
  99. static IntrusiveList<Object, &Object::m_all_objects_list_node>& all_objects();
  100. void dispatch_event(Core::Event&, Object* stay_within = nullptr);
  101. void remove_from_parent()
  102. {
  103. if (m_parent)
  104. m_parent->remove_child(*this);
  105. }
  106. template<class T, class... Args>
  107. inline T& add(Args&&... args)
  108. {
  109. auto child = T::construct(forward<Args>(args)...);
  110. add_child(*child);
  111. return child;
  112. }
  113. virtual bool is_visible_for_timer_purposes() const;
  114. bool is_being_inspected() const { return m_inspector_count; }
  115. void increment_inspector_count(Badge<RPCClient>);
  116. void decrement_inspector_count(Badge<RPCClient>);
  117. protected:
  118. explicit Object(Object* parent = nullptr);
  119. void register_property(const String& name, Function<JsonValue()> getter, Function<bool(const JsonValue&)> setter = nullptr);
  120. virtual void event(Core::Event&);
  121. virtual void timer_event(TimerEvent&);
  122. virtual void custom_event(CustomEvent&);
  123. // NOTE: You may get child events for children that are not yet fully constructed!
  124. virtual void child_event(ChildEvent&);
  125. virtual void did_begin_inspection() { }
  126. virtual void did_end_inspection() { }
  127. private:
  128. Object* m_parent { nullptr };
  129. String m_name;
  130. int m_timer_id { 0 };
  131. unsigned m_inspector_count { 0 };
  132. HashMap<String, NonnullOwnPtr<Property>> m_properties;
  133. NonnullRefPtrVector<Object> m_children;
  134. Function<bool(Core::Event&)> m_event_filter;
  135. };
  136. }
  137. template<>
  138. struct AK::Formatter<Core::Object> : AK::Formatter<FormatString> {
  139. void format(FormatBuilder& builder, const Core::Object& value)
  140. {
  141. return AK::Formatter<FormatString>::format(builder, "{}({})", value.class_name(), &value);
  142. }
  143. };
  144. namespace Core {
  145. template<typename T, typename Callback>
  146. inline void Object::for_each_child_of_type(Callback callback) requires IsBaseOf<Object, T>::value
  147. {
  148. for_each_child([&](auto& child) {
  149. if (auto* child_as_t = dynamic_cast<T*>(&child); child_as_t)
  150. return callback(*child_as_t);
  151. return IterationDecision::Continue;
  152. });
  153. }
  154. template<typename T>
  155. T* Object::find_child_of_type_named(const String& name) requires IsBaseOf<Object, T>::value
  156. {
  157. T* found_child = nullptr;
  158. for_each_child_of_type<T>([&](auto& child) {
  159. if (child.name() == name) {
  160. found_child = &child;
  161. return IterationDecision::Break;
  162. }
  163. return IterationDecision::Continue;
  164. });
  165. return found_child;
  166. }
  167. template<typename T>
  168. T* Object::find_descendant_of_type_named(const String& name) requires IsBaseOf<Object, T>::value
  169. {
  170. auto* this_as_t = dynamic_cast<T*>(this);
  171. if (this_as_t && this->name() == name)
  172. return this_as_t;
  173. T* found_child = nullptr;
  174. for_each_child([&](auto& child) {
  175. found_child = child.template find_descendant_of_type_named<T>(name);
  176. if (found_child)
  177. return IterationDecision::Break;
  178. return IterationDecision::Continue;
  179. });
  180. return found_child;
  181. }
  182. #define REGISTER_INT_PROPERTY(property_name, getter, setter) \
  183. register_property( \
  184. property_name, \
  185. [this] { return this->getter(); }, \
  186. [this](auto& value) { \
  187. this->setter(value.template to_number<int>()); \
  188. return true; \
  189. });
  190. #define REGISTER_BOOL_PROPERTY(property_name, getter, setter) \
  191. register_property( \
  192. property_name, \
  193. [this] { return this->getter(); }, \
  194. [this](auto& value) { \
  195. this->setter(value.to_bool()); \
  196. return true; \
  197. });
  198. #define REGISTER_STRING_PROPERTY(property_name, getter, setter) \
  199. register_property( \
  200. property_name, \
  201. [this] { return this->getter(); }, \
  202. [this](auto& value) { \
  203. this->setter(value.to_string()); \
  204. return true; \
  205. });
  206. #define REGISTER_READONLY_STRING_PROPERTY(property_name, getter) \
  207. register_property( \
  208. property_name, \
  209. [this] { return this->getter(); }, \
  210. {});
  211. #define REGISTER_RECT_PROPERTY(property_name, getter, setter) \
  212. register_property( \
  213. property_name, \
  214. [this] { \
  215. auto rect = this->getter(); \
  216. JsonObject rect_object; \
  217. rect_object.set("x", rect.x()); \
  218. rect_object.set("y", rect.y()); \
  219. rect_object.set("width", rect.width()); \
  220. rect_object.set("height", rect.height()); \
  221. return rect_object; \
  222. }, \
  223. [this](auto& value) { \
  224. if (!value.is_object()) \
  225. return false; \
  226. Gfx::IntRect rect; \
  227. rect.set_x(value.as_object().get("x").to_i32()); \
  228. rect.set_y(value.as_object().get("y").to_i32()); \
  229. rect.set_width(value.as_object().get("width").to_i32()); \
  230. rect.set_height(value.as_object().get("height").to_i32()); \
  231. setter(rect); \
  232. return true; \
  233. });
  234. #define REGISTER_SIZE_PROPERTY(property_name, getter, setter) \
  235. register_property( \
  236. property_name, \
  237. [this] { \
  238. auto size = this->getter(); \
  239. JsonObject size_object; \
  240. size_object.set("width", size.width()); \
  241. size_object.set("height", size.height()); \
  242. return size_object; \
  243. }, \
  244. [this](auto& value) { \
  245. if (!value.is_object()) \
  246. return false; \
  247. Gfx::IntSize size; \
  248. size.set_width(value.as_object().get("width").to_i32()); \
  249. size.set_height(value.as_object().get("height").to_i32()); \
  250. setter(size); \
  251. return true; \
  252. });
  253. #define REGISTER_ENUM_PROPERTY(property_name, getter, setter, EnumType, ...) \
  254. register_property( \
  255. property_name, \
  256. [this]() -> JsonValue { \
  257. struct { \
  258. EnumType enum_value; \
  259. String string_value; \
  260. } options[] = { __VA_ARGS__ }; \
  261. auto enum_value = getter(); \
  262. for (size_t i = 0; i < array_size(options); ++i) { \
  263. auto& option = options[i]; \
  264. if (enum_value == option.enum_value) \
  265. return option.string_value; \
  266. } \
  267. return JsonValue(); \
  268. }, \
  269. [this](auto& value) { \
  270. struct { \
  271. EnumType enum_value; \
  272. String string_value; \
  273. } options[] = { __VA_ARGS__ }; \
  274. if (!value.is_string()) \
  275. return false; \
  276. auto string_value = value.as_string(); \
  277. for (size_t i = 0; i < array_size(options); ++i) { \
  278. auto& option = options[i]; \
  279. if (string_value == option.string_value) { \
  280. setter(option.enum_value); \
  281. return true; \
  282. } \
  283. } \
  284. return false; \
  285. })
  286. #define REGISTER_TEXT_ALIGNMENT_PROPERTY(property_name, getter, setter) \
  287. REGISTER_ENUM_PROPERTY( \
  288. property_name, getter, setter, Gfx::TextAlignment, \
  289. { Gfx::TextAlignment::TopLeft, "TopLeft" }, \
  290. { Gfx::TextAlignment::CenterLeft, "CenterLeft" }, \
  291. { Gfx::TextAlignment::Center, "Center" }, \
  292. { Gfx::TextAlignment::CenterRight, "CenterRight" }, \
  293. { Gfx::TextAlignment::TopRight, "TopRight" }, \
  294. { Gfx::TextAlignment::BottomRight, "BottomRight" })
  295. }