SimpleConfig: Add const char * key variants.

This commit is contained in:
Kyle Isom 2023-10-23 21:47:33 -07:00
parent 9a83cf6c08
commit 1bc8a9ad82
3 changed files with 88 additions and 2 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.22) cmake_minimum_required(VERSION 3.22)
project(scsl LANGUAGES CXX project(scsl LANGUAGES CXX
VERSION 1.0.3 VERSION 1.1.0
DESCRIPTION "Shimmering Clarity Standard Library") DESCRIPTION "Shimmering Clarity Standard Library")
set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD 14)

View File

@ -110,6 +110,27 @@ public:
/// \return The value stored under the key, or an empty string. /// \return The value stored under the key, or an empty string.
static std::string GetGlobal(std::string &key); static std::string GetGlobal(std::string &key);
/// \brief Get the value stored for the key from the config.
///
/// \note This operates on the global config.
///
/// \param key The key to look up. See the class documentation
/// for information on how this is used.
/// \return The value stored under the key, or an empty string.
static std::string GetGlobal(const char *key);
/// \brief Get the value stored for the key from the config.
///
/// \note This operates on the global config.
///
/// \param key The key to look up. See the class documentation
/// for information on how this is used.
/// \param defaultValue A default value to cache and use if no
/// value is stored under the key.
/// \return The value stored under the key, or the default
/// value.
static std::string GetGlobal(const char *key, const std::string &defaultValue);
/// \brief Get the value stored for the key from the config. /// \brief Get the value stored for the key from the config.
/// ///
/// \note This operates on the global config. /// \note This operates on the global config.
@ -125,6 +146,10 @@ public:
/// \brief Set a configuration value. This will override any /// \brief Set a configuration value. This will override any
/// value set. /// value set.
static void PutGlobal(std::string &key, const std::string &value); static void PutGlobal(std::string &key, const std::string &value);
/// \brief Set a configuration value. This will override any
/// value set.
static void PutGlobal(const char *key, const std::string &value);
#endif #endif
/// \brief The constructor doesn't need any initialisation. /// \brief The constructor doesn't need any initialisation.
@ -167,6 +192,13 @@ public:
/// \return The value stored under the key, or an empty string. /// \return The value stored under the key, or an empty string.
std::string Get(std::string &key); std::string Get(std::string &key);
/// \brief Get the value stored for the key from the config.
///
/// \param key The key to look up. See the class documentation
/// for information on how this is used.
/// \return The value stored under the key, or an empty string.
std::string Get(const char *key);
/// \brief Get the value stored for the key from the config. /// \brief Get the value stored for the key from the config.
/// ///
/// \param key The key to look up. See the class documentation /// \param key The key to look up. See the class documentation
@ -177,15 +209,31 @@ public:
/// value. /// value.
std::string Get(std::string &key, std::string defaultValue); std::string Get(std::string &key, std::string defaultValue);
/// \brief Get the value stored for the key from the config.
///
/// \param key The key to look up. See the class documentation
/// for information on how this is used.
/// \param defaultValue A default value to cache and use if no
/// value is stored under the key.
/// \return The value stored under the key, or the default
/// value.
std::string Get(const char *key, std::string defaultValue);
/// \brief Set a configuration value. This will override any /// \brief Set a configuration value. This will override any
/// value set. /// value set.
/// ///
/// \param key The key to look up. See the class documentation /// \param key The key to look up. See the class documentation
/// for information on how this is used. /// for information on how this is used.
/// \param value The value to set. /// \param value The value to set.
/// \return
void Put(std::string &key, const std::string value); void Put(std::string &key, const std::string value);
/// \brief Set a configuration value. This will override any
/// value set.
///
/// \param key The key to look up. See the class documentation
/// for information on how this is used.
/// \param value The value to set.
void Put(const char *key, const std::string value);
private: private:
std::string envPrefix; std::string envPrefix;
std::map<std::string, std::string> vars; std::map<std::string, std::string> vars;

View File

@ -83,6 +83,13 @@ SimpleConfig::GetGlobal(std::string &key)
} }
std::string
SimpleConfig::GetGlobal(const char *key)
{
return globalConfig.Get(key);
}
std::string std::string
SimpleConfig::GetGlobal(std::string &key, const std::string &defaultValue) SimpleConfig::GetGlobal(std::string &key, const std::string &defaultValue)
{ {
@ -90,6 +97,13 @@ SimpleConfig::GetGlobal(std::string &key, const std::string &defaultValue)
} }
std::string
SimpleConfig::GetGlobal(const char *key, const std::string &defaultValue)
{
return globalConfig.Get(key, defaultValue);
}
void void
SimpleConfig::PutGlobal(std::string &key, const std::string &value) SimpleConfig::PutGlobal(std::string &key, const std::string &value)
{ {
@ -160,6 +174,14 @@ SimpleConfig::Get(std::string &key)
} }
std::string
SimpleConfig::Get(const char *key)
{
auto keyStr = std::string(key);
return this->Get(keyStr, "");
}
std::string std::string
SimpleConfig::Get(std::string &key, std::string defaultValue) SimpleConfig::Get(std::string &key, std::string defaultValue)
{ {
@ -180,6 +202,14 @@ SimpleConfig::Get(std::string &key, std::string defaultValue)
} }
std::string
SimpleConfig::Get(const char *key, std::string defaultValue)
{
auto keyStr = std::string(key);
return this->Get(keyStr, std::move(defaultValue));
}
void void
SimpleConfig::Put(std::string &key, const std::string value) SimpleConfig::Put(std::string &key, const std::string value)
{ {
@ -187,6 +217,14 @@ SimpleConfig::Put(std::string &key, const std::string value)
} }
void
SimpleConfig::Put(const char *key, const std::string value)
{
auto keyStr = std::string(key);
this->vars[std::move(keyStr)] = std::move(key);
}
std::vector<std::string> std::vector<std::string>
SimpleConfig::KeyList() SimpleConfig::KeyList()
{ {