gz-cpp-util 1.3
A c++20 library containing various utilities
|
#include <settings_manager.hpp>
Public Member Functions | |
SettingsManager (SettingsManagerCreateInfo< CacheTypes... > &createInfo) | |
Create a SettingsManager. More... | |
SettingsManager (const SettingsManager &)=delete | |
SettingsManager & | operator= (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 | |
| |
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... | |
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.
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.
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.
You can add a callback function for each key that gets called when a setting gets changed.
gz::SettingsManager< CacheTypes >::SettingsManager | ( | SettingsManagerCreateInfo< CacheTypes... > & | createInfo | ) |
Create a SettingsManager.
The maps from createInfo are no longer valid after this function.
const T & gz::SettingsManager< CacheTypes >::get | ( | const std::string & | key | ) |
Get a reference to the value coverted to T.
key | The key to the setting |
InvalidArgument | if key is not present |
InvalidType | if T is not a registered type |
InvalidType | if type T can not be constructed from value (string) |
const std::string & gz::SettingsManager< CacheTypes >::get | ( | const std::string & | key | ) | const |
Get a reference to a value.
key | The key to the value |
InvalidArgument | if key is not present |
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.
key | The key to the value |
fallback | A fallback to return if the key is not present |
InvalidArgument | if key is not present |
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.
key | The key to the value |
fallback | A fallback to return if the key is not present |
InvalidArgument | if key is not present |
InvalidType | if T is not a registered type |
InvalidType | if type T can not be constructed from value (string) |
InvalidType | if the fallback can not be converted to string |
|
private |
Make sure the allowedValues map is valid.
InvalidArgument | if any of the initial allowedValues structs is invalid |
|
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...>().
void gz::SettingsManager< CacheTypes >::readFromFile | ( | bool | checkValidity = true | ) |
void gz::SettingsManager< CacheTypes >::set | ( | const std::string & | key, |
const std::string & | value | ||
) |
Set the value of key to value.
InvalidArgument | if value is invalid |
Exception | if an exception occurs during a potential callback function |
void gz::SettingsManager< CacheTypes >::set | ( | const std::string & | key, |
const T & | value | ||
) |
Set the value of key to value.
InvalidArgument | if value is invalid |
InvalidType | if T is not a registered type |
Exception | if an exception occurs during a potential callback function |
void gz::SettingsManager< CacheTypes >::setAllowedValues | ( | const std::string & | key, |
std::vector< T > & | allowedValues, | ||
SettingsManagerAllowedValueTypes | type = SM_LIST |
||
) |
Set the allowed values for a key.
key | The key where the allowedValues should be applied |
type | The type containing information on the values in allowedValues |
allowedValues | Vector containing:
|
If the current value for key is now invalid, it will be removed. allowedValues vector is no longer valid after this function
InvalidArgument | if call is invalid |