sptk2 logo
SPTK Home Page
Timer.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#include "Semaphore.h"
30#include "Thread.h"
31
32#include <functional>
33#include <set>
34
35namespace sptk {
36
37class TimerThread;
38
43class SP_EXPORT Timer
44{
45public:
49 struct EventId {
50 uint64_t serial {++Timer::nextSerial};
56 explicit EventId(const DateTime& when);
57 };
58
65 {
66 friend class Timer;
67
68 public:
73 using Callback = std::function<void()>;
74
79 EventData(const EventData& other) = delete;
80
84 const EventId& getId() const;
85
90 EventData& operator=(const EventData& other) = delete;
91
99 EventData(const DateTime& timestamp, const Callback& eventCallback, std::chrono::milliseconds repeatEvery,
100 int repeatCount = -1);
101
105 const DateTime& getWhen() const
106 {
107 return m_id.when;
108 }
109
114 bool shift(std::chrono::milliseconds interval)
115 {
116 if (m_repeatCount == 0)
117 {
118 return false;
119 }
120
121 if (m_repeatCount > 0)
122 {
123 m_id.when = m_id.when + interval;
124 --m_repeatCount;
125 return true;
126 }
127
128 // Repeat count < 0 - infinite repeats
129 m_id.when = m_id.when + interval;
130 return true;
131 }
132
136 const std::chrono::milliseconds& getInterval() const
137 {
138 return m_repeatInterval;
139 }
140
144 int getRepeatCount() const
145 {
146 return m_repeatCount;
147 }
148
152 bool fire();
153
154 private:
155 EventId m_id;
156 Callback m_callback;
157 std::chrono::milliseconds m_repeatInterval;
158 int m_repeatCount {0};
159 };
160
164 using Event = std::shared_ptr<EventData>;
165
169 Timer() = default;
170
175 Timer(const Timer& other) = delete;
176
181 virtual ~Timer();
182
189 Event fireAt(const DateTime& timestamp, const EventData::Callback& eventCallback);
190
199 Event repeat(std::chrono::milliseconds interval, const EventData::Callback& eventCallback, int repeatCount = -1);
200
205 void cancel(const Event& event);
206
210 void cancel();
211
212protected:
213 void unlink(const Event& event);
214
215private:
216 mutable std::mutex m_mutex;
217 std::set<Event> m_events;
218
219 static std::atomic<uint64_t> nextSerial;
220 static std::mutex timerThreadMutex;
221 static std::shared_ptr<TimerThread> timerThread;
222
223 std::set<Timer::Event> moveOutEvents();
224
225 static void checkTimerThreadRunning();
226};
227
228} // namespace sptk
Definition: DateTime.h:86
Definition: Timer.h:65
bool shift(std::chrono::milliseconds interval)
Definition: Timer.h:114
EventData(const DateTime &timestamp, const Callback &eventCallback, std::chrono::milliseconds repeatEvery, int repeatCount=-1)
int getRepeatCount() const
Definition: Timer.h:144
EventData & operator=(const EventData &other)=delete
EventData(const EventData &other)=delete
const std::chrono::milliseconds & getInterval() const
Definition: Timer.h:136
const EventId & getId() const
const DateTime & getWhen() const
Definition: Timer.h:105
std::function< void()> Callback
Definition: Timer.h:73
Definition: Timer.h:44
Event fireAt(const DateTime &timestamp, const EventData::Callback &eventCallback)
std::shared_ptr< EventData > Event
Definition: Timer.h:164
Timer(const Timer &other)=delete
void unlink(const Event &event)
Remove event from this timer.
Event repeat(std::chrono::milliseconds interval, const EventData::Callback &eventCallback, int repeatCount=-1)
virtual ~Timer()
void cancel(const Event &event)
void cancel()
Timer()=default
Definition: Timer.h:49
DateTime when
Execution date and time.
Definition: Timer.h:51
EventId(const DateTime &when)

Fri Oct 14 2022 09:58:32: SPTK 5.4.1