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"
23 SGTimerQueue(int preSize=1);
26 void update(double deltaSecs);
28 double now() { return _now; }
30 void insert(SGTimer* timer, double time);
31 SGTimer* remove(SGTimer* timer);
34 SGTimer* nextTimer() { return _numEntries ? _table[0].timer : 0; }
35 double nextTime() { return -_table[0].pri; }
37 SGTimer* findByName(const std::string& name) const;
39 // The "priority" is stored as a negative time. This allows the
40 // implemenetation to treat the "top" of the heap as the largest
41 // value and avoids developer mindbugs. ;)
42 struct HeapEntry { double pri; SGTimer* timer; };
44 int parent(int n) { return ((n+1)/2) - 1; }
45 int lchild(int n) { return ((n+1)*2) - 1; }
46 int rchild(int n) { return ((n+1)*2 + 1) - 1; }
47 double pri(int n) { return _table[n].pri; }
48 void swap(int a, int b) {
49 HeapEntry tmp = _table[a];
50 _table[a] = _table[b];
57 // gcc complains there is no function specification anywhere.
66 class SGEventMgr : public SGSubsystem
69 SGEventMgr() { _rtProp = 0; }
70 ~SGEventMgr() { _rtProp = 0; }
72 virtual void init() {}
73 virtual void update(double delta_time_sec);
75 void setRealtimeProperty(SGPropertyNode* node) { _rtProp = node; }
78 * Add a single function callback event as a repeating task.
79 * ex: addTask("foo", &Function ... )
81 template<typename FUNC>
82 inline void addTask(const std::string& name, const FUNC& f,
83 double interval, double delay=0, bool sim=false)
84 { add(name, make_callback(f), interval, delay, true, sim); }
87 * Add a single function callback event as a one-shot event.
88 * ex: addEvent("foo", &Function ... )
90 template<typename FUNC>
91 inline void addEvent(const std::string& name, const FUNC& f,
92 double delay, bool sim=false)
93 { add(name, make_callback(f), 0, delay, false, sim); }
96 * Add a object/method pair as a repeating task.
97 * ex: addTask("foo", &object, &ClassName::Method, ...)
99 template<class OBJ, typename METHOD>
100 inline void addTask(const std::string& name,
101 const OBJ& o, METHOD m,
102 double interval, double delay=0, bool sim=false)
103 { add(name, make_callback(o,m), interval, delay, true, sim); }
106 * Add a object/method pair as a repeating task.
107 * ex: addEvent("foo", &object, &ClassName::Method, ...)
109 template<class OBJ, typename METHOD>
110 inline void addEvent(const std::string& name,
111 const OBJ& o, METHOD m,
112 double delay, bool sim=false)
113 { add(name, make_callback(o,m), 0, delay, false, sim); }
116 void removeTask(const std::string& name);
118 friend struct SGTimer;
120 void add(const std::string& name, SGCallback* cb,
121 double interval, double delay,
122 bool repeat, bool simtime);
124 SGPropertyNode_ptr _freezeProp;
125 SGPropertyNode_ptr _rtProp;
126 SGTimerQueue _rtQueue;
127 SGTimerQueue _simQueue;
130 #endif // _SG_EVENT_MGR_HXX