27#ifndef EASY3D_UTIL_TIMER_H
28#define EASY3D_UTIL_TIMER_H
46 template<
class... Args>
49 Timer() : stopped_(
false), paused_(
false) {}
51 ~Timer() { stopped_ =
true; }
61 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);
85 static void single_shot(
int delay, Class
const *inst,
void (Class::*func)(Args...)
const, Args... args);
96 void set_timeout(
int delay, std::function<
void(Args...)>
const &func, Args... args)
const;
108 template<
class Class>
109 void set_timeout(
int delay, Class *inst,
void (Class::*func)(Args...), Args... args)
const;
121 template<
class Class>
122 void set_timeout(
int delay, Class
const *inst,
void (Class::*func)(Args...)
const, Args... args)
const;
132 void set_interval(
int interval, std::function<
void(Args...)>
const &func, Args... args)
const;
143 template<
class Class>
144 void set_interval(
int interval, Class *inst,
void (Class::*func)(Args...), Args... args)
const;
155 template<
class Class>
156 void set_interval(
int interval, Class
const *inst,
void (Class::*func)(Args...)
const, Args... args)
const;
163 void stop() { stopped_ =
true; }
189 void resume() {
if (!stopped_ && paused_) paused_ =
false; }
201 template<
class... Args>
203 std::thread t([=]() {
204 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
211 template<
class... Args>
212 template<
class Class>
214 std::thread t([=]() {
215 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
216 (inst->*func)(args...);
222 template<
class... Args>
223 template<
class Class>
225 std::thread t([=]() {
226 std::this_thread::sleep_for(std::chrono::milliseconds(delay));
227 (inst->*func)(args...);
233 template<
class... Args>
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;
246 template<
class... Args>
247 template<
class Class>
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...);
260 template<
class... Args>
261 template<
class Class>
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...);
274 template<
class... Args>
276 const_cast<Timer<Args...
>*>(
this)->stopped_ =
false;
277 std::thread t([=]() {
279 if (stopped_)
return;
280 std::this_thread::sleep_for(std::chrono::milliseconds(interval));
281 if (paused_)
continue;
282 else if (stopped_)
return;
290 template<
class... Args>
291 template<
class Class>
293 const_cast<Timer<Args...
>*>(
this)->stopped_ =
false;
294 std::thread t([=]() {
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...);
306 template<
class... Args>
307 template<
class Class>
309 const_cast<Timer<Args...
>*>(
this)->stopped_ =
false;
310 std::thread t([=]() {
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...);
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