]> Dogcows Code - chaz/yoink/blob - src/moof/timer.hh
the massive refactoring effort
[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 moof {
29
30
31 class timer
32 {
33 public:
34
35 enum mode
36 {
37 invalid = -1,
38 normal = 0,
39 absolute = 1,
40 repeat = 2
41 };
42
43 typedef boost::function<void(timer&,scalar)> function;
44
45
46 timer() :
47 mode_(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, mode mode = normal);
60
61 bool is_valid() const;
62 void invalidate();
63
64 void fire();
65
66 scalar seconds_remaining() const;
67 bool is_expired() const;
68 bool is_repeating() const;
69
70
71 /**
72 * Get the number of seconds since a fixed, arbitrary point in the
73 * past.
74 * \return Seconds.
75 */
76 static scalar ticks();
77
78
79 /**
80 * Put the thread to sleep for a certain period of time. If absolute
81 * is true, then it will sleep until seconds after the fixed time in
82 * the past. If absolute is false, it will sleep for seconds starting
83 * now. Unlike system sleep functions, this one automatically resumes
84 * sleep if sleep was interrupted by a signal. Therefore, calling this
85 * function is guaranteed to sleep for the requested amount of time
86 * (and maybe longer).
87 * \param seconds Number of seconds.
88 * \param mode The timer mode.
89 */
90 static void sleep(scalar seconds, mode mode = normal);
91
92
93 static scalar next_expiration()
94 {
95 return gNextFire;
96 }
97
98 static void fire_expired_timers();
99 static void fire_expired_timers(scalar t);
100
101
102 private:
103
104 static unsigned new_identifier();
105 static scalar find_next_expiration();
106
107 function function_;
108 mode mode_;
109 scalar absolute_;
110 scalar interval_;
111 unsigned id_;
112
113 static scalar gNextFire;
114 static std::map<unsigned,timer*> gTimers;
115 };
116
117
118 } // namespace moof
119
120 #endif // _MOOF_TIMER_HH_
121
This page took 0.038783 seconds and 4 git commands to generate.