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