SPH
Classes | Public Member Functions | Static Public Member Functions | Friends | List of all members
Settings< TEnum > Class Template Reference

Generic object containing various settings and parameters of the run. More...

#include <Settings.h>

Public Member Functions

 Settings ()
 Initialize settings by settings all value to their defaults. More...
 
 Settings (EmptySettingsTag)
 Initialize empty settings object. More...
 
 Settings (const Settings &other)
 
 Settings (Settings &&other)
 
Settingsoperator= (std::initializer_list< Entry > list)
 Assigns a list of settings into the object, erasing all previous entries. More...
 
Settingsoperator= (const Settings &other)
 
Settingsoperator= (Settings &&other)
 
template<typename TValue >
Settingsset (const TEnum idx, TValue &&value, std::enable_if_t<!std::is_enum< std::decay_t< TValue >>::value, int >=0)
 Saves a value into the settings. More...
 
template<typename TValue >
Settingsset (const TEnum idx, TValue &&value, std::enable_if_t< std::is_enum< std::decay_t< TValue >>::value, int >=0)
 Saves a value into the settings. More...
 
template<typename TValue , typename = std::enable_if_t<std::is_enum<TValue>::value>>
Settingsset (const TEnum idx, const Flags< TValue > flags)
 Saves flags into the settings. More...
 
Settingsset (const TEnum idx, EmptyFlags)
 Clear flags of given parameter in settings. More...
 
Settingsset (const TEnum idx, const EnumWrapper ew)
 Special setter for value of type EnumWrapper. More...
 
void addEntries (const Settings &settings)
 Adds entries from different Settings object into this one, overriding current entries. More...
 
void unset (const TEnum idx)
 Removes given parameter from settings. More...
 
template<typename TValue >
TValue get (const TEnum idx, std::enable_if_t<!std::is_enum< std::decay_t< TValue >>::value, int >=0) const
 Returns a value of given type from the settings. More...
 
template<typename TValue >
TValue get (const TEnum idx, std::enable_if_t< std::is_enum< std::decay_t< TValue >>::value, int >=0) const
 Returns a value of given type from the settings. More...
 
template<typename TValue >
Flags< TValue > getFlags (const TEnum idx) const
 Returns Flags from underlying value stored in settings. More...
 
bool has (const TEnum idx) const
 Checks if the given entry is stored in the settings. More...
 
template<typename TValue >
bool hasType (const TEnum idx) const
 Checks if the given entry has specified type. More...
 
Outcome saveToFile (const Path &path) const
 Saves all values stored in settings into file. More...
 
Outcome loadFromFile (const Path &path)
 Loads the settings from file. More...
 
bool tryLoadFileOrSaveCurrent (const Path &path, const Settings &overrides=EMPTY_SETTINGS)
 If the specified file exists, loads the settings from it, otherwise creates the file and saves the current values. More...
 
SettingsIterator< TEnum > begin () const
 Iterator to the first entry of the settings storage. More...
 
SettingsIterator< TEnum > end () const
 Iterator to the one-past-end entry the settings storage. More...
 
Size size () const
 Returns the number of entries in the settings. More...
 

Static Public Member Functions

static Optional< std::string > getEntryName (const TEnum idx)
 Returns the human-readable name of the entry with given index. More...
 
static Optional< int > getEntryType (const TEnum idx)
 Returns the type of the entry with given index. More...
 
static std::string typeToString (const int type)
 Returns the string name for given type index. More...
 
static Optional< std::string > getEntryDesc (const TEnum idx)
 Returns a description of the entry with given index. More...
 
static Optional< TEnum > getEntryId (const std::string &name)
 Returns an ID for given entry name. More...
 
static const SettingsgetDefaults ()
 \brief Returns a reference to object containing default values of all settings. More...
 

Friends

template<typename >
class SettingsIterator
 

Detailed Description

template<typename TEnum>
class Settings< TEnum >

Generic object containing various settings and parameters of the run.

