From 1bc8a9ad828ffa395ae26429e1b6c36fc2352bd0 Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Mon, 23 Oct 2023 21:47:33 -0700 Subject: [PATCH] SimpleConfig: Add const char * key variants. --- CMakeLists.txt | 2 +- include/scsl/SimpleConfig.h | 50 ++++++++++++++++++++++++++++++++++++- src/sl/SimpleConfig.cc | 38 ++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2c5ef49..be73eaa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.22) project(scsl LANGUAGES CXX - VERSION 1.0.3 + VERSION 1.1.0 DESCRIPTION "Shimmering Clarity Standard Library") set(CMAKE_CXX_STANDARD 14) diff --git a/include/scsl/SimpleConfig.h b/include/scsl/SimpleConfig.h index f0d7f78..71ed47f 100644 --- a/include/scsl/SimpleConfig.h +++ b/include/scsl/SimpleConfig.h @@ -110,6 +110,27 @@ public: /// \return The value stored under the key, or an empty string. 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. /// /// \note This operates on the global config. @@ -125,6 +146,10 @@ public: /// \brief Set a configuration value. This will override any /// value set. 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 /// \brief The constructor doesn't need any initialisation. @@ -167,6 +192,13 @@ public: /// \return The value stored under the key, or an empty string. 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. /// /// \param key The key to look up. See the class documentation @@ -177,15 +209,31 @@ public: /// value. 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 /// 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. - /// \return 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: std::string envPrefix; std::map vars; diff --git a/src/sl/SimpleConfig.cc b/src/sl/SimpleConfig.cc index 4dae11c..ea0bb92 100644 --- a/src/sl/SimpleConfig.cc +++ b/src/sl/SimpleConfig.cc @@ -83,6 +83,13 @@ SimpleConfig::GetGlobal(std::string &key) } +std::string +SimpleConfig::GetGlobal(const char *key) +{ + return globalConfig.Get(key); +} + + std::string 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 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 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 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 SimpleConfig::KeyList() {