forked from willamowius/gnugk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gktimer.h
345 lines (293 loc) · 10.3 KB
/
gktimer.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* gktimer.h
*
* Generic support for time-based events.
*
* Copyright (c) 2004, Michal Zygmuntowicz
* Copyright (c) 2006-2010, Jan Willamowius
*
* This work is published under the GNU Public License version 2 (GPLv2)
* see file COPYING for details.
* We also explicitly grant the right to link this code
* with the OpenH323/H323Plus and OpenSSL library.
*
*/
#ifndef GKTIMER_H
#define GKTIMER_H "@(#) $Id$"
#include <list>
/** A base class for timer objects. Currently two types of timer objects
are implemented: a timer calling a regular function and a timer calling
an object member function on timer expiration.
*/
class GkTimer
{
public:
/** @return
True if the timer is periodic (timer function is called periodically).
*/
bool IsPeriodic() const { return m_periodic; }
/** @return
Return time interval (seconds) between subsequent timer function calls
for periodic timers. For one-shot timer the return value is 0.
*/
long GetInterval() const { return m_interval; }
/** @return
Get timer expiration time (time when a timer function will be called).
*/
const PTime& GetExpirationTime() const { return m_expirationTime; }
/** @return
Set a new timer expiration time.
*/
void SetExpirationTime(
const PTime & expirationTime /// the new expiration time
) { m_expirationTime = expirationTime; }
/** @return
True if the timer has expired (and the timer function should be called.
*/
bool IsExpired() const
{ return PTime() >= m_expirationTime && (m_periodic || !m_fired); }
/// Set timer expiration state to fired or not fired
void SetFired(
bool fired /// new state
) { m_fired = fired; }
/** @return
True if the timer has already been fired (timer function has been
called at least once).
*/
bool IsFired() const { return m_fired; }
/// destroy this timer object
virtual ~GkTimer() { }
protected:
/// build an one-shot timer object
GkTimer(
const PTime & expirationTime /// expiration time
) : m_periodic(false), m_fired(false), m_interval(0),
m_expirationTime(expirationTime) { }
/// build a periodic timer object
GkTimer(
const PTime & expirationTime, /// the first expiration time
long interval /// timer interval (seconds)
) : m_periodic(true), m_fired(false), m_interval(interval),
m_expirationTime(expirationTime) { }
/// This function is called by GkTimerManager when the timer expires
virtual void OnTimerExpired() = 0;
friend class GkTimerManager;
private:
GkTimer();
private:
bool m_periodic; /// one-shot(false)/periodic(true) timer
bool m_fired; /// true if the timer function has already been called
long m_interval; /// timer interval (seconds) for periodic timers
PTime m_expirationTime; /// next expiration time
};
/// A timer that calls an object member function on its expiration
template<class T>
class GkVoidMemberFuncTimer : public GkTimer
{
public:
/// build an one-shot timer object
GkVoidMemberFuncTimer(
const PTime & expirationTime, /// timer expiration time
T* classObj, /// object of class T
void (T::*timerFunc)() /// object's member function to call
) : GkTimer(expirationTime), m_classObj(classObj), m_timerFunc(timerFunc) { }
/// build a periodic timer object
GkVoidMemberFuncTimer(
const PTime & expirationTime, /// the first timer expiration time
long interval, /// timer interval (seconds)
T* classObj, /// object of class T
void (T::*timerFunc)() /// object's member function to call
) : GkTimer(expirationTime, interval), m_classObj(classObj), m_timerFunc(timerFunc) { }
protected:
/// This function is called by GkTimerManager when the timer expires
virtual void OnTimerExpired()
{
if (m_classObj && m_timerFunc)
(m_classObj->*m_timerFunc)();
}
private:
GkVoidMemberFuncTimer();
private:
T* m_classObj;
void (T::*m_timerFunc)();
};
/// A timer that calls an object member function on its expiration
template<class T>
class GkOneArgMemberFuncTimer : public GkTimer
{
public:
/// build an one-shot timer object
GkOneArgMemberFuncTimer(
const PTime & expirationTime, /// timer expiration time
T* classObj, /// object of class T
void (T::*timerFunc)(GkTimer*) /// object's member function to call
) : GkTimer(expirationTime), m_classObj(classObj), m_timerFunc(timerFunc) { }
/// build a periodic timer object
GkOneArgMemberFuncTimer(
const PTime & expirationTime, /// the first timer expiration time
long interval, /// timer interval (seconds)
T* classObj, /// object of class T
void (T::*timerFunc)(GkTimer*) /// object's member function to call
) : GkTimer(expirationTime, interval), m_classObj(classObj), m_timerFunc(timerFunc) { }
protected:
/// This function is called by GkTimerManager when the timer expires
virtual void OnTimerExpired()
{
if (m_classObj && m_timerFunc)
(m_classObj->*m_timerFunc)(this);
}
private:
GkOneArgMemberFuncTimer();
private:
T* m_classObj;
void (T::*m_timerFunc)(GkTimer *);
};
/** This class manages a list of running timers and calls a timer function
when the given timer expires. It support various timer object types:
simple timer functions (void and one arg), object member functions (void
and one arg). To make this class working, CheckTimers function has to be
called periodically. It checks the timers and calls timer functions
if it is necessary.
*/
class GkTimerManager
{
public:
/// timer "handle" object
typedef GkTimer* GkTimerHandle;
/// value for an invalid timer handle
const static GkTimerHandle INVALID_HANDLE;
GkTimerManager();
/** Register an one-shot timer that calls a simple void function
on timer expiration.
@return
A handle to the timer or INVALID_HANDLE, if something has failed.
*/
GkTimerHandle RegisterTimer(
void (*timerFunc)(), /// timer function
const PTime & tm /// timer expiration time
);
/** Register a periodic timer that calls a simple void function
on every timer expiration.
@return
A handle to the timer or INVALID_HANDLE, if something has failed.
*/
GkTimerHandle RegisterTimer(
void (*timerFunc)(), /// timer function
const PTime & tm, /// the first expiration time
long interval /// timer interval (seconds)
);
/** Register an one-shot timer that calls an one arg function
on timer expiration. The argument to the function is a pointer
to this timer object.
@return
A handle to the timer or INVALID_HANDLE, if something has failed.
*/
GkTimerHandle RegisterTimer(
void (*timerFunc)(GkTimer*), /// timer function
const PTime & tm /// timer expiration time
);
/** Register a periodic timer that calls an one arg function
on every timer expiration. The argument to the function is a pointer
to this timer object.
@return
A handle to the timer or INVALID_HANDLE, if something has failed.
*/
GkTimerHandle RegisterTimer(
void (*timerFunc)(GkTimer*), /// timer function
const PTime& tm, /// the first timer expiration time
long interval /// timer interval (seconds)
);
/** Register an one-shot timer that calls a simple object member void
function on timer expiration.
@return
A handle to the timer or INVALID_HANDLE, if something has failed.
*/
template<class T>
GkTimerHandle RegisterTimer(
T* obj, /// object of the class T
void (T::*timerFunc)(), /// timer function (a member of the class T)
const PTime& tm /// expiration time
)
{ // it has to be here to compile with VC6
GkTimer* const t = new GkVoidMemberFuncTimer<T>(tm, obj, timerFunc);
PWaitAndSignal lock(m_timersMutex);
m_timers.push_back(t);
return t;
}
/** Register a periodic timer that calls a simple object member void
function on every timer expiration.
@return
A handle to the timer or INVALID_HANDLE, if something has failed.
*/
template<class T>
GkTimerHandle RegisterTimer(
T* obj, /// object of the class T
void (T::*timerFunc)(), /// timer function (a member of the class T)
const PTime& tm, /// the first expiration time
long interval /// timer interval (seconds)
)
{ // it has to be here to compile with VC6
GkTimer* const t = new GkVoidMemberFuncTimer<T>(tm, interval, obj, timerFunc);
PWaitAndSignal lock(m_timersMutex);
m_timers.push_back(t);
return t;
}
/** Register an one-shot timer that calls an object member function
on timer expiration. The member function takes one parameter
which is a pointer to this timer object.
@return
A handle to the timer or INVALID_HANDLE, if something has failed.
*/
template<class T>
GkTimerHandle RegisterTimer(
T* obj, /// object of the class T
void (T::*timerFunc)(GkTimer*), /// timer function (a member of the class T)
const PTime & tm /// timer expiration time
)
{ // it has to be here to compile with VC6
GkTimer* const t = new GkOneArgMemberFuncTimer<T>(tm, obj, timerFunc);
PWaitAndSignal lock(m_timersMutex);
m_timers.push_back(t);
return t;
}
/** Register a periodic timer that calls an object member function
on every timer expiration. The member function takes one parameter
which is a pointer to this timer object.
@return
A handle to the timer or INVALID_HANDLE, if something has failed.
*/
template<class T>
GkTimerHandle RegisterTimer(
T* obj, /// object of the class T
void (T::*timerFunc)(GkTimer*), /// timer function (a member of the class T)
const PTime & tm, /// the first timer expiration time
long interval /// timer interval (seconds)
)
{ // it has to be here to compile with VC6
GkTimer* const t = new GkOneArgMemberFuncTimer<T>(tm, interval, obj, timerFunc);
PWaitAndSignal lock(m_timersMutex);
m_timers.push_back(t);
return t;
}
/** Unregisters (and stops) the timer. After this function completes
it is not valid to reference the timer handle.
@return
True if the timer has been found on the list, false otherwise.
*/
bool UnregisterTimer(
GkTimerHandle timer /// timer handle
);
/** Check timers and call timer functions for timers that have expired.
It is important that this function is called periodically, otherwise
timers will not work.
*/
void CheckTimers();
virtual ~GkTimerManager();
private:
GkTimerManager(const GkTimerManager &);
GkTimerManager& operator=(const GkTimerManager &);
private:
PMutex m_timersMutex; /// mutual access to the timers
std::list<GkTimerHandle> m_timers; /// a list of registered timers
};
#endif /* GKTIMER_H */