Settings is a storage containing pairs key-value objects, where key is one of predefined enums. The value can have multiple types within the same Settings object. Currently following types can be stored: bool, int, enums, float (or double), std::string, Interval, Vector, SymmetricTensor, TracelessTensor.

The template cannot be used directly as it is missing default values of parameters; instead specializations for specific enums should be used. The code defines two base specializations:

The object can be specialized for other usages, provided static member Settings::instance is created, see one of existing specializations.

Definition at line 108 of file Settings.h.

Constructor & Destructor Documentation

◆ Settings() [1/4]

template<typename TEnum >
Settings< TEnum >::Settings

Initialize settings by settings all value to their defaults.

Definition at line 22 of file Settings.impl.h.

◆ Settings() [2/4]

template<typename TEnum >
Settings< TEnum >::Settings ( EmptySettingsTag  )

Initialize empty settings object.

Definition at line 26 of file Settings.impl.h.

◆ Settings() [3/4]

template<typename TEnum >
Settings< TEnum >::Settings ( const Settings< TEnum > &  other)

Definition at line 29 of file Settings.impl.h.

◆ Settings() [4/4]

template<typename TEnum >
Settings< TEnum >::Settings ( Settings< TEnum > &&  other)

Definition at line 33 of file Settings.impl.h.

Member Function Documentation

◆ addEntries()

template<typename TEnum >
void Settings< TEnum >::addEntries ( const Settings< TEnum > &  settings)
inline

Adds entries from different Settings object into this one, overriding current entries.

Entries not stored in the given settings are kept unchanged.

Exceptions
InvalidSettingsAccessif the settings share entries that differ in type.

Definition at line 297 of file Settings.h.

◆ begin()

template<typename TEnum >
SettingsIterator< TEnum > Settings< TEnum >::begin

Iterator to the first entry of the settings storage.

Definition at line 342 of file Settings.impl.h.

◆ end()

template<typename TEnum >
SettingsIterator< TEnum > Settings< TEnum >::end

Iterator to the one-past-end entry the settings storage.

Definition at line 347 of file Settings.impl.h.

◆ get() [1/2]

template<typename TEnum >
template<typename TValue >
TValue Settings< TEnum >::get ( const TEnum  idx,
std::enable_if_t< std::is_enum< std::decay_t< TValue >>::value, int >  = 0 
) const
inline

Returns a value of given type from the settings.

Value must be stored in settings and must have corresponding type.

Template Parameters
TValueType of the value we wish to return. This type must match the type of the saved quantity.
Parameters
idxKey of the value.
Returns
Value correponsing to given key.
Exceptions
InvalidSettingsAccessif value is not stored in the settings or has a different type.

Definition at line 335 of file Settings.h.

◆ get() [2/2]

template<typename TEnum >
template<typename TValue >
TValue Settings< TEnum >::get ( const TEnum  idx,
std::enable_if_t<!std::is_enum< std::decay_t< TValue >>::value, int >  = 0 
) const
inline

Returns a value of given type from the settings.

Value must be stored in settings and must have corresponding type.

Template Parameters
TValueType of the value we wish to return. This type must match the type of the saved quantity.
Parameters
idxKey of the value.
Returns
Value correponsing to given key.
Exceptions
InvalidSettingsAccessif value is not stored in the settings or has a different type.

Definition at line 326 of file Settings.h.

◆ getDefaults()

template<typename TEnum >
const Settings< TEnum > & Settings< TEnum >::getDefaults
static

\brief Returns a reference to object containing default values of all settings.

Definition at line 357 of file Settings.impl.h.

◆ getEntryDesc()

template<typename TEnum >
static Optional<std::string> Settings< TEnum >::getEntryDesc ( const TEnum  idx)
inlinestatic

Returns a description of the entry with given index.

If the index does not correspond to any parameter, returns string NOTHING.

Definition at line 389 of file Settings.h.

◆ getEntryId()

template<typename TEnum >
static Optional<TEnum> Settings< TEnum >::getEntryId ( const std::string &  name)
inlinestatic

Returns an ID for given entry name.

This is the inverse of function getEntryName.

Definition at line 403 of file Settings.h.

