pacemaker 3.2.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
9#include "pacemaker/timer/pacemaker.timer_export.h"
10
12{
57 class Timer
58 {
59 public:
74 PACEMAKER_TIMER_EXPORT Timer(const std::chrono::milliseconds &period, const std::size_t &tasks_to_execute);
76 Timer(const Timer &) = delete;
78 Timer(Timer &&) = delete;
80 Timer &operator=(const Timer &) = delete;
82 Timer &operator=(Timer &&) = delete;
83
91 PACEMAKER_TIMER_EXPORT ~Timer();
92
105 auto PACEMAKER_TIMER_EXPORT start(const std::function<void(void)> &StartFcn = []() {}) -> void;
106
118 auto PACEMAKER_TIMER_EXPORT stop(const std::function<void(void)> &StopFcn = []() {}) -> void;
119
140 template<class F>
141 auto wait(const F &TimerFcn) -> void;
142
144 [[nodiscard]] auto PACEMAKER_TIMER_EXPORT is_running() const -> bool;
145
146 private:
148 auto PACEMAKER_TIMER_EXPORT wait_for_single_object() -> void;
150 auto PACEMAKER_TIMER_EXPORT set_thread_opts() -> void;
152 auto PACEMAKER_TIMER_EXPORT revert_thread_opts() -> void;
153
154 struct Impl;
156 std::unique_ptr<Impl> pimpl;
157
163 std::size_t m_tasksToExecute{};
164 };
165
166 template<class F>
167 inline auto Timer::wait(const F &TimerFcn) -> void
168 {
169 assert(this->is_running() && "Timer was not started correctly. Did you forget a Timer::start?");
170
171 this->set_thread_opts();
172 TimerFcn(0); // Warm up the cache
173 for (std::size_t i{}; i < this->m_tasksToExecute; ++i)
174 {
175 this->wait_for_single_object();
176 TimerFcn(i);
177 }
178 this->wait_for_single_object(); // Do not skip last frame
179 this->revert_thread_opts();
180 }
181} // namespace pacemaker::timer
182#endif // PACEMAKER_TIMER_TIMER_HPP_
Fixed-rate, fixed-count periodic timer backed by a Windows high-resolution waitable timer.
Definition Timer.hpp:58
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:167
~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.