]> Dogcows Code - chaz/yoink/blob - src/Moof/Timer.hh
reformatting
[chaz/yoink] / src / Moof / Timer.hh
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #ifndef _MOOF_TIMER_HH_
13 #define _MOOF_TIMER_HH_
14
15 /**
16 * @file Timer.hh
17 * Functions for measuring time in a friendly unit.
18 */
19
20 #include <map>
21
22 #include <boost/bind.hpp>
23 #include <boost/function.hpp>
24
25 #include <Moof/Math.hh>
26
27
28 namespace Mf {
29
30
31 class Timer
32 {
33 public:
34
35 enum Mode
36 {
37 INVALID = -1,
38 NORMAL = 0,
39 ACTUAL = 1,
40 REPEAT = 2
41 };
42
43 typedef boost::function<void(Timer&,Scalar)> Function;
44
45
46 Timer() :
47 mMode(INVALID) {}
48
49 Timer(const Function& function, Scalar seconds, Mode mode = NORMAL)
50 {
51 init(function, seconds, mode);
52 }
53
54 ~Timer()
55 {
56 invalidate();
57 }
58
59 void init(const Function& function, Scalar seconds,
60 Mode mode = NORMAL);
61
62 bool isValid() const;
63 void invalidate();
64
65 void fire();
66
67 Scalar getSecondsRemaining() const;
68 bool isExpired() const;
69 bool isRepeating() const;
70
71
72 /**
73 * Get the number of seconds since a fixed, arbitrary point in the
74 * past.
75 * @return Seconds.
76 */
77
78 static Scalar getTicks();
79
80
81 /**
82 * Put the thread to sleep for a certain period of time. If absolute
83 * is true, then it will sleep until seconds after the fixed time in
84 * the past. If absolute is false, it will sleep for seconds starting
85 * now. Unlike system sleep functions, this one automatically resumes
86 * sleep if sleep was interrupted by a signal. Therefore, calling this
87 * function is guaranteed to sleep for the requested amount of time
88 * (and maybe longer).
89 */
90
91 static void sleep(Scalar seconds, Mode mode = NORMAL);
92
93
94 static Scalar getNextFire()
95 {
96 return gNextFire;
97 }
98
99 static void fireIfExpired();
100 static void fireIfExpired(Scalar t);
101
102 private:
103
104 static unsigned getNewID();
105 static Scalar findNextFire();
106
107 Function mFunction;
108 Mode mMode;
109 Scalar mAbsolute;
110 Scalar mInterval;
111 unsigned mId;
112
113 static Scalar gNextFire;
114 static std::map<unsigned,Timer*> gTimers;
115 };
116
117
118 } // namespace Mf
119
120 #endif // _MOOF_TIMER_HH_
121
This page took 0.03321 seconds and 4 git commands to generate.