◆ getEntryName()

template<typename TEnum >
static Optional<std::string> Settings< TEnum >::getEntryName ( const TEnum  idx)
inlinestatic

Returns the human-readable name of the entry with given index.

If the index does not correspond to any parameter, returns string NOTHING.

Definition at line 357 of file Settings.h.

◆ getEntryType()

template<typename TEnum >
static Optional<int> Settings< TEnum >::getEntryType ( const TEnum  idx)
inlinestatic

Returns the type of the entry with given index.

If the index does not correspond to any parameter, returns NOTHING.

Definition at line 371 of file Settings.h.

◆ getFlags()

template<typename TEnum >
template<typename TValue >
Flags<TValue> Settings< TEnum >::getFlags ( const TEnum  idx) const
inline

Returns Flags from underlying value stored in settings.

Syntactic suggar, avoid cumbersome conversion to underlying type and then to Flags.

Definition at line 348 of file Settings.h.

◆ has()

template<typename TEnum >
bool Settings< TEnum >::has ( const TEnum  idx) const
inline

Checks if the given entry is stored in the settings.

Definition at line 414 of file Settings.h.

◆ hasType()

template<typename TEnum >
template<typename TValue >
bool Settings< TEnum >::hasType ( const TEnum  idx) const
inline

Checks if the given entry has specified type.

Exceptions
InvalidSettingsAccessif the entry is not in settings.

Definition at line 422 of file Settings.h.

◆ loadFromFile()

template<typename TEnum >
Outcome Settings< TEnum >::loadFromFile ( const Path path)

Loads the settings from file.

Previous values stored in settings are removed. The file must have a valid settings format.

Parameters
pathPath to the file. The file must exist.
Returns
SUCCESS if the settings were correctly parsed from the file, otherwise returns encountered error.

Definition at line 200 of file Settings.impl.h.

◆ operator=() [1/3]

template<typename TEnum >
Settings< TEnum > & Settings< TEnum >::operator= ( const Settings< TEnum > &  other)

Definition at line 46 of file Settings.impl.h.

◆ operator=() [2/3]

template<typename TEnum >
Settings< TEnum > & Settings< TEnum >::operator= ( Settings< TEnum > &&  other)

Definition at line 52 of file Settings.impl.h.

◆ operator=() [3/3]

template<typename TEnum >
Settings< TEnum > & Settings< TEnum >::operator= ( std::initializer_list< Entry >  list)

Assigns a list of settings into the object, erasing all previous entries.

Definition at line 37 of file Settings.impl.h.

◆ saveToFile()

template<typename TEnum >
Outcome Settings< TEnum >::saveToFile ( const Path path) const

Saves all values stored in settings into file.

Parameters
pathPath (relative or absolute) to the file. The file will be created, any previous content will be overriden.
Returns
SUCCESS if the file has been correctly written, otherwise returns encountered error.

Definition at line 241 of file Settings.impl.h.

◆ set() [1/5]

template<typename TEnum >
Settings& Settings< TEnum >::set ( const TEnum  idx,
const EnumWrapper  ew 
)
inline

Special setter for value of type EnumWrapper.

While usually the enum can be stored directly, this overload is useful for deserialization, etc.

Returns
Reference to the settings object.
Exceptions
InvalidSettingsAccessif settings value of different type than the one currently stored.

Definition at line 283 of file Settings.h.

◆ set() [2/5]

template<typename TEnum >
template<typename TValue , typename = std::enable_if_t<std::is_enum<TValue>::value>>
Settings& Settings< TEnum >::set ( const TEnum  idx,
const Flags< TValue >  flags 
)
inline

Saves flags into the settings.

Flags are not considered as a different type, they are internally saved as enum wrapper.

Returns
Reference to the settings object.
Exceptions
InvalidSettingsAccessif settings value of different type than the one currently stored.

Definition at line 262 of file Settings.h.

◆ set() [3/5]

template<typename TEnum >
Settings& Settings< TEnum >::set ( const TEnum  idx,
EmptyFlags   
)
inline

