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);
29 void update(double deltaSecs);
31 double now() { return _now; }
33 void insert(SGTimer* timer, double time);
34 SGTimer* remove(SGTimer* timer);
37 SGTimer* nextTimer() { return _numEntries ? _table[0].timer : 0; }
38 double nextTime() { return -_table[0].pri; }
40 SGTimer* findByName(const std::string& name) const;
42 // The "priority" is stored as a negative time. This allows the
43 // implementation to treat the "top" of the heap as the largest
44 // value and avoids developer mindbugs. ;)
45 struct HeapEntry { double pri; SGTimer* timer; };
47 int parent(int n) { return ((n+1)/2) - 1; }
48 int lchild(int n) { return ((n+1)*2) - 1; }
49 int rchild(int n) { return ((n+1)*2 + 1) - 1; }
50 double pri(int n) { return _table[n].pri; }
51 void swap(int a, int b) {
52 HeapEntry tmp = _table[a];
53 _table[a] = _table[b];
60 // gcc complains there is no function specification anywhere.
69 class SGEventMgr : public SGSubsystem
75 virtual void init() {}
76 virtual void update(double delta_time_sec);
77 virtual void unbind();
78 virtual void shutdown();
80 void setRealtimeProperty(SGPropertyNode* node) { _rtProp = node; }
83 * Add a single function callback event as a repeating task.
84 * ex: addTask("foo", &Function ... )
86 template<typename FUNC>
87 inline void addTask(const std::string& name, const FUNC& f,
88 double interval, double delay=0, bool sim=false)
89 { add(name, make_callback(f), interval, delay, true, sim); }
92 * Add a single function callback event as a one-shot event.
93 * ex: addEvent("foo", &Function ... )
95 template<typename FUNC>
96 inline void addEvent(const std::string& name, const FUNC& f,
97 double delay, bool sim=false)
98 { add(name, make_callback(f), 0, delay, false, sim); }
101 * Add a object/method pair as a repeating task.
102 * ex: addTask("foo", &object, &ClassName::Method, ...)
104 template<class OBJ, typename METHOD>
105 inline void addTask(const std::string& name,
106 const OBJ& o, METHOD m,
107 double interval, double delay=0, bool sim=false)
108 { add(name, make_callback(o,m), interval, delay, true, sim); }
111 * Add a object/method pair as a repeating task.
112 * ex: addEvent("foo", &object, &ClassName::Method, ...)
114 template<class OBJ, typename METHOD>
115 inline void addEvent(const std::string& name,
116 const OBJ& o, METHOD m,
117 double delay, bool sim=false)
118 { add(name, make_callback(o,m), 0, delay, false, sim); }
121 void removeTask(const std::string& name);
123 friend class SGTimer;
125 void add(const std::string& name, SGCallback* cb,
126 double interval, double delay,
127 bool repeat, bool simtime);
129 SGPropertyNode_ptr _freezeProp;
130 SGPropertyNode_ptr _rtProp;
131 SGTimerQueue _rtQueue;
132 SGTimerQueue _simQueue;
135 #endif // _SG_EVENT_MGR_HXX