From 6aaa25df4b211a9bda4636ecbeb8649206ee9fdf Mon Sep 17 00:00:00 2001 From: Emily Boudreaux Date: Mon, 3 Mar 2025 09:53:42 -0500 Subject: [PATCH] fix(config): loaded flag and checks added added check to make sure that config file has been loaded before any config variables are accessed --- src/config/private/config.cpp | 1 + src/config/public/config.h | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/src/config/private/config.cpp b/src/config/private/config.cpp index 5eb3af6..bf39b37 100644 --- a/src/config/private/config.cpp +++ b/src/config/private/config.cpp @@ -26,6 +26,7 @@ bool Config::loadConfig(const std::string& configFile) { std::cerr << "Error: " << e.what() << std::endl; return false; } + loaded = true; return true; } diff --git a/src/config/public/config.h b/src/config/public/config.h index 9770eae..dff11d2 100644 --- a/src/config/public/config.h +++ b/src/config/public/config.h @@ -8,6 +8,7 @@ #include #include #include +#include #include "yaml-cpp/yaml.h" @@ -30,6 +31,7 @@ private: YAML::Node yamlRoot; ///< Root node of the YAML configuration. std::string configFilePath; ///< Path to the configuration file. bool debug = false; ///< Flag to enable debug output. + bool loaded = false; ///< Flag to indicate if the configuration has been loaded. std::map configMap; ///< Cache for the location of configuration settings. std::vector unknownKeys; ///< Cache for the existence of configuration settings. @@ -116,6 +118,9 @@ public: */ template T get(const std::string &key, T defaultValue) { + if (!loaded) { + throw std::runtime_error("Configuration file not loaded! This should be done at the very start of whatever main function you are using (and only done once!)"); + } // --- Check if the key has already been checked for existence 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