Object.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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. virtual bool load_from_json(const JsonObject&, RefPtr<Core::Object> (*)(const String&)) { return false; }
  141. protected:
  142. explicit Object(Object* parent = nullptr);
  143. void register_property(const String& name, Function<JsonValue()> getter, Function<bool(const JsonValue&)> setter = nullptr);
  144. virtual void event(Core::Event&);
  145. virtual void timer_event(TimerEvent&);
  146. virtual void custom_event(CustomEvent&);
  147. // NOTE: You may get child events for children that are not yet fully constructed!
  148. virtual void child_event(ChildEvent&);
  149. virtual void did_begin_inspection() { }
  150. virtual void did_end_inspection() { }
  151. private:
  152. Object* m_parent { nullptr };
  153. String m_name;
  154. int m_timer_id { 0 };
  155. unsigned m_inspector_count { 0 };
  156. HashMap<String, NonnullOwnPtr<Property>> m_properties;
  157. NonnullRefPtrVector<Object> m_children;
  158. Function<bool(Core::Event&)> m_event_filter;
  159. };
  160. }
  161. template<>
  162. struct AK::Formatter<Core::Object> : AK::Formatter<FormatString> {
  163. ErrorOr<void> format(FormatBuilder& builder, const Core::Object& value)
  164. {
  165. return AK::Formatter<FormatString>::format(builder, "{}({})", value.class_name(), &value);
  166. }
  167. };
  168. namespace Core {
  169. template<typename T, typename Callback>
  170. inline void Object::for_each_child_of_type(Callback callback) requires IsBaseOf<Object, T>
  171. {
  172. for_each_child([&](auto& child) {
  173. if (is<T>(child))
  174. return callback(static_cast<T&>(child));
  175. return IterationDecision::Continue;
  176. });
  177. }
  178. template<typename T>
  179. T* Object::find_child_of_type_named(const String& name) requires IsBaseOf<Object, T>
  180. {
  181. T* found_child = nullptr;
  182. for_each_child_of_type<T>([&](auto& child) {
  183. if (child.name() == name) {
  184. found_child = &child;
  185. return IterationDecision::Break;
  186. }
  187. return IterationDecision::Continue;
  188. });
  189. return found_child;
  190. }
  191. template<typename T>
  192. T* Object::find_descendant_of_type_named(String const& name) requires IsBaseOf<Object, T>
  193. {
  194. if (is<T>(*this) && this->name() == name) {
  195. return static_cast<T*>(this);
  196. }
  197. T* found_child = nullptr;
  198. for_each_child([&](auto& child) {
  199. found_child = child.template find_descendant_of_type_named<T>(name);
  200. if (found_child)
  201. return IterationDecision::Break;
  202. return IterationDecision::Continue;
  203. });
  204. return found_child;
  205. }
  206. #define REGISTER_INT_PROPERTY(property_name, getter, setter) \
  207. register_property( \
  208. property_name, \
  209. [this] { return this->getter(); }, \
  210. [this](auto& value) { \
  211. this->setter(value.template to_number<int>()); \
  212. return true; \
  213. });
  214. #define REGISTER_BOOL_PROPERTY(property_name, getter, setter) \
  215. register_property( \
  216. property_name, \
  217. [this] { return this->getter(); }, \
  218. [this](auto& value) { \
  219. this->setter(value.to_bool()); \
  220. return true; \
  221. });
  222. #define REGISTER_STRING_PROPERTY(property_name, getter, setter) \
  223. register_property( \
  224. property_name, \
  225. [this] { return this->getter(); }, \
  226. [this](auto& value) { \
  227. this->setter(value.to_string()); \
  228. return true; \
  229. });
  230. #define REGISTER_READONLY_STRING_PROPERTY(property_name, getter) \
  231. register_property( \
  232. property_name, \
  233. [this] { return this->getter(); }, \
  234. {});
  235. #define REGISTER_RECT_PROPERTY(property_name, getter, setter) \
  236. register_property( \
  237. property_name, \
  238. [this] { \
  239. auto rect = this->getter(); \
  240. JsonObject rect_object; \
  241. rect_object.set("x", rect.x()); \
  242. rect_object.set("y", rect.y()); \
  243. rect_object.set("width", rect.width()); \
  244. rect_object.set("height", rect.height()); \
  245. return rect_object; \
  246. }, \
  247. [this](auto& value) { \
  248. if (!value.is_object()) \
  249. return false; \
  250. Gfx::IntRect rect; \
  251. rect.set_x(value.as_object().get("x").to_i32()); \
  252. rect.set_y(value.as_object().get("y").to_i32()); \
  253. rect.set_width(value.as_object().get("width").to_i32()); \
  254. rect.set_height(value.as_object().get("height").to_i32()); \
  255. setter(rect); \
  256. return true; \
  257. });
  258. #define REGISTER_SIZE_PROPERTY(property_name, getter, setter) \
  259. register_property( \
  260. property_name, \
  261. [this] { \
  262. auto size = this->getter(); \
  263. JsonObject size_object; \
  264. size_object.set("width", size.width()); \
  265. size_object.set("height", size.height()); \
  266. return size_object; \
  267. }, \
  268. [this](auto& value) { \
  269. if (!value.is_object()) \
  270. return false; \
  271. Gfx::IntSize size; \
  272. size.set_width(value.as_object().get("width").to_i32()); \
  273. size.set_height(value.as_object().get("height").to_i32()); \
  274. setter(size); \
  275. return true; \
  276. });
  277. #define REGISTER_ENUM_PROPERTY(property_name, getter, setter, EnumType, ...) \
  278. register_property( \
  279. property_name, \
  280. [this]() -> JsonValue { \
  281. struct { \
  282. EnumType enum_value; \
  283. String string_value; \
  284. } options[] = { __VA_ARGS__ }; \
  285. auto enum_value = getter(); \
  286. for (size_t i = 0; i < array_size(options); ++i) { \
  287. auto& option = options[i]; \
  288. if (enum_value == option.enum_value) \
  289. return option.string_value; \
  290. } \
  291. return JsonValue(); \
  292. }, \
  293. [this](auto& value) { \
  294. struct { \
  295. EnumType enum_value; \
  296. String string_value; \
  297. } options[] = { __VA_ARGS__ }; \
  298. if (!value.is_string()) \
  299. return false; \
  300. auto string_value = value.as_string(); \
  301. for (size_t i = 0; i < array_size(options); ++i) { \
  302. auto& option = options[i]; \
  303. if (string_value == option.string_value) { \
  304. setter(option.enum_value); \
  305. return true; \
  306. } \
  307. } \
  308. return false; \
  309. })
  310. #define REGISTER_TEXT_ALIGNMENT_PROPERTY(property_name, getter, setter) \
  311. REGISTER_ENUM_PROPERTY( \
  312. property_name, getter, setter, Gfx::TextAlignment, \
  313. { Gfx::TextAlignment::Center, "Center" }, \
  314. { Gfx::TextAlignment::CenterLeft, "CenterLeft" }, \
  315. { Gfx::TextAlignment::CenterRight, "CenterRight" }, \
  316. { Gfx::TextAlignment::TopLeft, "TopLeft" }, \
  317. { Gfx::TextAlignment::TopRight, "TopRight" }, \
  318. { Gfx::TextAlignment::BottomLeft, "BottomLeft" }, \
  319. { Gfx::TextAlignment::BottomRight, "BottomRight" })
  320. #define REGISTER_FONT_WEIGHT_PROPERTY(property_name, getter, setter) \
  321. REGISTER_ENUM_PROPERTY( \
  322. property_name, getter, setter, unsigned, \
  323. { Gfx::FontWeight::Thin, "Thin" }, \
  324. { Gfx::FontWeight::ExtraLight, "ExtraLight" }, \
  325. { Gfx::FontWeight::Light, "Light" }, \
  326. { Gfx::FontWeight::Regular, "Regular" }, \
  327. { Gfx::FontWeight::Medium, "Medium" }, \
  328. { Gfx::FontWeight::SemiBold, "SemiBold" }, \
  329. { Gfx::FontWeight::Bold, "Bold" }, \
  330. { Gfx::FontWeight::ExtraBold, "ExtraBold" }, \
  331. { Gfx::FontWeight::Black, "Black" }, \
  332. { Gfx::FontWeight::ExtraBlack, "ExtraBlack" })
  333. #define REGISTER_TEXT_WRAPPING_PROPERTY(property_name, getter, setter) \
  334. REGISTER_ENUM_PROPERTY( \
  335. property_name, getter, setter, Gfx::TextWrapping, \
  336. { Gfx::TextWrapping::Wrap, "Wrap" }, \
  337. { Gfx::TextWrapping::DontWrap, "DontWrap" })
  338. }