-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager_timer.h
executable file
·150 lines (138 loc) · 2.35 KB
/
manager_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#ifndef __TIMER_H__
#define __TIMER_H__
#include "hulib.h"
#include "ParaseXML.h"
#include <set>
#define TIMER_CONTINUE 0
#define TIMER_DEL_ELE 9
class timer_manager;
class timer_element: virtual public Mutex
{
public:
virtual int doTimer(int tm) = 0;
int Timer(int tm)
{
int ret = 0;
lock();
if (timer_stop == 0)
ret = doTimer(tm);
unlock();
return ret;
}
int TimerSet(int tm)
{
m_tm = tm;
timer_stop = 0;
}
virtual int TimerStop()
{
timer_stop = 1;
}
virtual int TimerStart()
{
timer_stop = 0;
}
int GetUpTimer();
timer_element()
{
timer_stop = 1;
m_tm = 0;
}
virtual ~timer_element()
{
}
void TimerParaseXml(HUMap & mp)
{
timer_stop = mp["stop"]->getvalue_int();
//printf("TimerParaseXml lock=%d\r\n", timer_stop);
}
//pMutex mutex;
int m_tm;
int timer_stop;
timer_manager * timer_mgr;
};
class timer_manager: virtual public Mutex
{
public:
void UpdateTimer()
{
int cur = GetUpTimer();
lock();
iterator it;
for (it = mp.begin(); it != mp.end();)
{
//printf("UpdateTimer %dms %dms\r\n", cur, (*it)->m_tm);
if (cur > (*it)->m_tm && (*it)->timer_stop==0)
{
int ret = (*it)->Timer(cur);
if (ret == TIMER_DEL_ELE)
{
delete *it;
mp.erase(it++);
continue;
}
}
++it;
}
unlock();
}
void AddTimerElement(timer_element * ele)
{
lock();
//mp[name] = ele;
ele->timer_mgr = this;
mp.insert(ele);
unlock();
}
void DelTimerElement(timer_element * ele)
{
lock();
mp.erase(ele);
unlock();
}
void ClearElement()
{
lock();
mp.clear();
unlock();
}
void StopAll()
{
lock();
iterator it;
for (it = mp.begin(); it != mp.end(); ++it)
{
//printf("current = %dms %dms\r\n",cur,mp[i]->m_tm);
(*it)->TimerStop();
}
unlock();
}
void StartAll()
{
lock();
iterator it;
for (it = mp.begin(); it != mp.end(); ++it)
{
//printf("current = %dms %dms\r\n",cur,mp[i]->m_tm);
(*it)->TimerStart();
}
unlock();
}
inline int GetUpTimer()
{
return GetTickCount() - t;
}
timer_manager()
{
t = GetTickCount();
}
virtual ~timer_manager()
{
}
int t;
typedef set<timer_element *>::iterator iterator;
set<timer_element *> mp;
//map<hustr, timer_element *> mp;
}
;
#endif //__TIMER_H__