Austin Schuh | bf610b7 | 2024-04-04 20:04:55 -0700 | [diff] [blame^] | 1 | #ifndef AOS_EVENTS_FUNCTION_SCHEDULER_H_ |
| 2 | #define AOS_EVENTS_FUNCTION_SCHEDULER_H_ |
| 3 | |
| 4 | #include <functional> |
| 5 | #include <map> |
| 6 | |
| 7 | #include "aos/events/event_loop.h" |
| 8 | #include "aos/time/time.h" |
| 9 | |
| 10 | namespace aos { |
| 11 | |
| 12 | // Simple class to call a function at a time with a timer. |
| 13 | class FunctionScheduler { |
| 14 | public: |
| 15 | FunctionScheduler(aos::EventLoop *event_loop); |
| 16 | |
| 17 | // Schedules the function to be run at the provided time. |
| 18 | void ScheduleAt(std::function<void()> &&function, |
| 19 | aos::monotonic_clock::time_point time); |
| 20 | |
| 21 | private: |
| 22 | void RunFunctions(aos::monotonic_clock::time_point now); |
| 23 | |
| 24 | aos::EventLoop *event_loop_; |
| 25 | aos::TimerHandler *timer_; |
| 26 | |
| 27 | std::multimap<aos::monotonic_clock::time_point, std::function<void()>> |
| 28 | functions_; |
| 29 | }; |
| 30 | |
| 31 | } // namespace aos |
| 32 | |
| 33 | #endif // AOS_EVENTS_FUNCTION_SCHEDULER_H_ |