]> git.mxchange.org Git - flightgear.git/blob - src/Time/FGEventMgr.hxx
Commented out a cout statement.
[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     typedef int interval_type;
47
48 private:
49
50     /**
51      * 
52      */
53     class FGEvent
54     {
55     public:
56         /**
57          * 
58          */
59         FGEvent();
60
61         FGEvent( const char* desc,
62                  fgCallback* cb,
63                  interval_type repeat_value,
64                  interval_type initial_value );
65
66         /**
67          * 
68          */
69         ~FGEvent();
70
71         /**
72          * 
73          */
74         void reset()
75         {
76             ms_to_go_ = repeat_value_;
77         }
78
79         /**
80          * Execute this event's callback.
81          */
82         void run();
83
84         string name() const { return name_; }
85         interval_type repeat_value() const { return repeat_value_; }
86         int value() const { return ms_to_go_; }
87
88         /**
89          * Display event statistics.
90          */
91         void print_stats() const;
92
93         /**
94          * Update the elapsed time for this event.
95          * @param dt_ms elapsed time in milliseconds.
96          * @return true if elapsed time has expired.
97          */
98         bool update( int dt_ms )
99         {
100             ms_to_go_ -= dt_ms;
101             return ms_to_go_ <= 0;
102         }
103
104     private:
105         string name_;
106         fgCallback* callback_;
107         interval_type repeat_value_;
108         interval_type initial_value_;
109         int ms_to_go_;
110
111         unsigned long cum_time;    // cumulative processor time of this event
112         unsigned long min_time;    // time of quickest execution
113         unsigned long max_time;    // time of slowest execution
114         unsigned long count;       // number of times executed
115     };
116
117 public:
118     FGEventMgr();
119     ~FGEventMgr();
120
121     /**
122      * Initialize the scheduling subsystem.
123      */
124     void init();
125
126     void bind();
127
128     void unbind();
129
130     /*
131      * Update the elapsed time for all events.
132      * @param dt elapsed time in seconds.
133      */
134     void update( double dt );
135
136     /**
137      * Register a free standing function to be executed some time in the future.
138      * @param desc A brief description of this callback for logging.
139      * @param cb The callback function to be executed.
140      * @param repeat_value repetition rate in milliseconds.
141      * @param initial_value initial delay value in milliseconds.  A value of
142      * -1 means run immediately.
143      */
144     template< typename Fun >
145     void Register( const char* name,
146                    const Fun& f,
147                    interval_type repeat_value,
148                    interval_type initial_value = -1 )
149     {
150         this->Register( FGEvent( name,
151                                  make_callback(f),
152                                  repeat_value,
153                                  initial_value ) );
154     }
155
156     template< class ObjPtr, typename MemFn >
157     void Register( const char* name,
158                    const ObjPtr& p,
159                    MemFn pmf,
160                    interval_type repeat_value,
161                    interval_type initial_value = -1 )
162     {
163         this->Register( FGEvent( name,
164                                  make_callback(p,pmf),
165                                  repeat_value,
166                                  initial_value ) );
167     }
168
169     /**
170      * Display statistics for all registered events.
171      */
172     void print_stats() const;
173
174 private:
175     void Register( const FGEvent& event );
176
177 private:
178
179     typedef vector< FGEvent > event_container_type;
180
181     // Registered events.
182     event_container_type event_table;
183 };
184
185 extern FGEventMgr global_events;
186
187 #endif //FG_ EVENT_MGR_H_INCLUDED