diff options
| author | Léo Lam <leo@innovatetechnologi.es> | 2018-05-11 22:38:44 +0200 |
|---|---|---|
| committer | Léo Lam <leo@innovatetechnologi.es> | 2018-05-12 18:10:26 +0200 |
| commit | 6763a3fce1df5a949900dd976393c7fe89fa4aec (patch) | |
| tree | cdf3ad1c04d6746d62d7f53ff0a2c46e547727b9 /Source | |
| parent | 7dca7c237eb9ae40765e581317df6df4b4fc1ad4 (diff) | |
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).
Diffstat (limited to 'Source')
| -rw-r--r-- | Source/Core/Common/Config/ConfigInfo.h | 23 | ||||
| -rw-r--r-- | Source/Core/Common/Config/Layer.h | 17 | ||||
| -rw-r--r-- | Source/Core/Core/Config/GraphicsSettings.cpp | 16 | ||||
| -rw-r--r-- | Source/Core/Core/Config/GraphicsSettings.h | 12 | ||||
| -rw-r--r-- | Source/Core/DolphinQt2/Config/Graphics/GraphicsChoice.h | 8 | ||||
| -rw-r--r-- | Source/Core/DolphinQt2/Config/Graphics/GraphicsRadio.h | 8 | ||||
| -rw-r--r-- | Source/Core/DolphinQt2/HotkeyScheduler.cpp | 25 | ||||
| -rw-r--r-- | Source/Core/DolphinWX/Frame.cpp | 20 | ||||
| -rw-r--r-- | Source/Core/DolphinWX/VideoConfigDiag.cpp | 3 | ||||
| -rw-r--r-- | Source/Core/VideoCommon/VideoConfig.cpp | 9 |
10 files changed, 87 insertions, 54 deletions
diff --git a/Source/Core/Common/Config/ConfigInfo.h b/Source/Core/Common/Config/ConfigInfo.h index fc36ecee03..c5c4cc49a1 100644 --- a/Source/Core/Common/Config/ConfigInfo.h +++ b/Source/Core/Common/Config/ConfigInfo.h @@ -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; }; diff --git a/Source/Core/Common/Config/Layer.h b/Source/Core/Common/Config/Layer.h index e2af6a24ae..3e01b83fed 100644 --- a/Source/Core/Common/Config/Layer.h +++ b/Source/Core/Common/Config/Layer.h @@ -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) { diff --git a/Source/Core/Core/Config/GraphicsSettings.cpp b/Source/Core/Core/Config/GraphicsSettings.cpp index cfae1b14f2..6646cff79e 100644 --- a/Source/Core/Core/Config/GraphicsSettings.cpp +++ b/Source/Core/Core/Config/GraphicsSettings.cpp @@ -21,10 +21,10 @@ const ConfigInfo<int> GFX_ADAPTER{{System::GFX, "Hardware", "Adapter"}, 0}; // Graphics.Settings const ConfigInfo<bool> GFX_WIDESCREEN_HACK{{System::GFX, "Settings", "wideScreenHack"}, false}; -const ConfigInfo<int> GFX_ASPECT_RATIO{{System::GFX, "Settings", "AspectRatio"}, - static_cast<int>(AspectMode::Auto)}; -const ConfigInfo<int> GFX_SUGGESTED_ASPECT_RATIO{{System::GFX, "Settings", "SuggestedAspectRatio"}, - static_cast<int>(AspectMode::Auto)}; +const ConfigInfo<AspectMode> GFX_ASPECT_RATIO{{System::GFX, "Settings", "AspectRatio"}, + AspectMode::Auto}; +const ConfigInfo<AspectMode> GFX_SUGGESTED_ASPECT_RATIO{ + {System::GFX, "Settings", "SuggestedAspectRatio"}, AspectMode::Auto}; const ConfigInfo<bool> GFX_CROP{{System::GFX, "Settings", "Crop"}, false}; const ConfigInfo<int> GFX_SAFE_TEXTURE_CACHE_COLOR_SAMPLES{ {System::GFX, "Settings", "SafeTextureCacheColorSamples"}, 128}; @@ -78,9 +78,8 @@ const ConfigInfo<int> GFX_COMMAND_BUFFER_EXECUTE_INTERVAL{ const ConfigInfo<bool> GFX_SHADER_CACHE{{System::GFX, "Settings", "ShaderCache"}, true}; const ConfigInfo<bool> GFX_WAIT_FOR_SHADERS_BEFORE_STARTING{ {System::GFX, "Settings", "WaitForShadersBeforeStarting"}, false}; -const ConfigInfo<int> GFX_SHADER_COMPILATION_MODE{ - {System::GFX, "Settings", "ShaderCompilationMode"}, - static_cast<int>(ShaderCompilationMode::Synchronous)}; +const ConfigInfo<ShaderCompilationMode> GFX_SHADER_COMPILATION_MODE{ + {System::GFX, "Settings", "ShaderCompilationMode"}, ShaderCompilationMode::Synchronous}; const ConfigInfo<int> GFX_SHADER_COMPILER_THREADS{ {System::GFX, "Settings", "ShaderCompilerThreads"}, 1}; const ConfigInfo<int> GFX_SHADER_PRECOMPILER_THREADS{ @@ -111,7 +110,8 @@ const ConfigInfo<bool> GFX_ENHANCE_DISABLE_COPY_FILTER{ // Graphics.Stereoscopy -const ConfigInfo<int> GFX_STEREO_MODE{{System::GFX, "Stereoscopy", "StereoMode"}, 0}; +const ConfigInfo<StereoMode> GFX_STEREO_MODE{{System::GFX, "Stereoscopy", "StereoMode"}, + StereoMode::Off}; const ConfigInfo<int> GFX_STEREO_DEPTH{{System::GFX, "Stereoscopy", "StereoDepth"}, 20}; const ConfigInfo<int> GFX_STEREO_CONVERGENCE_PERCENTAGE{ {System::GFX, "Stereoscopy", "StereoConvergencePercentage"}, 100}; diff --git a/Source/Core/Core/Config/GraphicsSettings.h b/Source/Core/Core/Config/GraphicsSettings.h index 6b4c900e01..c0113ae83e 100644 --- a/Source/Core/Core/Config/GraphicsSettings.h +++ b/Source/Core/Core/Config/GraphicsSettings.h @@ -8,6 +8,10 @@ #include "Common/Config/Config.h" +enum class AspectMode : int; +enum class ShaderCompilationMode : int; +enum class StereoMode : int; + namespace Config { // Configuration Information @@ -20,8 +24,8 @@ extern const ConfigInfo<int> GFX_ADAPTER; // Graphics.Settings extern const ConfigInfo<bool> GFX_WIDESCREEN_HACK; -extern const ConfigInfo<int> GFX_ASPECT_RATIO; -extern const ConfigInfo<int> GFX_SUGGESTED_ASPECT_RATIO; +extern const ConfigInfo<AspectMode> GFX_ASPECT_RATIO; +extern const ConfigInfo<AspectMode> GFX_SUGGESTED_ASPECT_RATIO; extern const ConfigInfo<bool> GFX_CROP; extern const ConfigInfo<int> GFX_SAFE_TEXTURE_CACHE_COLOR_SAMPLES; extern const ConfigInfo<bool> GFX_SHOW_FPS; @@ -60,7 +64,7 @@ extern const ConfigInfo<bool> GFX_BACKEND_MULTITHREADING; extern const ConfigInfo<int> GFX_COMMAND_BUFFER_EXECUTE_INTERVAL; extern const ConfigInfo<bool> GFX_SHADER_CACHE; extern const ConfigInfo<bool> GFX_WAIT_FOR_SHADERS_BEFORE_STARTING; -extern const ConfigInfo<int> GFX_SHADER_COMPILATION_MODE; +extern const ConfigInfo<ShaderCompilationMode> GFX_SHADER_COMPILATION_MODE; extern const ConfigInfo<int> GFX_SHADER_COMPILER_THREADS; extern const ConfigInfo<int> GFX_SHADER_PRECOMPILER_THREADS; @@ -84,7 +88,7 @@ extern const ConfigInfo<bool> GFX_ENHANCE_DISABLE_COPY_FILTER; // Graphics.Stereoscopy -extern const ConfigInfo<int> GFX_STEREO_MODE; +extern const ConfigInfo<StereoMode> GFX_STEREO_MODE; extern const ConfigInfo<int> GFX_STEREO_DEPTH; extern const ConfigInfo<int> GFX_STEREO_CONVERGENCE_PERCENTAGE; extern const ConfigInfo<bool> GFX_STEREO_SWAP_EYES; diff --git a/Source/Core/DolphinQt2/Config/Graphics/GraphicsChoice.h b/Source/Core/DolphinQt2/Config/Graphics/GraphicsChoice.h index a0aa525fee..a689d21960 100644 --- a/Source/Core/DolphinQt2/Config/Graphics/GraphicsChoice.h +++ b/Source/Core/DolphinQt2/Config/Graphics/GraphicsChoice.h @@ -6,11 +6,7 @@ #include <QComboBox> -namespace Config -{ -template <typename T> -struct ConfigInfo; -} +#include "Common/Config/Config.h" class GraphicsChoice : public QComboBox { @@ -20,5 +16,5 @@ public: private: void Update(int choice); - const Config::ConfigInfo<int>& m_setting; + Config::ConfigInfo<int> m_setting; }; diff --git a/Source/Core/DolphinQt2/Config/Graphics/GraphicsRadio.h b/Source/Core/DolphinQt2/Config/Graphics/GraphicsRadio.h index dde18704ee..ff74600ad4 100644 --- a/Source/Core/DolphinQt2/Config/Graphics/GraphicsRadio.h +++ b/Source/Core/DolphinQt2/Config/Graphics/GraphicsRadio.h @@ -6,11 +6,7 @@ #include <QRadioButton> -namespace Config -{ -template <typename T> -struct ConfigInfo; -} +#include "Common/Config/Config.h" class GraphicsRadioInt : public QRadioButton { @@ -21,6 +17,6 @@ public: private: void Update(); - const Config::ConfigInfo<int>& m_setting; + Config::ConfigInfo<int> m_setting; int m_value; }; diff --git a/Source/Core/DolphinQt2/HotkeyScheduler.cpp b/Source/Core/DolphinQt2/HotkeyScheduler.cpp index 690a896012..3ac8ff8d2a 100644 --- a/Source/Core/DolphinQt2/HotkeyScheduler.cpp +++ b/Source/Core/DolphinQt2/HotkeyScheduler.cpp @@ -279,8 +279,8 @@ void HotkeyScheduler::Run() if (IsHotkey(HK_TOGGLE_AR)) { show_msg(OSDMessage::ARToggled); - const auto aspect_ratio = (Config::Get(Config::GFX_ASPECT_RATIO) + 1) & 3; - Config::SetCurrent(Config::GFX_ASPECT_RATIO, aspect_ratio); + const int aspect_ratio = (static_cast<int>(Config::Get(Config::GFX_ASPECT_RATIO)) + 1) & 3; + Config::SetCurrent(Config::GFX_ASPECT_RATIO, static_cast<AspectMode>(aspect_ratio)); } if (IsHotkey(HK_TOGGLE_EFBCOPIES)) { @@ -344,48 +344,47 @@ void HotkeyScheduler::Run() // Stereoscopy if (IsHotkey(HK_TOGGLE_STEREO_SBS) || IsHotkey(HK_TOGGLE_STEREO_TAB)) { - if (Config::Get(Config::GFX_STEREO_MODE) != static_cast<int>(StereoMode::SBS)) + if (Config::Get(Config::GFX_STEREO_MODE) != StereoMode::SBS) { // Disable post-processing shader, as stereoscopy itself is currently a shader if (Config::Get(Config::GFX_ENHANCE_POST_SHADER) == DUBOIS_ALGORITHM_SHADER) Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, ""); - Config::SetCurrent(Config::GFX_STEREO_MODE, IsHotkey(HK_TOGGLE_STEREO_SBS) ? - static_cast<int>(StereoMode::SBS) : - static_cast<int>(StereoMode::TAB)); + Config::SetCurrent(Config::GFX_STEREO_MODE, + IsHotkey(HK_TOGGLE_STEREO_SBS) ? StereoMode::SBS : StereoMode::TAB); } else { - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::Off)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Off); } } if (IsHotkey(HK_TOGGLE_STEREO_ANAGLYPH)) { - if (Config::Get(Config::GFX_STEREO_MODE) != static_cast<int>(StereoMode::Anaglyph)) + if (Config::Get(Config::GFX_STEREO_MODE) != StereoMode::Anaglyph) { - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::Anaglyph)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Anaglyph); Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, DUBOIS_ALGORITHM_SHADER); } else { - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::Off)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Off); Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, ""); } } if (IsHotkey(HK_TOGGLE_STEREO_3DVISION)) { - if (Config::Get(Config::GFX_STEREO_MODE) != static_cast<int>(StereoMode::Nvidia3DVision)) + if (Config::Get(Config::GFX_STEREO_MODE) != StereoMode::Nvidia3DVision) { if (Config::Get(Config::GFX_ENHANCE_POST_SHADER) == DUBOIS_ALGORITHM_SHADER) Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, ""); - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::Nvidia3DVision)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Nvidia3DVision); } else { - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::Off)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Off); } } } diff --git a/Source/Core/DolphinWX/Frame.cpp b/Source/Core/DolphinWX/Frame.cpp index 212c32430d..954be63fd3 100644 --- a/Source/Core/DolphinWX/Frame.cpp +++ b/Source/Core/DolphinWX/Frame.cpp @@ -1441,9 +1441,9 @@ void CFrame::ParseHotkeys() { show_msg(OSDMessage::ARToggled); // Toggle aspect ratio - int aspect_ratio = Config::Get(Config::GFX_ASPECT_RATIO); + int aspect_ratio = static_cast<int>(Config::Get(Config::GFX_ASPECT_RATIO)); aspect_ratio = (aspect_ratio + 1) & 3; - Config::SetCurrent(Config::GFX_ASPECT_RATIO, aspect_ratio); + Config::SetCurrent(Config::GFX_ASPECT_RATIO, static_cast<AspectMode>(aspect_ratio)); } if (IsHotkey(HK_TOGGLE_EFBCOPIES)) { @@ -1526,11 +1526,11 @@ void CFrame::ParseHotkeys() { Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, ""); } - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::SBS)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::SBS); } else { - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::Off)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Off); } } if (IsHotkey(HK_TOGGLE_STEREO_TAB)) @@ -1541,11 +1541,11 @@ void CFrame::ParseHotkeys() { Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, ""); } - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::TAB)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::TAB); } else { - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::Off)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Off); } } if (IsHotkey(HK_TOGGLE_STEREO_ANAGLYPH)) @@ -1554,12 +1554,12 @@ void CFrame::ParseHotkeys() { // Setting the anaglyph mode also requires a specific // post-processing shader to be activated. - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::Anaglyph)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Anaglyph); Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, "dubois"); } else { - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::Off)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Off); Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, ""); } } @@ -1571,11 +1571,11 @@ void CFrame::ParseHotkeys() { Config::SetCurrent(Config::GFX_ENHANCE_POST_SHADER, ""); } - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::Nvidia3DVision)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Nvidia3DVision); } else { - Config::SetCurrent(Config::GFX_STEREO_MODE, static_cast<int>(StereoMode::Off)); + Config::SetCurrent(Config::GFX_STEREO_MODE, StereoMode::Off); } } diff --git a/Source/Core/DolphinWX/VideoConfigDiag.cpp b/Source/Core/DolphinWX/VideoConfigDiag.cpp index c29ce83e1b..11f6ef49cd 100644 --- a/Source/Core/DolphinWX/VideoConfigDiag.cpp +++ b/Source/Core/DolphinWX/VideoConfigDiag.cpp @@ -465,7 +465,8 @@ VideoConfigDiag::VideoConfigDiag(wxWindow* parent, const std::string& title) { szr_shader_compilation->Add( CreateRadioButton(page_general, modes[i].first, modes[i].second, - Config::GFX_SHADER_COMPILATION_MODE, static_cast<int>(i)), + Config::GFX_SHADER_COMPILATION_MODE, + static_cast<ShaderCompilationMode>(i)), wxGBPosition(static_cast<int>(i / 2), static_cast<int>(i % 2)), wxDefaultSpan, wxALIGN_CENTER_VERTICAL); } diff --git a/Source/Core/VideoCommon/VideoConfig.cpp b/Source/Core/VideoCommon/VideoConfig.cpp index abdf27dcd1..2d7f958d8f 100644 --- a/Source/Core/VideoCommon/VideoConfig.cpp +++ b/Source/Core/VideoCommon/VideoConfig.cpp @@ -60,9 +60,9 @@ void VideoConfig::Refresh() iAdapter = Config::Get(Config::GFX_ADAPTER); bWidescreenHack = Config::Get(Config::GFX_WIDESCREEN_HACK); - const auto config_aspect_mode = static_cast<AspectMode>(Config::Get(Config::GFX_ASPECT_RATIO)); + const AspectMode config_aspect_mode = Config::Get(Config::GFX_ASPECT_RATIO); if (config_aspect_mode == AspectMode::Auto) - aspect_mode = static_cast<AspectMode>(Config::Get(Config::GFX_SUGGESTED_ASPECT_RATIO)); + aspect_mode = Config::Get(Config::GFX_SUGGESTED_ASPECT_RATIO); else aspect_mode = config_aspect_mode; bCrop = Config::Get(Config::GFX_CROP); @@ -103,8 +103,7 @@ void VideoConfig::Refresh() iCommandBufferExecuteInterval = Config::Get(Config::GFX_COMMAND_BUFFER_EXECUTE_INTERVAL); bShaderCache = Config::Get(Config::GFX_SHADER_CACHE); bWaitForShadersBeforeStarting = Config::Get(Config::GFX_WAIT_FOR_SHADERS_BEFORE_STARTING); - iShaderCompilationMode = - static_cast<ShaderCompilationMode>(Config::Get(Config::GFX_SHADER_COMPILATION_MODE)); + iShaderCompilationMode = Config::Get(Config::GFX_SHADER_COMPILATION_MODE); iShaderCompilerThreads = Config::Get(Config::GFX_SHADER_COMPILER_THREADS); iShaderPrecompilerThreads = Config::Get(Config::GFX_SHADER_PRECOMPILER_THREADS); @@ -122,7 +121,7 @@ void VideoConfig::Refresh() bForceTrueColor = Config::Get(Config::GFX_ENHANCE_FORCE_TRUE_COLOR); bDisableCopyFilter = Config::Get(Config::GFX_ENHANCE_DISABLE_COPY_FILTER); - stereo_mode = static_cast<StereoMode>(Config::Get(Config::GFX_STEREO_MODE)); + stereo_mode = Config::Get(Config::GFX_STEREO_MODE); iStereoDepth = Config::Get(Config::GFX_STEREO_DEPTH); iStereoConvergencePercentage = Config::Get(Config::GFX_STEREO_CONVERGENCE_PERCENTAGE); bStereoSwapEyes = Config::Get(Config::GFX_STEREO_SWAP_EYES); |
