Switchboard SDK
Loading...
Searching...
No Matches
switchboard::Config Class Reference

Configuration class to store and retrieve configuration values. More...

#include <Config.hpp>

Public Member Functions

 Config (const std::map< std::string, std::any > &config={})
 Config (const std::string &jsonString)
std::map< std::string, std::any > getValue () const
bool hasKey (const std::string &key) const
std::any getAny (const std::string &key) const
template<typename T>
get (const std::string &key) const
void set (const std::string &key, const std::any &value)

Static Public Member Functions

template<typename T>
static T convert (const std::any &value)
template<typename T>
static T get (const std::map< std::string, std::any > &config, const std::string &key, std::optional< T > defaultValue=std::nullopt)
static std::optional< std::string > findString (const std::map< std::string, std::any > &config, const std::string &preferredKey)
 Finds a string in the configuration map.
static std::map< std::string, std::any > jsonToMap (const std::string &jsonString)
static std::string mapToJson (const std::map< std::string, std::any > &config)

Detailed Description

Configuration class to store and retrieve configuration values.

Constructor & Destructor Documentation

◆ Config() [1/2]

switchboard::Config::Config ( const std::map< std::string, std::any > & config = {})

Constructor.

Parameters
configThe configuration map.

◆ Config() [2/2]

switchboard::Config::Config ( const std::string & jsonString)

Constructor.

Parameters
jsonStringThe JSON string.

Member Function Documentation

◆ convert()

template<typename T>
T switchboard::Config::convert ( const std::any & value)
static

Converts an any value to a specific type.

Template Parameters
TThe type to convert to.
Parameters
valueThe value.
Returns
The converted value.

◆ findString()

std::optional< std::string > switchboard::Config::findString ( const std::map< std::string, std::any > & config,
const std::string & preferredKey )
static

Finds a string in the configuration map.

If the preferred key is found, the value is returned. Otherwise, the first string value is returned.

Parameters
configThe configuration map.
preferredKeyThe preferred key for the string value.
Returns
The string value if found, otherwise std::nullopt.

◆ get() [1/2]

template<typename T>
T switchboard::Config::get ( const std::map< std::string, std::any > & config,
const std::string & key,
std::optional< T > defaultValue = std::nullopt )
static

Gets a value from the configuration map with an optional default value.

Template Parameters
TThe type of the value.
Parameters
configThe configuration map.
keyThe key.
defaultValueThe default value if the key is not found.
Returns
The value if found, otherwise the default value.

◆ get() [2/2]

template<typename T>
T switchboard::Config::get ( const std::string & key) const
nodiscard

Gets a value from the configuration map.

Template Parameters
TThe type of the value.
Parameters
keyThe key.
Returns
The value.

◆ getAny()

std::any switchboard::Config::getAny ( const std::string & key) const
nodiscard

Gets a value from the configuration map.

Parameters
keyThe key.
Returns
The value as std::any.

◆ getValue()

std::map< std::string, std::any > switchboard::Config::getValue ( ) const
nodiscard

Gets the configuration map.

Returns
The configuration map.

◆ hasKey()

bool switchboard::Config::hasKey ( const std::string & key) const
nodiscard

Checks if a key exists in the configuration map.

Parameters
keyThe key to check.
Returns
True if the key exists, false otherwise.

◆ jsonToMap()

std::map< std::string, std::any > switchboard::Config::jsonToMap ( const std::string & jsonString)
static

Converts a JSON string to a map.

Parameters
jsonStringThe JSON string.
Returns
The map.

◆ mapToJson()

std::string switchboard::Config::mapToJson ( const std::map< std::string, std::any > & config)
static

Converts a map to a JSON string.

Parameters
configThe configuration map.
Returns
The JSON string.

◆ set()

void switchboard::Config::set ( const std::string & key,
const std::any & value )

Sets a value in the configuration map.

Parameters
keyThe key.
valueThe value as std::any.

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