mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2024-11-15 22:09:19 -07:00
6763a3fce1
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).
54 lines
1.4 KiB
C++
54 lines
1.4 KiB
C++
// Copyright 2017 Dolphin Emulator Project
|
|
// Licensed under GPLv2+
|
|
// Refer to the license.txt file included.
|
|
|
|
#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;
|
|
std::string section;
|
|
std::string key;
|
|
|
|
bool operator==(const ConfigLocation& other) const;
|
|
bool operator!=(const ConfigLocation& other) const;
|
|
bool operator<(const ConfigLocation& other) const;
|
|
};
|
|
|
|
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;
|
|
};
|
|
}
|