00001 // $Id: Scheduler.h,v 1.1.1.1 2005/10/17 06:11:40 maqm Exp $ 00002 00003 #ifndef SCHEDULER_H 00004 #define SCHEDULER_H 00005 00006 #ifdef _MSC_VER 00007 #pragma warning(disable:4786) 00008 #endif 00009 00010 #include <map> 00011 #include <iostream> 00012 class ScheduledEvent; 00013 00014 00015 class GPStime { 00016 // Represent absolute time. Units seconds. 00017 public: 00018 GPStime(double t=0):m_t(t){} 00019 /* */ operator double()const{return m_t;} 00020 private: 00021 double m_t; 00022 00023 friend GPStime& operator += (GPStime&, const double& ); 00024 }; 00025 00026 inline GPStime& operator += (GPStime& t, const double& dt) 00027 { 00028 t.m_t += dt; 00029 return t; 00030 } 00031 00032 00033 class Scheduler : private std::multimap<double, ScheduledEvent*> 00034 { 00035 //create a sequence of call backs to a list of ScheduledEvent 00036 // instances, in time order. Delete each after running 00037 00038 public: 00039 Scheduler(); 00040 // ctor 00041 00042 ~Scheduler(); 00043 // dtor, removes all entries still in list 00044 00045 void schedule(double deltaT, ScheduledEvent* event); 00046 // add a scheduled event, to occur deltaT after the present 00047 // the instance will be deleted after being run 00048 00049 void start(); 00050 // start the scheduler 00051 00052 void stop(); 00053 // stop the scheduler 00054 00055 bool running(){return m_running;} 00056 // status 00057 00058 void clear(); 00059 // purge pending events 00060 00061 GPStime elapsed_time()const{return m_time;} 00062 // access to the current time 00063 00064 void setLog(std::ostream& out); 00065 // define the log file, start logging to it 00066 00067 void endLogging(); 00068 // end logging. 00069 00070 void printOn(std::ostream& out)const; 00071 // make table of pending entries 00072 00073 static Scheduler* instance(); 00074 private: 00075 static Scheduler* s_instance; 00076 GPStime m_time; // current time 00077 bool m_running; 00078 std::ostream* m_log; 00079 }; 00080 #endif