Object.h 5.7 KB

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