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
55 typedef int interval_type;
70 FGEvent( const char* desc,
85 status_ = FG_EVENT_READY;
90 * Execute this event's callback.
94 bool is_ready() const { return status_ == FG_EVENT_READY; }
96 string name() const { return name_; }
97 interval_type interval() const { return ms_; }
100 * Display event statistics.
102 void print_stats() const;
105 * Update the elapsed time for this event.
106 * @param dt_ms elapsed time in milliseconds.
107 * @return true if elapsed time has expired.
109 bool update( int dt_ms )
111 if (status_ != FG_EVENT_READY)
115 return ms_to_go_ <= 0;
120 fgCallback* callback_;
125 unsigned long cum_time; // cumulative processor time of this event
126 unsigned long min_time; // time of quickest execution
127 unsigned long max_time; // time of slowest execution
128 unsigned long count; // number of times executed
136 * Initialize the scheduling subsystem.
145 * Update the elapsed time for all events.
146 * @param dt elapsed time in milliseconds.
148 void update( int dt );
151 * Register a function to be executed every 'interval' milliseconds.
152 * @param desc A brief description of this callback for logging.
153 * @param cb The callback function to be executed.
155 * @param interval Callback repetition rate in milliseconds.
157 void Register( const char* name,
159 interval_type interval_ms,
160 EventState state = FG_EVENT_READY )
162 this->Register( FGEvent( name, new fgFunctionCallback(cb), state, interval_ms ) );
165 template< class Obj >
166 void Register( const char* name,
167 Obj* obj, void (Obj::*pmf)() const,
168 interval_type interval_ms,
169 EventState state = FG_EVENT_READY )
171 this->Register( FGEvent( name, new fgMethodCallback<Obj>(obj,pmf), state, interval_ms ) );
174 template< class Obj >
175 void Register( const char* name,
176 Obj* obj, void (Obj::*pmf)(),
177 interval_type interval_ms,
178 EventState state = FG_EVENT_READY )
180 this->Register( FGEvent( name, new fgMethodCallback<Obj>(obj,pmf), state, interval_ms ) );
184 * Display statistics for all registered events.
186 void print_stats() const;
189 void Register( const FGEvent& event );
193 typedef vector< FGEvent > event_container_type;
195 // Registered events.
196 event_container_type event_table;
199 extern FGEventMgr global_events;
201 #endif //FG_ EVENT_MGR_H_INCLUDED