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"
24 SGTimerQueue(int preSize=1);
27 void update(double deltaSecs);
29 double now() { return _now; }
31 void insert(SGTimer* timer, double time);
32 SGTimer* remove(SGTimer* timer);
35 SGTimer* nextTimer() { return _numEntries ? _table[0].timer : 0; }
36 double nextTime() { return -_table[0].pri; }
38 SGTimer* findByName(const std::string& name) const;
40 // The "priority" is stored as a negative time. This allows the
41 // implementation to treat the "top" of the heap as the largest
42 // value and avoids developer mindbugs. ;)
43 struct HeapEntry { double pri; SGTimer* timer; };
45 int parent(int n) { return ((n+1)/2) - 1; }
46 int lchild(int n) { return ((n+1)*2) - 1; }
47 int rchild(int n) { return ((n+1)*2 + 1) - 1; }
48 double pri(int n) { return _table[n].pri; }
49 void swap(int a, int b) {
50 HeapEntry tmp = _table[a];
51 _table[a] = _table[b];
58 // gcc complains there is no function specification anywhere.
67 class SGEventMgr : public SGSubsystem
70 SGEventMgr() { _rtProp = 0; }
71 ~SGEventMgr() { _rtProp = 0; }
73 virtual void init() {}
74 virtual void update(double delta_time_sec);
76 void setRealtimeProperty(SGPropertyNode* node) { _rtProp = node; }
79 * Add a single function callback event as a repeating task.
80 * ex: addTask("foo", &Function ... )
82 template<typename FUNC>
83 inline void addTask(const std::string& name, const FUNC& f,
84 double interval, double delay=0, bool sim=false)
85 { add(name, make_callback(f), interval, delay, true, sim); }
88 * Add a single function callback event as a one-shot event.
89 * ex: addEvent("foo", &Function ... )
91 template<typename FUNC>
92 inline void addEvent(const std::string& name, const FUNC& f,
93 double delay, bool sim=false)
94 { add(name, make_callback(f), 0, delay, false, sim); }
97 * Add a object/method pair as a repeating task.
98 * ex: addTask("foo", &object, &ClassName::Method, ...)
100 template<class OBJ, typename METHOD>
101 inline void addTask(const std::string& name,
102 const OBJ& o, METHOD m,
103 double interval, double delay=0, bool sim=false)
104 { add(name, make_callback(o,m), interval, delay, true, sim); }
107 * Add a object/method pair as a repeating task.
108 * ex: addEvent("foo", &object, &ClassName::Method, ...)
110 template<class OBJ, typename METHOD>
111 inline void addEvent(const std::string& name,
112 const OBJ& o, METHOD m,
113 double delay, bool sim=false)
114 { add(name, make_callback(o,m), 0, delay, false, sim); }
117 void removeTask(const std::string& name);
119 friend class SGTimer;
121 void add(const std::string& name, SGCallback* cb,
122 double interval, double delay,
123 bool repeat, bool simtime);
125 SGPropertyNode_ptr _freezeProp;
126 SGPropertyNode_ptr _rtProp;
127 SGTimerQueue _rtQueue;
128 SGTimerQueue _simQueue;
131 #endif // _SG_EVENT_MGR_HXX