-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimer.h
More file actions
45 lines (32 loc) · 898 Bytes
/
timer.h
File metadata and controls
45 lines (32 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#ifndef TIMER_H
#define TIMER_H
#include <thread>
#include <chrono>
class Timer
{
public:
typedef std::chrono::milliseconds Interval;
typedef std::function<void(void)> Timeout;
Timer(const Timeout &timeout);
Timer(const Timeout &timeout,
const Interval &interval,
bool singleShot = true);
void start(bool multiThread = false);
void stop();
bool running() const;
void setSingleShot(bool singleShot);
bool isSingleShot() const;
void setInterval(const Interval &interval);
const Interval &interval() const;
void setTimeout(const Timeout &timeout);
const Timeout &timeout() const;
private:
std::thread _thread;
bool _running = false;
bool _isSingleShot = true;
Interval _interval = Interval(0);
Timeout _timeout = nullptr;
void _temporize();
void _sleepThenTimeout();
};
#endif // TIMER_H