4 * Created on: Aug 10, 2012
19 TimerData() : time(0), target(0), refCount(0) { }
20 TimerData(Time target) : time(0), target(target), refCount(0) { }
34 ~Timer() { if (data) --data->refCount; }
35 Timer(TimerData<Time> *data) : data(data) { ++data->refCount; }
36 Timer(const Timer<Time> &other)
37 : data(other.data) { if (data) ++(data->refCount); }
38 Timer<Time> &operator =(const Timer<Time> &other) {
39 Timer<Time> temp(other);
43 void Swap(Timer<Time> &other) {
44 std::swap(data, other.data);
48 bool Running() const {
51 bool Finished() const {
52 return data ? data->time >= data->target : false;
54 Time Elapsed() const {
55 return data ? data->time : Time();
57 Time Remaining() const {
58 return data ? (data->target - data->time) : Time();
62 TimerData<Time> *data;
74 void Update(Time delta) {
75 for (typename std::list<TimerData<Time> >::iterator i(data.begin()), end(data.end()); i != end;) {
77 if (i->refCount <= 0) {
84 Timer<Time> StartTimer() {
85 data.push_back(TimerData<Time>());
86 return Timer<Time>(&data.back());
88 Timer<Time> StartCountdown(Time duration) {
89 data.push_back(TimerData<Time>(duration));
90 return Timer<Time>(&data.back());
94 std::list<TimerData<Time> > data;
100 #endif /* APP_TIMER_H_ */