]> git.mxchange.org Git - flightgear.git/blob - Time/event.hxx
57d81e81de0b55dd1159452b97ccb4a46f1e9bd0
[flightgear.git] / Time / event.hxx
1 // event.hxx -- Flight Gear periodic event scheduler
2 //
3 // Written by Curtis Olson, started December 1997.
4 //
5 // Copyright (C) 1997  Curtis L. Olson  - curt@infoplane.com
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22 // (Log is kept at end of this file)
23
24
25 #ifndef _EVENT_HXX
26 #define _EVENT_HXX
27
28
29 #ifndef __cplusplus                                                          
30 # error This library requires C++
31 #endif                                   
32
33
34 #include "Include/compiler.h"
35 #include "Include/fg_callback.hxx"
36
37 #include <deque>        // STL double ended queue
38 #include <list>         // STL list
39 #include <string>
40
41 FG_USING_STD(deque);
42 FG_USING_STD(list);
43 FG_USING_STD(string);
44
45 #include "fg_time.hxx"
46 #include "timestamp.hxx"
47
48
49 class fgEVENT
50 {
51 public:
52     enum EventState
53     {
54         FG_EVENT_SUSP   = 0,
55         FG_EVENT_READY  = 1,
56         FG_EVENT_QUEUED = 2
57     };
58
59     friend class fgEVENT_MGR;
60
61     fgEVENT() {} // Required by deque<>.
62
63     fgEVENT( const string& desc,
64              const fgCallback& cb,
65              EventState evt_status,
66              int evt_interval );
67
68     ~fgEVENT();
69
70     void run();
71
72 //     void PrintStats() const;
73     int PrintStats() const;
74
75 private:
76     // not defined
77     fgEVENT( const fgEVENT& evt );
78     fgEVENT& operator= ( const fgEVENT& evt );
79
80 private:
81
82     string description;
83
84     // The callback object.
85     fgCallback* event_cb;
86
87     EventState status;       // status flag
88
89     long interval;    // interval in ms between each iteration of this event
90
91     fgTIMESTAMP last_run;
92     fgTIMESTAMP current;
93     fgTIMESTAMP next_run;
94
95     long cum_time;    // cumulative processor time of this event
96     long min_time;    // time of quickest execution
97     long max_time;    // time of slowest execution
98     long count;       // number of times executed
99 };
100
101
102 class fgEVENT_MGR {
103
104     // Event table
105     typedef deque < fgEVENT* >             EventContainer;
106     typedef EventContainer::iterator       EventIterator;
107     typedef EventContainer::const_iterator ConstEventIterator;
108
109     EventContainer event_table;
110
111     // Run Queue
112     typedef list < fgEVENT * > RunContainer;
113
114     RunContainer run_queue;
115
116 public:
117
118     // Constructor
119     fgEVENT_MGR ( void );
120
121     // Initialize the scheduling subsystem
122     void Init( void );
123
124     // Register an event with the scheduler
125     void Register( const string& desc, void (*event)( void ),
126                    fgEVENT::EventState status, int interval) {
127         Register( desc, fgFunctionCallback(event), status, interval );
128     }
129
130     void Register( const string& desc,
131                    const fgCallback& cb,
132                    fgEVENT::EventState status, 
133                    int interval );
134
135     // Update the scheduling parameters for an event
136     void Update( void );
137
138     // Delete a scheduled event
139     void Delete( void );
140
141     // Temporarily suspend scheduling of an event
142     void Suspend( void );
143
144     // Resume scheduling and event
145     void Resume( void );
146
147     // Dump scheduling stats
148     void PrintStats( void );
149
150     // Add pending jobs to the run queue and run the job at the front
151     // of the queue
152     void Process( void );
153
154     // Destructor
155     ~fgEVENT_MGR ( void );
156 };
157
158
159 // Wrapper to dump scheduling stats
160 void fgEventPrintStats( void );
161
162 extern fgEVENT_MGR global_events;
163
164
165 #endif // _EVENT_HXX
166
167
168 // $Log$
169 // Revision 1.15  1999/01/07 20:25:33  curt
170 // Portability changes and updates from Bernie Bright.
171 //
172 // Revision 1.14  1998/12/05 14:21:28  curt
173 // Moved struct fg_timestamp to class fgTIMESTAMP and moved it's definition
174 // to it's own file, timestamp.hxx.
175 //
176 // Revision 1.13  1998/12/04 01:32:47  curt
177 // Converted "struct fg_timestamp" to "class fgTIMESTAMP" and added some
178 // convenience inline operators.
179 //
180 // Revision 1.12  1998/10/16 00:56:08  curt
181 // Converted to Point3D class.
182 //
183 // Revision 1.11  1998/09/15 02:09:30  curt
184 // Include/fg_callback.hxx
185 //   Moved code inline to stop g++ 2.7 from complaining.
186 //
187 // Simulator/Time/event.[ch]xx
188 //   Changed return type of fgEVENT::printStat().  void caused g++ 2.7 to
189 //   complain bitterly.
190 //
191 // Minor bugfix and changes.
192 //
193 // Simulator/Main/GLUTmain.cxx
194 //   Added missing type to idle_state definition - eliminates a warning.
195 //
196 // Simulator/Main/fg_init.cxx
197 //   Changes to airport lookup.
198 //
199 // Simulator/Main/options.cxx
200 //   Uses fg_gzifstream when loading config file.
201 //
202 // Revision 1.10  1998/09/08 21:41:06  curt
203 // Added constructor for fgEVENT.
204 //
205 // Revision 1.9  1998/09/02 14:37:45  curt
206 // Renamed struct -> class.
207 //
208 // Revision 1.8  1998/08/29 13:11:32  curt
209 // Bernie Bright writes:
210 //   I've created some new classes to enable pointers-to-functions and
211 //   pointers-to-class-methods to be treated like objects.  These objects
212 //   can be registered with fgEVENT_MGR.
213 //
214 //   File "Include/fg_callback.hxx" contains the callback class defns.
215 //
216 //   Modified fgEVENT and fgEVENT_MGR to use the callback classes.  Also
217 //   some minor tweaks to STL usage.
218 //
219 //   Added file "Include/fg_stl_config.h" to deal with STL portability
220 //   issues.  I've added an initial config for egcs (and probably gcc-2.8.x).
221 //   I don't have access to Visual C++ so I've left that for someone else.
222 //   This file is influenced by the stl_config.h file delivered with egcs.
223 //
224 //   Added "Include/auto_ptr.hxx" which contains an implementation of the
225 //   STL auto_ptr class which is not provided in all STL implementations
226 //   and is needed to use the callback classes.
227 //
228 //   Deleted fgLightUpdate() which was just a wrapper to call
229 //   fgLIGHT::Update().
230 //
231 //   Modified fg_init.cxx to register two method callbacks in place of the
232 //   old wrapper functions.
233 //
234 // Revision 1.7  1998/07/30 23:48:54  curt
235 // Sgi build tweaks.
236 // Pause support.
237 //
238 // Revision 1.6  1998/07/24 21:42:25  curt
239 // Output message tweaks.
240 //
241 // Revision 1.5  1998/07/13 21:02:07  curt
242 // Wrote access functions for current fgOPTIONS.
243 //
244 // Revision 1.4  1998/06/12 00:59:52  curt
245 // Build only static libraries.
246 // Declare memmove/memset for Sloaris.
247 // Rewrote fg_time.c routine to get LST start seconds to better handle
248 //   Solaris, and be easier to port, and understand the GMT vs. local
249 //   timezone issues.
250 //
251 // Revision 1.3  1998/06/03 00:48:12  curt
252 // No .h for STL includes.
253 //
254 // Revision 1.2  1998/05/22 21:14:54  curt
255 // Rewrote event.cxx in C++ as a class using STL for the internal event list
256 // and run queue this removes the arbitrary list sizes and makes things much
257 // more dynamic.  Because this is C++-classified we can now have multiple
258 // event_tables if we'd ever want them.
259 //
260 // Revision 1.1  1998/04/24 00:52:26  curt
261 // Wrapped "#include <config.h>" in "#ifdef HAVE_CONFIG_H"
262 // Fog color fixes.
263 // Separated out lighting calcs into their own file.
264 //
265 // Revision 1.4  1998/04/21 17:01:43  curt
266 // Fixed a problems where a pointer to a function was being passed around.  In
267 // one place this functions arguments were defined as ( void ) while in another
268 // place they were defined as ( int ).  The correct answer was ( int ).
269 //
270 // Prepairing for C++ integration.
271 //
272 // Revision 1.3  1998/01/22 02:59:43  curt
273 // Changed #ifdef FILE_H to #ifdef _FILE_H
274 //
275 // Revision 1.2  1998/01/19 18:40:39  curt
276 // Tons of little changes to clean up the code and to remove fatal errors
277 // when building with the c++ compiler.
278 //
279 // Revision 1.1  1997/12/30 04:19:22  curt
280 // Initial revision.
281