SUMO - Simulation of Urban MObility
MSEventControl.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
21 // Stores time-dependant events and executes them at the proper time
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <cassert>
35 #include "MSEventControl.h"
37 #include <utils/common/Command.h>
38 #include "MSNet.h"
39 
40 
41 // ===========================================================================
42 // member definitions
43 // ===========================================================================
45  : currentTimeStep(-1), myEvents() {}
46 
47 
49  // delete the events
50  while (!myEvents.empty()) {
51  Event e = myEvents.top();
52  delete e.first;
53  myEvents.pop();
54  }
55 }
56 
57 
58 void
59 MSEventControl::addEvent(Command* operation, SUMOTime execTimeStep) {
60  myEvents.push(Event(operation, execTimeStep));
61 }
62 
63 
64 void
66  // Execute all events that are scheduled for execTime.
67  while (!myEvents.empty()) {
68  Event currEvent = myEvents.top();
69  if (currEvent.second < 0) {
70  currEvent.second = execTime;
71  }
72  if (currEvent.second < execTime + DELTA_T) {
73  Command* command = currEvent.first;
74  myEvents.pop();
75  SUMOTime time = 0;
76  try {
77  time = command->execute(execTime);
78  } catch (...) {
79  delete command;
80  throw;
81  }
82 
83  // Delete nonrecurring events, reinsert recurring ones
84  // with new execution time = execTime + returned offset.
85  if (time <= 0) {
86  if (time < 0) {
87  WRITE_WARNING("Command returned negative repeat number; will be deleted.");
88  }
89  delete currEvent.first;
90  } else {
91  currEvent.second += time;
92  myEvents.push(currEvent);
93  }
94  } else {
95  break;
96  }
97  }
98 }
99 
100 
101 bool
103  return myEvents.empty();
104 }
105 
106 void
108  currentTimeStep = time;
109 }
110 
111 SUMOTime
113  if (currentTimeStep < 0) {
115  }
116  return currentTimeStep;
117 }
118 
119 
120 
121 /****************************************************************************/
122 
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:167
SUMOTime DELTA_T
Definition: SUMOTime.cpp:39
Base (microsim) event class.
Definition: Command.h:60
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
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:253
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:51