Object.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2018-2020, 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/IntrusiveList.h>
  29. #include <AK/Noncopyable.h>
  30. #include <AK/NonnullRefPtrVector.h>
  31. #include <AK/String.h>
  32. #include <AK/Weakable.h>
  33. #include <LibCore/Forward.h>
  34. namespace Core {
  35. class RPCClient;
  36. enum class TimerShouldFireWhenNotVisible {
  37. No = 0,
  38. Yes
  39. };
  40. #define C_OBJECT(klass) \
  41. public: \
  42. virtual const char* class_name() const override { return #klass; } \
  43. template<class... Args> \
  44. static inline NonnullRefPtr<klass> construct(Args&&... args) \
  45. { \
  46. return adopt(*new klass(forward<Args>(args)...)); \
  47. }
  48. #define C_OBJECT_ABSTRACT(klass) \
  49. public: \
  50. virtual const char* class_name() const override { return #klass; }
  51. class Object
  52. : public RefCounted<Object>
  53. , public Weakable<Object> {
  54. // NOTE: No C_OBJECT macro for Core::Object itself.
  55. AK_MAKE_NONCOPYABLE(Object)
  56. AK_MAKE_NONMOVABLE(Object)
  57. public:
  58. IntrusiveListNode m_all_objects_list_node;
  59. virtual ~Object();
  60. virtual const char* class_name() const = 0;
  61. virtual void event(Core::Event&);
  62. const String& name() const { return m_name; }
  63. void set_name(const StringView& name) { m_name = name; }
  64. NonnullRefPtrVector<Object>& children() { return m_children; }
  65. const NonnullRefPtrVector<Object>& children() const { return m_children; }
  66. template<typename Callback>
  67. void for_each_child(Callback callback)
  68. {
  69. for (auto& child : m_children) {
  70. if (callback(child) == IterationDecision::Break)
  71. return;
  72. }
  73. }
  74. template<typename T, typename Callback>
  75. void for_each_child_of_type(Callback callback);
  76. bool is_ancestor_of(const Object&) const;
  77. Object* parent() { return m_parent; }
  78. const Object* parent() const { return m_parent; }
  79. void start_timer(int ms, TimerShouldFireWhenNotVisible = TimerShouldFireWhenNotVisible::No);
  80. void stop_timer();
  81. bool has_timer() const { return m_timer_id; }
  82. void add_child(Object&);
  83. void insert_child_before(Object& new_child, Object& before_child);
  84. void remove_child(Object&);
  85. void dump_tree(int indent = 0);
  86. void deferred_invoke(Function<void(Object&)>);
  87. bool is_widget() const { return m_widget; }
  88. virtual bool is_action() const { return false; }
  89. virtual bool is_window() const { return false; }
  90. virtual void save_to(AK::JsonObject&);
  91. virtual bool set_property(const StringView& name, const JsonValue& value);
  92. static IntrusiveList<Object, &Object::m_all_objects_list_node>& all_objects();
  93. void dispatch_event(Core::Event&, Object* stay_within = nullptr);
  94. void remove_from_parent()
  95. {
  96. if (m_parent)
  97. m_parent->remove_child(*this);
  98. }
  99. template<class T, class... Args>
  100. inline T& add(Args&&... args)
  101. {
  102. auto child = T::construct(forward<Args>(args)...);
  103. add_child(*child);
  104. return child;
  105. }
  106. virtual bool is_visible_for_timer_purposes() const;
  107. bool is_being_inspected() const { return m_inspector_count; }
  108. void increment_inspector_count(Badge<RPCClient>);
  109. void decrement_inspector_count(Badge<RPCClient>);
  110. protected:
  111. explicit Object(Object* parent = nullptr, bool is_widget = false);
  112. virtual void timer_event(TimerEvent&);
  113. virtual void custom_event(CustomEvent&);
  114. // NOTE: You may get child events for children that are not yet fully constructed!
  115. virtual void child_event(ChildEvent&);
  116. virtual void did_begin_inspection() {}
  117. virtual void did_end_inspection() {}
  118. private:
  119. Object* m_parent { nullptr };
  120. String m_name;
  121. int m_timer_id { 0 };
  122. unsigned m_inspector_count { 0 };
  123. bool m_widget { false };
  124. NonnullRefPtrVector<Object> m_children;
  125. };
  126. template<typename T>
  127. inline bool is(const Object&) { return false; }
  128. template<typename T>
  129. inline T& to(Object& object)
  130. {
  131. ASSERT(is<typename RemoveConst<T>::Type>(object));
  132. return static_cast<T&>(object);
  133. }
  134. template<typename T>
  135. inline const T& to(const Object& object)
  136. {
  137. ASSERT(is<typename RemoveConst<T>::Type>(object));
  138. return static_cast<const T&>(object);
  139. }
  140. template<typename T, typename Callback>
  141. inline void Object::for_each_child_of_type(Callback callback)
  142. {
  143. for_each_child([&](auto& child) {
  144. if (is<T>(child))
  145. return callback(to<T>(child));
  146. return IterationDecision::Continue;
  147. });
  148. }
  149. const LogStream& operator<<(const LogStream&, const Object&);
  150. }