MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
Train.hpp
Go to the documentation of this file.
1#ifndef CDA_RAIL_DATASTRUCTURE_TRAIN_HPP
2#define CDA_RAIL_DATASTRUCTURE_TRAIN_HPP
3
5#include "Definitions.hpp"
6
7#include <cstddef>
8#include <filesystem>
9#include <string>
10#include <unordered_map>
11#include <utility>
12#include <vector>
13
14namespace cda_rail {
26class Train {
27private:
28 std::string m_name; // name of train
29 double m_length; // length of train in m: >= 0
30 double m_max_speed; // maximal speed of train in m/s: >= MIN_NON_ZERO
31 double m_acceleration; // maximal acceleration in m/s^2: >= MIN_NON_ZERO
32 double m_deceleration; // maximal deceleration in m/s^2: >= MIN_NON_ZERO
33 bool m_tim{true}; // indicates if train has a train integrity monitoring:
34 // default true
35
36public:
53 Train(std::string name, double length, double max_speed, double acceleration,
54 double deceleration, bool tim = true);
55 // Rule of 0 suffices
56
57 /*
58 * GETTER
59 */
65 [[nodiscard]] std::string const& get_name() const { return m_name; };
66
72 [[nodiscard]] double get_length() const { return m_length; };
73
79 [[nodiscard]] double get_max_speed() const { return m_max_speed; };
80
86 [[nodiscard]] double get_acceleration() const { return m_acceleration; };
87
93 [[nodiscard]] double get_deceleration() const { return m_deceleration; };
94
100 [[nodiscard]] bool has_tim() const { return m_tim; };
101
102 /*
103 * SETTER
104 */
105private:
106 // Only TrainList may rename a train, because it has to keep its name to
107 // index mapping in sync. Use TrainList::rename_train instead.
108 friend class TrainList;
114 void set_name(std::string name) { m_name = std::move(name); };
115
116public:
125 void set_length(double const length) {
126 cda_rail::exceptions::throw_if_negative(length, "Train length");
127 m_length = length;
128 };
129
137 void set_max_speed(double const maxSpeed) {
139 "Train max speed");
140 m_max_speed = maxSpeed;
141 };
142
151 void set_acceleration(double const acceleration) {
153 "Train acceleration");
154 m_acceleration = acceleration;
155 }
156
165 void set_deceleration(double const deceleration) {
167 "Train deceleration");
168 m_deceleration = deceleration;
169 };
170
176 void set_tim_value(bool const tim) { m_tim = tim; };
177
181 void set_tim() { set_tim_value(true); };
182
186 void set_no_tim() { set_tim_value(false); };
187};
188
201private:
202 std::vector<Train> trains{}; // list of trains
203 std::unordered_map<std::string, size_t>
204 train_name_to_index{}; // helper struct: for any train name store its
205 // index in the vector above
206
207public:
208 /*
209 * CONSTRUCTORS
210 */
211
215 TrainList() = default;
216
228 explicit TrainList(std::filesystem::path const& p);
229
235 explicit TrainList(std::string const& path)
236 : TrainList(std::filesystem::path(path)) {};
237
249 explicit TrainList(char const* const path)
250 : TrainList(std::filesystem::path(path)) {};
251
252 // Rule of 0 suffices
253
254 /*
255 * ITERATORS (for range-based for loops) that do not allow modification of the
256 * underlying data
257 */
258
260 [[nodiscard]] constexpr auto begin() const { return trains.cbegin(); };
262 [[nodiscard]] constexpr auto end() const { return trains.cend(); };
267 [[nodiscard]] constexpr auto cbegin() const { return trains.cbegin(); };
272 [[nodiscard]] constexpr auto cend() const { return trains.cend(); };
274 [[nodiscard]] constexpr auto crbegin() const { return trains.crbegin(); };
276 [[nodiscard]] constexpr auto crend() const { return trains.crend(); };
277
283 [[nodiscard]] size_t size() const { return get_number_of_trains(); };
284
285 /*
286 * GETTER
287 */
288
297 [[nodiscard]] size_t get_train_index(const std::string& name) const;
298
307 [[nodiscard]] Train const& get_train(size_t index) const;
308
317 [[nodiscard]] Train const& get_train(std::string const& name) const {
318 return get_train(get_train_index(name));
319 };
320
329 [[nodiscard]] Train& editable_train(size_t index) {
330 if (!has_train(index)) {
332 }
333 return trains.at(index);
334 };
335
344 [[nodiscard]] Train& editable_train(std::string const& name) {
345 return editable_train(get_train_index(name));
346 }
347
353 [[nodiscard]] size_t get_number_of_trains() const { return trains.size(); };
354
361 [[nodiscard]] bool has_train(const std::string& name) const {
362 return train_name_to_index.contains(name);
363 };
364
371 [[nodiscard]] bool has_train(size_t const index) const {
372 return (index < get_number_of_trains());
373 };
374
375 /*
376 * TRAIN SETTERS
377 */
378
394 size_t add_train(std::string const& name, double const length,
395 double const maxSpeed, double const acceleration,
396 double const deceleration, bool const tim = true) {
397 return add_train({name, length, maxSpeed, acceleration, deceleration, tim});
398 };
399
408 size_t add_train(Train train);
409
420 void rename_train(std::string const& old_name, std::string new_name);
421
422 // No removal method for now (academic code): Is assumed that trains are only
423 // added and no "mistakes" are made.
424
425 /*
426 * EXPORT/IMPORT
427 */
428
434 void export_trains(std::string const& path) const {
435 export_trains(std::filesystem::path(path));
436 };
437
443 void export_trains(char const* const path) const {
444 export_trains(std::filesystem::path(path));
445 };
446
457 void export_trains(std::filesystem::path const& p) const;
458
465 [[nodiscard]] static TrainList import_trains(std::string const& path) {
466 return TrainList(path);
467 };
468
483 [[nodiscard]] static TrainList import_trains(char const* const path) {
484 return TrainList(path);
485 };
486
493 [[nodiscard]] static TrainList import_trains(std::filesystem::path const& p) {
494 return TrainList(p);
495 };
496
504 void throw_if_train_not_exist(std::string const& train_name) const {
505 if (!has_train(train_name)) {
507 }
508 }
509
515 void throw_if_train_not_exist(size_t const trainIndex) const {
516 if (!has_train(trainIndex)) {
518 }
519 }
520};
521} // namespace cda_rail
522
523#endif // CDA_RAIL_DATASTRUCTURE_TRAIN_HPP
size_t size() const
Retrieves the number of trains in the list.
Definition Train.hpp:283
size_t add_train(std::string const &name, double const length, double const maxSpeed, double const acceleration, double const deceleration, bool const tim=true)
Creates and adds a train from its attributes.
Definition Train.hpp:394
void rename_train(std::string const &old_name, std::string new_name)
Renames an existing train while preserving its index and properties.
Definition Train.cpp:81
bool has_train(const std::string &name) const
Checks whether a train with the given name exists.
Definition Train.hpp:361
size_t get_number_of_trains() const
Queries the number of trains in the list.
Definition Train.hpp:353
constexpr auto crend() const
Read-only reverse iterator before the first train.
Definition Train.hpp:276
constexpr auto cbegin() const
Const iterator to the first train.
Definition Train.hpp:267
constexpr auto end() const
Read-only iterator past the last train.
Definition Train.hpp:262
constexpr auto begin() const
Read-only iterator to the first train.
Definition Train.hpp:260
TrainList(std::string const &path)
Constructs a train list by importing from a path string.
Definition Train.hpp:235
Train const & get_train(size_t index) const
Returns a train by index.
Definition Train.cpp:63
Train & editable_train(size_t index)
Retrieves the train at the specified index.
Definition Train.hpp:329
constexpr auto crbegin() const
Read-only reverse iterator to the last train.
Definition Train.hpp:274
bool has_train(size_t const index) const
Checks whether a train index is valid.
Definition Train.hpp:371
constexpr auto cend() const
Gets a const iterator past the end of the train list.
Definition Train.hpp:272
void throw_if_train_not_exist(std::string const &train_name) const
Ensures a train with the given name exists.
Definition Train.hpp:504
static TrainList import_trains(std::string const &path)
Creates a train list by importing trains from a directory.
Definition Train.hpp:465
size_t get_train_index(const std::string &name) const
Returns the index of a train by name.
Definition Train.cpp:55
void export_trains(std::string const &path) const
Exports all trains to trains.json in the target directory.
Definition Train.hpp:434
TrainList(char const *const path)
Constructs a train list from a C-string path.
Definition Train.hpp:249
Train const & get_train(std::string const &name) const
Retrieves a train by name.
Definition Train.hpp:317
TrainList()=default
Constructs an empty train list.
static TrainList import_trains(std::filesystem::path const &p)
Imports a train list from a filesystem path.
Definition Train.hpp:493
static TrainList import_trains(char const *const path)
Imports a train list from a directory.
Definition Train.hpp:483
Train & editable_train(std::string const &name)
Accesses a train by name for editing.
Definition Train.hpp:344
void throw_if_train_not_exist(size_t const trainIndex) const
Validates that a train exists at the specified index.
Definition Train.hpp:515
void export_trains(char const *const path) const
Exports all trains to trains.json in the target directory.
Definition Train.hpp:443
Properties and characteristics of a train.
Definition Train.hpp:26
double get_max_speed() const
Accesses the train's maximum speed.
Definition Train.hpp:79
void set_acceleration(double const acceleration)
Sets the maximum acceleration of the train.
Definition Train.hpp:151
bool has_tim() const
Returns whether the train has train integrity monitoring.
Definition Train.hpp:100
std::string const & get_name() const
Accesses the train's name.
Definition Train.hpp:65
double get_acceleration() const
Retrieves the train's acceleration.
Definition Train.hpp:86
void set_length(double const length)
Sets the length of the train.
Definition Train.hpp:125
double get_deceleration() const
Retrieves the train's maximum deceleration.
Definition Train.hpp:93
void set_deceleration(double const deceleration)
Sets the maximum deceleration of the train.
Definition Train.hpp:165
double get_length() const
Retrieves the train's length.
Definition Train.hpp:72
void set_tim_value(bool const tim)
Sets the train integrity monitoring flag to a specific value.
Definition Train.hpp:176
void set_tim()
Enables train integrity monitoring.
Definition Train.hpp:181
friend class TrainList
Definition Train.hpp:108
void set_no_tim()
Disables train integrity monitoring.
Definition Train.hpp:186
void set_max_speed(double const maxSpeed)
Sets the maximum speed of the train.
Definition Train.hpp:137
Train(std::string name, double length, double max_speed, double acceleration, double deceleration, bool tim=true)
Constructs a train with specified dynamic properties.
Definition Train.cpp:15
Thrown when a referenced train does not exist.
Definition CustomExceptions.hpp:207
void throw_if_less_than(double const value, double const threshold, std::string_view const name)
Throws if a value is less than a threshold.
Definition CustomExceptions.hpp:323
void throw_if_negative(double const value, std::string_view const name)
Validates that a value is non-negative.
Definition CustomExceptions.hpp:349
Definition CustomExceptions.hpp:15
constexpr double MIN_NON_ZERO
Definition Definitions.hpp:33