1
0
Fork 0

Here is the 'boostified' event manager plus relevant changes to

radiostack.cxx, fg_init.cxx and main.cxx.  If these changes are accepted
then you can remove Time/event.[ch]xx and Include/fg_callback.hxx from
the repository.
This commit is contained in:
curt 2002-04-05 20:03:49 +00:00
parent ec6888d4a4
commit 41a4d0b351
11 changed files with 368 additions and 685 deletions

View file

@ -34,7 +34,8 @@
#include <Navaids/ilslist.hxx>
#include <Navaids/mkrbeacons.hxx>
#include <Navaids/navlist.hxx>
#include <Time/event.hxx>
#include <Time/FGEventMgr.hxx>
#include <boost/bind.hpp>
#include "radiostack.hxx"
@ -77,11 +78,6 @@ static double kludgeRange ( double stationElev, double aircraftElev,
}
// periodic radio station search wrapper
static void fgRadioSearch( void ) {
current_radiostack->search();
}
// Constructor
FGRadioStack::FGRadioStack() :
lon_node(fgGetNode("/position/longitude-deg", true)),
@ -146,8 +142,10 @@ FGRadioStack::init ()
update(1); // FIXME: use dt
// Search radio database once per second
global_events.Register( "fgRadioSearch()", fgRadioSearch,
fgEVENT::FG_EVENT_READY, 1000);
global_events.Register( "fgRadioSearch()",
boost::bind( &FGRadioStack::search,
current_radiostack),
1000 );
}
void

View file

@ -3,7 +3,6 @@ EXTRA_DIST = \
config.h.in \
config.h-msvc6 \
cmdargs.h \
fg_callback.hxx \
fg_typedefs.h \
fg_stl_config.h \
general.hxx

View file

@ -1,177 +0,0 @@
/**************************************************************************
* fg_callback.hxx -- Wrapper classes to treat function and method pointers
* as objects.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id$
**************************************************************************/
#ifndef _FG_CALLBACK_HXX
#define _FG_CALLBACK_HXX
// -dw- need size_t for params() function
#ifdef __MWERKS__
typedef unsigned long size_t;
#endif
//-----------------------------------------------------------------------------
//
// Abstract base class for all FlightGear callbacks.
//
class fgCallback
{
public:
virtual ~fgCallback() {}
virtual fgCallback* clone() const = 0;
virtual void* call( void** ) = 0;
size_t params() const { return n_params; }
protected:
fgCallback( size_t params )
: n_params(params) {}
protected:
// The number of parameters to pass to the callback routine.
size_t n_params;
private:
};
//-----------------------------------------------------------------------------
//
// Callback for invoking a file scope function.
//
class fgFunctionCallback : public fgCallback
{
public:
// Pointer to function taking no arguments and returning void.
typedef void (*Proc0v)();
// A callback instance to invoke the function 'p'
fgFunctionCallback( Proc0v p );
// Create a clone on the heap.
virtual fgCallback* clone() const;
private:
void* call( void** in );
inline void* call0v( void** );
private:
// Not defined.
fgFunctionCallback();
private:
typedef void* (fgFunctionCallback::*DoPtr)( void** );
DoPtr doPtr;
Proc0v proc0v;
};
inline
fgFunctionCallback::fgFunctionCallback( Proc0v p )
: fgCallback(0),
doPtr(&fgFunctionCallback::call0v),
proc0v(p)
{
// empty
}
inline fgCallback*
fgFunctionCallback::clone() const
{
return new fgFunctionCallback( *this );
}
inline void*
fgFunctionCallback::call( void** in )
{
return (this->*doPtr)( in );
}
inline void*
fgFunctionCallback::call0v( void** )
{
(*proc0v)();
return (void*) NULL;
}
//-----------------------------------------------------------------------------
//
// Callback for invoking an object method.
//
template< class T >
class fgMethodCallback : public fgCallback
{
public:
// Pointer to method taking no arguments and returning void.
typedef void (T::*Method0v)();
// A callback instance to invoke method 'm' of object 'o'
fgMethodCallback( T* o, Method0v m )
: fgCallback(0),
object(o),
method0v(m),
doPtr(&fgMethodCallback<T>::call0v) {}
// Create a clone on the heap.
fgCallback* clone() const;
private:
//
void* call( void** in );
//
void* call0v( void** );
private:
// Not defined.
fgMethodCallback();
private:
T* object;
Method0v method0v;
// typedef void * (fgMethodCallback::*DoPtr)( void ** );
typedef void * (fgMethodCallback<T>::*DoPtr)( void ** );
DoPtr doPtr;
};
template< class T > inline fgCallback*
fgMethodCallback<T>::clone() const
{
return new fgMethodCallback( *this );
}
template< class T > inline void*
fgMethodCallback<T>::call( void** in )
{
return (this->*doPtr)( in );
}
template< class T > inline void*
fgMethodCallback<T>::call0v( void** )
{
(object->*method0v)();
return (void*) NULL;
}
#endif // _FG_CALLBACK_HXX

View file

@ -103,7 +103,8 @@
#include <Scenery/tilemgr.hxx>
#include <Sound/fg_fx.hxx>
#include <Sound/soundmgr.hxx>
#include <Time/event.hxx>
#include <Time/FGEventMgr.hxx>
#include <boost/bind.hpp>
#include <Time/light.hxx>
#include <Time/sunpos.hxx>
#include <Time/moonpos.hxx>
@ -715,13 +716,13 @@ bool fgInitSubsystems( void ) {
// Initialize the event manager subsystem.
////////////////////////////////////////////////////////////////////
global_events.Init();
global_events.init();
// Output event stats every 60 seconds
global_events.Register( "fgEVENT_MGR::PrintStats()",
fgMethodCallback<fgEVENT_MGR>( &global_events,
&fgEVENT_MGR::PrintStats),
fgEVENT::FG_EVENT_READY, 60000 );
boost::bind( &FGEventMgr::print_stats,
&global_events ),
60000 );
////////////////////////////////////////////////////////////////////
@ -740,19 +741,18 @@ bool fgInitSubsystems( void ) {
// things for correctly orienting the sky.
fgUpdateSunPos();
fgUpdateMoonPos();
global_events.Register( "fgUpdateSunPos()", fgUpdateSunPos,
fgEVENT::FG_EVENT_READY, 60000);
global_events.Register( "fgUpdateMoonPos()", fgUpdateMoonPos,
fgEVENT::FG_EVENT_READY, 60000);
global_events.Register( "fgUpdateSunPos()", &fgUpdateSunPos,
60000);
global_events.Register( "fgUpdateMoonPos()", &fgUpdateMoonPos,
60000);
// Initialize Lighting interpolation tables
l->Init();
// update the lighting parameters (based on sun angle)
global_events.Register( "fgLight::Update()",
fgMethodCallback<fgLIGHT>( &cur_light_params,
&fgLIGHT::Update),
fgEVENT::FG_EVENT_READY, 30000 );
boost::bind( &fgLIGHT::Update, &cur_light_params ),
30000 );
////////////////////////////////////////////////////////////////////
@ -768,8 +768,8 @@ bool fgInitSubsystems( void ) {
////////////////////////////////////////////////////////////////////
// update the current timezone each 30 minutes
global_events.Register( "fgUpdateLocalTime()", fgUpdateLocalTime,
fgEVENT::FG_EVENT_READY, 1800000);
global_events.Register( "fgUpdateLocalTime()", &fgUpdateLocalTime,
30*60*1000 );
////////////////////////////////////////////////////////////////////
@ -815,8 +815,8 @@ bool fgInitSubsystems( void ) {
WeatherDatabase = FGLocalWeatherDatabase::theFGLocalWeatherDatabase;
// register the periodic update of the weather
global_events.Register( "weather update", fgUpdateWeatherDatabase,
fgEVENT::FG_EVENT_READY, 30000);
global_events.Register( "weather update", &fgUpdateWeatherDatabase,
30000);
#else
globals->get_environment_mgr()->init();
globals->get_environment_mgr()->bind();

View file

@ -102,7 +102,7 @@ private:
long int warp_delta;
// Logger
FGLogger * logger;
FGLogger *logger;
// Time structure
SGTime *time_params;

View file

@ -108,7 +108,7 @@ SG_USING_STD(endl);
# include <Sound/fg_fx.hxx>
# include <Sound/morse.hxx>
#endif
#include <Time/event.hxx>
#include <Time/FGEventMgr.hxx>
#include <Time/fg_timer.hxx>
#include <Time/light.hxx>
#include <Time/sunpos.hxx>
@ -170,6 +170,8 @@ float scene_farplane = 120000.0f;
#endif
FGEventMgr global_events;
// This is a record containing a bit of global housekeeping information
FGGeneral general;
@ -404,6 +406,9 @@ void fgRenderFrame( void ) {
int dt_ms = int(globals->get_elapsed_time_ms() - old_elapsed_ms);
old_elapsed_ms = globals->get_elapsed_time_ms();
// Process/manage pending events
global_events.update( dt_ms );
static const SGPropertyNode *longitude
= fgGetNode("/position/longitude-deg");
static const SGPropertyNode *latitude
@ -1089,9 +1094,6 @@ static void fgMainLoop( void ) {
// see if we need to load any deferred-load textures
material_lib.load_next_deferred();
// Process/manage pending events
global_events.Process();
// Run audio scheduler
#ifdef ENABLE_AUDIO_SUPPORT
if ( fgGetBool("/sim/sound/audible")

154
src/Time/FGEventMgr.cxx Normal file
View file

@ -0,0 +1,154 @@
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/compiler.h>
#include <simgear/debug/logstream.hxx>
#include <functional>
#include <boost/bind.hpp>
#include "FGEventMgr.hxx"
FGEventMgr::FGEvent::FGEvent()
: status(FG_EVENT_SUSP),
interval_ms(0),
ms_to_go(0),
cum_time(0),
min_time(100000),
max_time(0),
count(0)
{
}
FGEventMgr::FGEvent::FGEvent( const string& desc,
callback_type cb,
EventState status_,
interval_type ms )
: description(desc),
callback(cb),
status(status_),
interval_ms(ms),
ms_to_go(ms),
cum_time(0),
min_time(100000),
max_time(0),
count(0)
{
}
FGEventMgr::FGEvent::~FGEvent()
{
}
void
FGEventMgr::FGEvent::run()
{
SGTimeStamp start_time;
SGTimeStamp finish_time;
start_time.stamp();
// run the event
this->callback();
finish_time.stamp();
++count;
unsigned long duration = finish_time - start_time;
cum_time += duration;
if ( duration < min_time ) {
min_time = duration;
}
if ( duration > max_time ) {
max_time = duration;
}
reset();
}
void
FGEventMgr::FGEvent::print_stats() const
{
SG_LOG( SG_EVENT, SG_INFO,
" " << description
<< " int=" << interval_ms / 1000.0
<< " cum=" << cum_time
<< " min=" << min_time
<< " max=" << max_time
<< " count=" << count
<< " ave=" << cum_time / (double)count );
}
FGEventMgr::FGEventMgr()
{
}
FGEventMgr::~FGEventMgr()
{
}
void
FGEventMgr::init()
{
SG_LOG( SG_EVENT, SG_INFO, "Initializing event manager" );
event_table.clear();
}
void
FGEventMgr::bind()
{
}
void
FGEventMgr::unbind()
{
}
void
FGEventMgr::update( int dt )
{
// Scan all events. Execute any whose interval has expired.
event_container_type::iterator first = event_table.begin();
event_container_type::iterator last = event_table.end();
for(; first != last; ++first)
{
if (first->update( dt ))
{
first->run();
}
}
}
void
FGEventMgr::Register( const string& desc,
callback_type cb,
EventState state,
interval_type ms )
{
FGEvent event( desc, cb, state, ms );
if (state == FG_EVENT_READY)
event.run();
event_table.push_back( event );
SG_LOG( SG_EVENT, SG_INFO, "Registered event " << desc
<< " to run every " << ms << "ms" );
}
void
FGEventMgr::print_stats() const
{
SG_LOG( SG_EVENT, SG_INFO, "" );
SG_LOG( SG_EVENT, SG_INFO, "Event Stats" );
SG_LOG( SG_EVENT, SG_INFO, "-----------" );
std::for_each( event_table.begin(), event_table.end(),
boost::bind( &FGEvent::print_stats, _1 ) );
SG_LOG( SG_EVENT, SG_INFO, "" );
}

183
src/Time/FGEventMgr.hxx Normal file
View file

@ -0,0 +1,183 @@
// FGEventMgr.hxx -- Flight Gear periodic event scheduler
//
// Written by Curtis Olson, started December 1997.
// Modified by Bernie Bright, April 2002.
//
// Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// $Id$
#ifndef FG_EVENT_MGR_H_INCLUDED
#define FG_EVENT_MGR_H_INCLUDED 1
#include <boost/function.hpp>
#include <simgear/compiler.h>
#include <simgear/timing/timestamp.hxx>
#include <Main/fgfs.hxx>
#include <vector>
#include <string>
SG_USING_STD(vector);
SG_USING_STD(string);
/**
*
*/
class FGEventMgr : public FGSubsystem
{
public:
/**
*
*/
enum EventState {
FG_EVENT_SUSP = 0,
FG_EVENT_READY = 1,
FG_EVENT_QUEUED = 2
};
typedef boost::function<void> callback_type;
typedef int interval_type;
private:
/**
*
*/
class FGEvent
{
public:
/**
*
*/
FGEvent();
FGEvent( const string& desc,
callback_type cb,
EventState status_,
interval_type ms );
/**
*
*/
~FGEvent();
/**
*
*/
void reset()
{
status = FG_EVENT_READY;
ms_to_go = interval_ms;
}
/**
* Execute this event's callback.
*/
void run();
/**
* Display event statistics.
*/
void print_stats() const;
/**
* Update the elapsed time for this event.
* @param dt_ms elapsed time in milliseconds.
* @return true if elapsed time has expired.
*/
bool update( int dt_ms )
{
if (status != FG_EVENT_READY)
return false;
ms_to_go -= dt_ms;
return ms_to_go <= 0;
}
private:
string description;
callback_type callback;
EventState status;
interval_type interval_ms;
int ms_to_go;
unsigned long cum_time; // cumulative processor time of this event
unsigned long min_time; // time of quickest execution
unsigned long max_time; // time of slowest execution
unsigned long count; // number of times executed
};
public:
FGEventMgr();
~FGEventMgr();
/**
* Initialize the scheduling subsystem.
*/
void init();
void bind();
void unbind();
/*
* Update the elapsed time for all events.
* @param dt elapsed time in milliseconds.
*/
void update( int dt );
/**
* Register a function to be executed every 'interval' milliseconds.
* @param desc A brief description of this callback for logging.
* @param cb The callback function to be executed.
* @param state
* @param interval Callback repetition rate in milliseconds.
*/
void Register( const string& desc,
callback_type cb,
EventState state,
interval_type interval_ms );
/**
*
*/
void Register( const string& desc,
callback_type cb,
interval_type interval_ms )
{
this->Register( desc, cb, FG_EVENT_READY, interval_ms );
}
/**
* Display statistics for all registered events.
*/
void print_stats() const;
private:
typedef vector< FGEvent > event_container_type;
// Registered events.
event_container_type event_table;
};
extern FGEventMgr global_events;
#endif //FG_ EVENT_MGR_H_INCLUDED

View file

@ -1,7 +1,7 @@
noinst_LIBRARIES = libTime.a
libTime_a_SOURCES = \
event.cxx event.hxx \
FGEventMgr.cxx FGEventMgr.hxx \
fg_timer.cxx fg_timer.hxx \
light.cxx light.hxx \
moonpos.cxx moonpos.hxx \

View file

@ -1,310 +0,0 @@
// event.cxx -- Flight Gear periodic event scheduler
//
// Written by Curtis Olson, started December 1997.
//
// Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// $Id$
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <simgear/compiler.h>
#ifdef SG_MATH_EXCEPTION_CLASH
# include <math.h>
#endif
#include STL_ALGORITHM
#include STL_FUNCTIONAL
#include STL_STRING
#ifdef SG_HAVE_STD_INCLUDES
# include <cstdio>
# ifdef HAVE_STDLIB_H
# include <cstdlib>
# endif
#else
# include <stdio.h>
# ifdef HAVE_STDLIB_H
# include <stdlib.h>
# endif
#endif
#if defined( HAVE_WINDOWS_H ) && defined(__MWERKS__)
# include <windows.h> // For Metrowerks environment
# include <winbase.h> // There is no ANSI/MSL time function that
// contains milliseconds
#endif
#include <simgear/debug/logstream.hxx>
#include "event.hxx"
SG_USING_STD(for_each);
SG_USING_STD(mem_fun);
SG_USING_STD(string);
fgEVENT_MGR global_events;
fgEVENT::fgEVENT( const string& desc,
const fgCallback& cb,
EventState evt_status,
int evt_interval )
: description(desc),
event_cb(cb.clone()),
status(evt_status),
interval(evt_interval),
cum_time(0),
min_time(100000),
max_time(0),
count(0)
{
}
#if 0
fgEVENT::fgEVENT( const fgEVENT& evt )
: description(evt.description),
event_cb(evt.event_cb),
status(evt.status),
interval(evt.interval),
last_run(evt.last_run),
current(evt.current),
next_run(evt.next_run),
cum_time(evt.cum_time),
min_time(evt.min_time),
max_time(evt.max_time),
count(evt.count)
{
}
fgEVENT&
fgEVENT::operator= ( const fgEVENT& evt )
{
if ( this != &evt )
{
description = evt.description;
event_cb = evt.event_cb;
status = evt.status;
interval = evt.interval;
last_run = evt.last_run;
current = evt.current;
next_run = evt.next_run;
cum_time = evt.cum_time;
min_time = evt.min_time;
max_time = evt.max_time;
count = evt.count;
}
return *this;
}
#endif
fgEVENT::~fgEVENT()
{
delete event_cb;
}
void
fgEVENT::run()
{
SG_LOG(SG_EVENT, SG_DEBUG, "Running " << description );
// record starting time
last_run.stamp();
// run the event
event_cb->call( (void**)NULL );
// increment the counter for this event
count++;
// update the event status
status = FG_EVENT_READY;
// calculate duration and stats
current.stamp();
long duration = current - last_run;
cum_time += duration;
if ( duration < min_time ) {
min_time = duration;
}
if ( duration > max_time ) {
max_time = duration;
}
// determine the next absolute run time
next_run = last_run + interval;
}
// Dump scheduling stats
int
fgEVENT::PrintStats() const
{
SG_LOG( SG_EVENT, SG_INFO,
" " << description
<< " int=" << interval / 1000.0
<< " cum=" << cum_time
<< " min=" << min_time
<< " max=" << max_time
<< " count=" << count
<< " ave=" << cum_time / (double)count );
return 0;
}
// Constructor
fgEVENT_MGR::fgEVENT_MGR( void ) {
}
// Initialize the scheduling subsystem
void fgEVENT_MGR::Init( void ) {
SG_LOG(SG_EVENT, SG_INFO, "Initializing event manager" );
run_queue.erase( run_queue.begin(), run_queue.end() );
event_table.erase( event_table.begin(), event_table.end() );
}
// Register an event with the scheduler.
void
fgEVENT_MGR::Register( const string& desc,
const fgCallback& cb,
fgEVENT::EventState status,
int interval )
{
// convert interval specified in milleseconds to usec
fgEVENT* e = new fgEVENT( desc, cb, status, interval * 1000 );
SG_LOG( SG_EVENT, SG_INFO, "Registering event: " << desc );
// Actually run the event
e->run();
// Now add to event_table
event_table.push_back(e);
}
// Update the scheduling parameters for an event
void fgEVENT_MGR::Update( void ) {
}
// Delete a scheduled event
void fgEVENT_MGR::Delete( void ) {
}
// Temporarily suspend scheduling of an event
void fgEVENT_MGR::Suspend( void ) {
}
// Resume scheduling and event
void fgEVENT_MGR::Resume( void ) {
}
// Dump scheduling stats
void
fgEVENT_MGR::PrintStats()
{
SG_LOG( SG_EVENT, SG_INFO, "" );
SG_LOG( SG_EVENT, SG_INFO, "Event Stats" );
SG_LOG( SG_EVENT, SG_INFO, "-----------" );
ConstEventIterator first = event_table.begin();
ConstEventIterator last = event_table.end();
while ( first != last )
{
(*first)->PrintStats();
++first;
}
#if 0 // msvc++ 6.0 barfs at mem_fun()
for_each( event_table.begin(),
event_table.end(),
mem_fun( &fgEVENT::PrintStats ) );
#endif
SG_LOG( SG_EVENT, SG_INFO, "");
}
// Add pending jobs to the run queue and run the job at the front of
// the queue
void fgEVENT_MGR::Process( void ) {
fgEVENT *e_ptr;
SGTimeStamp cur_time;
unsigned int i, size;
SG_LOG( SG_EVENT, SG_DEBUG, "Processing events" );
// get the current time
cur_time.stamp();
SG_LOG( SG_EVENT, SG_DEBUG,
" Current timestamp = " << cur_time.get_seconds() );
// printf("Checking if anything is ready to move to the run queue\n");
// see if anything else is ready to be placed on the run queue
size = event_table.size();
// while ( current != last ) {
for ( i = 0; i < size; i++ ) {
// e = *current++;
e_ptr = event_table[i];
if ( e_ptr->status == fgEVENT::FG_EVENT_READY ) {
SG_LOG( SG_EVENT, SG_DEBUG,
" Item " << i << " current " << cur_time.get_seconds()
<< " next run @ " << e_ptr->next_run.get_seconds() );
if ( ( e_ptr->next_run - cur_time ) <= 0 ) {
run_queue.push_back(e_ptr);
e_ptr->status = fgEVENT::FG_EVENT_QUEUED;
}
}
}
// Checking to see if there is anything on the run queue
// printf("Checking to see if there is anything on the run queue\n");
if ( run_queue.size() ) {
// printf("Yep, running it\n");
e_ptr = run_queue.front();
run_queue.pop_front();
e_ptr->run();
}
}
// Destructor
fgEVENT_MGR::~fgEVENT_MGR( void ) {
EventIterator first = event_table.begin();
EventIterator last = event_table.end();
for ( ; first != last; ++first )
{
delete (*first);
}
run_queue.erase( run_queue.begin(), run_queue.end() );
event_table.erase( event_table.begin(), event_table.end() );
}

View file

@ -1,166 +0,0 @@
// event.hxx -- Flight Gear periodic event scheduler
//
// Written by Curtis Olson, started December 1997.
//
// Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// $Id$
#ifndef _EVENT_HXX
#define _EVENT_HXX
#ifndef __cplusplus
# error This library requires C++
#endif
#include <simgear/compiler.h>
#include <simgear/timing/timestamp.hxx>
#include <Include/fg_callback.hxx>
#include <deque> // STL double ended queue
#include <list> // STL list
#include STL_STRING
SG_USING_STD(deque);
SG_USING_STD(list);
SG_USING_STD(string);
class fgEVENT
{
public:
enum EventState
{
FG_EVENT_SUSP = 0,
FG_EVENT_READY = 1,
FG_EVENT_QUEUED = 2
};
friend class fgEVENT_MGR;
fgEVENT() {} // Required by deque<>.
fgEVENT( const string& desc,
const fgCallback& cb,
EventState evt_status,
int evt_interval );
~fgEVENT();
void run();
// void PrintStats() const;
int PrintStats() const;
private:
// not defined
fgEVENT( const fgEVENT& evt );
fgEVENT& operator= ( const fgEVENT& evt );
private:
string description;
// The callback object.
fgCallback* event_cb;
EventState status; // status flag
long interval; // interval in ms between each iteration of this event
SGTimeStamp last_run;
SGTimeStamp current;
SGTimeStamp next_run;
long cum_time; // cumulative processor time of this event
long min_time; // time of quickest execution
long max_time; // time of slowest execution
long count; // number of times executed
};
class fgEVENT_MGR {
// Event table
typedef deque < fgEVENT* > EventContainer;
typedef EventContainer::iterator EventIterator;
typedef EventContainer::const_iterator ConstEventIterator;
EventContainer event_table;
// Run Queue
typedef list < fgEVENT * > RunContainer;
RunContainer run_queue;
public:
// Constructor
fgEVENT_MGR ( void );
// Initialize the scheduling subsystem
void Init( void );
// Register an event with the scheduler
void Register( const string& desc, void (*event)( void ),
fgEVENT::EventState status, int interval) {
Register( desc, fgFunctionCallback(event), status, interval );
}
void Register( const string& desc,
const fgCallback& cb,
fgEVENT::EventState status,
int interval );
// Update the scheduling parameters for an event
void Update( void );
// Delete a scheduled event
void Delete( void );
// Temporarily suspend scheduling of an event
void Suspend( void );
// Resume scheduling and event
void Resume( void );
// Dump scheduling stats
void PrintStats( void );
// Add pending jobs to the run queue and run the job at the front
// of the queue
void Process( void );
// Destructor
~fgEVENT_MGR ( void );
};
// Wrapper to dump scheduling stats
void fgEventPrintStats( void );
extern fgEVENT_MGR global_events;
#endif // _EVENT_HXX