Object.h 17 KB

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