diff --git a/Source/Core/Common/Config/Config.h b/Source/Core/Common/Config/Config.h index a46cd0de2d..ac5b1b5e4d 100644 --- a/Source/Core/Common/Config/Config.h +++ b/Source/Core/Common/Config/Config.h @@ -38,22 +38,6 @@ void InvokeConfigChangedCallbacks(); 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(); diff --git a/Source/Core/Core/Config/Config.cpp b/Source/Core/Core/Config/Config.cpp index 9814199304..2c6aecf96f 100644 --- a/Source/Core/Core/Config/Config.cpp +++ b/Source/Core/Core/Config/Config.cpp @@ -22,4 +22,20 @@ bool ConfigLocation::operator<(const ConfigLocation& other) const { return std::tie(system, section, key) < std::tie(other.system, other.section, other.key); } + +LayerType GetActiveLayerForConfig(const ConfigLocation& config) +{ + for (auto layer : SEARCH_ORDER) + { + if (!LayerExists(layer)) + continue; + + if (GetLayer(layer)->Exists(config.system, config.section, config.key)) + return layer; + } + + // If config is not present in any layer, base layer is considered active. + return LayerType::Base; +} + } // namespace Config diff --git a/Source/Core/Core/Config/Config.h b/Source/Core/Core/Config/Config.h index c3cbba3666..bd31fe22b2 100644 --- a/Source/Core/Core/Config/Config.h +++ b/Source/Core/Core/Config/Config.h @@ -2,8 +2,11 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#pragma once + #include +#include "Common/Config/Config.h" #include "Common/Config/Enums.h" namespace Config @@ -18,4 +21,61 @@ struct ConfigLocation bool operator!=(const ConfigLocation& other) const; bool operator<(const ConfigLocation& other) const; }; + +template +struct ConfigInfo +{ + ConfigLocation location; + T default_value; +}; + +template +T Get(LayerType layer, const ConfigInfo& info) +{ + return GetLayer(layer) + ->GetOrCreateSection(info.location.system, info.location.section) + ->template Get(info.location.key, info.default_value); +} + +template +T Get(const ConfigInfo& info) +{ + return Get(LayerType::Meta, info); +} + +template +T GetBase(const ConfigInfo& info) +{ + return Get(LayerType::Base, info); +} + +LayerType GetActiveLayerForConfig(const ConfigLocation&); + +template +LayerType GetActiveLayerForConfig(const ConfigInfo& info) +{ + return GetActiveLayerForConfig(info.location); +} + +template +void Set(LayerType layer, const ConfigInfo& info, const T& value) +{ + GetLayer(layer) + ->GetOrCreateSection(info.location.system, info.location.section) + ->Set(info.location.key, value); + InvokeConfigChangedCallbacks(); +} + +template +void SetBase(const ConfigInfo& info, const T& value) +{ + Set(LayerType::Base, info, value); +} + +template +void SetCurrent(const ConfigInfo& info, const T& value) +{ + Set(LayerType::CurrentRun, info, value); +} + } // namespace Config