Easy3D 2.5.3
timer.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_UTIL_TIMER_H
28#define EASY3D_UTIL_TIMER_H
29
30#include <thread>
31#include <chrono>
32#include <functional>
33
34namespace easy3d {
35
48 template<class... Args>
49 class Timer {
50 public:
51 Timer() : stopped_(false), paused_(false) {}
52
53 ~Timer() { stopped_ = true; }
54
62 static void single_shot(int delay, std::function<void(Args...)> const &func, Args... args);
63
72 template<class Class>
73 static void single_shot(int delay, Class *inst, void (Class::*func)(Args...), Args... args);
74
83 template<class Class>
84 static void single_shot(int delay, Class const *inst, void (Class::*func)(Args...) const, Args... args);
85
94 void set_timeout(int delay, std::function<void(Args...)> const &func, Args... args) const;
95
105 template<class Class>
106 void set_timeout(int delay, Class *inst, void (Class::*func)(Args...), Args... args) const;
107
117 template<class Class>
118 void set_timeout(int delay, Class const *inst, void (Class::*func)(Args...) const, Args... args) const;
119
127 void set_interval(int interval, std::function<void(Args...)> const &func, Args... args) const;
128
137 template<class Class>
138 void set_interval(int interval, Class *inst, void (Class::*func)(Args...), Args... args) const;
139
148 template<class Class>
149 void set_interval(int interval, Class const *inst, void (Class::*func)(Args...) const, Args... args) const;
150
156 void stop() { stopped_ = true; }
157
162 bool is_stopped() const { return stopped_; }
163
169 void pause() { paused_ = true; }
170
176 bool is_paused() const { return paused_; }
177
182 void resume() { if (!stopped_ && paused_) paused_ = false; }
183
184 private:
185 bool stopped_;
186 bool paused_;
187 };
188
189
190
191 //-------------------------- IMPLEMENTATION ---------------------------
192
193
194 template<class... Args>
195 void Timer<Args...>::single_shot(int delay, std::function<void(Args...)> const &func, Args... args) {
196 std::thread t([=]() {
197 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
198 func(args...);
199 });
200 t.detach();
201 }
202
203
204 template<class... Args>
205 template<class Class>
206 void Timer<Args...>::single_shot(int delay, Class *inst, void (Class::*func)(Args...), Args... args) {
207 std::thread t([=]() {
208 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
209 (inst->*func)(args...);
210 });
211 t.detach();
212 }
213
214
215 template<class... Args>
216 template<class Class>
217 void Timer<Args...>::single_shot(int delay, Class const *inst, void (Class::*func)(Args...) const, Args... args) {
218 std::thread t([=]() {
219 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
220 (inst->*func)(args...);
221 });
222 t.detach();
223 }
224
225
226 template<class... Args>
227 void Timer<Args...>::set_timeout(int delay, std::function<void(Args...)> const &func, Args... args) const {
228 const_cast<Timer<Args...>*>(this)->stopped_ = false;
229 std::thread t([=]() {
230 if (stopped_) return;
231 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
232 if (stopped_) return;
233 func(args...);
234 });
235 t.detach();
236 }
237
238
239 template<class... Args>
240 template<class Class>
241 void Timer<Args...>::set_timeout(int delay, Class *inst, void (Class::*func)(Args...), Args... args) const {
242 const_cast<Timer<Args...>*>(this)->stopped_ = false;
243 std::thread t([=]() {
244 if (stopped_) return;
245 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
246 if (stopped_) return;
247 (inst->*func)(args...);
248 });
249 t.detach();
250 }
251
252
253 template<class... Args>
254 template<class Class>
255 void Timer<Args...>::set_timeout(int delay, Class const *inst, void (Class::*func)(Args...) const, Args... args) const {
256 const_cast<Timer<Args...>*>(this)->stopped_ = false;
257 std::thread t([=]() {
258 if (stopped_) return;
259 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
260 if (stopped_) return;
261 (inst->*func)(args...);
262 });
263 t.detach();
264 }
265
266
267 template<class... Args>
268 void Timer<Args...>::set_interval(int interval, std::function<void(Args...)> const &func, Args... args) const {
269 const_cast<Timer<Args...>*>(this)->stopped_ = false;
270 std::thread t([=]() {
271 while (true) {
272 if (stopped_) return;
273 std::this_thread::sleep_for(std::chrono::milliseconds(interval));
274 if (paused_) continue;
275 else if (stopped_) return;
276 func(args...);
277 }
278 });
279 t.detach();
280 }
281
282
283 template<class... Args>
284 template<class Class>
285 void Timer<Args...>::set_interval(int interval, Class *inst, void (Class::*func)(Args...), Args... args) const {
286 const_cast<Timer<Args...>*>(this)->stopped_ = false;
287 std::thread t([=]() {
288 while (true) {
289 if (stopped_) return;
290 std::this_thread::sleep_for(std::chrono::milliseconds(interval));
291 if (paused_) continue;
292 else if (stopped_) return;
293 (inst->*func)(args...);
294 }
295 });
296 t.detach();
297 }
298
299 template<class... Args>
300 template<class Class>
301 void Timer<Args...>::set_interval(int interval, Class const *inst, void (Class::*func)(Args...) const, Args... args) const {
302 const_cast<Timer<Args...>*>(this)->stopped_ = false;
303 std::thread t([=]() {
304 while (true) {
305 if (stopped_) return;
306 std::this_thread::sleep_for(std::chrono::milliseconds(interval));
307 if (paused_) continue;
308 else if (stopped_) return;
309 (inst->*func)(args...);
310 }
311 });
312 t.detach();
313 }
314
315
316} // namespace easy3d
317
318
319#endif // EASY3D_UTIL_TIMER_H
A light-weight implementation of the timer mechanism.
Definition: timer.h:49
void resume()
Resumes the timer. This will be effective only when the timer has been paused and not stopped.
Definition: timer.h:182
void pause()
Pauses the timer. After a timer is paused, it can be resumed by calling resume(). You can permanently...
Definition: timer.h:169
void set_interval(int interval, std::function< void(Args...)> const &func, Args... args) const
Executes function func for every interval milliseconds.
Definition: timer.h:268
void stop()
Stops the timer. After a timer is stopped, it cannot be restarted again. If you want to temporarily p...
Definition: timer.h:156
static void single_shot(int delay, std::function< void(Args...)> const &func, Args... args)
Executes function func after delay milliseconds.
Definition: timer.h:195
bool is_paused() const
Returns whether the timer has been paused. After a timer is paused, it can be resumed by calling resu...
Definition: timer.h:176
void set_timeout(int delay, std::function< void(Args...)> const &func, Args... args) const
Executes function func after delay milliseconds.
Definition: timer.h:227
bool is_stopped() const
Returns whether the timer has been stopped. If a timer is stopped, it cannot be restarted again.
Definition: timer.h:162
Definition: collider.cpp:182