]> git.mxchange.org Git - flightgear.git/blob - src/Time/FGEventMgr.hxx
Bernie Bright:
[flightgear.git] / src / Time / FGEventMgr.hxx
1 // FGEventMgr.hxx -- Flight Gear periodic event scheduler
2 //
3 // Written by Curtis Olson, started December 1997.
4 // Modified by Bernie Bright, April 2002.
5 //
6 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License as
10 // published by the Free Software Foundation; either version 2 of the
11 // License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 // General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22 // $Id$
23
24 #ifndef FG_EVENT_MGR_H_INCLUDED
25 #define FG_EVENT_MGR_H_INCLUDED 1
26
27 #include <simgear/compiler.h>
28 #include <simgear/timing/timestamp.hxx>
29
30 #include <Main/fgfs.hxx>
31 #include <Include/fg_callback.hxx>
32
33 #include <vector>
34 #include <string>
35
36 SG_USING_STD(vector);
37 SG_USING_STD(string);
38
39 /**
40  * 
41  */
42 class FGEventMgr : public FGSubsystem
43 {
44 public:
45
46     /**
47      * 
48      */
49     enum EventState {
50         FG_EVENT_SUSP   = 0,
51         FG_EVENT_READY  = 1,
52         FG_EVENT_QUEUED = 2
53     };
54
55     typedef int interval_type;
56
57 private:
58
59     /**
60      * 
61      */
62     class FGEvent
63     {
64     public:
65         /**
66          * 
67          */
68         FGEvent();
69
70         FGEvent( const char* desc,
71                  fgCallback* cb,
72                  EventState status,
73                  interval_type ms );
74
75         /**
76          * 
77          */
78         ~FGEvent();
79
80         /**
81          * 
82          */
83         void reset()
84         {
85             status_ = FG_EVENT_READY;
86             ms_to_go_ = ms_;
87         }
88
89         /**
90          * Execute this event's callback.
91          */
92         void run();
93
94         bool is_ready() const { return status_ == FG_EVENT_READY; }
95
96         string name() const { return name_; }
97         interval_type interval() const { return ms_; }
98
99         /**
100          * Display event statistics.
101          */
102         void print_stats() const;
103
104         /**
105          * Update the elapsed time for this event.
106          * @param dt_ms elapsed time in milliseconds.
107          * @return true if elapsed time has expired.
108          */
109         bool update( int dt_ms )
110         {
111             if (status_ != FG_EVENT_READY)
112                 return false;
113
114             ms_to_go_ -= dt_ms;
115             return ms_to_go_ <= 0;
116         }
117
118     private:
119         string name_;
120         fgCallback* callback_;
121         EventState status_;
122         interval_type ms_;
123         int ms_to_go_;
124
125         unsigned long cum_time;    // cumulative processor time of this event
126         unsigned long min_time;    // time of quickest execution
127         unsigned long max_time;    // time of slowest execution
128         unsigned long count;       // number of times executed
129     };
130
131 public:
132     FGEventMgr();
133     ~FGEventMgr();
134
135     /**
136      * Initialize the scheduling subsystem.
137      */
138     void init();
139
140     void bind();
141
142     void unbind();
143
144     /*
145      * Update the elapsed time for all events.
146      * @param dt elapsed time in milliseconds.
147      */
148     void update( int dt );
149
150     /**
151      * Register a function to be executed every 'interval' milliseconds.
152      * @param desc A brief description of this callback for logging.
153      * @param cb The callback function to be executed.
154      * @param state
155      * @param interval Callback repetition rate in milliseconds.
156      */
157     void Register( const char* name,
158                    void (*cb)(),
159                    interval_type interval_ms,
160                    EventState state = FG_EVENT_READY )
161     {
162         this->Register( FGEvent( name, new fgFunctionCallback(cb), state, interval_ms ) );
163     }
164
165     template< class Obj >
166     void Register( const char* name,
167                    Obj* obj, void (Obj::*pmf)() const,
168                    interval_type interval_ms,
169                    EventState state = FG_EVENT_READY )
170     {
171         this->Register( FGEvent( name, new fgMethodCallback<Obj>(obj,pmf), state, interval_ms ) );
172     }
173
174     template< class Obj >
175     void Register( const char* name,
176                    Obj* obj, void (Obj::*pmf)(),
177                    interval_type interval_ms,
178                    EventState state = FG_EVENT_READY )
179     {
180         this->Register( FGEvent( name, new fgMethodCallback<Obj>(obj,pmf), state, interval_ms ) );
181     }
182
183     /**
184      * Display statistics for all registered events.
185      */
186     void print_stats() const;
187
188 private:
189     void Register( const FGEvent& event );
190
191 private:
192
193     typedef vector< FGEvent > event_container_type;
194
195     // Registered events.
196     event_container_type event_table;
197 };
198
199 extern FGEventMgr global_events;
200
201 #endif //FG_ EVENT_MGR_H_INCLUDED