SUMO - Simulation of Urban MObility
MSEventControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Stores time-dependant events and executes them at the proper time
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <cassert>
36 #include "MSEventControl.h"
38 #include <utils/common/Command.h>
39 #include "MSNet.h"
40 
41 
42 // ===========================================================================
43 // member definitions
44 // ===========================================================================
46  : currentTimeStep(-1), myEvents() {}
47 
48 
50  // delete the events
51  while (!myEvents.empty()) {
52  Event e = myEvents.top();
53  delete e.first;
54  myEvents.pop();
55  }
56 }
57 
58 
59 void
60 MSEventControl::addEvent(Command* operation, SUMOTime execTimeStep) {
61  myEvents.push(Event(operation, execTimeStep));
62 }
63 
64 
65 void
67  // Execute all events that are scheduled for execTime.
68  while (!myEvents.empty()) {
69  Event currEvent = myEvents.top();
70  if (currEvent.second < 0) {
71  currEvent.second = execTime;
72  }
73  if (currEvent.second < execTime + DELTA_T) {
74  Command* command = currEvent.first;
75  myEvents.pop();
76  SUMOTime time = 0;
77  try {
78  time = command->execute(execTime);
79  } catch (...) {
80  delete command;
81  throw;
82  }
83 
84  // Delete nonrecurring events, reinsert recurring ones
85  // with new execution time = execTime + returned offset.
86  if (time <= 0) {
87  if (time < 0) {
88  WRITE_WARNING("Command returned negative repeat number; will be deleted.");
89  }
90  delete currEvent.first;
91  } else {
92  currEvent.second += time;
93  myEvents.push(currEvent);
94  }
95  } else {
96  break;
97  }
98  }
99 }
100 
101 
102 bool
104  return myEvents.empty();
105 }
106 
107 void
109  currentTimeStep = time;
110 }
111 
112 SUMOTime
114  if (currentTimeStep < 0) {
116  }
117  return currentTimeStep;
118 }
119 
120 
121 
122 /****************************************************************************/
123 
MSEventControl()
Default constructor.
virtual void execute(SUMOTime time)
Executes time-dependant commands.
static MSNet * getInstance()
Returns the pointer to the unique instance of MSNet (singleton).
Definition: MSNet.cpp:158
SUMOTime DELTA_T
Definition: SUMOTime.cpp:40
Base (microsim) event class.
Definition: Command.h:61
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
SUMOTime currentTimeStep
The current TimeStep.
virtual SUMOTime execute(SUMOTime currentTime)=0
Executes the command.
void setCurrentTimeStep(SUMOTime time)
Set the current Time.
std::pair< Command *, SUMOTime > Event
Combination of an event and the time it shall be executed at.
virtual void addEvent(Command *operation, SUMOTime execTimeStep=-1)
Adds an Event.
SUMOTime getCurrentTimeStep() const
Returns the current simulation step.
Definition: MSNet.h:257
EventCont myEvents
Event-container, holds executable events.
SUMOTime getCurrentTimeStep()
get the Current TimeStep used in addEvent.
virtual ~MSEventControl()
Destructor.
bool isEmpty()
Returns whether events are in the que.
long long int SUMOTime
Definition: TraCIDefs.h:52