Object.h 16 KB

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