]> git.mxchange.org Git - simgear.git/blob - simgear/structure/event_mgr.hxx
Add optional attribute condition to "copyProperties".
[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 };
21
22 class SGTimerQueue {
23 public:
24     SGTimerQueue(int preSize=1);
25     ~SGTimerQueue();
26
27     void update(double deltaSecs);
28
29     double now() { return _now; }
30
31     void     insert(SGTimer* timer, double time);
32     SGTimer* remove(SGTimer* timer);
33     SGTimer* remove();
34
35     SGTimer* nextTimer() { return _numEntries ? _table[0].timer : 0; }
36     double   nextTime()  { return -_table[0].pri; }
37
38     SGTimer* findByName(const std::string& name) const;
39 private:
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; };
44
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];
52         _table[b] = tmp;
53     }
54     void siftDown(int n);
55     void siftUp(int n);
56     void growArray();
57
58     // gcc complains there is no function specification anywhere.
59     // void check();
60
61     double _now;
62     HeapEntry *_table;
63     int _numEntries;
64     int _tableSize;
65 };
66
67 class SGEventMgr : public SGSubsystem
68 {
69 public:
70     SGEventMgr() { _rtProp = 0; }
71     ~SGEventMgr() { _rtProp = 0; }
72
73     virtual void init() {}
74     virtual void update(double delta_time_sec);
75
76     void setRealtimeProperty(SGPropertyNode* node) { _rtProp = node; }
77
78     /**
79      * Add a single function callback event as a repeating task.
80      * ex: addTask("foo", &Function ... )
81      */
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); }
86
87     /**
88      * Add a single function callback event as a one-shot event.
89      * ex: addEvent("foo", &Function ... )
90      */
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); }
95
96     /**
97      * Add a object/method pair as a repeating task.
98      * ex: addTask("foo", &object, &ClassName::Method, ...)
99      */
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); }
105
106     /**
107      * Add a object/method pair as a repeating task.
108      * ex: addEvent("foo", &object, &ClassName::Method, ...)
109      */
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); }
115
116
117     void removeTask(const std::string& name);
118 private:
119     friend class SGTimer;
120
121     void add(const std::string& name, SGCallback* cb,
122              double interval, double delay,
123              bool repeat, bool simtime);
124
125     SGPropertyNode_ptr _freezeProp;
126     SGPropertyNode_ptr _rtProp;
127     SGTimerQueue _rtQueue; 
128     SGTimerQueue _simQueue;
129 };
130
131 #endif // _SG_EVENT_MGR_HXX