Merge pull request #6825 from leoetlino/onion-types

Config: Fix implicit conversions/enum config types
This commit is contained in:
Léo Lam
2018-05-12 21:59:43 +02:00
committed by GitHub
14 changed files with 110 additions and 74 deletions

View File

@ -70,26 +70,26 @@ LayerType GetActiveLayerForConfig(const ConfigInfo<T>& info)
}
template <typename T>
void Set(LayerType layer, const ConfigInfo<T>& info, const T& value)
void Set(LayerType layer, const ConfigInfo<T>& info, const std::common_type_t<T>& value)
{
GetLayer(layer)->Set(info, value);
InvokeConfigChangedCallbacks();
}
template <typename T>
void SetBase(const ConfigInfo<T>& info, const T& value)
void SetBase(const ConfigInfo<T>& info, const std::common_type_t<T>& value)
{
Set<T>(LayerType::Base, info, value);
}
template <typename T>
void SetCurrent(const ConfigInfo<T>& info, const T& value)
void SetCurrent(const ConfigInfo<T>& info, const std::common_type_t<T>& value)
{
Set<T>(LayerType::CurrentRun, info, value);
}
template <typename T>
void SetBaseOrCurrent(const ConfigInfo<T>& info, const T& value)
void SetBaseOrCurrent(const ConfigInfo<T>& info, const std::common_type_t<T>& value)
{
if (GetActiveLayerForConfig(info) == LayerType::Base)
Set<T>(LayerType::Base, info, value);

View File

@ -5,11 +5,19 @@
#pragma once
#include <string>
#include <type_traits>
#include "Common/Config/Enums.h"
namespace Config
{
namespace detail
{
// std::underlying_type may only be used with enum types, so make sure T is an enum type first.
template <typename T>
using UnderlyingType = typename std::enable_if_t<std::is_enum<T>{}, std::underlying_type<T>>::type;
} // namespace detail
struct ConfigLocation
{
System system;
@ -24,6 +32,21 @@ struct ConfigLocation
template <typename T>
struct ConfigInfo
{
ConfigInfo(const ConfigLocation& location_, const T& default_value_)
: location{location_}, default_value{default_value_}
{
}
// Make it easy to convert ConfigInfo<Enum> into ConfigInfo<UnderlyingType<Enum>>
// so that enum settings can still easily work with code that doesn't care about the enum values.
template <typename Enum,
std::enable_if_t<std::is_same<T, detail::UnderlyingType<Enum>>::value>* = nullptr>
ConfigInfo(const ConfigInfo<Enum>& other)
: location{other.location}, default_value{static_cast<detail::UnderlyingType<Enum>>(
other.default_value)}
{
}
ConfigLocation location;
T default_value;
};

View File

@ -8,6 +8,7 @@
#include <memory>
#include <optional>
#include <string>
#include <type_traits>
#include <vector>
#include "Common/Config/ConfigInfo.h"
@ -25,8 +26,13 @@ std::string ValueToString(double value);
std::string ValueToString(int value);
std::string ValueToString(bool value);
std::string ValueToString(const std::string& value);
template <typename T, std::enable_if_t<std::is_enum<T>::value>* = nullptr>
std::string ValueToString(T value)
{
return ValueToString(static_cast<std::underlying_type_t<T>>(value));
}
template <typename T>
template <typename T, std::enable_if_t<!std::is_enum<T>::value>* = nullptr>
std::optional<T> TryParse(const std::string& str_value)
{
T value;
@ -35,6 +41,15 @@ std::optional<T> TryParse(const std::string& str_value)
return value;
}
template <typename T, std::enable_if_t<std::is_enum<T>::value>* = nullptr>
std::optional<T> TryParse(const std::string& str_value)
{
const auto result = TryParse<std::underlying_type_t<T>>(str_value);
if (result)
return static_cast<T>(*result);
return {};
}
template <>
inline std::optional<std::string> TryParse(const std::string& str_value)
{
@ -116,7 +131,7 @@ public:
}
template <typename T>
void Set(const ConfigInfo<T>& config_info, const T& value)
void Set(const ConfigInfo<T>& config_info, const std::common_type_t<T>& value)
{
Set<T>(config_info.location, value);
}

View File

@ -169,8 +169,11 @@ void LogManager::SaveSettings()
Config::SetBaseOrCurrent(LOGGER_VERBOSITY, static_cast<int>(GetLogLevel()));
for (const auto& container : m_log)
Config::SetBaseOrCurrent({{Config::System::Logger, "Logs", container.m_short_name}, false},
container.m_enable);
{
const Config::ConfigInfo<bool> info{{Config::System::Logger, "Logs", container.m_short_name},
false};
Config::SetBaseOrCurrent(info, container.m_enable);
}
Config::Save();
}