]> git.mxchange.org Git - simgear.git/blob - simgear/structure/event_mgr.hxx
Reset: event manager can be unbound.
[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     virtual void unbind();
77     
78     void setRealtimeProperty(SGPropertyNode* node) { _rtProp = node; }
79
80     /**
81      * Add a single function callback event as a repeating task.
82      * ex: addTask("foo", &Function ... )
83      */
84     template<typename FUNC>
85     inline void addTask(const std::string& name, const FUNC& f,
86                         double interval, double delay=0, bool sim=false)
87     { add(name, make_callback(f), interval, delay, true, sim); }
88
89     /**
90      * Add a single function callback event as a one-shot event.
91      * ex: addEvent("foo", &Function ... )
92      */
93     template<typename FUNC>
94     inline void addEvent(const std::string& name, const FUNC& f,
95                          double delay, bool sim=false)
96     { add(name, make_callback(f), 0, delay, false, sim); }
97
98     /**
99      * Add a object/method pair as a repeating task.
100      * ex: addTask("foo", &object, &ClassName::Method, ...)
101      */
102     template<class OBJ, typename METHOD>
103     inline void addTask(const std::string& name,
104                         const OBJ& o, METHOD m,
105                         double interval, double delay=0, bool sim=false)
106     { add(name, make_callback(o,m), interval, delay, true, sim); }
107
108     /**
109      * Add a object/method pair as a repeating task.
110      * ex: addEvent("foo", &object, &ClassName::Method, ...)
111      */
112     template<class OBJ, typename METHOD>
113     inline void addEvent(const std::string& name,
114                          const OBJ& o, METHOD m,
115                          double delay, bool sim=false)
116     { add(name, make_callback(o,m), 0, delay, false, sim); }
117
118
119     void removeTask(const std::string& name);
120 private:
121     friend class SGTimer;
122
123     void add(const std::string& name, SGCallback* cb,
124              double interval, double delay,
125              bool repeat, bool simtime);
126
127     SGPropertyNode_ptr _freezeProp;
128     SGPropertyNode_ptr _rtProp;
129     SGTimerQueue _rtQueue; 
130     SGTimerQueue _simQueue;
131 };
132
133 #endif // _SG_EVENT_MGR_HXX