Modular Megafauna Model 1.1.5
A physiological, dynamic herbivore simulator in C++.
Loading...
Searching...
No Matches
Fauna::InsfileReader Class Reference

Class to read parameters and HFTs from given instruction file. More...

#include <insfile_reader.h>

Public Member Functions

 InsfileReader (const std::string filename)
 Constructor. More...
 
const HftListget_hfts () const
 Get the HFT list that was read from the instruction file. More...
 
const Parametersget_params () const
 Get the global parameters that were read from the instruction file. More...
 

Private Types

enum class  GetValueOpt { RemoveKey , KeepKey }
 How to treat a parameter after it has been parsed by get_value(). More...
 

Private Member Functions

std::shared_ptr< cpptoml::table > get_group_table (const std::string &group_name) const
 Find table with group. More...
 
template<class T >
std::shared_ptr< T > get_value (const std::shared_ptr< cpptoml::table > &table, const std::string &key, const GetValueOpt opt=GetValueOpt::RemoveKey) const
 Retrieve a single value from the TOML file. More...
 
template<class T >
std::shared_ptr< std::vector< T > > get_value_array (const std::shared_ptr< cpptoml::table > &table, const std::string &key, const GetValueOpt opt=GetValueOpt::RemoveKey) const
 Retrieve a value array from the TOML file. More...
 
template<class Expected >
void check_wrong_type (std::shared_ptr< cpptoml::table > table, const std::string &key) const
 Throw an exception if parameter is present, but with wrong type. More...
 
template<class T >
std::shared_ptr< T > find_hft_parameter (const std::shared_ptr< cpptoml::table > &hft_table, const std::string &key, const bool mandatory)
 Retrieve HFT parameter from HFT table itself or one of its groups. More...
 
template<class T >
std::shared_ptr< std::vector< T > > find_hft_array_parameter (const std::shared_ptr< cpptoml::table > &hft_table, const std::string &key, const bool mandatory)
 Like find_hft_parameter(), but for an array of values. More...
 
std::list< std::string > get_all_keys (std::shared_ptr< cpptoml::table > table)
 Iterate through TOML tree and list all "leaves". More...
 
void read_table_forage ()
 Read the TOML table forage. More...
 
void read_table_output ()
 Read the TOML table output. More...
 
void read_table_output_text_tables ()
 Read the TOML table output.text_tables. More...
 
void read_table_simulation ()
 Read the TOML table simulation. More...
 
void remove_qualified_key (std::shared_ptr< cpptoml::table > table, const std::string key) const
 Remove TOML element (in order to indicate it’s been parsed). More...
 
Hft read_hft (const std::shared_ptr< cpptoml::table > &table)
 Construct HFT object from an entry in the array of tables. More...
 

Private Attributes

std::shared_ptr< cpptoml::table > ins
 The root table of the instruction file from cpptoml::parse_file(). More...
 
std::set< std::string > hft_keys_parsed
 All TOML keys in HFTs and HFT groups that have been parsed. More...
 
Parameters params
 
HftList hfts
 

Detailed Description

Class to read parameters and HFTs from given instruction file.

Member Enumeration Documentation

◆ GetValueOpt

enum class Fauna::InsfileReader::GetValueOpt
strongprivate

How to treat a parameter after it has been parsed by get_value().

Enumerator
RemoveKey 

Remove the parameter with remove_qualified_key().

KeepKey 

Re-use parameter.

Constructor & Destructor Documentation

◆ InsfileReader()

InsfileReader::InsfileReader ( const std::string  filename)

Constructor.

Parameters
[in]filenameRelative or absolute file path to the instruction file.
Exceptions
std::runtime_errorIf the instruction file cannot be parsed completely, contains logical errors, or has missing parameters.

Member Function Documentation

◆ check_wrong_type()

template<class Expected >
void InsfileReader::check_wrong_type ( std::shared_ptr< cpptoml::table >  table,
const std::string &  key 
) const
private

Throw an exception if parameter is present, but with wrong type.

Parameters
tableThe TOML table to search through. This could be ins or an HFT table for instance.
keyThe fully qualified TOML key.
Template Parameters
ExpectedExpected type of the parameter key.
Exceptions
wrong_param_typeIf key found, but of wrong type.

◆ find_hft_array_parameter()

template<class T >
std::shared_ptr< std::vector< T > > InsfileReader::find_hft_array_parameter ( const std::shared_ptr< cpptoml::table > &  hft_table,
const std::string &  key,
const bool  mandatory 
)
private

Like find_hft_parameter(), but for an array of values.

Retrieve HFT parameter from HFT table itself or one of its groups.

If the key is not defined in the HFT itself, the groups are checked in the order they are defined until the value is found.

Parameters
hft_tableThe TOML table of the HFT itself.
keyThe string identifier of the parameter, e.g. "digestion.limit".
mandatoryWhether to throw missing_parameter if the value couldn’t be found.
Returns
A pointer to the value. If the value wasn’t found and is not mandatory, the result is NULL.

