2 // FGEventMgr.cxx -- Event Manager
4 // Written by Bernie Bright, started April 2002.
6 // Copyright (C) 2002 Curtis L. Olson - curt@me.umn.edu
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.
28 #include <simgear/compiler.h>
29 #include <simgear/debug/logstream.hxx>
31 #include "FGEventMgr.hxx"
33 FGEventMgr::FGEvent::FGEvent()
46 FGEventMgr::FGEvent::FGEvent( const char* name,
48 interval_type repeat_value,
49 interval_type initial_value )
52 repeat_value_(repeat_value),
53 initial_value_(initial_value),
54 //ms_to_go_(repeat_value_),
60 if (initial_value_ < 0)
63 ms_to_go_ = repeat_value_;
67 ms_to_go_ = initial_value_;
72 FGEventMgr::FGEvent::~FGEvent()
78 FGEventMgr::FGEvent::run()
80 SGTimeStamp start_time;
81 SGTimeStamp finish_time;
92 unsigned long duration = finish_time - start_time;
96 if ( duration < min_time ) {
100 if ( duration > max_time ) {
106 FGEventMgr::FGEvent::print_stats() const
108 SG_LOG( SG_EVENT, SG_INFO,
110 << " int=" << repeat_value_ / 1000.0
111 << " cum=" << cum_time
112 << " min=" << min_time
113 << " max=" << max_time
114 << " count=" << count
115 << " ave=" << cum_time / (double)count );
118 FGEventMgr::FGEventMgr()
122 FGEventMgr::~FGEventMgr()
129 SG_LOG( SG_EVENT, SG_INFO, "Initializing event manager" );
145 FGEventMgr::update( double dt )
147 int dt_ms = int(dt * 1000);
151 SG_LOG( SG_GENERAL, SG_ALERT,
152 "FGEventMgr::update() called with negative delta T" );
157 event_container_type::iterator first = event_table.begin();
158 event_container_type::iterator last = event_table.end();
159 event_container_type::iterator event = event_table.end();
161 // Scan all events. Run one whose interval has expired.
162 while (first != last)
164 if (first->update( dt_ms ))
166 if (first->value() < min_value)
168 // Select event with largest negative value.
169 // Its been waiting longest.
170 min_value = first->value();
181 if (event->repeat_value() > 0)
187 SG_LOG( SG_GENERAL, SG_DEBUG, "Deleting event " << event->name() );
188 event_table.erase( event );
194 FGEventMgr::Register( const FGEvent& event )
196 event_table.push_back( event );
198 SG_LOG( SG_EVENT, SG_INFO, "Registered event " << event.name()
199 << " to run every " << event.repeat_value() << "ms" );
203 FGEventMgr::print_stats() const
205 SG_LOG( SG_EVENT, SG_INFO, "" );
206 SG_LOG( SG_EVENT, SG_INFO, "Event Stats" );
207 SG_LOG( SG_EVENT, SG_INFO, "-----------" );
209 event_container_type::const_iterator first = event_table.begin();
210 event_container_type::const_iterator last = event_table.end();
211 for (; first != last; ++first)
213 first->print_stats();
216 SG_LOG( SG_EVENT, SG_INFO, "" );