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"
25 SGTimerQueue(int preSize=1);
28 void update(double deltaSecs);
30 double now() { return _now; }
32 void insert(SGTimer* timer, double time);
33 SGTimer* remove(SGTimer* timer);
36 SGTimer* nextTimer() { return _numEntries ? _table[0].timer : 0; }
37 double nextTime() { return -_table[0].pri; }
39 SGTimer* findByName(const std::string& name) const;
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; };
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];
59 // gcc complains there is no function specification anywhere.
68 class SGEventMgr : public SGSubsystem
71 SGEventMgr() { _rtProp = 0; }
72 ~SGEventMgr() { _rtProp = 0; }
74 virtual void init() {}
75 virtual void update(double delta_time_sec);
77 void setRealtimeProperty(SGPropertyNode* node) { _rtProp = node; }
80 * Add a single function callback event as a repeating task.
81 * ex: addTask("foo", &Function ... )
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); }
89 * Add a single function callback event as a one-shot event.
90 * ex: addEvent("foo", &Function ... )
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); }
98 * Add a object/method pair as a repeating task.
99 * ex: addTask("foo", &object, &ClassName::Method, ...)
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); }
108 * Add a object/method pair as a repeating task.
109 * ex: addEvent("foo", &object, &ClassName::Method, ...)
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); }
118 void removeTask(const std::string& name);
120 friend class SGTimer;
122 void add(const std::string& name, SGCallback* cb,
123 double interval, double delay,
124 bool repeat, bool simtime);
126 SGPropertyNode_ptr _freezeProp;
127 SGPropertyNode_ptr _rtProp;
128 SGTimerQueue _rtQueue;
129 SGTimerQueue _simQueue;
132 #endif // _SG_EVENT_MGR_HXX