Config: Allow passing a DefaultState object to Set functions to delete keys.

This commit is contained in:
Jordan Woyak 2025-02-23 19:05:56 -06:00
parent 137d1375d4
commit ba1bf6959e
2 changed files with 26 additions and 13 deletions

View file

@ -100,32 +100,32 @@ LayerType GetActiveLayerForConfig(const Info<T>& info)
return GetActiveLayerForConfig(info.GetLocation()); return GetActiveLayerForConfig(info.GetLocation());
} }
template <typename T> template <typename InfoT, typename ValueT>
void Set(LayerType layer, const Info<T>& info, const std::common_type_t<T>& value) void Set(LayerType layer, const InfoT& info, const ValueT& value)
{ {
if (GetLayer(layer)->Set(info, value)) if (GetLayer(layer)->Set(info, value))
OnConfigChanged(); OnConfigChanged();
} }
template <typename T> template <typename InfoT, typename ValueT>
void SetBase(const Info<T>& info, const std::common_type_t<T>& value) void SetBase(const Info<InfoT>& info, const ValueT& value)
{ {
Set<T>(LayerType::Base, info, value); Set(LayerType::Base, info, value);
} }
template <typename T> template <typename InfoT, typename ValueT>
void SetCurrent(const Info<T>& info, const std::common_type_t<T>& value) void SetCurrent(const Info<InfoT>& info, const ValueT& value)
{ {
Set<T>(LayerType::CurrentRun, info, value); Set(LayerType::CurrentRun, info, value);
} }
template <typename T> template <typename InfoT, typename ValueT>
void SetBaseOrCurrent(const Info<T>& info, const std::common_type_t<T>& value) void SetBaseOrCurrent(const Info<InfoT>& info, const ValueT& value)
{ {
if (GetActiveLayerForConfig(info) == LayerType::Base) if (GetActiveLayerForConfig(info) == LayerType::Base)
Set<T>(LayerType::Base, info, value); Set(LayerType::Base, info, value);
else else
Set<T>(LayerType::CurrentRun, info, value); Set(LayerType::CurrentRun, info, value);
} }
template <typename T> template <typename T>

View file

@ -8,7 +8,6 @@
#include <optional> #include <optional>
#include <string> #include <string>
#include <type_traits> #include <type_traits>
#include <vector>
#include "Common/Config/ConfigInfo.h" #include "Common/Config/ConfigInfo.h"
#include "Common/Config/Enums.h" #include "Common/Config/Enums.h"
@ -16,6 +15,12 @@
namespace Config namespace Config
{ {
// Setting a key to an object of this type will delete the key.
struct DefaultState
{
friend constexpr bool operator==(DefaultState, DefaultState) { return true; };
};
namespace detail namespace detail
{ {
template <typename T, std::enable_if_t<!std::is_enum<T>::value>* = nullptr> template <typename T, std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
@ -116,12 +121,20 @@ public:
return detail::TryParse<T>(*iter->second); return detail::TryParse<T>(*iter->second);
} }
template <typename T>
bool Set(const Info<T>& config_info, DefaultState)
{
return DeleteKey(config_info.GetLocation());
}
template <typename T> template <typename T>
bool Set(const Info<T>& config_info, const std::common_type_t<T>& value) bool Set(const Info<T>& config_info, const std::common_type_t<T>& value)
{ {
return Set(config_info.GetLocation(), value); return Set(config_info.GetLocation(), value);
} }
bool Set(const Location& location, DefaultState) { return DeleteKey(location); }
template <typename T> template <typename T>
bool Set(const Location& location, const T& value) bool Set(const Location& location, const T& value)
{ {