32#include "yaml-cpp/yaml.h"
58 std::string configFilePath;
62 std::map<std::string, YAML::Node> configMap;
63 std::vector<std::string> unknownKeys;
73 T getFromCache(
const std::string &key, T defaultValue) {
74 if (configMap.find(key) != configMap.end()) {
76 return configMap[key].as<T>();
77 }
catch (
const YAML::Exception& e) {
90 bool isKeyInCache(
const std::string &key);
97 void addToCache(
const std::string &key,
const YAML::Node &node);
103 void registerUnknownKey(
const std::string &key);
105 bool m_loaded =
false;
108 template <
typename T>
109 T get(
const std::string &key) {
111 throw std::runtime_error(
"Error! Config file not loaded");
114 return getFromCache<T>(key, T());
116 throw std::runtime_error(
"Error! Key not found in config file");
125 static Config& getInstance();
127 Config (
const Config&) =
delete;
128 Config& operator= (
const Config&) =
delete;
129 Config (Config&&) =
delete;
130 Config& operator= (Config&&) =
delete;
132 void setDebug(
bool debug) { this->debug = debug; }
139 bool loadConfig(
const std::string& configFilePath);
145 std::string getInputTable()
const;
160 template <
typename T>
161 T get(
const std::string &key, T defaultValue) {
164#if defined(CONFIG_HARSH)
165 throw std::runtime_error(
"Error! Config file not loaded. To disable this error, recompile with CONFIG_HARSH=0");
166#elif defined(CONFIG_WARN)
167 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;
171 if (std::find(unknownKeys.begin(), unknownKeys.end(), key) != unknownKeys.end()) {
176 if (isKeyInCache(key)) {
177 return getFromCache<T>(key, defaultValue);
181 YAML::Node node = YAML::Clone(yamlRoot);
182 std::istringstream keyStream(key);
184 while (std::getline(keyStream, subKey,
':')) {
187 registerUnknownKey(key);
195 addToCache(key, node);
197 }
catch (
const YAML::Exception& e) {
199 registerUnknownKey(key);
210 bool has(
const std::string &key);
216 std::vector<std::string> keys()
const;
224 friend std::ostream&
operator<<(std::ostream& os,
const Config& config) {
225 if (!config.m_loaded) {
226 os <<
"Config file not loaded" << std::endl;
230 os <<
"Config file: " << config.configFilePath << std::endl;
233 os <<
"Config file: " << config.configFilePath << std::endl;
234 os << config.yamlRoot << std::endl;
240 friend class ::configTestPrivateAccessor;
242 friend class serif::resource::ResourceManager;
std::ostream & operator<<(std::ostream &os, const GlobalComposition &comp)