1 #ifndef _SG_EVENT_MGR_HXX
2 #define _SG_EVENT_MGR_HXX
4 #include <simgear/props/props.hxx>
5 #include <simgear/structure/subsystem_mgr.hxx>
7 #include "callback.hxx"
22 SGTimerQueue(int preSize=1);
25 void update(double deltaSecs);
27 double now() { return _now; }
29 void insert(SGTimer* timer, double time);
30 SGTimer* remove(SGTimer* timer);
33 SGTimer* nextTimer() { return _numEntries ? _table[0].timer : 0; }
34 double nextTime() { return -_table[0].pri; }
37 // The "priority" is stored as a negative time. This allows the
38 // implemenetation to treat the "top" of the heap as the largest
39 // value and avoids developer mindbugs. ;)
40 struct HeapEntry { double pri; SGTimer* timer; };
42 int parent(int n) { return ((n+1)/2) - 1; }
43 int lchild(int n) { return ((n+1)*2) - 1; }
44 int rchild(int n) { return ((n+1)*2 + 1) - 1; }
45 double pri(int n) { return _table[n].pri; }
46 void swap(int a, int b) {
47 HeapEntry tmp = _table[a];
48 _table[a] = _table[b];
62 class SGEventMgr : public SGSubsystem
65 SGEventMgr() { _freezeProp = 0; }
66 ~SGEventMgr() { _freezeProp = 0; }
68 virtual void init() {}
69 virtual void update(double delta_time_sec);
71 void setFreezeProperty(SGPropertyNode* node) { _freezeProp = node; }
74 * Add a single function callback event as a repeating task.
75 * ex: addTask("foo", &Function ... )
77 template<typename FUNC>
78 inline void addTask(const char* name, const FUNC& f,
79 double interval, double delay=0, bool sim=false)
80 { add(make_callback(f), interval, delay, true, sim); }
83 * Add a single function callback event as a one-shot event.
84 * ex: addEvent("foo", &Function ... )
86 template<typename FUNC>
87 inline void addEvent(const char* name, const FUNC& f,
88 double delay, bool sim=false)
89 { add(make_callback(f), 0, delay, false, sim); }
92 * Add a object/method pair as a repeating task.
93 * ex: addTask("foo", &object, &ClassName::Method, ...)
95 template<class OBJ, typename METHOD>
96 inline void addTask(const char* name,
97 const OBJ& o, METHOD m,
98 double interval, double delay=0, bool sim=false)
99 { add(make_callback(o,m), interval, delay, true, sim); }
102 * Add a object/method pair as a repeating task.
103 * ex: addEvent("foo", &object, &ClassName::Method, ...)
105 template<class OBJ, typename METHOD>
106 inline void addEvent(const char* name,
107 const OBJ& o, METHOD m,
108 double delay, bool sim=false)
109 { add(make_callback(o,m), 0, delay, false, sim); }
112 friend struct SGTimer;
114 void add(SGCallback* cb,
115 double interval, double delay,
116 bool repeat, bool simtime);
118 SGPropertyNode* _freezeProp;
119 SGTimerQueue _rtQueue;
120 SGTimerQueue _simQueue;
123 #endif // _SG_EVENT_MGR_HXX