xoreos  0.0.5
timerman.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include "src/common/error.h"
26 
27 #include "src/events/timerman.h"
28 
30 
31 namespace Events {
32 
33 TimerHandle::TimerHandle() : _empty(true) {
34 }
35 
37  TimerMan.removeTimer(*this);
38 }
39 
40 
42 }
43 
45  for (std::list<TimerID>::iterator t = _timers.begin(); t != _timers.end(); ++t)
46  removeTimer(*t);
47 }
48 
50 }
51 
52 void TimerManager::addTimer(uint32 interval, TimerHandle &handle, const TimerFunc &func) {
54 
55  removeTimer(handle);
56 
57  _timers.push_back(TimerID());
58 
59  std::list<TimerID>::iterator id = --_timers.end();
60 
61  handle._iterator = id;
62  handle._empty = false;
63 
64  id->_func = func;
65  id->_id = SDL_AddTimer(interval, &TimerManager::timerCallback, static_cast<void *>(&handle));
66  if (id->_id)
67  return;
68 
69  handle._iterator = _timers.end();
70  handle._empty = true;
71 
72  _timers.erase(id);
73 
74  throw Common::Exception("Failed to add timer");
75 }
76 
79 
80  if (handle._empty)
81  return;
82 
83  removeTimer(*handle._iterator);
84 
85  _timers.erase(handle._iterator);
86 
87  handle._iterator = _timers.end();
88  handle._empty = true;
89 }
90 
92  if (id._id == 0)
93  return;
94 
95  SDL_RemoveTimer(id._id);
96 }
97 
98 uint32 TimerManager::timerCallback(uint32 interval, void *data) {
99  TimerHandle &handle = *static_cast<TimerHandle *>(data);
100 
101  if (handle._empty)
102  return 0;
103 
104  uint32 newInterval = handle._iterator->_func(interval);
105  if (newInterval == 0) {
106  handle._iterator->_id = 0;
107  TimerMan.removeTimer(handle);
108  return 0;
109  }
110 
111  return newInterval;
112 }
113 
114 } // End of namespace Events
static uint32 timerCallback(uint32 interval, void *data)
Definition: timerman.cpp:98
#define TimerMan
Shortcut for accessing the timer manager.
Definition: timerman.h:112
void removeTimer(TimerHandle &handle)
Remove that timer function.
Definition: timerman.cpp:77
The global timer manager.
Definition: timerman.h:59
Basic exceptions to throw.
#define DECLARE_SINGLETON(T)
Note that you need to use this macro from the global namespace.
Definition: singleton.h:122
std::list< TimerID >::iterator _iterator
Definition: timerman.h:104
StackException Exception
Definition: error.h:59
Convenience class that locks a mutex on creation and unlocks it on destruction.
Definition: mutex.h:71
boost::function< uint32(uint32)> TimerFunc
A timer callback function.
Definition: timerman.h:50
std::list< TimerID > _timers
Definition: timerman.h:81
uint32_t uint32
Definition: types.h:204
void addTimer(uint32 interval, TimerHandle &handle, const TimerFunc &func)
Add a function to be called regularly.
Definition: timerman.cpp:52
The global timer manager.
Common::Mutex _mutex
Definition: timerman.h:79