1#ifndef CDA_RAIL_CUSTOMEXCEPTIONS_HPP
2#define CDA_RAIL_CUSTOMEXCEPTIONS_HPP
11#include <initializer_list>
23 std::string m_error_message;
31 : m_error_message(message) {};
38 [[nodiscard]]
const char*
what() const noexcept
override {
39 return m_error_message.c_str();
146 {
"Vertex ", vertexName,
" does not exist"})) {}
153 {
"Vertex with ID ", std::to_string(vertexId),
" does not exist"})) {
181 {
"Edge with ID ", std::to_string(edgeId),
" does not exist."})) {}
189 "Edge connecting vertices with IDs ",
190 std::to_string(source),
192 std::to_string(target),
199 const std::string_view target)
201 {
"Edge connecting ", source,
"->", target,
" does not exist."})) {}
219 {
"Train ", trainName,
" does not exist."})) {}
225 {
"Train with ID ", std::to_string(trainId),
" does not exist."})) {}
245 {
"Station ", stationName,
" does not exist."})) {}
265 {
"Schedule ", scheduleName,
" does not exist."})) {}
273 std::to_string(scheduleId),
290 std::string_view
const name,
bool inclusive) {
292 if (value <= threshold) {
295 " must be strictly larger than ",
296 std::to_string(threshold),
298 std::to_string(value),
303 if (value < threshold) {
306 " must be at least ",
307 std::to_string(threshold),
309 std::to_string(value),
324 std::string_view
const name) {
338 double const threshold,
339 std::string_view
const name) {
361 std::string_view
const name) {
372 std::string_view
const name) {
382 std::string_view
const name) {
408 if (folderName.empty()) {
412 if (folderName.size() > 255) {
414 "Folder name must not be longer than 255 characters.");
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 ==
' ');
424 return is_uppercase_letter || is_lowercase_letter || is_digit ||
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.");
434 if (folderName.front() ==
' ') {
438 if (folderName.back() ==
' ' || folderName.back() ==
'.') {
440 "Folder name must not end with a space or a dot.");
443 std::string_view windows_base_name =
444 folderName.substr(0, folderName.find(
'.'));
447 std::size_t
const last_non_trim_character =
448 windows_base_name.find_last_not_of(
" .");
450 (last_non_trim_character == std::string_view::npos)
452 : windows_base_name.substr(0, last_non_trim_character + 1);
454 const auto matches_reserved_name = [&windows_base_name](
455 std::string_view reserved_name) {
456 if (windows_base_name.size() != reserved_name.size()) {
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;
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",
476 const bool is_reserved_name =
477 std::ranges::any_of(RESERVED_NAMES, matches_reserved_name);
479 if (is_reserved_name) {
481 "Folder name must not be a Windows reserved device name.");
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
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