// // class event // execution event in a discrete event driven simulation // // Described in Chapter 15 of // Data Structures in C++ using the STL // Published by Addison-Wesley, 1997 // Written by Tim Budd, budd@cs.orst.edu // Oregon State University // class event { public: // constructor requires time of event event (unsigned int t) : time(t) { } // time is a public data field unsigned int time; // execute event by invoking this method virtual void processEvent() { } }; class eventComparison { public: bool operator () (event * left, event * right) { return left->time > right->time; } }; class simulation { public: simulation () : eventQueue(), currentTime(0) { } void scheduleEvent (event * newEvent) { eventQueue.push (newEvent); } void run(); unsigned int currentTime; protected: priority_queue, eventComparison> eventQueue; }; void simulation::run() // execute events until event queue becomes empty { while (! eventQueue.empty()) { event * nextEvent = eventQueue.top(); eventQueue.pop(); currentTime = nextEvent->time; nextEvent->processEvent(); delete nextEvent; } }