This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Road.cpp
189 lines (163 loc) · 8.75 KB
/
Road.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "Road.h"
#include "Vehicle.h"
#include "TrafficLight.h"
#include "DesignByContract.h"
#include "BusStop.h"
#include "CrossRoad.h"
#include <typeinfo>
Road::Road(unsigned int length, const string &roadName) : length(length), roadName(roadName) , init(this) {
REQUIRE( *typeid(length).name() == 'j' && length >= 0, "Length is not a number or is negative");
REQUIRE( *typeid(roadName).name() == 'N' && roadName.length() > 0 , "Road has no name" );
ENSURE(Road::length == length , "Length was not properly initialized");
ENSURE(Road::roadName == roadName , "roadName was not properly initialized");
ENSURE(properlyInitialized() , "constructor must end in properlyInitialized state");
}
Road::Road() : length(0) , roadName("") , init(this) {
ENSURE(length == 0 , "Length was not properly initialized");
ENSURE(roadName.empty(), "roadName was not properly initialized");
ENSURE(properlyInitialized() , "constructor must end in properlyInitialized state");
}
bool Road::properlyInitialized() const {
return init == this;
}
unsigned int Road::getLength() const {
// Returns the length of the road
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling getLength");
return length;
}
void Road::setLength(unsigned int newLength) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling setLength");
REQUIRE( *typeid(newLength).name() == 'j' , "setLength was called with invalid parameter");
Road::length = newLength;
ENSURE( Road::length == newLength , "setLength was failed");
}
const string &Road::getRoadName() const {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling getRoadName");
return roadName;
}
void Road::setRoadName(const string &newRoadName) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling setRoadName");
REQUIRE( *typeid(newRoadName).name() == 'N' && newRoadName.length() > 0 , "setRoadName was called with invalid parameter");
Road::roadName = newRoadName;
ENSURE( Road::roadName == newRoadName , "setRoadName failed");
}
const vector<TrafficLight *> &Road::getTrafficLights() const {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling getTrafficLights");
return trafficLights;
}
void Road::setTrafficLights(const vector<TrafficLight *> &newTrafficLights) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling setTrafficLights");
REQUIRE(*typeid(newTrafficLights).name() == 'S' , "setTrafficLights was called with invalid parameter");
Road::trafficLights = newTrafficLights;
ENSURE(Road::trafficLights == newTrafficLights , "setTrafficLights failed");
}
TrafficLight* &Road::getTrafficLight(unsigned int index) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling getTrafficLight");
REQUIRE( *typeid(index).name() == 'j' , "Index was not a number when calling getTrafficLight");
REQUIRE( index < trafficLights.size() , "Index was out of range when calling getTrafficLight");
return trafficLights.at(index);
}
void Road::addLight(TrafficLight *newLight) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling addLight");
REQUIRE(*typeid(newLight).name() == 'P' , "addLight was called with invalid parameter");
REQUIRE(newLight->properlyInitialized(), "addLight was called with uninitialized parameter");
unsigned int* oldSize = new unsigned int;
*oldSize = static_cast<unsigned int>(trafficLights.size());
trafficLights.push_back(newLight);
ENSURE(*oldSize == trafficLights.size() - 1 , "addLight failed");
delete oldSize;
}
const vector<Vehicle *> &Road::getVehicles() const {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling getVehicles");
return vehicles;
}
void Road::setVehicles(const vector<Vehicle *> &newVehicles) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling setVehicles");
REQUIRE(*typeid(newVehicles).name() == 'S' , "setVehicles was called with invalid parameter");
Road::vehicles = newVehicles;
ENSURE(Road::vehicles == newVehicles , "setVehicles failed");
}
Vehicle *&Road::getVehicle(unsigned int index) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling getVehicle");
REQUIRE( *typeid(index).name() == 'j' , "Index was not a number when calling getVehicle");
REQUIRE( index < vehicles.size() , "Index was out of range when calling getVehicle");
return vehicles.at(index);
}
void Road::addVehicle(Vehicle *newVehicle) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling addVehicle");
REQUIRE(*typeid(newVehicle).name() == 'P' , "addVehicle was called with invalid parameter");
REQUIRE(newVehicle->properlyInitialized() , "addVehicle was called with uninitialized parameter");
unsigned int* oldSize = new unsigned int;
*oldSize = static_cast<unsigned int>(vehicles.size());
vehicles.push_back(newVehicle);
newVehicle->setRoad(this);
ENSURE( *oldSize == vehicles.size() - 1 , "addVehicle failed");
ENSURE( newVehicle->getRoad() == this , "addVehicle failed");
delete oldSize;
}
void Road::removeVehicle(Vehicle *oldVehicle) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling removeVehicle");
REQUIRE(*typeid(oldVehicle).name() == 'P' , "removeVehicle was called with invalid parameter");
REQUIRE(oldVehicle->properlyInitialized() , "removeVehicle was called with uninitialized parameter");
unsigned int* oldSize = new unsigned int;
*oldSize = static_cast<unsigned int>(vehicles.size());
for (long unsigned int i = 0; i < this->vehicles.size(); ++i) {
if (oldVehicle == this->vehicles.at(i)){
this->vehicles.erase(vehicles.begin() + i);
}
}
ENSURE(*oldSize == vehicles.size() + 1 , "removeVehicle failed");
delete oldSize;
}
const vector<BusStop *> &Road::getBusStops() {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling getBusStops");
return busStops;
}
void Road::setBusStops(const vector<BusStop *> &newBusStops) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling setBusStops");
Road::busStops = newBusStops;
ENSURE(busStops == newBusStops, "busStops was not assigned to newBusStops, when calling setBusStops");
}
void Road::addBusStop(BusStop *newBusStop) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling addBusStop");
REQUIRE(*typeid(newBusStop).name() == 'P' , "addBusStop was called with invalid parameter: wrong type");
REQUIRE(newBusStop->properlyInitialized(), "bus stop isn't properly initialized");
unsigned int originalSize = busStops.size();
busStops.push_back(newBusStop);
ENSURE(busStops.size() == originalSize + 1, "newBusStop wasn't appended in vector busStops, when calling addBusStop");
ENSURE(busStops[originalSize] == newBusStop, "last item in vector busStops is not equal to newBusStop, when calling addBusStop");
}
const vector<CrossRoad *> &Road::getCrossRaods() {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling getCrossRaods");
return crossRoads;
}
void Road::setCrossRaods(const vector<CrossRoad *> &newCrossRaods) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling setCrossRaods");
Road::crossRoads = newCrossRaods;
ENSURE(crossRoads == newCrossRaods, "crossRoads was not assigned to newCrossRoads, when calling newCrossRoads");
}
void Road::addCrossRoad(CrossRoad *crossRoad) {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling addCrossRoad");
REQUIRE(*typeid(crossRoad).name() == 'P' , "addCrossRoad was called with invalid parameter: wrong type");
REQUIRE(crossRoad->properlyInitialized(), "crossroad wasn't properly initialized");
unsigned int originalSize = static_cast<unsigned int>(crossRoads.size());
crossRoads.push_back(crossRoad);
ENSURE(crossRoads.size() == originalSize + 1, "crossRoads didn't change in size, when calling addCrossRoad");
ENSURE(crossRoads[originalSize] == crossRoad, "Last item in crossRoads is not the same as crossRoad, when calling addCrossRoad");
}
int Road::getVehicleAmount() {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling getVehicleAmount");
return static_cast<int>(this->vehicles.size());
}
int Road::getTrafficLightsAmount() {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling getTrafficLightsAmount");
return static_cast<int>(this->trafficLights.size());
}
void Road::print() {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling print");
cout << '\t' << "-> Name: " << this->getRoadName() << endl;
cout << '\t' << "-> Length: " << this->getLength() << endl;
}
Road::~Road() {
REQUIRE(properlyInitialized() , "Road wasn't initialized when calling destructor");
}