MSEventControl.cpp

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

Generated on Wed May 5 00:06:31 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6