]> git.mxchange.org Git - flightgear.git/blobdiff - Time/event.cxx
Convert fgTIMESTAMP to FGTimeStamp which holds usec instead of ms.
[flightgear.git] / Time / event.cxx
index 24e7c490e4b4f3840b6dd5821aa87f32ec8e5cf8..5f4d204dc76dacf55ef6222a79a7f9a7ed6715ac 100644 (file)
 #  include <config.h>
 #endif
 
-#include <string.h>
-#include <stdio.h>
+#include <string>
 
-#ifdef HAVE_STDLIB_H
-#include <stdlib.h>
+#include "Include/compiler.h"
+
+#include STL_ALGORITHM
+#include STL_FUNCTIONAL
+
+#ifdef FG_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__)
                          // contains milliseconds
 #endif
 
-#include "Include/fg_stl_config.h"
-#include STL_ALGORITHM
-#include STL_FUNCTIONAL
-
 #include <Debug/logstream.hxx>
 
 #include "event.hxx"
 
+FG_USING_STD(for_each);
+FG_USING_STD(mem_fun);
 
 fgEVENT_MGR global_events;
 
 
 fgEVENT::fgEVENT( const string& desc,
                  const fgCallback& cb,
-                 EventState _status,
-                 int _interval )
+                 EventState evt_status,
+                 int evt_interval )
     : description(desc),
       event_cb(cb.clone()),
-      status(_status),
-      interval(_interval),
+      status(evt_status),
+      interval(evt_interval),
       cum_time(0),
       min_time(100000),
       max_time(0),
@@ -66,14 +76,10 @@ fgEVENT::fgEVENT( const string& desc,
 {
 }
 
+#if 0
 fgEVENT::fgEVENT( const fgEVENT& evt )
     : description(evt.description),
-#ifdef _FG_NEED_AUTO_PTR
-      // Ugly - cast away const until proper auto_ptr implementation.
-      event_cb((auto_ptr<fgCallback>&)(evt.event_cb)),
-#else
       event_cb(evt.event_cb),
-#endif
       status(evt.status),
       interval(evt.interval),
       last_run(evt.last_run),
@@ -86,18 +92,39 @@ fgEVENT::fgEVENT( const fgEVENT& evt )
 {
 }
 
+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()
 {
-//     cout << "fgEVENT::~fgEVENT" << endl;
+    delete event_cb;
 }
 
 void
 fgEVENT::run()
 {
-    printf("Running %s\n", description.c_str() );
+    FG_LOG(FG_EVENT, FG_INFO, "Running " << description );
 
     // record starting time
-    timestamp( &last_run );
+    last_run.stamp();
 
     // run the event
     event_cb->call( (void**)NULL );
@@ -109,8 +136,8 @@ fgEVENT::run()
     status = FG_EVENT_READY;
 
     // calculate duration and stats
-    timestamp( &current );
-    long duration = timediff( &last_run, &current );
+    current.stamp();
+    long duration = current - last_run;
 
     cum_time += duration;
 
@@ -123,7 +150,7 @@ fgEVENT::run()
     }
 
     // determine the next absolute run time
-    timesum( &next_run, &last_run, interval );
+    next_run =  last_run + interval;
 }
 
 
@@ -131,11 +158,14 @@ fgEVENT::run()
 int
 fgEVENT::PrintStats() const
 {
-    printf("  %-30s int=%.2fs cum=%ld min=%ld max=%ld count=%ld ave=%.2f\n",
-          description.c_str(), 
-          interval / 1000.0,
-          cum_time, min_time, max_time, count, 
-          cum_time / (double)count);
+    FG_LOG( FG_EVENT, FG_INFO, 
+           "  " << description 
+           << " int=" << interval / 1000.0
+           << " cum=" << cum_time
+           << " min=" << min_time
+           << " max=" <<  max_time
+           << " count=" << count
+           << " ave=" << cum_time / (double)count );
     return 0;
 }
 
@@ -146,7 +176,7 @@ fgEVENT_MGR::fgEVENT_MGR( void ) {
 
 // Initialize the scheduling subsystem
 void fgEVENT_MGR::Init( void ) {
-    printf("Initializing event manager\n");
+    FG_LOG(FG_EVENT, FG_INFO, "Initializing event manager" );
 
     run_queue.erase( run_queue.begin(), run_queue.end() );
     event_table.erase( event_table.begin(), event_table.end() );
@@ -160,12 +190,13 @@ fgEVENT_MGR::Register( const string& desc,
                       fgEVENT::EventState status, 
                       int interval )
 {
-    fgEVENT e( desc, cb, status, interval );
+    // convert interval specified in milleseconds to usec
+    fgEVENT* e = new fgEVENT( desc, cb, status, interval * 1000 );
 
-    printf("Registering event: %s\n", desc.c_str() );
+    FG_LOG( FG_EVENT, FG_INFO, "Registering event: " << desc );
 
     // Actually run the event
-    e.run();
+    e->run();
 
     // Now add to event_table
     event_table.push_back(e);
@@ -195,15 +226,23 @@ void fgEVENT_MGR::Resume( void ) {
 void
 fgEVENT_MGR::PrintStats()
 {
-    printf("\n");
-    printf("Event Stats\n");
-    printf("-----------\n");
-
+    FG_LOG( FG_EVENT, FG_INFO, "" );
+    FG_LOG( FG_EVENT, FG_INFO, "Event Stats" );
+    FG_LOG( FG_EVENT, FG_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_ref( &fgEVENT::PrintStats ));
-
-    printf("\n");
+             mem_fun( &fgEVENT::PrintStats ) );
+#endif
+    FG_LOG( FG_EVENT, FG_INFO, "");
 }
 
 
@@ -211,16 +250,16 @@ fgEVENT_MGR::PrintStats()
 // the queue
 void fgEVENT_MGR::Process( void ) {
     fgEVENT *e_ptr;
-    fg_timestamp cur_time;
+    FGTimeStamp cur_time;
     unsigned int i, size;
 
     FG_LOG( FG_EVENT, FG_DEBUG, "Processing events" );
     
     // get the current time
-    timestamp(&cur_time);
+    cur_time.stamp();
 
     FG_LOG( FG_EVENT, FG_DEBUG, 
-           "  Current timestamp = " << cur_time.seconds );
+           "  Current timestamp = " << cur_time.get_seconds() );
 
     // printf("Checking if anything is ready to move to the run queue\n");
 
@@ -229,12 +268,12 @@ void fgEVENT_MGR::Process( void ) {
     // while ( current != last ) {
     for ( i = 0; i < size; i++ ) {
        // e = *current++;
-       e_ptr = &event_table[i];
+       e_ptr = event_table[i];
        if ( e_ptr->status == fgEVENT::FG_EVENT_READY ) {
            FG_LOG( FG_EVENT, FG_DEBUG, 
-                   "  Item " << i << " current " << cur_time.seconds 
-                   << " next run @ " << e_ptr->next_run.seconds );
-           if ( timediff(&cur_time, &(e_ptr->next_run)) <= 0) {
+                   "  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;
            }
@@ -254,10 +293,40 @@ void fgEVENT_MGR::Process( void ) {
 
 // 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() );
 }
 
 
 // $Log$
+// Revision 1.15  1999/01/09 13:37:42  curt
+// Convert fgTIMESTAMP to FGTimeStamp which holds usec instead of ms.
+//
+// Revision 1.14  1999/01/07 20:25:32  curt
+// Portability changes and updates from Bernie Bright.
+//
+// Revision 1.13  1998/12/04 01:32:46  curt
+// Converted "struct fg_timestamp" to "class fgTIMESTAMP" and added some
+// convenience inline operators.
+//
+// Revision 1.12  1998/11/23 21:49:07  curt
+// Borland portability tweaks.
+//
+// Revision 1.11  1998/11/09 23:41:51  curt
+// Log message clean ups.
+//
+// Revision 1.10  1998/11/07 19:07:13  curt
+// Enable release builds using the --without-logging option to the configure
+// script.  Also a couple log message cleanups, plus some C to C++ comment
+// conversion.
+//
 // Revision 1.9  1998/11/06 21:18:24  curt
 // Converted to new logstream debugging facility.  This allows release
 // builds with no messages at all (and no performance impact) by using