1 // eventmMgr.hxx -- 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 SG_EVENT_MGR_HXX
25 #define SG_EVENT_MGR_HXX 1
27 #include <simgear/compiler.h>
29 #include <simgear/structure/callback.hxx>
30 #include <simgear/structure/subsystem_mgr.hxx>
31 #include <simgear/timing/timestamp.hxx>
45 typedef int interval_type;
50 SGCallback* _callback;
51 SGSubsystem* _subsystem;
52 interval_type _repeat_value;
53 interval_type _initial_value;
56 unsigned long _cum_time; // cumulative processor time of this event
57 unsigned long _min_time; // time of quickest execution
58 unsigned long _max_time; // time of slowest execution
59 unsigned long _count; // number of times executed
68 SGEvent( const char* desc,
70 interval_type repeat_value,
71 interval_type initial_value );
73 SGEvent( const char* desc,
74 const SGSubsystem* subsystem,
75 interval_type repeat_value,
76 interval_type initial_value );
88 _ms_to_go = _repeat_value;
92 * Execute this event's callback.
96 inline string name() const { return _name; }
97 inline interval_type repeat_value() const { return _repeat_value; }
98 inline int value() const { return _ms_to_go; }
101 * Display event statistics.
103 void print_stats() const;
106 * Update the elapsed time for this event.
107 * @param dt_ms elapsed time in milliseconds.
108 * @return true if elapsed time has expired.
110 inline bool update( int dt_ms )
112 return (_ms_to_go -= dt_ms) <= 0;
117 class SGEventMgr : public SGSubsystem
121 typedef SGEvent::interval_type interval_type;
122 typedef vector< SGEvent > event_container_type;
124 void add( const SGEvent& event );
126 // registered events.
127 event_container_type event_table;
135 * Initialize the scheduling subsystem.
143 * Update the elapsed time for all events.
144 * @param dt elapsed time in seconds.
146 void update( double dt );
149 * register a free standing function to be executed some time in the future.
150 * @param desc A brief description of this callback for logging.
151 * @param cb The callback function to be executed.
152 * @param repeat_value repetition rate in milliseconds.
153 * @param initial_value initial delay value in milliseconds. A value of
154 * -1 means run immediately.
156 template< typename Fun >
157 inline void add( const char* name,
159 interval_type repeat_value,
160 interval_type initial_value = -1 )
162 this->add( SGEvent( name,
169 * register a subsystem of which the update function will be executed some
170 * time in the future.
171 * @param desc A brief description of this callback for logging.
172 * @param subsystem The subsystem of which the update function will be
174 * @param repeat_value repetition rate in milliseconds.
175 * @param initial_value initial delay value in milliseconds. A value of
176 * -1 means run immediately.
178 inline void add( const char* name,
179 const SGSubsystem* subsystem,
180 interval_type repeat_value,
181 interval_type initial_value = -1 )
183 this->add( SGEvent( name,
189 template< class ObjPtr, typename MemFn >
190 inline void add( const char* name,
193 interval_type repeat_value,
194 interval_type initial_value = -1 )
196 this->add( SGEvent( name,
197 make_callback(p,pmf),
203 * Display statistics for all registered events.
205 void print_stats() const;
209 #endif //SG_EVENT_MGR_HXX