Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/microsim/MSEventControl.h
185785 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2001-2025 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file MSEventControl.h
15
/// @author Christian Roessel
16
/// @author Daniel Krajzewicz
17
/// @author Michael Behrisch
18
/// @author Matthias Heppner
19
/// @date Mon, 12 Mar 2001
20
///
21
// Stores time-dependant events and executes them at the proper time
22
/****************************************************************************/
23
#pragma once
24
#include <config.h>
25
26
#include <utility>
27
#include <queue>
28
#include <vector>
29
#include <map>
30
#include <utils/common/SUMOTime.h>
31
#include <utils/common/UtilExceptions.h>
32
33
34
// ===========================================================================
35
// class declarations
36
// ===========================================================================
37
class Command;
38
39
40
// ===========================================================================
41
// class definitions
42
// ===========================================================================
43
/**
44
* @class MSEventControl
45
* @brief Stores time-dependant events and executes them at the proper time
46
*/
47
class MSEventControl {
48
public:
49
/// @brief Combination of an event and the time it shall be executed at
50
typedef std::pair< Command*, SUMOTime > Event;
51
52
53
public:
54
/// @brief Default constructor.
55
MSEventControl();
56
57
58
/// @brief Destructor.
59
virtual ~MSEventControl();
60
61
62
/** @brief Adds an Event.
63
*
64
* @param[in] operation The event to add
65
* @param[in] execTimeStep The time the event shall be executed at (-1 means at sim start)
66
* @see Command
67
*/
68
virtual void addEvent(Command* operation, SUMOTime execTimeStep = -1);
69
70
71
/** @brief Executes time-dependant commands
72
*
73
* Loops over all stored events, continuing until the first event which
74
* execution time lies beyond the given time + deltaT. If the event
75
* had to be executed before the given time, a warning is generated and
76
* the event deleted. Otherwise (the event is valid), the event is executed.
77
*
78
* Each executed event must return the time that has to pass until it shall
79
* be executed again. If the returned time is 0, the event is deleted.
80
* Otherwise it is readded, after the new execution time (returned + current)
81
* is computed.
82
*
83
* ProcessErrors thrown by executed commands are rethrown.
84
*
85
* @param[in] time The current simulation time
86
* @exception ProcessError From an executed Command
87
*/
88
virtual void execute(SUMOTime time);
89
90
91
/** @brief Returns whether events are in the que.
92
*
93
* @return whether events are in the que
94
*/
95
bool isEmpty();
96
97
/** @brief Remove all events before quick-loading state */
98
void clearState(SUMOTime currentTime, SUMOTime newTime);
99
100
/** @brief get the next scheduled event time for the given command, -2 if it is not scheduled */
101
SUMOTime getEventTime(Command* cmd) const;
102
103
protected:
104
/// @brief compares two events
105
static bool eventCompare(const Event& e1, const Event& e2);
106
107
108
private:
109
/// @brief Event-container, holds executable events.
110
std::vector<Event> myEvents;
111
112
113
private:
114
/// @brief invalid copy constructor.
115
MSEventControl(const MSEventControl&);
116
117
/// @brief invalid assignment operator.
118
MSEventControl& operator=(const MSEventControl&);
119
120
121
};
122
123