Object.h 18 KB

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