1 // FGEventMgr.hxx -- Flight Gear periodic event scheduler
3 // Written by Curtis Olson, started December 1997.
4 // Modified by Bernie Bright, April 2002.
6 // Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
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.
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.
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.
24 #ifndef FG_EVENT_MGR_H_INCLUDED
25 #define FG_EVENT_MGR_H_INCLUDED 1
27 #include <simgear/compiler.h>
28 #include <simgear/timing/timestamp.hxx>
30 #include <Main/fgfs.hxx>
31 #include <Include/fg_callback.hxx>
42 class FGEventMgr : public FGSubsystem
46 typedef int interval_type;
61 FGEvent( const char* desc,
63 interval_type repeat_value,
64 interval_type initial_value );
76 ms_to_go_ = repeat_value_;
80 * Execute this event's callback.
84 string name() const { return name_; }
85 interval_type repeat_value() const { return repeat_value_; }
86 int value() const { return ms_to_go_; }
89 * Display event statistics.
91 void print_stats() const;
94 * Update the elapsed time for this event.
95 * @param dt_ms elapsed time in milliseconds.
96 * @return true if elapsed time has expired.
98 bool update( int dt_ms )
101 return ms_to_go_ <= 0;
106 fgCallback* callback_;
107 interval_type repeat_value_;
108 interval_type initial_value_;
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
122 * Initialize the scheduling subsystem.
131 * Update the elapsed time for all events.
132 * @param dt elapsed time in seconds.
134 void update( double dt );
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.
144 template< typename Fun >
145 void Register( const char* name,
147 interval_type repeat_value,
148 interval_type initial_value = -1 )
150 this->Register( FGEvent( name,
156 template< class ObjPtr, typename MemFn >
157 void Register( const char* name,
160 interval_type repeat_value,
161 interval_type initial_value = -1 )
163 this->Register( FGEvent( name,
164 make_callback(p,pmf),
170 * Display statistics for all registered events.
172 void print_stats() const;
175 void Register( const FGEvent& event );
179 typedef vector< FGEvent > event_container_type;
181 // Registered events.
182 event_container_type event_table;
185 extern FGEventMgr global_events;
187 #endif //FG_ EVENT_MGR_H_INCLUDED