xoreos  0.0.5
timerman.h
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 #ifndef EVENTS_TIMERMAN_H
26 #define EVENTS_TIMERMAN_H
27 
28 #include "src/common/fallthrough.h"
30 #include <SDL_timer.h>
32 
33 #include <list>
34 
35 #include <boost/function.hpp>
36 
37 #include "src/common/types.h"
38 #include "src/common/singleton.h"
39 #include "src/common/mutex.h"
40 
41 #include "src/events/types.h"
42 
43 namespace Events {
44 
50 typedef boost::function<uint32 (uint32)> TimerFunc;
51 
52 class TimerID;
53 class TimerHandle;
54 
59 class TimerManager : public Common::Singleton<TimerManager> {
60 public:
61  TimerManager();
62  ~TimerManager();
63 
64  void init();
65 
73  void addTimer(uint32 interval, TimerHandle &handle, const TimerFunc &func);
74 
76  void removeTimer(TimerHandle &handle);
77 
78 private:
80 
81  std::list<TimerID> _timers;
82 
83  void removeTimer(TimerID &id);
84 
85  static uint32 timerCallback(uint32 interval, void *data);
86 };
87 
88 class TimerID {
89 private:
90  SDL_TimerID _id;
92 
93  friend class TimerManager;
94 };
95 
96 class TimerHandle {
97 public:
98  TimerHandle();
99  ~TimerHandle();
100 
101 private:
102  bool _empty;
103 
104  std::list<TimerID>::iterator _iterator;
105 
106  friend class TimerManager;
107 };
108 
109 } // End of namespace Events
110 
112 #define TimerMan Events::TimerManager::instance()
113 
114 #endif // EVENTS_TIMERMAN_H
Basic event types.
TimerFunc _func
Definition: timerman.h:91
Class and macro for implementing singletons.
#define START_IGNORE_IMPLICIT_FALLTHROUGH
Definition: fallthrough.h:79
static uint32 timerCallback(uint32 interval, void *data)
Definition: timerman.cpp:98
void removeTimer(TimerHandle &handle)
Remove that timer function.
Definition: timerman.cpp:77
The global timer manager.
Definition: timerman.h:59
Generic template base class for implementing the singleton design pattern.
Definition: singleton.h:61
A mutex.
Definition: mutex.h:40
std::list< TimerID >::iterator _iterator
Definition: timerman.h:104
Low-level type definitions to handle fixed width types portably.
SDL_TimerID _id
Definition: timerman.h:90
#define STOP_IGNORE_IMPLICIT_FALLTHROUGH
Definition: fallthrough.h:80
Thread mutex classes.
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
Compiler-specific defines to mark an implicit switch-case fallthrough.
Common::Mutex _mutex
Definition: timerman.h:79