1.10
timer.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_TIMER_H
7#define EGT_TIMER_H
8
14#include <chrono>
15#include <egt/asio.hpp>
16#include <egt/detail/meta.h>
17#include <memory>
18#include <vector>
19
20namespace egt
21{
22inline namespace v1
23{
69class EGT_API Timer
70{
71public:
72
76 using TimerCallback = std::function<void()>;
77
84 Timer() noexcept;
85
89 explicit Timer(std::chrono::milliseconds duration) noexcept;
90
91 Timer(const Timer&) = delete;
92 Timer& operator=(const Timer&) = delete;
93 // NOLINTNEXTLINE(hicpp-noexcept-move,performance-noexcept-move-constructor)
95 // NOLINTNEXTLINE(hicpp-noexcept-move,performance-noexcept-move-constructor)
96 Timer& operator=(Timer&&);
97
106 virtual void start();
107
112 void start(std::chrono::milliseconds duration)
113 {
114 m_duration = duration;
115 start();
116 }
117
123 EGT_DEPRECATED void start_with_duration(std::chrono::milliseconds duration);
124
131 void change_duration(std::chrono::milliseconds duration);
132
136 void cancel();
137
141 void stop() { cancel(); }
142
150 void timeout();
151
155 EGT_NODISCARD std::chrono::milliseconds duration() const { return m_duration; }
156
160 EGT_NODISCARD bool running() const { return m_running; }
161
163 using RegisterHandle = uint64_t;
164
175
180
187
191 EGT_NODISCARD const std::string& name() const { return m_name; }
192
201 void name(const std::string& name) { m_name = name; }
202
203 virtual ~Timer() noexcept;
204
205protected:
206
210 RegisterHandle m_handle_counter{0};
211
216
221 struct CallbackMeta
222 {
223 CallbackMeta(TimerCallback c,
224 RegisterHandle h) noexcept
225 : callback(std::move(c)),
226 handle(h)
227 {}
228
229 TimerCallback callback;
230 RegisterHandle handle{0};
231 };
232
234 using CallbackArray = std::vector<CallbackMeta>;
235
237 asio::steady_timer m_timer;
238
240 std::chrono::milliseconds m_duration{};
241
244
246 bool m_running{false};
247
249 std::string m_name;
250
251 struct TimerImpl;
252 std::unique_ptr<TimerImpl> m_impl;
253
254private:
255
256 void internal_timer_callback(const asio::error_code& error);
257 void do_cancel();
258};
259
280class EGT_API PeriodicTimer : public Timer
281{
282public:
283
284 using Timer::Timer;
285 using Timer::start;
286 void start() override;
287
288private:
289
290 void internal_timer_callback(const asio::error_code& error);
291};
292
293}
294}
295
296#endif
Periodic timer.
Definition timer.h:281
void start() override
Start the timer.
Basic one shot timer.
Definition timer.h:70
void cancel()
Cancel, or stop, the timer.
void clear_handlers()
Clear all handlers.
std::function< void()> TimerCallback
Timer callback function definition.
Definition timer.h:76
Timer() noexcept
Construct a one-shot timer.
std::unique_ptr< TimerImpl > m_impl
Definition timer.h:252
CallbackArray m_callbacks
Array of registered callbacks.
Definition timer.h:243
std::vector< CallbackMeta > CallbackArray
Type for array of registered callbacks.
Definition timer.h:234
void change_duration(std::chrono::milliseconds duration)
Change the duration of the timer.
uint64_t RegisterHandle
Handle type.
Definition timer.h:163
EGT_DEPRECATED void start_with_duration(std::chrono::milliseconds duration)
Start the timer with the specified duration.
EGT_NODISCARD const std::string & name() const
Get the name of the Timer.
Definition timer.h:191
void stop()
Alias for cancel().
Definition timer.h:141
EGT_NODISCARD bool running() const
Returns true if the timer is currently running.
Definition timer.h:160
asio::steady_timer m_timer
Asio timer object.
Definition timer.h:237
RegisterHandle on_timeout(TimerCallback callback)
Add a handler callback to be called with the timer times out.
void name(const std::string &name)
Set the name of the Timer.
Definition timer.h:201
void remove_handler(RegisterHandle handle)
Remove an event handler.
void timeout()
Called when the timer times out.
std::string m_name
A user defined name for the Timer.
Definition timer.h:249
void invoke_handlers()
Invoke any registered handlers.
virtual ~Timer() noexcept
EGT_NODISCARD std::chrono::milliseconds duration() const
Return the current duration of the timer.
Definition timer.h:155
EGT framework namespace.
Definition animation.h:24