gz-cpp-util 1.3
A c++20 library containing various utilities
gz::SettingsManager< CacheTypes > Class Template Reference

#include <settings_manager.hpp>

Public Member Functions

 SettingsManager (SettingsManagerCreateInfo< CacheTypes... > &createInfo)
 Create a SettingsManager. More...
 
 SettingsManager (const SettingsManager &)=delete
 
SettingsManageroperator= (const SettingsManager &)=delete
 
std::string toString () const
 
const util::unordered_string_map< std::string > & getSettingsMap () const
 Get a const reference to the underlying map.
 
Retrieve values
const std::string & get (const std::string &key) const
 Get a reference to a value. More...
 
template<util::IsInPack< CacheTypes... > T>
const T & get (const std::string &key)
 Get a reference to the value coverted to T. More...
 
const std::string & getOr (const std::string &key, const std::string &fallback)
 Get a reference to the value, or return the fallback if the setting is not present. More...
 
template<util::IsInPack< CacheTypes... > T>
requires std::copy_constructible<T>
const T & getOr (const std::string &key, const T &fallback)
 Get a reference to the value coverted to T, or return the fallback if the setting is not present. More...
 
const std::string getCopy (const std::string &key) const
 Same as get, but returns a copy of value and not a const reference.
 
template<util::IsInPack< CacheTypes... > T>
const T getCopy (const std::string &key)
 Same as get<T>, but returns a copy of value and not a const reference.
 
const std::string getCopyOr (const std::string &key, const std::string &fallback)
 Same as getOr, but returns a copy of value and not a const reference.
 
template<util::IsInPack< CacheTypes... > T>
const T getCopyOr (const std::string &key, const T &fallback)
 Same as getOr<T>, but returns a copy of value and not a const reference.
 
Set values
void set (const std::string &key, const std::string &value)
 Set the value of key to value. More...
 
template<util::IsInPack< CacheTypes... > T>
void set (const std::string &key, const T &value)
 Set the value of key to value. More...
 
Callback functions
void addCallbackFunction (const std::string &key, SettingsCallbackFunction callbackFunction)
 Add a callback function that gets called when the the value to key is changed with set.
 
void removeCallbackFunction (const std::string &key)
 Remove the callback function for key.
 
File operations
void writeToFile () const
 
void readFromFile (bool checkValidity=true)
 Read the contents from the provided filepath. More...
 

Private Attributes

util::unordered_string_map< std::string > settings
 
util::unordered_string_map< SettingsCallbackFunction > settingsCallbackFunctions
 
bool insertFallbacks
 Add fallback to values if key is not present when using getOr.
 
bool writeFileOnExit
 
std::string filepath
 
std::set< std::string > cacheTypes
 
util::unordered_string_map< util::unordered_string_map< std::variant< std::monostate, CacheTypes... > > > settingsCache
 
template<std::same_as< void > Void, StringConvertible CacheType1, StringConvertible... CacheTypesOther>
void initCache ()
 Recursive initialitation of the cache. More...
 
template<std::same_as< void > Void>
void initCache ()
 End for the recursive cache initialization.
 

Restricting values

See also
sm_validity
util::unordered_string_map< SettingsManagerAllowedValues< CacheTypes... > > allowedValues
 
bool throwExceptionWhenNewValueNotAllowed
 
template<NumberInPack< std::string, CacheTypes... > T>
bool isValueAllowed (const std::string &key, const T &value) const noexcept
 Check if a value is allowed for key.
 
template<NotNumberInPack< std::string, CacheTypes... > T>
bool isValueAllowed (const std::string &key, const T &value) const noexcept
 
template<util::IsInPack< CacheTypes... > T>
void setAllowedValues (const std::string &key, std::vector< T > &allowedValues, SettingsManagerAllowedValueTypes type=SM_LIST)
 Set the allowed values for a key. More...
 
template<util::IsInPack< CacheTypes... > T>
void setAllowedValues (const std::string &key, std::vector< T > &&allowedValues, SettingsManagerAllowedValueTypes type=SM_LIST)
 
void removeAllowedValues (const std::string &key) noexcept
 Remove allowed values for key. All values are allowed afterwards.
 
void initAllowedValues ()
 Make sure the allowedValues map is valid. More...
 

Detailed Description

template<StringConvertible... CacheTypes>
class gz::SettingsManager< CacheTypes >

About

The SettingsManager is basically a map with extra features. It stores key-value pairs in a map. Both key and value are strings, but other types can also be stored.

Restricting values

SettingsManager can restrict the values for a given keys to a range of numbers, or a list of possible strings. This can be done by providing a map containing information on the allowed values in the create info. The settings manager will then never return an invalid value, but it might still throw errors if there is no value set.

Storing other types

While all values are internally strings, SettingsManager can also store different types. The types must satisfy the concept StringConvertible = ConstructibleFromString and ConvertibleToString.

If a value is requested using get<T>(), the value will be converted to T and returned. Additionaly, the converted type is cached so that the conversion can be skipped if it is requested again. The user must instantiate the SettingsManager with all the cache-types beforehand, only those can be retrieved using get.

The strings are converted using fromString(T), which is either user defined for custom types or this library. There is of course no guarantee that the conversion works, the getters will throw an exception if it doesnt.

Callback functions

You can add a callback function for each key that gets called when a setting gets changed.

Constructor & Destructor Documentation

◆ SettingsManager()

template<StringConvertible... CacheTypes>
gz::SettingsManager< CacheTypes >::SettingsManager ( SettingsManagerCreateInfo< CacheTypes... > &  createInfo)

Create a SettingsManager.

The maps from createInfo are no longer valid after this function.

Member Function Documentation

◆ get() [1/2]

template<StringConvertible... CacheTypes>
template<util::IsInPack< CacheTypes... > T>
const T & gz::SettingsManager< CacheTypes >::get ( const std::string &  key)

Get a reference to the value coverted to T.

Parameters
keyThe key to the setting
Returns
The setting belonging to key, constructed by getTypeFromString<T>()
Exceptions
InvalidArgumentif key is not present
InvalidTypeif T is not a registered type
InvalidTypeif type T can not be constructed from value (string)

◆ get() [2/2]

template<StringConvertible... CacheTypes>
const std::string & gz::SettingsManager< CacheTypes >::get ( const std::string &  key) const

Get a reference to a value.

Parameters
keyThe key to the value
Returns
The value belonging to key
Exceptions
InvalidArgumentif key is not present

◆ getOr() [1/2]

template<StringConvertible... CacheTypes>
const std::string & gz::SettingsManager< CacheTypes >::getOr ( const std::string &  key,
const std::string &  fallback 
)

Get a reference to the value, or return the fallback if the setting is not present.

Parameters
keyThe key to the value
fallbackA fallback to return if the key is not present
Returns
The value belonging to key, constructed by getTypeFromString<T>()
Exceptions
InvalidArgumentif key is not present

◆ getOr() [2/2]

template<StringConvertible... CacheTypes>
requires std::copy_constructible<T>
template<util::IsInPack< CacheTypes... > T>
requires std::copy_constructible<T>
const T & gz::SettingsManager< CacheTypes >::getOr ( const std::string &  key,
const T &  fallback 
)

Get a reference to the value coverted to T, or return the fallback if the setting is not present.

Parameters
keyThe key to the value
fallbackA fallback to return if the key is not present
Returns
The value belonging to key, constructed by getTypeFromString<T>()
Exceptions
InvalidArgumentif key is not present
InvalidTypeif T is not a registered type
InvalidTypeif type T can not be constructed from value (string)
InvalidTypeif the fallback can not be converted to string

◆ initAllowedValues()

template<StringConvertible... CacheTypes>
void gz::SettingsManager< CacheTypes >::initAllowedValues ( )
private

Make sure the allowedValues map is valid.

Exceptions
InvalidArgumentif any of the initial allowedValues structs is invalid

◆ initCache()

template<StringConvertible... CacheTypes>
template<std::same_as< void > Void, StringConvertible CacheType1, StringConvertible... CacheTypesOther>
void gz::SettingsManager< CacheTypes >::initCache
private

Recursive initialitation of the cache.

The cache is initialized by placing the the typename in the cacheTypes set. It will not actually be added as cache type. The "Void" type template parameter is needed because the exit for the recursion must also have a template signature. When calling this, use initCache<void, CacheTypes...>().

◆ readFromFile()

template<StringConvertible... CacheTypes>
void gz::SettingsManager< CacheTypes >::readFromFile ( bool  checkValidity = true)

Read the contents from the provided filepath.

Parameters
checkValidityif true, inserts only files that are valid

◆ set() [1/2]

template<StringConvertible... CacheTypes>
void gz::SettingsManager< CacheTypes >::set ( const std::string &  key,
const std::string &  value 
)

Set the value of key to value.

Exceptions
InvalidArgumentif value is invalid
Exceptionif an exception occurs during a potential callback function

◆ set() [2/2]

template<StringConvertible... CacheTypes>
template<util::IsInPack< CacheTypes... > T>
void gz::SettingsManager< CacheTypes >::set ( const std::string &  key,
const T &  value 
)

Set the value of key to value.

Exceptions
InvalidArgumentif value is invalid
InvalidTypeif T is not a registered type
Exceptionif an exception occurs during a potential callback function

◆ setAllowedValues()

template<StringConvertible... CacheTypes>
template<util::IsInPack< CacheTypes... > T>
void gz::SettingsManager< CacheTypes >::setAllowedValues ( const std::string &  key,
std::vector< T > &  allowedValues,
SettingsManagerAllowedValueTypes  type = SM_LIST 
)

Set the allowed values for a key.

Parameters
keyThe key where the allowedValues should be applied
typeThe type containing information on the values in allowedValues
allowedValuesVector containing:
  • a list of allowed values if type=SM_LIST
  • [min, max] if type=SM_RANGE

If the current value for key is now invalid, it will be removed. allowedValues vector is no longer valid after this function

Exceptions
InvalidArgumentif call is invalid

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