◆ find_hft_parameter()

template<class T >
std::shared_ptr< T > InsfileReader::find_hft_parameter ( const std::shared_ptr< cpptoml::table > &  hft_table,
const std::string &  key,
const bool  mandatory 
)
private

Retrieve HFT parameter from HFT table itself or one of its groups.

If the key is not defined in the HFT itself, the groups are checked in the order they are defined until the value is found.

Parameters
hft_tableThe TOML table of the HFT itself.
keyThe string identifier of the parameter, e.g. "digestion.limit".
mandatoryWhether to throw missing_parameter if the value couldn’t be found.
Returns
A pointer to the value. If the value wasn’t found and is not mandatory, the result is NULL.

◆ get_all_keys()

std::list< std::string > InsfileReader::get_all_keys ( std::shared_ptr< cpptoml::table >  table)
private

Iterate through TOML tree and list all "leaves".

Parameters
tableA TOML tree or subtree.
Returns
A list of fully qualified TOML keys.

◆ get_group_table()

std::shared_ptr< cpptoml::table > InsfileReader::get_group_table ( const std::string &  group_name) const
private

Find table with group.

Parameters
group_nameThe name of the group.
Returns
Pointer to the group. NULL if group was not found.

◆ get_hfts()

const HftList & Fauna::InsfileReader::get_hfts ( ) const
inline

Get the HFT list that was read from the instruction file.

◆ get_params()

const Parameters & Fauna::InsfileReader::get_params ( ) const
inline

Get the global parameters that were read from the instruction file.

◆ get_value()

template<class T >
std::shared_ptr< T > InsfileReader::get_value ( const std::shared_ptr< cpptoml::table > &  table,
const std::string &  key,
const GetValueOpt  opt = GetValueOpt::RemoveKey 
) const
private

Retrieve a single value from the TOML file.

Parameters
tableThe TOML table to read from. Normally this is just the TOML file ins, but for table arrays, this is might be one HFT table for instance.
keyThe TOML key, qualified as a child of table.
optWhether to remove with remove_qualified_key() or keep the key.
Template Parameters
TExpected type of the parameter key.
Exceptions
wrong_param_typeIf key is present, but has a value that does not match the expected type T. See check_wrong_type().
std::invalid_argumentIf table is NULL.

◆ get_value_array()

template<class T >
std::shared_ptr< std::vector< T > > InsfileReader::get_value_array ( const std::shared_ptr< cpptoml::table > &  table,
const std::string &  key,
const GetValueOpt  opt = GetValueOpt::RemoveKey 
) const
private

Retrieve a value array from the TOML file.

Parameters
tableThe TOML table to read from. Normally this is just the TOML file ins, but for table arrays, this is might be one HFT table for instance.
keyThe TOML key, qualified as a child of table.
optWhether to remove with remove_qualified_key() or keep the key.
Template Parameters
TExpected type of the parameter key.
Exceptions
wrong_param_typeIf key is present, but has a value that does not match the expected type T. See check_wrong_type().
std::invalid_argumentIf table is NULL.

◆ read_hft()

Hft InsfileReader::read_hft ( const std::shared_ptr< cpptoml::table > &  table)
private

Construct HFT object from an entry in the array of tables.

Exceptions
std::invalid_argumentIf table is NULL.

◆ read_table_forage()

void InsfileReader::read_table_forage ( )
private

Read the TOML table forage.

◆ read_table_output()

void InsfileReader::read_table_output ( )
private

Read the TOML table output.

◆ read_table_output_text_tables()

void InsfileReader::read_table_output_text_tables ( )
private

Read the TOML table output.text_tables.

◆ read_table_simulation()

void InsfileReader::read_table_simulation ( )
private

Read the TOML table simulation.

◆ remove_qualified_key()

void InsfileReader::remove_qualified_key ( std::shared_ptr< cpptoml::table >  table,
const std::string  key 
) const
private

Remove TOML element (in order to indicate it’s been parsed).

Parameters
tableA TOML (sub)tree.
keyA fully qualified TOML key that should exist in table.
Exceptions
std::invalid_argumentIf table is NULL.
std::out_of_rangeIf key couldn’t be found.

Member Data Documentation

◆ hft_keys_parsed

std::set<std::string> Fauna::InsfileReader::hft_keys_parsed
private

All TOML keys in HFTs and HFT groups that have been parsed.

In general, TOML elements are erased from ins as soon as they have been parsed. Any remaining keys/elements are unknown and issue an error. However, parameters in HFT groups may be read several times. Therefore they need to be erased after all HFTs have been read. This variable keeps track of all valid keys so that they can be removed from the HFT groups in the end.

◆ hfts

HftList Fauna::InsfileReader::hfts
private

◆ ins

std::shared_ptr<cpptoml::table> Fauna::InsfileReader::ins
private

The root table of the instruction file from cpptoml::parse_file().

◆ params

Parameters Fauna::InsfileReader::params
private

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