4 * Created on: Aug 10, 2012
20 TimerData() : time(0), target(0), refCount(0), justHit(false) { }
21 TimerData(Time target) : time(0), target(target), refCount(0), justHit(false) { }
36 ~Timer() { if (data) --data->refCount; }
37 Timer(TimerData<Time> *data) : data(data) { ++data->refCount; }
38 Timer(const Timer<Time> &other)
39 : data(other.data) { if (data) ++(data->refCount); }
40 Timer<Time> &operator =(const Timer<Time> &other) {
41 Timer<Time> temp(other);
45 void Swap(Timer<Time> &other) {
46 std::swap(data, other.data);
50 bool Running() const {
53 bool Finished() const {
54 return data ? data->time >= data->target : false;
56 Time Elapsed() const {
57 return data ? data->time : Time();
59 Time Remaining() const {
60 return data ? (data->target - data->time) : Time();
62 int Iteration() const {
63 return (data && data->target > 0) ? std::floor(data->time / data->target) : 0;
65 bool JustHit() const {
66 return data && data->justHit;
70 if (data) data->time = 0;
74 if (data->target > 0 && data->justHit) {
75 data->time -= data->target;
83 TimerData<Time> *data;
95 void Update(Time delta) {
96 for (typename std::list<TimerData<Time> >::iterator i(data.begin()), end(data.end()); i != end;) {
97 TimerData<Time> &t(*i);
99 Time intervalTime(i->time);
100 while (intervalTime > i->target) intervalTime -= i->target;
101 i->justHit = intervalTime < i->target && intervalTime + delta >= i->target;
104 if (i->refCount <= 0) {
111 Timer<Time> StartTimer() {
112 data.push_back(TimerData<Time>());
113 return Timer<Time>(&data.back());
115 Timer<Time> StartCountdown(Time duration) {
116 data.push_back(TimerData<Time>(duration));
117 return Timer<Time>(&data.back());
119 Timer<Time> StartInterval(Time duration) {
120 data.push_back(TimerData<Time>(duration));
121 return Timer<Time>(&data.back());
125 std::list<TimerData<Time> > data;
131 #endif /* APP_TIMER_H_ */