-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.h
83 lines (68 loc) · 1.31 KB
/
Timer.h
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#ifndef TIMER_H_
#define TIMER_H_
#include<time.h>
#include<functional>
#include<atomic>
#include<sys/time.h>
#include<assert.h>
extern const int kMicroSecondsPerSencond;
extern std::atomic<int> Atomic_num;
class Timer
{
public:
typedef std::function<void()> Timercallback;
typedef int64_t Timestamp;
Timer(Timercallback cb,Timestamp when,double interval)
:cb_(std::move(cb)),
expiration_(when),
interval_(interval),
repeat_(interval>0),
seq_(++Atomic_num)
{
}
~Timer()=default;
void run() const
{
assert(cb_);
cb_();
}
bool repeat()const
{
return repeat_;
}
int64_t sequence()const
{
return seq_;
}
Timestamp expiration() const
{
return expiration_;
}
void restart(Timestamp now);
static Timestamp addTime(Timestamp timestamp,double seconds);
private:
const Timercallback cb_;
Timestamp expiration_;
const bool repeat_;
const double interval_;
const int seq_;
};
class TimerId
{
public:
TimerId(Timer *timer,int seq)
:timer_(timer),
seq_(seq)
{}
TimerId()
:timer_(NULL),
seq_(0)
{}
~TimerId()=default;
private:
friend class TimeQueue;
Timer *timer_;
int seq_;
};
Timer::Timestamp now();
#endif