Periodic Timer
Loading...
Searching...
No Matches
periodic_timer.hpp
1
83#ifndef PERIODIC_TIMER_HPP_INCLUDED
84#define PERIODIC_TIMER_HPP_INCLUDED
85
86#include "asio/basic_waitable_timer.hpp"
87#include "asio/io_context.hpp"
88
89#include <chrono>
90#include <system_error>
91#include <utility> // std::move, std::forward
92
93namespace chops {
94
95template <typename Clock = std::chrono::steady_clock>
97public:
98
99 using duration = typename Clock::duration;
100 using time_point = typename Clock::time_point;
101
102private:
103
104 asio::basic_waitable_timer<Clock> m_timer;
105
106private:
107 template <typename F>
108 void duration_handler_impl(const time_point& last_tp, const duration& dur,
109 const std::error_code& err, F&& func) {
110 time_point now_time { Clock::now() };
111 // pass err and elapsed time to app function obj
112 if (!func(err, now_time - last_tp) ||
113 err == asio::error::operation_aborted) {
114 return; // app is finished with timer for now or timer was cancelled
115 }
116 m_timer.expires_after(dur);
117 m_timer.async_wait( [now_time, dur, f = std::move(func), this]
118 (const std::error_code& e) {
119 duration_handler_impl(now_time, dur, e, std::move(f));
120 }
121 );
122 }
123 template <typename F>
124 void timepoint_handler_impl(const time_point& last_tp, const duration& dur,
125 const std::error_code& err, F&& func) {
126 // pass err and elapsed time to app function obj
127 if (!func(err, (Clock::now() - last_tp)) ||
128 err == asio::error::operation_aborted) {
129 return; // app is finished with timer for now or timer was cancelled
130 }
131 m_timer.expires_at(last_tp + dur + dur);
132 m_timer.async_wait( [f = std::move(func), last_tp, dur, this]
133 (const std::error_code& e) {
134 timepoint_handler_impl(last_tp+dur, dur, e, std::move(f));
135 }
136 );
137 }
138
139public:
140
168 explicit periodic_timer(asio::io_context& ioc) noexcept : m_timer(ioc) { }
169
170 periodic_timer() = delete; // no default ctor
171
172 // disallow copy construction and copy assignment
173 periodic_timer(const periodic_timer&) = delete;
174 periodic_timer& operator=(const periodic_timer&) = delete;
175
176 // allow move construction and move assignment
177 periodic_timer(periodic_timer&&) = default;
178 periodic_timer& operator=(periodic_timer&& rhs) {
179 m_timer.cancel();
180 m_timer = std::move(rhs.m_timer);
181 }
182
183 // modifying methods
184
196 template <typename F>
197 void start_duration_timer(const duration& dur, F&& func) {
198 m_timer.expires_after(dur);
199 m_timer.async_wait( [dur, f = std::move(func), this] (const std::error_code& e) {
200 duration_handler_impl(Clock::now(), dur, e, std::move(f));
201 }
202 );
203 }
218 template <typename F>
219 void start_duration_timer(const duration& dur, const time_point& when, F&& func) {
220 m_timer.expires_at(when);
221 m_timer.async_wait( [dur, f = std::move(func), this] (const std::error_code& e) {
222 duration_handler_impl(Clock::now(), dur, e, std::move(f));
223 }
224 );
225 }
237 template <typename F>
238 void start_timepoint_timer(const duration& dur, F&& func) {
239 start_timepoint_timer(dur, (Clock::now() + dur), std::forward<F>(func));
240 }
256 template <typename F>
257 void start_timepoint_timer(const duration& dur, const time_point& when, F&& func) {
258 m_timer.expires_at(when);
259 m_timer.async_wait( [when, dur, f = std::move(func), this]
260 (const std::error_code& e) {
261 timepoint_handler_impl((when-dur), dur, e, std::move(f));
262 }
263 );
264 }
265
273 void cancel() {
274 m_timer.cancel();
275 }
276};
277
278} // end namespace
279
280#endif
281
Definition periodic_timer.hpp:96
void start_duration_timer(const duration &dur, const time_point &when, F &&func)
Definition periodic_timer.hpp:219
void start_duration_timer(const duration &dur, F &&func)
Definition periodic_timer.hpp:197
void start_timepoint_timer(const duration &dur, const time_point &when, F &&func)
Definition periodic_timer.hpp:257
periodic_timer(asio::io_context &ioc) noexcept
Definition periodic_timer.hpp:168
void cancel()
Definition periodic_timer.hpp:273
void start_timepoint_timer(const duration &dur, F &&func)
Definition periodic_timer.hpp:238