]> Dogcows Code - chaz/carfire/blob - CarFire/CarFire/CarFire/Timer.cs
git-svn-id: https://bd85.net/svn/cs3505_group@168 92bb83a3-7c8f-8a45-bc97-515c4e399668
[chaz/carfire] / CarFire / CarFire / CarFire / Timer.cs
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace CarFire
7 {
8 /// <summary>
9 /// A simple timer class. I'm not sure why .NET or at least
10 /// XNA doesn't already have one of these.
11 /// </summary>
12 public class Timer<T> : IComparable<Timer<T>> where T : IComparable<T>
13 {
14 #region Public Properties
15
16 /// <summary>
17 /// Get and set the expiration time. If the time is already set
18 /// and the timer is registered, you cannot push the expiration time
19 /// further back, but you can decrease it.
20 /// </summary>
21 public T Time
22 {
23 get
24 {
25 return mTime;
26 }
27 set
28 {
29 if (mTime.CompareTo(value) > 0)
30 {
31 mTime = value;
32 gTimers.Promote(this);
33 }
34 }
35 }
36
37 /// <summary>
38 /// Get the current absolute time, accurate up until
39 /// the last time Timer.FireAll was called.
40 /// </summary>
41 public static T Now { get { return gNow; } }
42
43 /// <summary>
44 /// Get and set the events that will be called when the
45 /// timer expires.
46 /// </summary>
47 public event ExpiredEventHandler Expired;
48
49 /// <summary>
50 /// Get and set a user contextual object.
51 /// </summary>
52 public object Context;
53
54 #endregion
55
56
57 #region Public Types
58
59 /// <summary>
60 /// The type for an event handler for timers that expire.
61 /// </summary>
62 /// <param name="timer">The timer that expired.</param>
63 public delegate void ExpiredEventHandler(Timer<T> timer);
64
65 #endregion
66
67
68 #region Public Methods
69
70 /// <summary>
71 /// Construct a timer with an absolute time (in seconds)
72 /// when the timer should expire.
73 /// </summary>
74 /// <param name="time">Absolute time, in seconds.</param>
75 public Timer(T time)
76 {
77 mTime = time;
78 gTimers.Add(this);
79 }
80
81 /// <summary>
82 /// Construct a timer with an absolute time (in seconds)
83 /// when the timer should expire, and an event handler.
84 /// </summary>
85 /// <param name="time">Absolute time, in seconds.</param>
86 /// <param name="expired">An event handler.</param>
87 public Timer(T time, ExpiredEventHandler expired)
88 {
89 mTime = time;
90 Expired += expired;
91 gTimers.Add(this);
92 }
93
94 /// <summary>
95 /// Construct a timer with an absolute time (in seconds)
96 /// when the timer should expire, and a contextual object.
97 /// </summary>
98 /// <param name="time">Absolute time, in seconds.</param>
99 /// <param name="context">A user contextual object.</param>
100 public Timer(T time, object context)
101 {
102 mTime = time;
103 Context = context;
104 gTimers.Add(this);
105 }
106
107 /// <summary>
108 /// Construct a timer with an absolute time (in seconds)
109 /// when the timer should expire, an event handler, and a
110 /// contextual object.
111 /// </summary>
112 /// <param name="time">Absolute time, in seconds.</param>
113 /// <param name="expired">An event handler.</param>
114 /// <param name="context">A user contextual object.</param>
115 public Timer(T time, ExpiredEventHandler expired, object context)
116 {
117 mTime = time;
118 Expired += expired;
119 Context = context;
120 gTimers.Add(this);
121 }
122
123
124 /// <summary>
125 /// Fire all outstanding timers which should be expired
126 /// before a certain time.
127 /// </summary>
128 /// <param name="time">Absolute time, in seconds.</param>
129 public static void FireExpired(T time)
130 {
131 gNow = time;
132
133 while (gTimers.Count > 0)
134 {
135 Timer<T> timer = gTimers.Peek();
136 if (timer.Time.CompareTo(Now) <= 0)
137 {
138 if (timer.Expired != null) timer.Expired(timer);
139 if (timer.Time.CompareTo(Now) <= 0) gTimers.GetNext();
140 }
141 else
142 {
143 break;
144 }
145 }
146 }
147
148
149 /// <summary>
150 /// Unregister all registered timers without firing any events.
151 /// </summary>
152 public static void Clear()
153 {
154 gTimers.Clear();
155 }
156
157
158 /// <summary>
159 /// Compare a timer with another, based on the expiration
160 /// times of both timers.
161 /// </summary>
162 /// <param name="other">The timer to compare against.</param>
163 /// <returns>A negative value if this timer is less than the
164 /// other, a positive value if this timer is greater than the
165 /// other, or zero if they are the same.</returns>
166 public int CompareTo(Timer<T> other)
167 {
168 return Time.CompareTo(other.Time);
169 }
170
171 #endregion
172
173
174 #region Private Variables
175
176 T mTime;
177
178 static IPriorityQueue<Timer<T>> gTimers = new BinaryHeap<Timer<T>>(10);
179 static T gNow;
180
181 #endregion
182 }
183 }
This page took 0.038158 seconds and 4 git commands to generate.