-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimulationObject.cpp
160 lines (129 loc) · 3.59 KB
/
SimulationObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/************************************************************
* SimLib simulation library for event-based simulations *
* Author: Martin Ubl (A16N0026P) *
************************************************************/
#include "SimulationObject.h"
#include "Simulation.h"
#include "Calendar.h"
#include "Process.h"
#include "Event.h"
SimulationObject::SimulationObject(SimulationObjectType type, SimulationPtr simulation, uint32_t objectClass)
: m_type(type), m_objectClass(objectClass), m_simulation(simulation)
{
m_guid = 0;
}
uint64_t SimulationObject::GetGUID() const
{
return m_guid;
}
SimulationObjectType SimulationObject::GetType() const
{
return m_type;
}
void SimulationObject::SetGUID(uint64_t guid)
{
m_guid = guid;
}
void SimulationObject::SetObjectClass(uint32_t objectClass)
{
m_objectClass = objectClass;
}
void SimulationObject::SetSimulation(SimulationPtr simulation)
{
m_simulation = simulation;
}
uint32_t SimulationObject::GetObjectClass() const
{
return m_objectClass;
}
SimulationPtr SimulationObject::GetSimulation() const
{
return m_simulation.lock();
}
simtime_t SimulationObject::GetNextSimTime() const
{
return m_simTimeNext;
}
CalendarPtr SimulationObject::GetCurrentCalendar() const
{
return m_currCalendar;
}
void SimulationObject::Schedule(CalendarPtr calendar, simtime_t scheduleTime, bool relative)
{
auto self = shared_from_this();
// remove from calendar, if present
if (m_currCalendar)
m_currCalendar->remove(self);
// relative time needs to have base schedule time retrieved from simulation object
if (relative)
{
SimulationPtr simulation = GetSimulation();
if (simulation)
m_simTimeNext = simulation->GetSimulationTime() + scheduleTime;
else
return;
}
else
m_simTimeNext = scheduleTime;
m_currCalendar = calendar;
m_currCalendar->push(self);
}
void SimulationObject::ClearScheduleInfo()
{
m_simTimeNext = 0;
m_currCalendar = nullptr;
}
SimProcess* SimulationObject::ToProcess()
{
return dynamic_cast<SimProcess*>(this);
}
SimEvent* SimulationObject::ToEvent()
{
return dynamic_cast<SimEvent*>(this);
}
void SimulationObject::Terminate()
{
auto simulation = GetSimulation();
if (!simulation)
return;
auto self = (SimulationObjectPtr)shared_from_this();
// at first, remove from calendar
if (m_currCalendar)
{
m_currCalendar->remove(self);
m_currCalendar = nullptr;
}
m_simTimeNext = 0;
// remove from simulation
simulation->RemoveObject(self);
m_simulation.reset();
}
bool SimulationObject::HasPeriodicSchedule() const
{
return (m_scheduleGenerator != nullptr);
}
void SimulationObject::NextPeriodicSchedule(CalendarPtr calendar)
{
if (!m_scheduleGenerator)
return;
Schedule(calendar, (*m_scheduleGenerator)(), true);
}
void SimulationObject::CancelPeriodicSchedule(bool removeFromCalendar)
{
if (!m_scheduleGenerator)
return;
m_scheduleGenerator.reset();
if (removeFromCalendar)
{
auto self = (SimulationObjectPtr)shared_from_this();
// at first, remove from calendar
if (m_currCalendar)
{
m_currCalendar->remove(self);
m_currCalendar = nullptr;
}
// then reset time
m_simTimeNext = 0;
}
}