Config: Implement Get and Set

This commit is contained in:
MerryMage 2017-05-13 16:22:53 +01:00
parent 6151bc1714
commit 1548a15c68
3 changed files with 76 additions and 16 deletions

View File

@ -38,22 +38,6 @@ void InvokeConfigChangedCallbacks();
void Load(); void Load();
void Save(); void Save();
// Often used functions for getting or setting configuration on the base layer for the main system
template <typename T>
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 <typename T>
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 Init();
void Shutdown(); void Shutdown();

View File

@ -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); 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 } // namespace Config

View File

@ -2,8 +2,11 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#pragma once
#include <string> #include <string>
#include "Common/Config/Config.h"
#include "Common/Config/Enums.h" #include "Common/Config/Enums.h"
namespace Config namespace Config
@ -18,4 +21,61 @@ struct ConfigLocation
bool operator!=(const ConfigLocation& other) const; bool operator!=(const ConfigLocation& other) const;
bool operator<(const ConfigLocation& other) const; bool operator<(const ConfigLocation& other) const;
}; };
template <typename T>
struct ConfigInfo
{
ConfigLocation location;
T default_value;
};
template <typename T>
T Get(LayerType layer, const ConfigInfo<T>& info)
{
return GetLayer(layer)
->GetOrCreateSection(info.location.system, info.location.section)
->template Get<T>(info.location.key, info.default_value);
}
template <typename T>
T Get(const ConfigInfo<T>& info)
{
return Get(LayerType::Meta, info);
}
template <typename T>
T GetBase(const ConfigInfo<T>& info)
{
return Get(LayerType::Base, info);
}
LayerType GetActiveLayerForConfig(const ConfigLocation&);
template <typename T>
LayerType GetActiveLayerForConfig(const ConfigInfo<T>& info)
{
return GetActiveLayerForConfig(info.location);
}
template <typename T>
void Set(LayerType layer, const ConfigInfo<T>& info, const T& value)
{
GetLayer(layer)
->GetOrCreateSection(info.location.system, info.location.section)
->Set(info.location.key, value);
InvokeConfigChangedCallbacks();
}
template <typename T>
void SetBase(const ConfigInfo<T>& info, const T& value)
{
Set<T>(LayerType::Base, info, value);
}
template <typename T>
void SetCurrent(const ConfigInfo<T>& info, const T& value)
{
Set<T>(LayerType::CurrentRun, info, value);
}
} // namespace Config } // namespace Config