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
74 virtual void init() {}
75 virtual void update(double delta_time_sec);
76 virtual void unbind();
78 void setRealtimeProperty(SGPropertyNode* node) { _rtProp = node; }
81 * Add a single function callback event as a repeating task.
82 * ex: addTask("foo", &Function ... )
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); }
90 * Add a single function callback event as a one-shot event.
91 * ex: addEvent("foo", &Function ... )
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); }
99 * Add a object/method pair as a repeating task.
100 * ex: addTask("foo", &object, &ClassName::Method, ...)
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); }
109 * Add a object/method pair as a repeating task.
110 * ex: addEvent("foo", &object, &ClassName::Method, ...)
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); }
119 void removeTask(const std::string& name);
121 friend class SGTimer;
123 void add(const std::string& name, SGCallback* cb,
124 double interval, double delay,
125 bool repeat, bool simtime);
127 SGPropertyNode_ptr _freezeProp;
128 SGPropertyNode_ptr _rtProp;
129 SGTimerQueue _rtQueue;
130 SGTimerQueue _simQueue;
133 #endif // _SG_EVENT_MGR_HXX