CObject.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #pragma once
  2. #include <AK/AKString.h>
  3. #include <AK/Function.h>
  4. #include <AK/StdLibExtras.h>
  5. #include <AK/Vector.h>
  6. #include <AK/Weakable.h>
  7. class CEvent;
  8. class CChildEvent;
  9. class CCustomEvent;
  10. class CTimerEvent;
  11. class CObject : public Weakable<CObject> {
  12. public:
  13. CObject(CObject* parent = nullptr, bool is_widget = false);
  14. virtual ~CObject();
  15. virtual const char* class_name() const { return "CObject"; }
  16. virtual void event(CEvent&);
  17. const String& name() const { return m_name; }
  18. void set_name(const StringView& name) { m_name = name; }
  19. Vector<CObject*>& children() { return m_children; }
  20. const Vector<CObject*>& children() const { return m_children; }
  21. template<typename Callback>
  22. void for_each_child(Callback callback)
  23. {
  24. for (auto* child : m_children) {
  25. if (callback(*child) == IterationDecision::Break)
  26. return;
  27. }
  28. }
  29. template<typename T, typename Callback>
  30. void for_each_child_of_type(Callback callback);
  31. CObject* parent() { return m_parent; }
  32. const CObject* parent() const { return m_parent; }
  33. void start_timer(int ms);
  34. void stop_timer();
  35. bool has_timer() const { return m_timer_id; }
  36. void add_child(CObject&);
  37. void remove_child(CObject&);
  38. void delete_later();
  39. void dump_tree(int indent = 0);
  40. void deferred_invoke(Function<void(CObject&)>);
  41. bool is_widget() const { return m_widget; }
  42. virtual bool is_window() const { return false; }
  43. protected:
  44. virtual void timer_event(CTimerEvent&);
  45. virtual void child_event(CChildEvent&);
  46. virtual void custom_event(CCustomEvent&);
  47. private:
  48. CObject* m_parent { nullptr };
  49. String m_name;
  50. int m_timer_id { 0 };
  51. bool m_widget { false };
  52. Vector<CObject*> m_children;
  53. };
  54. template<typename T>
  55. inline bool is(const CObject&) { return false; }
  56. template<>
  57. inline bool is<CObject>(const CObject&) { return true; }
  58. template<typename T>
  59. inline T& to(CObject& object)
  60. {
  61. ASSERT(is<typename RemoveConst<T>::Type>(object));
  62. return static_cast<T&>(object);
  63. }
  64. template<typename T>
  65. inline const T& to(const CObject& object)
  66. {
  67. ASSERT(is<typename RemoveConst<T>::Type>(object));
  68. return static_cast<const T&>(object);
  69. }
  70. template<typename T, typename Callback>
  71. inline void CObject::for_each_child_of_type(Callback callback)
  72. {
  73. for_each_child([&](auto& child) {
  74. if (is<T>(child))
  75. return callback(to<T>(child));
  76. return IterationDecision::Continue;
  77. });
  78. }
  79. inline const LogStream& operator<<(const LogStream& stream, const CObject& object)
  80. {
  81. return stream << object.class_name() << '{' << &object << '}';
  82. }