Config: Implement Get and Set

This commit is contained in:
MerryMage committed 2017-06-03 18:11:56 +01:00
1 parent 6151bc1714
commit 1548a15c68
3 files changed
+76 -16

No files matched your search

+16
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);
}
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
+60
View File
@@ -2,8 +2,11 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <string>
#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 <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