]> git.mxchange.org Git - simgear.git/blob - simgear/structure/event_mgr.hxx
Csaba Halasz: fix SGTimerQueue so tasks can remove themselves properly
[simgear.git] / simgear / structure / event_mgr.hxx
1 #ifndef _SG_EVENT_MGR_HXX
2 #define _SG_EVENT_MGR_HXX
3
4 #include <simgear/props/props.hxx>
5 #include <simgear/structure/subsystem_mgr.hxx>
6
7 #include "callback.hxx"
8
9 class SGEventMgr;
10
11 class SGTimer {
12 public:
13     ~SGTimer();
14     void run();
15     
16     std::string name;
17     double interval;
18     SGCallback* callback;
19     bool repeat;
20     bool running;
21 };
22
23 class SGTimerQueue {
24 public:
25     SGTimerQueue(int preSize=1);
26     ~SGTimerQueue();
27
28     void update(double deltaSecs);
29
30     double now() { return _now; }
31
32     void     insert(SGTimer* timer, double time);
33     SGTimer* remove(SGTimer* timer);
34     SGTimer* remove();
35
36     SGTimer* nextTimer() { return _numEntries ? _table[0].timer : 0; }
37     double   nextTime()  { return -_table[0].pri; }
38
39     SGTimer* findByName(const std::string& name) const;
40 private:
41     // The "priority" is stored as a negative time.  This allows the
42     // implementation to treat the "top" of the heap as the largest
43     // value and avoids developer mindbugs. ;)
44     struct HeapEntry { double pri; SGTimer* timer; };
45
46     int parent(int n) { return ((n+1)/2) - 1; }
47     int lchild(int n) { return ((n+1)*2) - 1; }
48     int rchild(int n) { return ((n+1)*2 + 1) - 1; }
49     double pri(int n) { return _table[n].pri; }
50     void swap(int a, int b) {
51         HeapEntry tmp = _table[a];
52         _table[a] = _table[b];
53         _table[b] = tmp;
54     }
55     void siftDown(int n);
56     void siftUp(int n);
57     void growArray();
58
59     // gcc complains there is no function specification anywhere.
60     // void check();
61
62     double _now;
63     HeapEntry *_table;
64     int _numEntries;
65     int _tableSize;
66 };
67
68 class SGEventMgr : public SGSubsystem
69 {
70 public:
71     SGEventMgr() { _rtProp = 0; }
72     ~SGEventMgr() { _rtProp = 0; }
73
74     virtual void init() {}
75     virtual void update(double delta_time_sec);
76
77     void setRealtimeProperty(SGPropertyNode* node) { _rtProp = node; }
78
79     /**
80      * Add a single function callback event as a repeating task.
81      * ex: addTask("foo", &Function ... )
82      */
83     template<typename FUNC>
84     inline void addTask(const std::string& name, const FUNC& f,
85                         double interval, double delay=0, bool sim=false)
86     { add(name, make_callback(f), interval, delay, true, sim); }
87
88     /**
89      * Add a single function callback event as a one-shot event.
90      * ex: addEvent("foo", &Function ... )
91      */
92     template<typename FUNC>
93     inline void addEvent(const std::string& name, const FUNC& f,
94                          double delay, bool sim=false)
95     { add(name, make_callback(f), 0, delay, false, sim); }
96
97     /**
98      * Add a object/method pair as a repeating task.
99      * ex: addTask("foo", &object, &ClassName::Method, ...)
100      */
101     template<class OBJ, typename METHOD>
102     inline void addTask(const std::string& name,
103                         const OBJ& o, METHOD m,
104                         double interval, double delay=0, bool sim=false)
105     { add(name, make_callback(o,m), interval, delay, true, sim); }
106
107     /**
108      * Add a object/method pair as a repeating task.
109      * ex: addEvent("foo", &object, &ClassName::Method, ...)
110      */
111     template<class OBJ, typename METHOD>
112     inline void addEvent(const std::string& name,
113                          const OBJ& o, METHOD m,
114                          double delay, bool sim=false)
115     { add(name, make_callback(o,m), 0, delay, false, sim); }
116
117
118     void removeTask(const std::string& name);
119 private:
120     friend class SGTimer;
121
122     void add(const std::string& name, SGCallback* cb,
123              double interval, double delay,
124              bool repeat, bool simtime);
125
126     SGPropertyNode_ptr _freezeProp;
127     SGPropertyNode_ptr _rtProp;
128     SGTimerQueue _rtQueue; 
129     SGTimerQueue _simQueue;
130 };
131
132 #endif // _SG_EVENT_MGR_HXX