fix(config): loaded flag and checks added
added check to make sure that config file has been loaded before any config variables are accessed
This commit is contained in:
@@ -26,6 +26,7 @@ bool Config::loadConfig(const std::string& configFile) {
|
|||||||
std::cerr << "Error: " << e.what() << std::endl;
|
std::cerr << "Error: " << e.what() << std::endl;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
loaded = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include "yaml-cpp/yaml.h"
|
#include "yaml-cpp/yaml.h"
|
||||||
|
|
||||||
@@ -30,6 +31,7 @@ private:
|
|||||||
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.
|
||||||
|
|
||||||
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.
|
||||||
@@ -116,6 +118,9 @@ public:
|
|||||||
*/
|
*/
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T get(const std::string &key, T defaultValue) {
|
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
|
// --- 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
|
||||||
|
|||||||
Reference in New Issue
Block a user