SUMO - Simulation of Urban MObility
MSEventControl.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00011 // Stores time-dependant events and executes them at the proper time
00012 /****************************************************************************/
00013 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00014 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00015 /****************************************************************************/
00016 //
00017 //   This file is part of SUMO.
00018 //   SUMO is free software: you can redistribute it and/or modify
00019 //   it under the terms of the GNU General Public License as published by
00020 //   the Free Software Foundation, either version 3 of the License, or
00021 //   (at your option) any later version.
00022 //
00023 /****************************************************************************/
00024 
00025 
00026 // ===========================================================================
00027 // included modules
00028 // ===========================================================================
00029 #ifdef _MSC_VER
00030 #include <windows_config.h>
00031 #else
00032 #include <config.h>
00033 #endif
00034 
00035 #include <cassert>
00036 #include "MSEventControl.h"
00037 #include <utils/common/MsgHandler.h>
00038 #include <utils/common/Command.h>
00039 #include "MSNet.h"
00040 
00041 #ifdef CHECK_MEMORY_LEAKS
00042 #include <foreign/nvwa/debug_new.h>
00043 #endif // CHECK_MEMORY_LEAKS
00044 
00045 
00046 // ===========================================================================
00047 // member definitions
00048 // ===========================================================================
00049 MSEventControl::MSEventControl()
00050     : currentTimeStep(-1), myEvents() {}
00051 
00052 
00053 MSEventControl::~MSEventControl() {
00054     // delete the events
00055     while (! myEvents.empty()) {
00056         Event e = myEvents.top();
00057         delete e.first;
00058         myEvents.pop();
00059     }
00060 }
00061 
00062 
00063 SUMOTime
00064 MSEventControl::addEvent(Command* operation,
00065                          SUMOTime execTimeStep,
00066                          AdaptType type) {
00067     SUMOTime currTimeStep = getCurrentTimeStep();
00068     if (type == ADAPT_AFTER_EXECUTION && execTimeStep <= currTimeStep) {
00069         execTimeStep = currTimeStep;
00070     }
00071     Event newEvent = Event(operation, execTimeStep);
00072     myEvents.push(newEvent);
00073     return execTimeStep;
00074 }
00075 
00076 
00077 void
00078 MSEventControl::execute(SUMOTime execTime) {
00079     // Execute all events that are scheduled for execTime.
00080     for (; !myEvents.empty();) {
00081         Event currEvent = myEvents.top();
00082         if (currEvent.second == execTime || currEvent.second < execTime + DELTA_T) {
00083             Command* command = currEvent.first;
00084             myEvents.pop();
00085             SUMOTime time = 0;
00086             try {
00087                 time = command->execute(execTime);
00088             } catch (...) {
00089                 delete command;
00090                 throw;
00091             }
00092 
00093             // Delete nonrecurring events, reinsert recurring ones
00094             // with new execution time = execTime + returned offset.
00095             if (time <= 0) {
00096                 if (time < 0) {
00097                     WRITE_WARNING("Command returned negative repeat number; will be deleted.");
00098                 }
00099                 delete currEvent.first;
00100             } else {
00101                 currEvent.second = execTime + time;
00102                 myEvents.push(currEvent);
00103             }
00104         } else {
00105             if (currEvent.second < execTime) {
00106                 // !!! more verbose information
00107                 WRITE_WARNING("Could not execute scheduled event.");
00108                 delete currEvent.first;
00109                 myEvents.pop();
00110             } else {
00111                 break;
00112             }
00113         }
00114     }
00115 }
00116 
00117 
00118 bool
00119 MSEventControl::isEmpty() {
00120     return myEvents.empty();
00121 }
00122 
00123 void
00124 MSEventControl::setCurrentTimeStep(SUMOTime time) {
00125     currentTimeStep = time;
00126 }
00127 
00128 SUMOTime
00129 MSEventControl::getCurrentTimeStep() {
00130     if (currentTimeStep < 0) {
00131         return MSNet::getInstance()->getCurrentTimeStep();
00132     }
00133     return currentTimeStep;
00134 }
00135 
00136 
00137 
00138 /****************************************************************************/
00139 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines