MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
cda_rail::TrainList Class Reference

Container for trains with name-based indexing. More...

#include <Train.hpp>

Public Member Functions

 TrainList ()=default
 Constructs an empty train list.
 TrainList (std::filesystem::path const &p)
 Constructs a train list by importing trains.json from a directory.
 TrainList (std::string const &path)
 Constructs a train list by importing from a path string.
 TrainList (char const *const path)
 Constructs a train list from a C-string path.
constexpr auto begin () const
 Read-only iterator to the first train.
constexpr auto end () const
 Read-only iterator past the last train.
constexpr auto cbegin () const
 Const iterator to the first train.
constexpr auto cend () const
 Gets a const iterator past the end of the train list.
constexpr auto crbegin () const
 Read-only reverse iterator to the last train.
constexpr auto crend () const
 Read-only reverse iterator before the first train.
size_t size () const
 Retrieves the number of trains in the list.
size_t get_train_index (const std::string &name) const
 Returns the index of a train by name.
Train const & get_train (size_t index) const
 Returns a train by index.
Train const & get_train (std::string const &name) const
 Retrieves a train by name.
Train & editable_train (size_t index)
 Retrieves the train at the specified index.
Train & editable_train (std::string const &name)
 Accesses a train by name for editing.
size_t get_number_of_trains () const
 Queries the number of trains in the list.
bool has_train (const std::string &name) const
 Checks whether a train with the given name exists.
bool has_train (size_t const index) const
 Checks whether a train index is valid.
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.
size_t add_train (Train train)
 Adds a train to the list.
void rename_train (std::string const &old_name, std::string new_name)
 Renames an existing train while preserving its index and properties.
void export_trains (std::string const &path) const
 Exports all trains to trains.json in the target directory.
void export_trains (char const *const path) const
 Exports all trains to trains.json in the target directory.
void export_trains (std::filesystem::path const &p) const
 Exports all trains to trains.json in the target directory.
void throw_if_train_not_exist (std::string const &train_name) const
 Ensures a train with the given name exists.
void throw_if_train_not_exist (size_t const trainIndex) const
 Validates that a train exists at the specified index.

Static Public Member Functions

static TrainList import_trains (std::string const &path)
 Creates a train list by importing trains from a directory.
static TrainList import_trains (char const *const path)
 Imports a train list from a directory.
static TrainList import_trains (std::filesystem::path const &p)
 Imports a train list from a filesystem path.

Detailed Description

Container for trains with name-based indexing.

Maintains a sequence of trains and a lookup map from train name to index.

Invariant
train_name_to_index contains exactly one entry per train in trains.
For every train name n, train_name_to_index.at(n) is the index of that train in trains.
All train names in trains are unique.

Constructor & Destructor Documentation

◆ TrainList() [1/4]

cda_rail::TrainList::TrainList ( )
default

Constructs an empty train list.

◆ TrainList() [2/4]

cda_rail::TrainList::TrainList ( std::filesystem::path const & p)
explicit

Constructs a train list by importing trains.json from a directory.

Parameters
pPath to the directory containing trains.json.
Exceptions
cda_rail::exceptions::ImportExceptionIf p is not an existing directory.
cda_rail::exceptions::ConsistencyExceptionIf imported train names are duplicated.
cda_rail::exceptions::InvalidInputExceptionIf imported train attributes violate train constraints.

◆ TrainList() [3/4]

cda_rail::TrainList::TrainList ( std::string const & path)
inlineexplicit

Constructs a train list by importing from a path string.

Parameters
pathPath to the directory containing trains.json.

◆ TrainList() [4/4]

cda_rail::TrainList::TrainList ( char const *const path)
inlineexplicit

Constructs a train list from a C-string path.

Parameters
pathPath to the directory containing trains.json.
Exceptions
cda_rail::exceptions::ImportExceptionif the path is not an existing directory.
cda_rail::exceptions::ConsistencyExceptionif imported train names are duplicated.
cda_rail::exceptions::InvalidInputExceptionif imported train attributes violate train constraints.

Member Function Documentation

◆ add_train() [1/2]

size_t cda_rail::TrainList::add_train ( std::string const & name,
double const length,
double const maxSpeed,
double const acceleration,
double const deceleration,
bool const tim = true )
inline

Creates and adds a train from its attributes.

Parameters
nameTrain name.
lengthTrain length in meters.
maxSpeedMaximum speed in m/s.
accelerationMaximum acceleration in m/s^2.
decelerationMaximum deceleration in m/s^2.
timTrain integrity monitoring flag.
Returns
Index of the added train.
Exceptions
cda_rail::exceptions::ConsistencyExceptionIf a train with name already exists.
cda_rail::exceptions::InvalidInputExceptionIf train attributes are invalid.

◆ add_train() [2/2]

size_t cda_rail::TrainList::add_train ( Train train)

Adds a train to the list.

Parameters
trainTrain to add.
Returns
Index of the added train.
Exceptions
cda_rail::exceptions::ConsistencyExceptionIf a train with the same name already exists.

◆ begin()

auto cda_rail::TrainList::begin ( ) const
inlinenodiscardconstexpr

Read-only iterator to the first train.

◆ cbegin()

auto cda_rail::TrainList::cbegin ( ) const
inlinenodiscardconstexpr

Const iterator to the first train.

Returns
Const iterator to the beginning of the train collection.

◆ cend()

auto cda_rail::TrainList::cend ( ) const
inlinenodiscardconstexpr

Gets a const iterator past the end of the train list.

Returns
A const iterator past the end of the train list.

◆ crbegin()

auto cda_rail::TrainList::crbegin ( ) const
inlinenodiscardconstexpr

Read-only reverse iterator to the last train.

◆ crend()

auto cda_rail::TrainList::crend ( ) const
inlinenodiscardconstexpr

Read-only reverse iterator before the first train.

◆ editable_train() [1/2]

Train & cda_rail::TrainList::editable_train ( size_t index)
inlinenodiscard

Retrieves the train at the specified index.

Parameters
indexIndex of the train.
Returns
Reference to the train at the given index.
Exceptions
cda_rail::exceptions::TrainNotExistentExceptionIf index is out of range.

◆ editable_train() [2/2]

Train & cda_rail::TrainList::editable_train ( std::string const & name)
inlinenodiscard

Accesses a train by name for editing.

Parameters
nameThe name of the train to retrieve.
Returns
A mutable reference to the requested train.
Exceptions
cda_rail::exceptions::TrainNotExistentExceptionIf no train with the given name exists.

◆ end()

auto cda_rail::TrainList::end ( ) const
inlinenodiscardconstexpr

Read-only iterator past the last train.

◆ export_trains() [1/3]

void cda_rail::TrainList::export_trains ( char const *const path) const
inline

Exports all trains to trains.json in the target directory.

Parameters
pathPath to the export directory.

◆ export_trains() [2/3]

void cda_rail::TrainList::export_trains ( std::filesystem::path const & p) const

Exports all trains to trains.json in the target directory.

Creates the directory if needed and writes one JSON object keyed by train name.

Parameters
pPath to the export directory.
Exceptions
cda_rail::exceptions::ExportExceptionIf the directory cannot be created.

◆ export_trains() [3/3]

void cda_rail::TrainList::export_trains ( std::string const & path) const
inline

Exports all trains to trains.json in the target directory.

Parameters
pathPath to the export directory.

◆ get_number_of_trains()

size_t cda_rail::TrainList::get_number_of_trains ( ) const
inlinenodiscard

Queries the number of trains in the list.

Returns
The number of trains.

◆ get_train() [1/2]

const cda_rail::Train & cda_rail::TrainList::get_train ( size_t index) const
nodiscard

Returns a train by index.

Parameters
indexIndex of the train.
Returns
Constant reference to the requested train.
Exceptions
cda_rail::exceptions::TrainNotExistentExceptionIf index is out of range.

◆ get_train() [2/2]

Train const & cda_rail::TrainList::get_train ( std::string const & name) const
inlinenodiscard

Retrieves a train by name.

Parameters
nameTrain name.
Returns
Constant reference to the train.
Exceptions
cda_rail::exceptions::TrainNotExistentExceptionIf no train with the specified name exists.

◆ get_train_index()

size_t cda_rail::TrainList::get_train_index ( const std::string & name) const
nodiscard

Returns the index of a train by name.

Parameters
nameTrain name.
Returns
Index of the train in the internal vector.
Exceptions
cda_rail::exceptions::TrainNotExistentExceptionIf no train with name exists.

◆ has_train() [1/2]

bool cda_rail::TrainList::has_train ( const std::string & name) const
inlinenodiscard

Checks whether a train with the given name exists.

Parameters
nameTrain name.
Returns
true if a train with name exists, otherwise false.

◆ has_train() [2/2]

bool cda_rail::TrainList::has_train ( size_t const index) const
inlinenodiscard

Checks whether a train index is valid.

Parameters
indexTrain index.
Returns
true if index refers to an existing train, otherwise false.

◆ import_trains() [1/3]

TrainList cda_rail::TrainList::import_trains ( char const *const path)
inlinestaticnodiscard

Imports a train list from a directory.

Reads the trains.json file from the specified directory.

Parameters
pathPath to the directory containing trains.json.
Returns
The imported train list.
Exceptions
cda_rail::exceptions::ImportExceptionif path is not an existing directory.
cda_rail::exceptions::ConsistencyExceptionif the imported data contains duplicate train names.
cda_rail::exceptions::InvalidInputExceptionif any imported train attributes violate train constraints.

◆ import_trains() [2/3]

TrainList cda_rail::TrainList::import_trains ( std::filesystem::path const & p)
inlinestaticnodiscard

Imports a train list from a filesystem path.

Parameters
pPath to the directory containing trains.json.
Returns
Imported train list.

◆ import_trains() [3/3]

TrainList cda_rail::TrainList::import_trains ( std::string const & path)
inlinestaticnodiscard

Creates a train list by importing trains from a directory.

Parameters
pathPath to the directory containing trains.json.
Returns
The imported train list.

◆ rename_train()

void cda_rail::TrainList::rename_train ( std::string const & old_name,
std::string new_name )

Renames an existing train while preserving its index and properties.

Parameters
old_nameCurrent train name.
new_nameNew train name.
Exceptions
cda_rail::exceptions::TrainNotExistentExceptionIf no train named old_name exists.
cda_rail::exceptions::ConsistencyExceptionIf new_name is already used by another train.

◆ size()

size_t cda_rail::TrainList::size ( ) const
inlinenodiscard

Retrieves the number of trains in the list.

Returns
The total count of trains.

◆ throw_if_train_not_exist() [1/2]

void cda_rail::TrainList::throw_if_train_not_exist ( size_t const trainIndex) const
inline

Validates that a train exists at the specified index.

Parameters
trainIndexIndex of the train to validate.
Exceptions
TrainNotExistentExceptionIf no train exists at the given index.

◆ throw_if_train_not_exist() [2/2]

void cda_rail::TrainList::throw_if_train_not_exist ( std::string const & train_name) const
inline

Ensures a train with the given name exists.

Parameters
train_nameThe name of the train to validate.
Exceptions
TrainNotExistentExceptionif no train with the given name exists.

The documentation for this class was generated from the following files: