Merge pull request #62 from tboudreaux/refactor/spelling

refactor(serif): fixed typos and updated names to reflect 4DSSE->SERiF
This commit is contained in:
2025-06-12 09:06:38 -04:00
committed by GitHub
2 changed files with 178 additions and 181 deletions

View File

@@ -29,83 +29,81 @@
#include "config.h" #include "config.h"
namespace serif { namespace serif::config {
namespace config {
Config::Config() {} Config::Config() {}
Config::~Config() {} Config::~Config() {}
Config& Config::getInstance() { Config& Config::getInstance() {
static Config instance; static Config instance;
return instance; return instance;
}
bool Config::loadConfig(const std::string& configFile) {
configFilePath = configFile;
try {
yamlRoot = YAML::LoadFile(configFile);
} catch (YAML::BadFile& e) {
std::cerr << "Error: " << e.what() << std::endl;
return false;
} }
m_loaded = true;
return true;
}
bool Config::isKeyInCache(const std::string &key) { bool Config::loadConfig(const std::string& configFile) {
return configMap.find(key) != configMap.end(); configFilePath = configFile;
} try {
yamlRoot = YAML::LoadFile(configFile);
void Config::addToCache(const std::string &key, const YAML::Node &node) { } catch (YAML::BadFile& e) {
configMap[key] = node; std::cerr << "Error: " << e.what() << std::endl;
}
void Config::registerUnknownKey(const std::string &key) {
unknownKeys.push_back(key);
}
bool Config::has(const std::string &key) {
if (!m_loaded) {
throw std::runtime_error("Error! Config file not loaded");
}
if (isKeyInCache(key)) { return true; }
YAML::Node node = YAML::Clone(yamlRoot);
std::istringstream keyStream(key);
std::string subKey;
while (std::getline(keyStream, subKey, ':')) {
if (!node[subKey]) {
registerUnknownKey(key);
return false; return false;
}
node = node[subKey]; // go deeper
}
// Key exists and is of the requested type
addToCache(key, node);
return true;
}
void recurse_keys(const YAML::Node& node, std::vector<std::string>& keyList, const std::string& path = "") {
if (node.IsMap()) {
for (const auto& it : node) {
std::string key = it.first.as<std::string>();
std::string new_path = path.empty() ? key : path + ":" + key;
recurse_keys(it.second, keyList, new_path);
} }
} else { m_loaded = true;
keyList.push_back(path); return true;
}
bool Config::isKeyInCache(const std::string &key) {
return configMap.find(key) != configMap.end();
}
void Config::addToCache(const std::string &key, const YAML::Node &node) {
configMap[key] = node;
}
void Config::registerUnknownKey(const std::string &key) {
unknownKeys.push_back(key);
}
bool Config::has(const std::string &key) {
if (!m_loaded) {
throw std::runtime_error("Error! Config file not loaded");
}
if (isKeyInCache(key)) { return true; }
YAML::Node node = YAML::Clone(yamlRoot);
std::istringstream keyStream(key);
std::string subKey;
while (std::getline(keyStream, subKey, ':')) {
if (!node[subKey]) {
registerUnknownKey(key);
return false;
}
node = node[subKey]; // go deeper
}
// Key exists and is of the requested type
addToCache(key, node);
return true;
}
void recurse_keys(const YAML::Node& node, std::vector<std::string>& keyList, const std::string& path = "") {
if (node.IsMap()) {
for (const auto& it : node) {
auto key = it.first.as<std::string>();
auto new_path = path.empty() ? key : path + ":" + key;
recurse_keys(it.second, keyList, new_path);
}
} else {
keyList.push_back(path);
}
}
std::vector<std::string> Config::keys() const {
std::vector<std::string> keyList;
YAML::Node node = YAML::Clone(yamlRoot);
recurse_keys(node, keyList);
return keyList;
} }
} }
std::vector<std::string> Config::keys() const {
std::vector<std::string> keyList;
YAML::Node node = YAML::Clone(yamlRoot);
recurse_keys(node, keyList);
return keyList;
}
} // namespace config
} // namespace serif

View File

@@ -32,215 +32,214 @@
#include "yaml-cpp/yaml.h" #include "yaml-cpp/yaml.h"
// -- Forward Def of Resource manager to let it act as a friend of Config -- // -- Forward Def of Resource manager to let it act as a friend of Config --
namespace serif { namespace resource { class ResourceManager; } } // Forward declaration namespace serif::resource { class ResourceManager; }
class configTestPrivateAccessor; // Forward declaration for test utility class configTestPrivateAccessor; // Forward declaration for test utility
namespace serif { namespace serif::config {
namespace config {
/** /**
* @class Config * @class Config
* @brief Singleton class to manage configuration settings loaded from a YAML file. * @brief Singleton class to manage configuration settings loaded from a YAML file.
*/ */
class Config { class Config {
private: private:
/** /**
* @brief Private constructor to prevent instantiation. * @brief Private constructor to prevent instantiation.
*/ */
Config(); Config();
/** /**
* @brief Destructor. * @brief Destructor.
*/ */
~Config(); ~Config();
YAML::Node yamlRoot; ///< Root node of the YAML configuration. YAML::Node yamlRoot; ///< Root node of the YAML configuration.
std::string configFilePath; ///< Path to the configuration file. std::string configFilePath; ///< Path to the configuration file.
bool debug = false; ///< Flag to enable debug output. bool debug = false; ///< Flag to enable debug output.
bool loaded = false; ///< Flag to indicate if the configuration has been loaded. bool loaded = false; ///< Flag to indicate if the configuration has been loaded.
std::map<std::string, YAML::Node> configMap; ///< Cache for the location of configuration settings. std::map<std::string, YAML::Node> configMap; ///< Cache for the location of configuration settings.
std::vector<std::string> unknownKeys; ///< Cache for the existence of configuration settings. std::vector<std::string> unknownKeys; ///< Cache for the existence of configuration settings.
/** /**
* @brief Get a value from the configuration cache. * @brief Get a value from the configuration cache.
* @tparam T Type of the value to retrieve. * @tparam T Type of the value to retrieve.
* @param key Key of the configuration value. * @param key Key of the configuration value.
* @param defaultValue Default value to return if the key does not exist. * @param defaultValue Default value to return if the key does not exist.
* @return Configuration value of type T. * @return Configuration value of type T.
*/ */
template <typename T> template <typename T>
T getFromCache(const std::string &key, T defaultValue) { T getFromCache(const std::string &key, T defaultValue) {
if (configMap.find(key) != configMap.end()) { if (configMap.find(key) != configMap.end()) {
try { try {
return configMap[key].as<T>(); return configMap[key].as<T>();
} catch (const YAML::Exception& e) { } catch (const YAML::Exception& e) {
return defaultValue; return defaultValue;
}
} }
return defaultValue;
} }
return defaultValue;
}
/** /**
* @brief Check if a key exists in the configuration cache. * @brief Check if a key exists in the configuration cache.
* @param key Key to check. * @param key Key to check.
* @return True if the key exists in the cache, false otherwise. * @return True if the key exists in the cache, false otherwise.
*/ */
bool isKeyInCache(const std::string &key); bool isKeyInCache(const std::string &key);
/** /**
* @brief Add a key-value pair to the configuration cache. * @brief Add a key-value pair to the configuration cache.
* @param key Key of the configuration value. * @param key Key of the configuration value.
* @param node YAML node containing the configuration value. * @param node YAML node containing the configuration value.
*/ */
void addToCache(const std::string &key, const YAML::Node &node); void addToCache(const std::string &key, const YAML::Node &node);
/** /**
* @brief Register a key as not found in the configuration. * @brief Register a key as not found in the configuration.
* @param key Key that was not found. * @param key Key that was not found.
*/ */
void registerUnknownKey(const std::string &key); void registerUnknownKey(const std::string &key);
bool m_loaded = false; bool m_loaded = false;
// Only friends can access get without a default value // Only friends can access get without a default value
template <typename T> template <typename T>
T get(const std::string &key) { T get(const std::string &key) {
if (!m_loaded) { if (!m_loaded) {
throw std::runtime_error("Error! Config file not loaded"); throw std::runtime_error("Error! Config file not loaded");
}
if (has(key)) {
return getFromCache<T>(key, T());
} else {
throw std::runtime_error("Error! Key not found in config file");
}
} }
if (has(key)) {
return getFromCache<T>(key, T());
} else {
throw std::runtime_error("Error! Key not found in config file");
}
}
public: public:
/** /**
* @brief Get the singleton instance of the Config class. * @brief Get the singleton instance of the Config class.
* @return Reference to the Config instance. * @return Reference to the Config instance.
*/ */
static Config& getInstance(); static Config& getInstance();
Config (const Config&) = delete; Config (const Config&) = delete;
Config& operator= (const Config&) = delete; Config& operator= (const Config&) = delete;
Config (Config&&) = delete; Config (Config&&) = delete;
Config& operator= (Config&&) = delete; Config& operator= (Config&&) = delete;
void setDebug(bool debug) { this->debug = debug; } void setDebug(bool debug) { this->debug = debug; }
/** /**
* @brief Load configuration from a YAML file. * @brief Load configuration from a YAML file.
* @param configFilePath Path to the YAML configuration file. * @param configFilePath Path to the YAML configuration file.
* @return True if the configuration was loaded successfully, false otherwise. * @return True if the configuration was loaded successfully, false otherwise.
*/ */
bool loadConfig(const std::string& configFilePath); bool loadConfig(const std::string& configFilePath);
/** /**
* @brief Get the input table from the configuration. * @brief Get the input table from the configuration.
* @return Input table as a string. * @return Input table as a string.
*/ */
std::string getInputTable() const; std::string getInputTable() const;
/** /**
* @brief Get a configuration value by key. * @brief Get a configuration value by key.
* @tparam T Type of the value to retrieve. * @tparam T Type of the value to retrieve.
* @param key Key of the configuration value. * @param key Key of the configuration value.
* @param defaultValue Default value to return if the key does not exist. * @param defaultValue Default value to return if the key does not exist.
* @return Configuration value of type T. * @return Configuration value of type T.
* *
* @example * @example
* @code * @code
* Config& config = Config::getInstance(); * Config& config = Config::getInstance();
* config.loadConfig("example.yaml"); * config.loadConfig("example.yaml");
* int maxIter = config.get<int>("opac:lowTemp:numeric:maxIter", 10); * int maxIter = config.get<int>("opac:lowTemp:numeric:maxIter", 10);
*/ */
template <typename T> template <typename T>
T get(const std::string &key, T defaultValue) { T get(const std::string &key, T defaultValue) {
if (!m_loaded) { if (!m_loaded) {
// ONLY THROW ERROR IF HARSH OR WARN CONFIGURATION // ONLY THROW ERROR IF HARSH OR WARN CONFIGURATION
#if defined(CONFIG_HARSH) #if defined(CONFIG_HARSH)
throw std::runtime_error("Error! Config file not loaded. To disable this error, recompile with CONFIG_HARSH=0"); throw std::runtime_error("Error! Config file not loaded. To disable this error, recompile with CONFIG_HARSH=0");
#elif defined(CONFIG_WARN) #elif defined(CONFIG_WARN)
std::cerr << "Warning! Config file not loaded. This instance of 4DSSE was compiled with CONFIG_WARN so the code will continue using only default values" << std::endl; std::cerr << "Warning! Config file not loaded. This instance of 4DSSE was compiled with CONFIG_WARN so the code will continue using only default values" << std::endl;
#endif #endif
} }
// --- Check if the key has already been checked for existence // --- Check if the key has already been checked for existence
if (std::find(unknownKeys.begin(), unknownKeys.end(), key) != unknownKeys.end()) { if (std::find(unknownKeys.begin(), unknownKeys.end(), key) != unknownKeys.end()) {
return defaultValue; // If the key has already been added to the unknown cache do not traverse the YAML tree or hit the cache return defaultValue; // If the key has already been added to the unknown cache do not traverse the YAML tree or hit the cache
} }
// --- Check if the key is already in the cache (avoid traversing YAML nodes) // --- Check if the key is already in the cache (avoid traversing YAML nodes)
if (isKeyInCache(key)) { if (isKeyInCache(key)) {
return getFromCache<T>(key, defaultValue); return getFromCache<T>(key, defaultValue);
} }
// --- If the key is not in the cache, check the YAML file // --- If the key is not in the cache, check the YAML file
else { else {
YAML::Node node = YAML::Clone(yamlRoot); YAML::Node node = YAML::Clone(yamlRoot);
std::istringstream keyStream(key); std::istringstream keyStream(key);
std::string subKey; std::string subKey;
while (std::getline(keyStream, subKey, ':')) { while (std::getline(keyStream, subKey, ':')) {
if (!node[subKey]) { if (!node[subKey]) {
// Key does not exist // Key does not exist
registerUnknownKey(key); registerUnknownKey(key);
return defaultValue; return defaultValue;
}
node = node[subKey]; // go deeper
} }
node = node[subKey]; // go deeper
}
try { try {
// Key exists and is of the requested type // Key exists and is of the requested type
addToCache(key, node); addToCache(key, node);
return node.as<T>(); return node.as<T>();
} catch (const YAML::Exception& e) { } catch (const YAML::Exception& e) {
// Key is not of the requested type // Key is not of the requested type
registerUnknownKey(key); registerUnknownKey(key);
return defaultValue; // return default value if the key does not exist return defaultValue; // return default value if the key does not exist
}
} }
} }
}
/** /**
* @brief Check if the key exists in the given config file * @brief Check if the key exists in the given config file
* @param key Key to check; * @param key Key to check;
* @return boolean true or false * @return boolean true or false
*/ */
bool has(const std::string &key); bool has(const std::string &key);
/** /**
* @brief Get all keys defined in the configuration file. * @brief Get all keys defined in the configuration file.
* @return Vector of all keys in the configuration file. * @return Vector of all keys in the configuration file.
*/ */
std::vector<std::string> keys() const; std::vector<std::string> keys() const;
/** /**
* @brief Print the configuration file path and the YAML root node. * @brief Print the configuration file path and the YAML root node.
* @param os Output stream. * @param os Output stream.
* @param config Config object to print. * @param config Config object to print.
* @return Output stream. * @return Output stream.
*/ */
friend std::ostream& operator<<(std::ostream& os, const Config& config) { friend std::ostream& operator<<(std::ostream& os, const Config& config) {
if (!config.m_loaded) { if (!config.m_loaded) {
os << "Config file not loaded" << std::endl; os << "Config file not loaded" << std::endl;
return os;
}
if (!config.debug) {
os << "Config file: " << config.configFilePath << std::endl;
} else{
// Print entire YAML file from root
os << "Config file: " << config.configFilePath << std::endl;
os << config.yamlRoot << std::endl;
}
return os; return os;
} }
if (!config.debug) {
os << "Config file: " << config.configFilePath << std::endl;
} else{
// Print entire YAML file from root
os << "Config file: " << config.configFilePath << std::endl;
os << config.yamlRoot << std::endl;
}
return os;
}
// Setup gTest class as a friend // Setup gTest class as a friend
friend class ::configTestPrivateAccessor; // Friend declaration for global test accessor friend class ::configTestPrivateAccessor; // Friend declaration for global test accessor
// -- Resource Manager is a friend of config so it can create a seperate instance // -- Resource Manager is a friend of config so it can create a seperate instance
friend class serif::resource::ResourceManager; // Adjusted friend declaration friend class serif::resource::ResourceManager; // Adjusted friend declaration
}; };
} // namespace config }
} // namespace serif