-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtimer.cpp
74 lines (56 loc) · 1.24 KB
/
timer.cpp
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
#include "prism/timer.h"
#include "prism/memoryhandler.h"
#include "prism/datastructures.h"
#include "prism/stlutil.h"
using namespace std;
namespace prism {
typedef struct TimerElement_internal {
Duration mNow;
Duration mDuration;
TimerCB mCB;
void* mCaller;
} TimerElement;
static struct {
map<int, TimerElement> mList;
} gTimerData;
int addTimerCB(Duration tDuration, TimerCB tCB, void* tCaller) {
TimerElement e;
e.mNow = 0;
e.mDuration = tDuration;
e.mCB = tCB;
e.mCaller = tCaller;
return stl_int_map_push_back(gTimerData.mList, e);
}
void removeTimer(int tID)
{
gTimerData.mList.erase(tID);
}
void setupTimer() {
gTimerData.mList.clear();
}
static int updateCB(void* tCaller, TimerElement& tData) {
(void)tCaller;
TimerElement* cur = &tData;
int isOver = handleDurationAndCheckIfOver(&cur->mNow, cur->mDuration);
if (isOver) {
if (cur->mCB)
{
cur->mCB(cur->mCaller);
}
return 1;
}
return 0;
}
void updateTimer() {
stl_int_map_remove_predicate(gTimerData.mList, updateCB);
}
void clearTimer() {
gTimerData.mList.clear();
}
void shutdownTimer() {
clearTimer();
}
int hasTimerFinished(int tID) {
return gTimerData.mList.find(tID) == gTimerData.mList.end();
}
}