]> git.mxchange.org Git - simgear.git/blob - simgear/structure/event_mgr.hxx
Remove SVN sync code.
[simgear.git] / simgear / structure / event_mgr.hxx
1 #ifndef _SG_EVENT_MGR_HXX
2 #define _SG_EVENT_MGR_HXX
3
4 #include <simgear/props/props.hxx>
5 #include <simgear/structure/subsystem_mgr.hxx>
6
7 #include "callback.hxx"
8
9 class SGEventMgr;
10
11 class SGTimer {
12 public:
13     ~SGTimer();
14     void run();
15     
16     std::string name;
17     double interval;
18     SGCallback* callback;
19     bool repeat;
20     bool running;
21 };
22
23 class SGTimerQueue {
24 public:
25     SGTimerQueue(int preSize=1);
26     ~SGTimerQueue();
27
28     void clear();
29     void update(double deltaSecs);
30
31     double now() { return _now; }
32
33     void     insert(SGTimer* timer, double time);
34     SGTimer* remove(SGTimer* timer);
35     SGTimer* remove();
36
37     SGTimer* nextTimer() { return _numEntries ? _table[0].timer : 0; }
38     double   nextTime()  { return -_table[0].pri; }
39
40     SGTimer* findByName(const std::string& name) const;
41 private:
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; };
46
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];
54         _table[b] = tmp;
55     }
56     void siftDown(int n);
57     void siftUp(int n);
58     void growArray();
59
60     // gcc complains there is no function specification anywhere.
61     // void check();
62
63     double _now;
64     HeapEntry *_table;
65     int _numEntries;
66     int _tableSize;
67 };
68
69 class SGEventMgr : public SGSubsystem
70 {
71 public:
72     SGEventMgr();
73     ~SGEventMgr();
74
75     virtual void init();
76     virtual void update(double delta_time_sec);
77     virtual void unbind();
78     virtual void shutdown();
79     
80     void setRealtimeProperty(SGPropertyNode* node) { _rtProp = node; }
81
82     /**
83      * Add a single function callback event as a repeating task.
84      * ex: addTask("foo", &Function ... )
85      */
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); }
90
91     /**
92      * Add a single function callback event as a one-shot event.
93      * ex: addEvent("foo", &Function ... )
94      */
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); }
99
100     /**
101      * Add a object/method pair as a repeating task.
102      * ex: addTask("foo", &object, &ClassName::Method, ...)
103      */
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); }
109
110     /**
111      * Add a object/method pair as a repeating task.
112      * ex: addEvent("foo", &object, &ClassName::Method, ...)
113      */
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); }
119
120
121     void removeTask(const std::string& name);
122 private:
123     friend class SGTimer;
124
125     void add(const std::string& name, SGCallback* cb,
126              double interval, double delay,
127              bool repeat, bool simtime);
128
129     SGPropertyNode_ptr _freezeProp;
130     SGPropertyNode_ptr _rtProp;
131     SGTimerQueue _rtQueue; 
132     SGTimerQueue _simQueue;
133     bool _inited;
134 };
135
136 #endif // _SG_EVENT_MGR_HXX