MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
CustomExceptions.hpp
Go to the documentation of this file.
1#ifndef CDA_RAIL_CUSTOMEXCEPTIONS_HPP
2#define CDA_RAIL_CUSTOMEXCEPTIONS_HPP
3
4#include "StringHelper.hpp"
5
6#include <algorithm>
7#include <array>
8#include <cctype>
9#include <cstddef>
10#include <exception>
11#include <initializer_list>
12#include <string>
13#include <string_view>
14
21class CustomException : public std::exception {
22private:
23 std::string m_error_message;
24
25protected:
30 explicit CustomException(std::string_view const message)
31 : m_error_message(message) {};
32
33public:
38 [[nodiscard]] const char* what() const noexcept override {
39 return m_error_message.c_str();
40 }
41};
42
47public:
51 ModelCreationException() : CustomException("Model creation failed.") {}
56 explicit ModelCreationException(std::string_view const message)
57 : CustomException(message) {}
58};
59
64public:
68 ExportException() : CustomException("Export failed.") {}
73 explicit ExportException(std::string_view const message)
74 : CustomException(message) {}
75};
76
81public:
85 ConsistencyException() : CustomException("Consistency check failed.") {}
90 explicit ConsistencyException(std::string_view const message)
91 : CustomException(message) {}
92};
93
98public:
102 InvalidInputException() : CustomException("Invalid input.") {}
107 explicit InvalidInputException(std::string_view const message)
108 : CustomException(message) {}
109};
110
115public:
119 ImportException() : CustomException("Import failed.") {}
125 explicit ImportException(std::string_view const importName)
127 concatenate_string_views({"Import of ", importName, " failed."})) {}
128};
129
134public:
139 : CustomException("Some vertex specified does not exist.") {}
140
144 explicit VertexNotExistentException(std::string_view const vertexName)
146 {"Vertex ", vertexName, " does not exist"})) {}
147
151 explicit VertexNotExistentException(size_t const vertexId)
153 {"Vertex with ID ", std::to_string(vertexId), " does not exist"})) {
154 }
155};
156
161public:
166 : CustomException("Some edge specified does not exist.") {}
167
171 explicit EdgeNotExistentException(std::string_view const edgeName)
173 concatenate_string_views({"Edge ", edgeName, " does not exist."})) {
174 }
175
179 explicit EdgeNotExistentException(size_t const edgeId)
181 {"Edge with ID ", std::to_string(edgeId), " does not exist."})) {}
182
187 explicit EdgeNotExistentException(size_t const source, size_t const target)
189 "Edge connecting vertices with IDs ",
190 std::to_string(source),
191 "->",
192 std::to_string(target),
193 " does not exist.",
194 })) {}
195
198 explicit EdgeNotExistentException(const std::string_view source,
199 const std::string_view target)
201 {"Edge connecting ", source, "->", target, " does not exist."})) {}
202};
203
208public:
213 : CustomException("Some train specified does not exist.") {}
214
217 explicit TrainNotExistentException(std::string_view const trainName)
219 {"Train ", trainName, " does not exist."})) {}
220
223 explicit TrainNotExistentException(size_t const trainId)
225 {"Train with ID ", std::to_string(trainId), " does not exist."})) {}
226};
227
232public:
238 : CustomException("Some station specified does not exist.") {}
239
243 explicit StationNotExistentException(std::string_view const stationName)
245 {"Station ", stationName, " does not exist."})) {}
246};
247
252public:
258 : CustomException("Some schedule specified does not exist.") {}
259
263 explicit ScheduleNotExistentException(std::string_view const scheduleName)
265 {"Schedule ", scheduleName, " does not exist."})) {}
266
270 explicit ScheduleNotExistentException(size_t const scheduleId)
272 "Schedule with ID ",
273 std::to_string(scheduleId),
274 " does not exist.",
275 })) {}
276};
277
289inline void throw_if_less(double const value, double const threshold,
290 std::string_view const name, bool inclusive) {
291 if (inclusive) {
292 if (value <= threshold) {
294 name,
295 " must be strictly larger than ",
296 std::to_string(threshold),
297 ", but is ",
298 std::to_string(value),
299 ".",
300 }));
301 }
302 } else {
303 if (value < threshold) {
305 name,
306 " must be at least ",
307 std::to_string(threshold),
308 ", but is ",
309 std::to_string(value),
310 ".",
311 }));
312 }
313 }
314};
315
323inline void throw_if_less_than(double const value, double const threshold,
324 std::string_view const name) {
325 throw_if_less(value, threshold, name, false);
326};
327
337inline void throw_if_less_than_or_equal(double const value,
338 double const threshold,
339 std::string_view const name) {
340 throw_if_less(value, threshold, name, true);
341};
342
349inline void throw_if_negative(double const value, std::string_view const name) {
350 throw_if_less_than(value, 0, name);
351};
352
360inline void throw_if_non_positive(double const value, double tolerance,
361 std::string_view const name) {
362 throw_if_less_than_or_equal(value, tolerance, name);
363};
364
371inline void throw_if_non_positive(double const value,
372 std::string_view const name) {
373 throw_if_non_positive(value, 0.0, name);
374};
375
381inline void throw_if_non_positive(int const value,
382 std::string_view const name) {
383 throw_if_non_positive(static_cast<double>(value), 0.0, name);
384};
385
407inline void throw_if_invalid_folder_name(std::string_view const folderName) {
408 if (folderName.empty()) {
409 throw InvalidInputException("Folder name must not be empty.");
410 }
411
412 if (folderName.size() > 255) {
414 "Folder name must not be longer than 255 characters.");
415 }
416
417 const auto is_allowed_character = [](char const c) {
418 const bool is_uppercase_letter = (c >= 'A' && c <= 'Z');
419 const bool is_lowercase_letter = (c >= 'a' && c <= 'z');
420 const bool is_digit = (c >= '0' && c <= '9');
421 const bool is_allowed_symbol =
422 (c == '_') || (c == '-') || (c == '.') || (c == ' ');
423
424 return is_uppercase_letter || is_lowercase_letter || is_digit ||
425 is_allowed_symbol;
426 };
427
428 if (!std::ranges::all_of(folderName, is_allowed_character)) {
430 "Folder name contains unsupported characters. Allowed are A-Z, a-z, "
431 "0-9, '_', '-', '.', and space.");
432 }
433
434 if (folderName.front() == ' ') {
435 throw InvalidInputException("Folder name must not start with a space.");
436 }
437
438 if (folderName.back() == ' ' || folderName.back() == '.') {
440 "Folder name must not end with a space or a dot.");
441 }
442
443 std::string_view windows_base_name =
444 folderName.substr(0, folderName.find('.'));
445
446 // Trim trailing spaces/dots: find last char that is neither ' ' nor '.'.
447 std::size_t const last_non_trim_character =
448 windows_base_name.find_last_not_of(" .");
449 windows_base_name =
450 (last_non_trim_character == std::string_view::npos)
451 ? std::string_view{}
452 : windows_base_name.substr(0, last_non_trim_character + 1);
453
454 const auto matches_reserved_name = [&windows_base_name](
455 std::string_view reserved_name) {
456 if (windows_base_name.size() != reserved_name.size()) {
457 return false;
458 }
459
460 // Safe here by design: input was validated against a conservative
461 // ASCII allowlist, so byte-wise ASCII case folding is sufficient.
462 return std::ranges::equal(
463 windows_base_name, reserved_name,
464 [](char const inputChar, char const reservedChar) {
465 return static_cast<char>(::toupper(
466 static_cast<unsigned char>(inputChar))) == reservedChar;
467 });
468 };
469
470 static constexpr std::array<std::string_view, 22> RESERVED_NAMES = {
471 "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4",
472 "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3",
473 "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9",
474 };
475
476 const bool is_reserved_name =
477 std::ranges::any_of(RESERVED_NAMES, matches_reserved_name);
478
479 if (is_reserved_name) {
481 "Folder name must not be a Windows reserved device name.");
482 }
483}
484
485} // namespace cda_rail::exceptions
486
487#endif // CDA_RAIL_CUSTOMEXCEPTIONS_HPP
ConsistencyException(std::string_view const message)
Constructs with a custom consistency-check error message.
Definition CustomExceptions.hpp:90
ConsistencyException()
Constructs with the default consistency-check error message.
Definition CustomExceptions.hpp:85
CustomException(std::string_view const message)
Constructs the exception with a custom error message.
Definition CustomExceptions.hpp:30
const char * what() const noexcept override
Provides the error message stored in this exception.
Definition CustomExceptions.hpp:38
EdgeNotExistentException(std::string_view const edgeName)
Raised when an edge with the specified name does not exist.
Definition CustomExceptions.hpp:171
EdgeNotExistentException(const std::string_view source, const std::string_view target)
Creates an exception for a missing edge between two named vertices.
Definition CustomExceptions.hpp:198
EdgeNotExistentException(size_t const source, size_t const target)
Exception for a missing edge between two vertices.
Definition CustomExceptions.hpp:187
EdgeNotExistentException()
Constructs with a generic edge-not-found message.
Definition CustomExceptions.hpp:165
EdgeNotExistentException(size_t const edgeId)
Creates an exception for an edge that does not exist, identified by its ID.
Definition CustomExceptions.hpp:179
ExportException()
Constructs with the default export error message.
Definition CustomExceptions.hpp:68
ExportException(std::string_view const message)
Creates an exception with a custom export error message.
Definition CustomExceptions.hpp:73
ImportException()
Constructs with the default import error message.
Definition CustomExceptions.hpp:119
ImportException(std::string_view const importName)
Creates an ImportException with a formatted message that names the failed import.
Definition CustomExceptions.hpp:125
Thrown when input values do not satisfy expected constraints.
Definition CustomExceptions.hpp:97
InvalidInputException()
Constructs with the default invalid-input error message.
Definition CustomExceptions.hpp:102
InvalidInputException(std::string_view const message)
Constructs an InvalidInputException with a custom error message.
Definition CustomExceptions.hpp:107
ModelCreationException(std::string_view const message)
Constructs with a custom model creation error message.
Definition CustomExceptions.hpp:56
ModelCreationException()
Constructs with the default model creation error message.
Definition CustomExceptions.hpp:51
ScheduleNotExistentException()
Constructs the exception with a default message indicating a schedule does not exist.
Definition CustomExceptions.hpp:257
ScheduleNotExistentException(size_t const scheduleId)
Constructs an exception for a missing schedule identified by ID.
Definition CustomExceptions.hpp:270
ScheduleNotExistentException(std::string_view const scheduleName)
Constructs an exception for a schedule that does not exist.
Definition CustomExceptions.hpp:263
StationNotExistentException(std::string_view const stationName)
Constructs an exception for a station that does not exist, identified by name.
Definition CustomExceptions.hpp:243
StationNotExistentException()
Initializes the exception with a default message indicating no station was found.
Definition CustomExceptions.hpp:237
TrainNotExistentException(size_t const trainId)
Creates an exception for a train not found by ID.
Definition CustomExceptions.hpp:223
TrainNotExistentException()
Constructs with a generic train-not-found message.
Definition CustomExceptions.hpp:212
TrainNotExistentException(std::string_view const trainName)
Creates an exception for a missing train identified by name.
Definition CustomExceptions.hpp:217
VertexNotExistentException(std::string_view const vertexName)
Exception indicating a vertex with the specified name does not exist.
Definition CustomExceptions.hpp:144
VertexNotExistentException()
Creates an exception indicating a vertex does not exist.
Definition CustomExceptions.hpp:138
VertexNotExistentException(size_t const vertexId)
Constructs for a missing vertex identified by ID.
Definition CustomExceptions.hpp:151
Definition CustomExceptions.hpp:15
void throw_if_non_positive(double const value, double tolerance, std::string_view const name)
Ensures a value is strictly greater than a tolerance.
Definition CustomExceptions.hpp:360
void throw_if_invalid_folder_name(std::string_view const folderName)
Validates that a folder name is portable across Linux, Windows, and macOS.
Definition CustomExceptions.hpp:407
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_less_than_or_equal(double const value, double const threshold, std::string_view const name)
Validates that a value is greater than a threshold.
Definition CustomExceptions.hpp:337
void throw_if_less(double const value, double const threshold, std::string_view const name, bool inclusive)
Validates that a value meets a minimum threshold constraint.
Definition CustomExceptions.hpp:289
void throw_if_negative(double const value, std::string_view const name)
Validates that a value is non-negative.
Definition CustomExceptions.hpp:349
std::string concatenate_string_views(std::initializer_list< std::string_view > parts)
Concatenates multiple string views into a single string.
Definition StringHelper.cpp:11