pacemaker 3.0.3
COM automation for people with deadlines and a hatred of GUIs
 
Loading...
Searching...
No Matches
Timer.hpp
Go to the documentation of this file.
1#ifndef PACEMAKER_TIMER_TIMER_HPP_
2#define PACEMAKER_TIMER_TIMER_HPP_
3
4#include <cassert>
5#include <chrono>
6#include <functional>
7#include <memory>
8
10{
55 class Timer
56 {
57 public:
72 Timer(const std::chrono::milliseconds &period, const std::size_t &tasks_to_execute);
74 Timer(const Timer &) = delete;
76 Timer(Timer &&) = delete;
78 Timer &operator=(const Timer &) = delete;
80 Timer &operator=(Timer &&) = delete;
81
90
103 auto start(const std::function<void(void)> &StartFcn = []() {}) -> void;
104
116 auto stop(const std::function<void(void)> &StopFcn = []() {}) -> void;
117
138 template<class F>
139 auto wait(const F &TimerFcn) -> void;
140
142 [[nodiscard]] auto is_running() const -> bool;
143
144 private:
146 auto wait_for_single_object() -> void;
148 auto set_thread_opts() -> void;
150 auto revert_thread_opts() -> void;
151
152 struct Impl;
154 std::unique_ptr<Impl> pimpl;
155
161 std::size_t m_tasksToExecute{};
162 };
163
164 template<class F>
165 inline auto Timer::wait(const F &TimerFcn) -> void
166 {
167 assert(this->is_running() && "Timer was not started correctly. Did you forget a Timer::start?");
168
169 this->set_thread_opts();
170 for (std::size_t i{}; i < this->m_tasksToExecute; ++i)
171 {
172 this->wait_for_single_object();
173 TimerFcn(i);
174 }
175 this->revert_thread_opts();
176 }
177} // namespace pacemaker::timer
178#endif // PACEMAKER_TIMER_TIMER_HPP_
Fixed-rate, fixed-count periodic timer backed by a Windows high-resolution waitable timer.
Definition Timer.hpp:56
Timer(Timer &&)=delete
Move construction is deleted; the timer has a unique identity.
auto start(const std::function< void(void)> &StartFcn=[]() {}) -> void
Arms the timer and invokes the start callback.
auto wait(const F &TimerFcn) -> void
Blocks the calling thread and drives the timer callback to tasks_to_execute iterations.
Definition Timer.hpp:165
~Timer()
Destructor; stops the timer if running and releases the Win32 handle.
auto is_running() const -> bool
Getter to check whether or not the timer is running.
Timer(const std::chrono::milliseconds &period, const std::size_t &tasks_to_execute)
Constructs a timer with the given period and iteration count.
Timer & operator=(Timer &&)=delete
Move assignment is deleted; the timer has a unique identity.
Timer(const Timer &)=delete
Copying is deleted; The Win32 timer handle must not be duplicated.
auto stop(const std::function< void(void)> &StopFcn=[]() {}) -> void
Invokes the stop callback and marks the timer as no longer running.
Timer & operator=(const Timer &)=delete
Copying assignment is deleted; The Win32 timer handle must not be duplicated.