Clear flags of given parameter in settings.

This function can be used only if the value is already stored in the settings.

Returns
Reference to the settings object.
Exceptions
InvalidSettingsAccessif the value is not stored or has a different type.

Definition at line 271 of file Settings.h.

◆ set() [4/5]

template<typename TEnum >
template<typename TValue >
Settings& Settings< TEnum >::set ( const TEnum  idx,
TValue &&  value,
std::enable_if_t< std::is_enum< std::decay_t< TValue >>::value, int >  = 0 
)
inline

Saves a value into the settings.

Any previous value of the same ID is overriden.

Template Parameters
TValueType of the value to be saved. Does not have to be specified, type deduction can be used to determine it. Must be one of types listed in object description. Using other types will result in compile error.
Parameters
idxKey identifying the value. This key can be used to retrive the value later.
valueValue being stored into settings.
Returns
Reference to the settings object, allowing to queue multiple set functions.
Exceptions
InvalidSettingsAccessif settings value of different type than the one currently stored.

Definition at line 242 of file Settings.h.

◆ set() [5/5]

template<typename TEnum >
template<typename TValue >
Settings& Settings< TEnum >::set ( const TEnum  idx,
TValue &&  value,
std::enable_if_t<!std::is_enum< std::decay_t< TValue >>::value, int >  = 0 
)
inline

Saves a value into the settings.

Any previous value of the same ID is overriden.

Template Parameters
TValueType of the value to be saved. Does not have to be specified, type deduction can be used to determine it. Must be one of types listed in object description. Using other types will result in compile error.
Parameters
idxKey identifying the value. This key can be used to retrive the value later.
valueValue being stored into settings.
Returns
Reference to the settings object, allowing to queue multiple set functions.
Exceptions
InvalidSettingsAccessif settings value of different type than the one currently stored.

Definition at line 226 of file Settings.h.

◆ size()

template<typename TEnum >
Size Settings< TEnum >::size

Returns the number of entries in the settings.

This includes default entries in case the object was not constructed with EMPTY_SETTINGS tag.

Definition at line 352 of file Settings.impl.h.

◆ tryLoadFileOrSaveCurrent()

template<typename TEnum >
bool Settings< TEnum >::tryLoadFileOrSaveCurrent ( const Path path,
const Settings< TEnum > &  overrides = EMPTY_SETTINGS 
)

If the specified file exists, loads the settings from it, otherwise creates the file and saves the current values.

This is a standard behavior of configuration files used in the simulation. First time the simulation is started, the configuration files are created with default values. The files can then be modified by the user and the next time simulation is started, the parameters are parsed and used instead of the defaults.

For convenience, it is possible to pass a set of overrides, which are used instead of the values loaded from the configuration file (or defaults, if the file does not exist). This can be used to specify settings as command-line parameters, for example.

Parameters
pathPath to the configuration file.
overridesSet of overrides applied on the current values or after the settings are loaded.
Returns
True if the settings have been successfully loaded, false if the configuration file did not exist, in which case the function attempted to create the file using current values (with applied overrides).
Exceptions
Exceptionif loading of the file failed (invalid content, unknown parameters, ...).
Note
Function returns a valid result (false) even if the configuration file cannot be created, for example if the directory access is denied. This does not prohibit the simulation in any way, the settings object is in valid state.

Definition at line 304 of file Settings.impl.h.

◆ typeToString()

template<typename TEnum >
std::string Settings< TEnum >::typeToString ( const int  type)
static

Returns the string name for given type index.

Exceptions
Exceptionfor unknown type index

Definition at line 322 of file Settings.impl.h.

◆ unset()

template<typename TEnum >
void Settings< TEnum >::unset ( const TEnum  idx)
inline

Removes given parameter from settings.

If the parameter is not in settings, nothing happens (it isn't considered as an error).

Definition at line 313 of file Settings.h.

Friends And Related Function Documentation

◆ SettingsIterator

template<typename TEnum >
template<typename >
friend class SettingsIterator
friend

Definition at line 110 of file Settings.h.


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