mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 14:19:46 -06:00
Config: Add support for enums
This makes it possible to use enums as the config type. Default values are now clearer and there's no need for casts when calling Config::Get/Set anymore. In order to add support for enums, the common code was updated to handle enums by using the underlying type when loading/saving settings. A copy constructor is also provided for conversions from `ConfigInfo<Enum>` to `ConfigInfo<underlying_type<Enum>>` so that enum settings can still easily work with code that doesn't care about the actual enum values (like Graphics{Choice,Radio} in DolphinQt2 which only treat the setting as an integer).
This commit is contained in:
@ -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)
|
||||
{
|
||||
|
Reference in New Issue
Block a user