Event.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Function.h>
  8. #include <AK/String.h>
  9. #include <AK/Types.h>
  10. #include <AK/WeakPtr.h>
  11. #include <LibCore/Forward.h>
  12. namespace Core {
  13. class Event {
  14. public:
  15. enum Type {
  16. Invalid = 0,
  17. Quit,
  18. Timer,
  19. NotifierRead,
  20. NotifierWrite,
  21. DeferredInvoke,
  22. ChildAdded,
  23. ChildRemoved,
  24. Custom,
  25. };
  26. Event() { }
  27. explicit Event(unsigned type)
  28. : m_type(type)
  29. {
  30. }
  31. virtual ~Event() { }
  32. unsigned type() const { return m_type; }
  33. bool is_accepted() const { return m_accepted; }
  34. void accept() { m_accepted = true; }
  35. void ignore() { m_accepted = false; }
  36. private:
  37. unsigned m_type { Type::Invalid };
  38. bool m_accepted { true };
  39. };
  40. class DeferredInvocationEvent : public Event {
  41. friend class EventLoop;
  42. public:
  43. DeferredInvocationEvent(Function<void(Object&)> invokee)
  44. : Event(Event::Type::DeferredInvoke)
  45. , m_invokee(move(invokee))
  46. {
  47. }
  48. private:
  49. Function<void(Object&)> m_invokee;
  50. };
  51. class TimerEvent final : public Event {
  52. public:
  53. explicit TimerEvent(int timer_id)
  54. : Event(Event::Timer)
  55. , m_timer_id(timer_id)
  56. {
  57. }
  58. ~TimerEvent() { }
  59. int timer_id() const { return m_timer_id; }
  60. private:
  61. int m_timer_id;
  62. };
  63. class NotifierReadEvent final : public Event {
  64. public:
  65. explicit NotifierReadEvent(int fd)
  66. : Event(Event::NotifierRead)
  67. , m_fd(fd)
  68. {
  69. }
  70. ~NotifierReadEvent() { }
  71. int fd() const { return m_fd; }
  72. private:
  73. int m_fd;
  74. };
  75. class NotifierWriteEvent final : public Event {
  76. public:
  77. explicit NotifierWriteEvent(int fd)
  78. : Event(Event::NotifierWrite)
  79. , m_fd(fd)
  80. {
  81. }
  82. ~NotifierWriteEvent() { }
  83. int fd() const { return m_fd; }
  84. private:
  85. int m_fd;
  86. };
  87. class ChildEvent final : public Event {
  88. public:
  89. ChildEvent(Type, Object& child, Object* insertion_before_child = nullptr);
  90. ~ChildEvent();
  91. Object* child();
  92. const Object* child() const;
  93. Object* insertion_before_child();
  94. const Object* insertion_before_child() const;
  95. private:
  96. WeakPtr<Object> m_child;
  97. WeakPtr<Object> m_insertion_before_child;
  98. };
  99. class CustomEvent : public Event {
  100. public:
  101. CustomEvent(int custom_type)
  102. : Event(Event::Type::Custom)
  103. , m_custom_type(custom_type)
  104. {
  105. }
  106. ~CustomEvent() { }
  107. int custom_type() const { return m_custom_type; }
  108. private:
  109. int m_custom_type { 0 };
  110. };
  111. }