]> git.mxchange.org Git - flightgear.git/blob - src/Time/FGEventMgr.hxx
Recover this file. We are not able to use boost as a replacement because
[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 <boost/function.hpp>
28
29 #include <simgear/compiler.h>
30 #include <simgear/timing/timestamp.hxx>
31
32 #include <Main/fgfs.hxx>
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 boost::function<void> callback_type;
56     typedef int interval_type;
57
58 private:
59
60     /**
61      * 
62      */
63     class FGEvent
64     {
65     public:
66         /**
67          * 
68          */
69         FGEvent();
70
71         FGEvent( const string& desc,
72                  callback_type cb,
73                  EventState status_,
74                  interval_type ms );
75
76         /**
77          * 
78          */
79         ~FGEvent();
80
81         /**
82          * 
83          */
84         void reset()
85         {
86             status = FG_EVENT_READY;
87             ms_to_go = interval_ms;
88         }
89
90         /**
91          * Execute this event's callback.
92          */
93         void run();
94
95         /**
96          * Display event statistics.
97          */
98         void print_stats() const;
99
100         /**
101          * Update the elapsed time for this event.
102          * @param dt_ms elapsed time in milliseconds.
103          * @return true if elapsed time has expired.
104          */
105         bool update( int dt_ms )
106         {
107             if (status != FG_EVENT_READY)
108                 return false;
109
110             ms_to_go -= dt_ms;
111             return ms_to_go <= 0;
112         }
113
114     private:
115         string description;
116         callback_type callback;
117         EventState status;
118         interval_type interval_ms;
119         int ms_to_go;
120
121         unsigned long cum_time;    // cumulative processor time of this event
122         unsigned long min_time;    // time of quickest execution
123         unsigned long max_time;    // time of slowest execution
124         unsigned long count;       // number of times executed
125     };
126
127 public:
128     FGEventMgr();
129     ~FGEventMgr();
130
131     /**
132      * Initialize the scheduling subsystem.
133      */
134     void init();
135
136     void bind();
137
138     void unbind();
139
140     /*
141      * Update the elapsed time for all events.
142      * @param dt elapsed time in milliseconds.
143      */
144     void update( int dt );
145
146     /**
147      * Register a function to be executed every 'interval' milliseconds.
148      * @param desc A brief description of this callback for logging.
149      * @param cb The callback function to be executed.
150      * @param state
151      * @param interval Callback repetition rate in milliseconds.
152      */
153     void Register( const string& desc,
154                    callback_type cb,
155                    EventState state,
156                    interval_type interval_ms );
157
158     /**
159      * 
160      */
161     void Register( const string& desc,
162                    callback_type cb,
163                    interval_type interval_ms )
164     {
165         this->Register( desc, cb, FG_EVENT_READY, interval_ms );
166     }
167
168     /**
169      * Display statistics for all registered events.
170      */
171     void print_stats() const;
172
173 private:
174
175     typedef vector< FGEvent > event_container_type;
176
177     // Registered events.
178     event_container_type event_table;
179 };
180
181 extern FGEventMgr global_events;
182
183 #endif //FG_ EVENT_MGR_H_INCLUDED