build(config): added compile time option to disable config file load error
In general we may want to enforce that a config file is explicitly loaded before any access is requested. However, there are times when this is non ideal behavior. We introduce a compile time flag (CONFIG_HARSH, and CONFIG_WARN). If config hars is defined then a runtime error will be thrown if a config value is requested before the config file has been loaded. If Config warn is defined (and config harsh is not) then a warning will be printed, otherwise nothing will happen. If either warn or nothing is defined this means that the default values defined in the get methods will be used. Note that the meson build system has had an option added -Dconfig_error_handling=["none", "warn", "harsh"] (default="none") which can be used to manage these compile time options. In general release builds should have this disabled while debug builts should have it set to harsh.
This commit is contained in:
@@ -156,7 +156,12 @@ public:
|
||||
template <typename T>
|
||||
T get(const std::string &key, T defaultValue) {
|
||||
if (!m_loaded) {
|
||||
throw std::runtime_error("Error! Config file not loaded");
|
||||
// ONLY THROW ERROR IF HARSH OR WARN CONFIGURATION
|
||||
#if defined(CONFIG_HARSH)
|
||||
throw std::runtime_error("Error! Config file not loaded. To disable this error, recompile with CONFIG_HARSH=0");
|
||||
#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;
|
||||
#endif
|
||||
}
|
||||
// --- Check if the key has already been checked for existence
|
||||
if (std::find(unknownKeys.begin(), unknownKeys.end(), key) != unknownKeys.end()) {
|
||||
|
||||
Reference in New Issue
Block a user