Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 1 | #ifndef AOS_EVENTS_EVENT_LOOP_EVENT_H |
| 2 | #define AOS_EVENTS_EVENT_LOOP_EVENT_H |
| 3 | |
Stephan Pleines | 5ecc8f1 | 2024-05-31 20:35:21 -0700 | [diff] [blame] | 4 | #include <stddef.h> |
| 5 | |
| 6 | #include <chrono> |
| 7 | |
Austin Schuh | 99f7c6a | 2024-06-25 22:07:44 -0700 | [diff] [blame] | 8 | #include "absl/log/check.h" |
| 9 | #include "absl/log/log.h" |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 10 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 11 | #include "aos/time/time.h" |
| 12 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 13 | namespace aos { |
| 14 | |
| 15 | // Common interface to track when callbacks and timers should have happened. |
| 16 | class EventLoopEvent { |
| 17 | public: |
| 18 | virtual ~EventLoopEvent() {} |
| 19 | |
| 20 | bool valid() const { return event_time_ != monotonic_clock::max_time; } |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 21 | void Invalidate() { |
| 22 | event_time_ = monotonic_clock::max_time; |
| 23 | generation_ = 0; |
| 24 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 25 | |
| 26 | monotonic_clock::time_point event_time() const { |
| 27 | DCHECK(valid()); |
| 28 | return event_time_; |
| 29 | } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 30 | void set_event_time(monotonic_clock::time_point event_time) { |
| 31 | event_time_ = event_time; |
| 32 | } |
| 33 | |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 34 | // Internal book-keeping for EventLoop. |
| 35 | size_t generation() const { |
| 36 | DCHECK(valid()); |
| 37 | return generation_; |
| 38 | } |
| 39 | void set_generation(size_t generation) { generation_ = generation; } |
| 40 | |
Austin Schuh | f4b09c7 | 2021-12-08 12:04:37 -0800 | [diff] [blame] | 41 | virtual void HandleEvent() noexcept = 0; |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 42 | |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 43 | private: |
| 44 | monotonic_clock::time_point event_time_ = monotonic_clock::max_time; |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 45 | size_t generation_ = 0; |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | // Adapter class to implement EventLoopEvent by calling HandleEvent on T. |
| 49 | template <typename T> |
| 50 | class EventHandler final : public EventLoopEvent { |
| 51 | public: |
| 52 | EventHandler(T *t) : t_(t) {} |
Brian Silverman | bd405c0 | 2020-06-23 16:25:23 -0700 | [diff] [blame] | 53 | ~EventHandler() override = default; |
Austin Schuh | f4b09c7 | 2021-12-08 12:04:37 -0800 | [diff] [blame] | 54 | void HandleEvent() noexcept override { t_->HandleEvent(); } |
Austin Schuh | 7d87b67 | 2019-12-01 20:23:49 -0800 | [diff] [blame] | 55 | |
| 56 | private: |
| 57 | T *const t_; |
| 58 | }; |
| 59 | |
| 60 | } // namespace aos |
| 61 | |
| 62 | #endif // AOS_EVENTS_EVENT_LOOP_EVENT_H |