10 /// Stores timing information.
11 /// For use by app::Timer.
15 TimerData() : time(0), target(0), refCount(0), justHit(false), repeat(false) { }
16 TimerData(Time target, bool repeat) : time(0), target(target), refCount(0), justHit(false), repeat(repeat) { }
28 /// How the various information returned by the const member functions is to be
29 /// interpreted highly depends on how the timer was created (by app::Timers).
35 ~Timer() { if (data) --data->refCount; }
36 Timer(TimerData<Time> *data) : data(data) { ++data->refCount; }
37 Timer(const Timer<Time> &other)
38 : data(other.data) { if (data) ++(data->refCount); }
39 Timer<Time> &operator =(const Timer<Time> &other) {
40 Timer<Time> temp(other);
44 void Swap(Timer<Time> &other) {
45 std::swap(data, other.data);
49 /// Check if the timer was started (and not cleared) yet.
50 bool Started() const {
53 /// Check if the timer has reached its target time (only sensible for
54 /// countdown timers).
55 bool Finished() const {
56 return data && data->target != Time() && !data->repeat && data->time >= data->target;
58 /// Check if the timer was started and has not finished yet (in case it's a
60 bool Running() const {
61 return data && !Finished();
63 /// Get the elapsed time since the timer started.
64 Time Elapsed() const {
65 return data ? data->time : Time();
67 /// Get the time remaining for countdowns.
68 Time Remaining() const {
69 return data ? (data->target - data->time) : Time();
71 /// Get the iteration index for interval timers.
72 int Iteration() const {
73 return (data && data->target > Time()) ? std::floor(data->time / data->target) : Time();
75 /// Check if the timer reached its interval or countdown goal this iteration.
76 bool JustHit() const {
77 return data && data->justHit;
80 /// Unset the timer (does not stop other handles for a shared timer).
87 /// Reset the timer, do not stop it if it's running.
89 if (data) data->time = Time();
91 /// Restart the timer.
92 /// Only works if the timer was started at least once.
95 if (data->target > Time() && data->justHit) {
96 data->time -= data->target;
104 TimerData<Time> *data;
109 /// Tracker for timers, responsible for creating and updating them.
117 /// Move all timers forward by delta.
118 void Update(Time delta) {
119 for (typename std::list<TimerData<Time> >::iterator i(data.begin()), end(data.end()); i != end;) {
121 Time intervalTime(i->time);
122 if (i->repeat) while (intervalTime > i->target) intervalTime -= i->target;
123 i->justHit = intervalTime < i->target && intervalTime + delta >= i->target;
126 if (i->refCount <= 0) {
133 /// Start a timer that counts elapsed time until stopped manually.
134 Timer<Time> StartTimer() {
135 data.push_back(TimerData<Time>());
136 return Timer<Time>(&data.back());
138 /// Start a countdown that hits (JustHit() returing true) when duration
140 Timer<Time> StartCountdown(Time duration) {
141 data.push_back(TimerData<Time>(duration, false));
142 return Timer<Time>(&data.back());
144 /// Start an interval timer that hits (JustHit() returning true) each time
145 /// duration has passed.
146 Timer<Time> StartInterval(Time duration) {
147 data.push_back(TimerData<Time>(duration, true));
148 return Timer<Time>(&data.back());
152 std::list<TimerData<Time> > data;
158 #endif /* APP_TIMER_H_ */