4 * Created on: Aug 10, 2012
17 /// Stores timing information.
18 /// For use by app::Timer.
22 TimerData() : time(0), target(0), refCount(0), justHit(false), repeat(false) { }
23 TimerData(Time target, bool repeat) : time(0), target(target), refCount(0), justHit(false), repeat(repeat) { }
35 /// How the various information returned by the const member functions is to be
36 /// interpreted highly depends on how the timer was created (by app::Timers).
42 ~Timer() { if (data) --data->refCount; }
43 Timer(TimerData<Time> *data) : data(data) { ++data->refCount; }
44 Timer(const Timer<Time> &other)
45 : data(other.data) { if (data) ++(data->refCount); }
46 Timer<Time> &operator =(const Timer<Time> &other) {
47 Timer<Time> temp(other);
51 void Swap(Timer<Time> &other) {
52 std::swap(data, other.data);
56 /// Check if the timer was started (and not cleared) yet.
57 bool Started() const {
60 /// Check if the timer has reached its target time (only sensible for
61 /// countdown timers).
62 bool Finished() const {
63 return data && data->target != Time() && !data->repeat && data->time >= data->target;
65 /// Check if the timer was started and has not finished yet (in case it's a
67 bool Running() const {
68 return data && !Finished();
70 /// Get the elapsed time since the timer started.
71 Time Elapsed() const {
72 return data ? data->time : Time();
74 /// Get the time remaining for countdowns.
75 Time Remaining() const {
76 return data ? (data->target - data->time) : Time();
78 /// Get the iteration index for interval timers.
79 int Iteration() const {
80 return (data && data->target > Time()) ? std::floor(data->time / data->target) : Time();
82 /// Check if the timer reached its interval or countdown goal this iteration.
83 bool JustHit() const {
84 return data && data->justHit;
87 /// Unset the timer (does not stop other handles for a shared timer).
94 /// Reset the timer, do not stop it if it's running.
96 if (data) data->time = Time();
98 /// Restart the timer.
99 /// Only works if the timer was started at least once.
102 if (data->target > Time() && data->justHit) {
103 data->time -= data->target;
111 TimerData<Time> *data;
116 /// Tracker for timers, responsible for creating and updating them.
124 /// Move all timers forward by delta.
125 void Update(Time delta) {
126 for (typename std::list<TimerData<Time> >::iterator i(data.begin()), end(data.end()); i != end;) {
128 Time intervalTime(i->time);
129 if (i->repeat) while (intervalTime > i->target) intervalTime -= i->target;
130 i->justHit = intervalTime < i->target && intervalTime + delta >= i->target;
133 if (i->refCount <= 0) {
140 /// Start a timer that counts elapsed time until stopped manually.
141 Timer<Time> StartTimer() {
142 data.push_back(TimerData<Time>());
143 return Timer<Time>(&data.back());
145 /// Start a countdown that hits (JustHit() returing true) when duration
147 Timer<Time> StartCountdown(Time duration) {
148 data.push_back(TimerData<Time>(duration, false));
149 return Timer<Time>(&data.back());
151 /// Start an interval timer that hits (JustHit() returning true) each time
152 /// duration has passed.
153 Timer<Time> StartInterval(Time duration) {
154 data.push_back(TimerData<Time>(duration, true));
155 return Timer<Time>(&data.back());
159 std::list<TimerData<Time> > data;
165 #endif /* APP_TIMER_H_ */