00001
00008 #ifndef EVSIM
00009 #define EVSIM
00010
00011 #include <queue>
00012 using namespace std;
00013
00014 #include <string.h>
00015 #include "timeunits.h"
00016
00021 class Event
00022 {
00023 private:
00024 timeunits evTime_;
00025 unsigned long id_;
00026 char name_[6];
00027
00028 public:
00035 Event(timeunits t, unsigned long id=0):
00036 evTime_(t), id_(id) { };
00037
00044 Event(timeunits t, char* name, unsigned long id = 0):
00045 evTime_(t), id_(id)
00046 {
00047 if (0 != name){
00048 for (unsigned int i=0; (i<5)&&(i<strlen(name));i++){
00049 name_[i] = *(name+i);
00050 }
00051 name[5]=0;
00052 };
00053 }
00054
00058 virtual ~Event(void) {
00059 };
00060
00065 virtual void Handle(void) = 0;
00066
00070 inline timeunits Time(void) const {return evTime_;};
00076 inline void SetTime(timeunits t) {evTime_ = t;};
00080 char* GetName(void) {return name_;};
00084 inline unsigned int GetId(void) const {return id_;};
00085 };
00086
00087
00091 class CmpEvents
00092 {
00093 public:
00098 inline bool operator() (Event*& p1, Event*& p2) const{
00099 return p1->Time() > p2->Time();
00100 }
00101 };
00102
00103
00107 class EvSim
00108 {
00109 private:
00110 timeunits now_;
00111 priority_queue <Event*,vector<Event*>, CmpEvents> evQ_;
00112 timeunits from_;
00113 timeunits to_;
00114 unsigned int len_;
00115 unsigned long evnum_;
00116
00117 public:
00121 EvSim(void): now_(0), from_(0), to_(MAXTIME),len_(0), evnum_(0) {};
00125 virtual ~EvSim(void);
00130 inline timeunits CurrentTime(void) const {return now_;};
00136 void Run(timeunits from = 0, timeunits to = MAXTIME);
00144 void Schedule(Event* e, bool fifo = true) ;
00145
00152 void dumpStats (void);
00153 };
00154
00158 extern EvSim Simulation;
00159
00160 #endif
00161