27#ifndef EASY3D_UTIL_TIMER_H
28#define EASY3D_UTIL_TIMER_H
48 template<
class... Args>
51 Timer() : stopped_(
false), paused_(
false) {}
53 ~Timer() { stopped_ =
true; }
62 static void single_shot(
int delay, std::function<
void(Args...)>
const &func, Args... args);
73 static void single_shot(
int delay, Class *inst,
void (Class::*func)(Args...), Args... args);
84 static void single_shot(
int delay, Class
const *inst,
void (Class::*func)(Args...)
const, Args... args);
94 void set_timeout(
int delay, std::function<
void(Args...)>
const &func, Args... args)
const;
105 template<
class Class>
106 void set_timeout(
int delay, Class *inst,
void (Class::*func)(Args...), Args... args)
const;
117 template<
class Class>
118 void set_timeout(
int delay, Class
const *inst,
void (Class::*func)(Args...)
const, Args... args)
const;
127 void set_interval(
int interval, std::function<
void(Args...)>
const &func, Args... args)
const;
137 template<
class Class>
138 void set_interval(
int interval, Class *inst,
void (Class::*func)(Args...), Args... args)
const;
148 template<
class Class>
149 void set_interval(
int interval, Class
const *inst,
void (Class::*func)(Args...)
const, Args... args)
const;
156 void stop() { stopped_ =
true; }
182 void resume() {
if (!stopped_ && paused_) paused_ =
false; }
194 template<
class... Args>
196 std::thread t([=]() {
197 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
204 template<
class... Args>
205 template<
class Class>
207 std::thread t([=]() {
208 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
209 (inst->*func)(args...);
215 template<
class... Args>
216 template<
class Class>
218 std::thread t([=]() {
219 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
220 (inst->*func)(args...);
226 template<
class... Args>
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;
239 template<
class... Args>
240 template<
class Class>
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...);
253 template<
class... Args>
254 template<
class Class>
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...);
267 template<
class... Args>
269 const_cast<Timer<Args...
>*>(
this)->stopped_ =
false;
270 std::thread t([=]() {
272 if (stopped_)
return;
273 std::this_thread::sleep_for(std::chrono::milliseconds(interval));
274 if (paused_)
continue;
275 else if (stopped_)
return;
283 template<
class... Args>
284 template<
class Class>
286 const_cast<Timer<Args...
>*>(
this)->stopped_ =
false;
287 std::thread t([=]() {
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...);
299 template<
class... Args>
300 template<
class Class>
302 const_cast<Timer<Args...
>*>(
this)->stopped_ =
false;
303 std::thread t([=]() {
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...);
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