From abe6f8766a5a1d9007584b4224ba9b74fdf645f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Thu, 16 Feb 2017 10:59:22 +0100 Subject: [PATCH] Config: Add a few helper functions for repetitive tasks Getting and setting configuration from the base config layer are common and repetitive tasks. This commit adds some simpler to use functions to make the new system easier to work with. Config::Get and Config::Set are intended to make switching from SConfig a bit less painful. They always operate on the main system. Example usage: // before auto base_layer = Config::GetLayer(Config::LayerType::Base); auto core = base_layer->GetOrCreateSection(Config::System::Main, "Core"); u8 language; core->Get("Language", &language, 0); SetData("IPL.LNG", language); // now auto base_layer = Config::GetLayer(Config::LayerType::Base); auto core = base_layer->GetOrCreateSection(Config::System::Main, "Core"); SetData("IPL.LNG", core->Get("Language", 0)); // or simply SetData("IPL.LNG", Config::Get("Core", "Language", 0)); --- Source/Core/Common/Config.h | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/Source/Core/Common/Config.h b/Source/Core/Common/Config.h index f08de4ec8b..cc880df5e2 100644 --- a/Source/Core/Common/Config.h +++ b/Source/Core/Common/Config.h @@ -91,6 +91,14 @@ public: bool Get(const std::string& key, float* value, float defaultValue = 0.0f) const; bool Get(const std::string& key, double* value, double defaultValue = 0.0) const; + template + T Get(const std::string& key, const T& default_value) const + { + T value; + Get(key, value, default_value); + return value; + } + // Section chunk void SetLines(const std::vector& lines); // XXX: Add to recursive layer @@ -187,6 +195,22 @@ void AddConfigChangedCallback(ConfigChangedCallback func); void Load(); void Save(); +// Often used functions for getting or setting configuration on the base layer for the main system +template +T Get(const std::string& section_name, const std::string& key, const T& default_value) +{ + auto base_layer = GetLayer(Config::LayerType::Base); + return base_layer->GetOrCreateSection(Config::System::Main, section_name) + ->Get(key, default_value); +} + +template +void Set(const std::string& section_name, const std::string& key, const T& value) +{ + auto base_layer = GetLayer(Config::LayerType::Base); + base_layer->GetOrCreateSection(Config::System::Main, section_name)->Set(key, value); +} + void Init(); void Shutdown();