Easy3D 2.6.1
Loading...
Searching...
No Matches
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
45
46 template<class... Args>
47 class Timer {
48 public:
49 Timer() : stopped_(false), paused_(false) {}
50
51 ~Timer() { stopped_ = true; }
52
61 static void single_shot(int delay, std::function<void(Args...)> const &func, Args... args);
62
72 template<class Class>
73 static void single_shot(int delay, Class *inst, void (Class::*func)(Args...), Args... args);
74
84 template<class Class>
85 static void single_shot(int delay, Class const *inst, void (Class::*func)(Args...) const, Args... args);
86
96 void set_timeout(int delay, std::function<void(Args...)> const &func, Args... args) const;
97
108 template<class Class>
109 void set_timeout(int delay, Class *inst, void (Class::*func)(Args...), Args... args) const;
110
121 template<class Class>
122 void set_timeout(int delay, Class const *inst, void (Class::*func)(Args...) const, Args... args) const;
123
132 void set_interval(int interval, std::function<void(Args...)> const &func, Args... args) const;
133
143 template<class Class>
144 void set_interval(int interval, Class *inst, void (Class::*func)(Args...), Args... args) const;
145
155 template<class Class>
156 void set_interval(int interval, Class const *inst, void (Class::*func)(Args...) const, Args... args) const;
157
163 void stop() { stopped_ = true; }
164
169 bool is_stopped() const { return stopped_; }
170
176 void pause() { paused_ = true; }
177
183 bool is_paused() const { return paused_; }
184
189 void resume() { if (!stopped_ && paused_) paused_ = false; }
190
191 private:
192 bool stopped_;
193 bool paused_;
194 };
195
196
197
198 //-------------------------- IMPLEMENTATION ---------------------------
199
200
201 template<class... Args>
202 void Timer<Args...>::single_shot(int delay, std::function<void(Args...)> const &func, Args... args) {
203 std::thread t([=]() {
204 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
205 func(args...);
206 });
207 t.detach();
208 }
209
210
211 template<class... Args>
212 template<class Class>
213 void Timer<Args...>::single_shot(int delay, Class *inst, void (Class::*func)(Args...), Args... args) {
214 std::thread t([=]() {
215 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
216 (inst->*func)(args...);
217 });
218 t.detach();
219 }
220
221
222 template<class... Args>
223 template<class Class>
224 void Timer<Args...>::single_shot(int delay, Class const *inst, void (Class::*func)(Args...) const, Args... args) {
225 std::thread t([=]() {
226 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
227 (inst->*func)(args...);
228 });
229 t.detach();
230 }
231
232
233 template<class... Args>
234 void Timer<Args...>::set_timeout(int delay, std::function<void(Args...)> const &func, Args... args) const {
235 const_cast<Timer<Args...>*>(this)->stopped_ = false;
236 std::thread t([=]() {
237 if (stopped_) return;
238 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
239 if (stopped_) return;
240 func(args...);
241 });
242 t.detach();
243 }
244
245
246 template<class... Args>
247 template<class Class>
248 void Timer<Args...>::set_timeout(int delay, Class *inst, void (Class::*func)(Args...), Args... args) const {
249 const_cast<Timer<Args...>*>(this)->stopped_ = false;
250 std::thread t([=]() {
251 if (stopped_) return;
252 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
253 if (stopped_) return;
254 (inst->*func)(args...);
255 });
256 t.detach();
257 }
258
259
260 template<class... Args>
261 template<class Class>
262 void Timer<Args...>::set_timeout(int delay, Class const *inst, void (Class::*func)(Args...) const, Args... args) const {
263 const_cast<Timer<Args...>*>(this)->stopped_ = false;
264 std::thread t([=]() {
265 if (stopped_) return;
266 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
267 if (stopped_) return;
268 (inst->*func)(args...);
269 });
270 t.detach();
271 }
272
273
274 template<class... Args>
275 void Timer<Args...>::set_interval(int interval, std::function<void(Args...)> const &func, Args... args) const {
276 const_cast<Timer<Args...>*>(this)->stopped_ = false;
277 std::thread t([=]() {
278 while (true) {
279 if (stopped_) return;
280 std::this_thread::sleep_for(std::chrono::milliseconds(interval));
281 if (paused_) continue;
282 else if (stopped_) return;
283 func(args...);
284 }
285 });
286 t.detach();
287 }
288
289
290 template<class... Args>
291 template<class Class>
292 void Timer<Args...>::set_interval(int interval, Class *inst, void (Class::*func)(Args...), Args... args) const {
293 const_cast<Timer<Args...>*>(this)->stopped_ = false;
294 std::thread t([=]() {
295 while (true) {
296 if (stopped_) return;
297 std::this_thread::sleep_for(std::chrono::milliseconds(interval));
298 if (paused_) continue;
299 else if (stopped_) return;
300 (inst->*func)(args...);
301 }
302 });
303 t.detach();
304 }
305
306 template<class... Args>
307 template<class Class>
308 void Timer<Args...>::set_interval(int interval, Class const *inst, void (Class::*func)(Args...) const, Args... args) const {
309 const_cast<Timer<Args...>*>(this)->stopped_ = false;
310 std::thread t([=]() {
311 while (true) {
312 if (stopped_) return;
313 std::this_thread::sleep_for(std::chrono::milliseconds(interval));
314 if (paused_) continue;
315 else if (stopped_) return;
316 (inst->*func)(args...);
317 }
318 });
319 t.detach();
320 }
321
322
323} // namespace easy3d
324
325
326#endif // EASY3D_UTIL_TIMER_H
void resume()
Resumes the timer. This will be effective only when the timer has been paused and not stopped.
Definition timer.h:189
void pause()
Pauses the timer. After a timer is paused, it can be resumed by calling resume(). You can permanently...
Definition timer.h:176
void set_interval(int interval, std::function< void(Args...)> const &func, Args... args) const
Executes function func for every interval milliseconds.
Definition timer.h:275
void stop()
Stops the timer. After a timer is stopped, it cannot be restarted again. If you want to temporarily p...
Definition timer.h:163
static void single_shot(int delay, std::function< void(Args...)> const &func, Args... args)
Executes function func after delay milliseconds.
Definition timer.h:202
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:183
void set_timeout(int delay, std::function< void(Args...)> const &func, Args... args) const
Executes function func after delay milliseconds.
Definition timer.h:234
bool is_stopped() const
Returns whether the timer has been stopped. If a timer is stopped, it cannot be restarted again.
Definition timer.h:169
Definition collider.cpp:182