Merge pull request #8593 from merryhime/ranged-setting-T

common/setting: Make ranged a property of the type
This commit is contained in:
bunnei 2022-07-16 15:32:52 -07:00 committed by GitHub
commit 7d66f8339e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 59 additions and 58 deletions

View File

@ -106,7 +106,7 @@ struct ResolutionScalingInfo {
* configurations. Specifying a default value and label is required. A minimum and maximum range can * configurations. Specifying a default value and label is required. A minimum and maximum range can
* be specified for sanitization. * be specified for sanitization.
*/ */
template <typename Type> template <typename Type, bool ranged = false>
class Setting { class Setting {
protected: protected:
Setting() = default; Setting() = default;
@ -126,8 +126,8 @@ public:
* @param default_val Intial value of the setting, and default value of the setting * @param default_val Intial value of the setting, and default value of the setting
* @param name Label for the setting * @param name Label for the setting
*/ */
explicit Setting(const Type& default_val, const std::string& name) explicit Setting(const Type& default_val, const std::string& name) requires(!ranged)
: value{default_val}, default_value{default_val}, ranged{false}, label{name} {} : value{default_val}, default_value{default_val}, label{name} {}
virtual ~Setting() = default; virtual ~Setting() = default;
/** /**
@ -139,9 +139,9 @@ public:
* @param name Label for the setting * @param name Label for the setting
*/ */
explicit Setting(const Type& default_val, const Type& min_val, const Type& max_val, explicit Setting(const Type& default_val, const Type& min_val, const Type& max_val,
const std::string& name) const std::string& name) requires(ranged)
: value{default_val}, default_value{default_val}, maximum{max_val}, minimum{min_val}, : value{default_val},
ranged{true}, label{name} {} default_value{default_val}, maximum{max_val}, minimum{min_val}, label{name} {}
/** /**
* Returns a reference to the setting's value. * Returns a reference to the setting's value.
@ -158,7 +158,7 @@ public:
* @param val The desired value * @param val The desired value
*/ */
virtual void SetValue(const Type& val) { virtual void SetValue(const Type& val) {
Type temp{(ranged) ? std::clamp(val, minimum, maximum) : val}; Type temp{ranged ? std::clamp(val, minimum, maximum) : val};
std::swap(value, temp); std::swap(value, temp);
} }
@ -188,7 +188,7 @@ public:
* @returns A reference to the setting * @returns A reference to the setting
*/ */
virtual const Type& operator=(const Type& val) { virtual const Type& operator=(const Type& val) {
Type temp{(ranged) ? std::clamp(val, minimum, maximum) : val}; Type temp{ranged ? std::clamp(val, minimum, maximum) : val};
std::swap(value, temp); std::swap(value, temp);
return value; return value;
} }
@ -207,7 +207,6 @@ protected:
const Type default_value{}; ///< The default value const Type default_value{}; ///< The default value
const Type maximum{}; ///< Maximum allowed value of the setting const Type maximum{}; ///< Maximum allowed value of the setting
const Type minimum{}; ///< Minimum allowed value of the setting const Type minimum{}; ///< Minimum allowed value of the setting
const bool ranged; ///< The setting has sanitization ranges
const std::string label{}; ///< The setting's label const std::string label{}; ///< The setting's label
}; };
@ -219,8 +218,8 @@ protected:
* *
* By default, the global setting is used. * By default, the global setting is used.
*/ */
template <typename Type> template <typename Type, bool ranged = false>
class SwitchableSetting : virtual public Setting<Type> { class SwitchableSetting : virtual public Setting<Type, ranged> {
public: public:
/** /**
* Sets a default value, label, and setting value. * Sets a default value, label, and setting value.
@ -228,7 +227,7 @@ public:
* @param default_val Intial value of the setting, and default value of the setting * @param default_val Intial value of the setting, and default value of the setting
* @param name Label for the setting * @param name Label for the setting
*/ */
explicit SwitchableSetting(const Type& default_val, const std::string& name) explicit SwitchableSetting(const Type& default_val, const std::string& name) requires(!ranged)
: Setting<Type>{default_val, name} {} : Setting<Type>{default_val, name} {}
virtual ~SwitchableSetting() = default; virtual ~SwitchableSetting() = default;
@ -241,8 +240,8 @@ public:
* @param name Label for the setting * @param name Label for the setting
*/ */
explicit SwitchableSetting(const Type& default_val, const Type& min_val, const Type& max_val, explicit SwitchableSetting(const Type& default_val, const Type& min_val, const Type& max_val,
const std::string& name) const std::string& name) requires(ranged)
: Setting<Type>{default_val, min_val, max_val, name} {} : Setting<Type, true>{default_val, min_val, max_val, name} {}
/** /**
* Tells this setting to represent either the global or custom setting when other member * Tells this setting to represent either the global or custom setting when other member
@ -290,7 +289,7 @@ public:
* @param val The new value * @param val The new value
*/ */
void SetValue(const Type& val) override { void SetValue(const Type& val) override {
Type temp{(this->ranged) ? std::clamp(val, this->minimum, this->maximum) : val}; Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val};
if (use_global) { if (use_global) {
std::swap(this->value, temp); std::swap(this->value, temp);
} else { } else {
@ -306,7 +305,7 @@ public:
* @returns A reference to the current setting value * @returns A reference to the current setting value
*/ */
const Type& operator=(const Type& val) override { const Type& operator=(const Type& val) override {
Type temp{(this->ranged) ? std::clamp(val, this->minimum, this->maximum) : val}; Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val};
if (use_global) { if (use_global) {
std::swap(this->value, temp); std::swap(this->value, temp);
return this->value; return this->value;
@ -374,15 +373,15 @@ struct Values {
Setting<std::string> audio_device_id{"auto", "output_device"}; Setting<std::string> audio_device_id{"auto", "output_device"};
Setting<std::string> sink_id{"auto", "output_engine"}; Setting<std::string> sink_id{"auto", "output_engine"};
Setting<bool> audio_muted{false, "audio_muted"}; Setting<bool> audio_muted{false, "audio_muted"};
SwitchableSetting<u8> volume{100, 0, 100, "volume"}; SwitchableSetting<u8, true> volume{100, 0, 100, "volume"};
// Core // Core
SwitchableSetting<bool> use_multi_core{true, "use_multi_core"}; SwitchableSetting<bool> use_multi_core{true, "use_multi_core"};
SwitchableSetting<bool> use_extended_memory_layout{false, "use_extended_memory_layout"}; SwitchableSetting<bool> use_extended_memory_layout{false, "use_extended_memory_layout"};
// Cpu // Cpu
SwitchableSetting<CPUAccuracy> cpu_accuracy{CPUAccuracy::Auto, CPUAccuracy::Auto, SwitchableSetting<CPUAccuracy, true> cpu_accuracy{CPUAccuracy::Auto, CPUAccuracy::Auto,
CPUAccuracy::Paranoid, "cpu_accuracy"}; CPUAccuracy::Paranoid, "cpu_accuracy"};
// TODO: remove cpu_accuracy_first_time, migration setting added 8 July 2021 // TODO: remove cpu_accuracy_first_time, migration setting added 8 July 2021
Setting<bool> cpu_accuracy_first_time{true, "cpu_accuracy_first_time"}; Setting<bool> cpu_accuracy_first_time{true, "cpu_accuracy_first_time"};
Setting<bool> cpu_debug_mode{false, "cpu_debug_mode"}; Setting<bool> cpu_debug_mode{false, "cpu_debug_mode"};
@ -409,7 +408,7 @@ struct Values {
true, "cpuopt_unsafe_ignore_global_monitor"}; true, "cpuopt_unsafe_ignore_global_monitor"};
// Renderer // Renderer
SwitchableSetting<RendererBackend> renderer_backend{ SwitchableSetting<RendererBackend, true> renderer_backend{
RendererBackend::Vulkan, RendererBackend::OpenGL, RendererBackend::Vulkan, "backend"}; RendererBackend::Vulkan, RendererBackend::OpenGL, RendererBackend::Vulkan, "backend"};
Setting<bool> renderer_debug{false, "debug"}; Setting<bool> renderer_debug{false, "debug"};
Setting<bool> renderer_shader_feedback{false, "shader_feedback"}; Setting<bool> renderer_shader_feedback{false, "shader_feedback"};
@ -423,28 +422,28 @@ struct Values {
SwitchableSetting<AntiAliasing> anti_aliasing{AntiAliasing::None, "anti_aliasing"}; SwitchableSetting<AntiAliasing> anti_aliasing{AntiAliasing::None, "anti_aliasing"};
// *nix platforms may have issues with the borderless windowed fullscreen mode. // *nix platforms may have issues with the borderless windowed fullscreen mode.
// Default to exclusive fullscreen on these platforms for now. // Default to exclusive fullscreen on these platforms for now.
SwitchableSetting<FullscreenMode> fullscreen_mode{ SwitchableSetting<FullscreenMode, true> fullscreen_mode{
#ifdef _WIN32 #ifdef _WIN32
FullscreenMode::Borderless, FullscreenMode::Borderless,
#else #else
FullscreenMode::Exclusive, FullscreenMode::Exclusive,
#endif #endif
FullscreenMode::Borderless, FullscreenMode::Exclusive, "fullscreen_mode"}; FullscreenMode::Borderless, FullscreenMode::Exclusive, "fullscreen_mode"};
SwitchableSetting<int> aspect_ratio{0, 0, 3, "aspect_ratio"}; SwitchableSetting<int, true> aspect_ratio{0, 0, 3, "aspect_ratio"};
SwitchableSetting<int> max_anisotropy{0, 0, 5, "max_anisotropy"}; SwitchableSetting<int, true> max_anisotropy{0, 0, 5, "max_anisotropy"};
SwitchableSetting<bool> use_speed_limit{true, "use_speed_limit"}; SwitchableSetting<bool> use_speed_limit{true, "use_speed_limit"};
SwitchableSetting<u16> speed_limit{100, 0, 9999, "speed_limit"}; SwitchableSetting<u16, true> speed_limit{100, 0, 9999, "speed_limit"};
SwitchableSetting<bool> use_disk_shader_cache{true, "use_disk_shader_cache"}; SwitchableSetting<bool> use_disk_shader_cache{true, "use_disk_shader_cache"};
SwitchableSetting<GPUAccuracy> gpu_accuracy{GPUAccuracy::High, GPUAccuracy::Normal, SwitchableSetting<GPUAccuracy, true> gpu_accuracy{GPUAccuracy::High, GPUAccuracy::Normal,
GPUAccuracy::Extreme, "gpu_accuracy"}; GPUAccuracy::Extreme, "gpu_accuracy"};
SwitchableSetting<bool> use_asynchronous_gpu_emulation{true, "use_asynchronous_gpu_emulation"}; SwitchableSetting<bool> use_asynchronous_gpu_emulation{true, "use_asynchronous_gpu_emulation"};
SwitchableSetting<NvdecEmulation> nvdec_emulation{NvdecEmulation::GPU, "nvdec_emulation"}; SwitchableSetting<NvdecEmulation> nvdec_emulation{NvdecEmulation::GPU, "nvdec_emulation"};
SwitchableSetting<bool> accelerate_astc{true, "accelerate_astc"}; SwitchableSetting<bool> accelerate_astc{true, "accelerate_astc"};
SwitchableSetting<bool> use_vsync{true, "use_vsync"}; SwitchableSetting<bool> use_vsync{true, "use_vsync"};
SwitchableSetting<u16> fps_cap{1000, 1, 1000, "fps_cap"}; SwitchableSetting<u16, true> fps_cap{1000, 1, 1000, "fps_cap"};
Setting<bool> disable_fps_limit{false, "disable_fps_limit"}; Setting<bool> disable_fps_limit{false, "disable_fps_limit"};
SwitchableSetting<ShaderBackend> shader_backend{ShaderBackend::GLASM, ShaderBackend::GLSL, SwitchableSetting<ShaderBackend, true> shader_backend{ShaderBackend::GLASM, ShaderBackend::GLSL,
ShaderBackend::SPIRV, "shader_backend"}; ShaderBackend::SPIRV, "shader_backend"};
SwitchableSetting<bool> use_asynchronous_shaders{false, "use_asynchronous_shaders"}; SwitchableSetting<bool> use_asynchronous_shaders{false, "use_asynchronous_shaders"};
SwitchableSetting<bool> use_fast_gpu_time{true, "use_fast_gpu_time"}; SwitchableSetting<bool> use_fast_gpu_time{true, "use_fast_gpu_time"};
@ -460,10 +459,10 @@ struct Values {
s64 custom_rtc_differential; s64 custom_rtc_differential;
Setting<s32> current_user{0, "current_user"}; Setting<s32> current_user{0, "current_user"};
SwitchableSetting<s32> language_index{1, 0, 17, "language_index"}; SwitchableSetting<s32, true> language_index{1, 0, 17, "language_index"};
SwitchableSetting<s32> region_index{1, 0, 6, "region_index"}; SwitchableSetting<s32, true> region_index{1, 0, 6, "region_index"};
SwitchableSetting<s32> time_zone_index{0, 0, 45, "time_zone_index"}; SwitchableSetting<s32, true> time_zone_index{0, 0, 45, "time_zone_index"};
SwitchableSetting<s32> sound_index{1, 0, 2, "sound_index"}; SwitchableSetting<s32, true> sound_index{1, 0, 2, "sound_index"};
// Controls // Controls
InputSetting<std::array<PlayerInput, 10>> players; InputSetting<std::array<PlayerInput, 10>> players;
@ -485,7 +484,7 @@ struct Values {
Setting<bool> tas_loop{false, "tas_loop"}; Setting<bool> tas_loop{false, "tas_loop"};
Setting<bool> mouse_panning{false, "mouse_panning"}; Setting<bool> mouse_panning{false, "mouse_panning"};
Setting<u8> mouse_panning_sensitivity{10, 1, 100, "mouse_panning_sensitivity"}; Setting<u8, true> mouse_panning_sensitivity{10, 1, 100, "mouse_panning_sensitivity"};
Setting<bool> mouse_enabled{false, "mouse_enabled"}; Setting<bool> mouse_enabled{false, "mouse_enabled"};
Setting<bool> emulate_analog_keyboard{false, "emulate_analog_keyboard"}; Setting<bool> emulate_analog_keyboard{false, "emulate_analog_keyboard"};

View File

@ -143,8 +143,8 @@ void Config::ReadBasicSetting(Settings::Setting<std::string>& setting) {
} }
} }
template <typename Type> template <typename Type, bool ranged>
void Config::ReadBasicSetting(Settings::Setting<Type>& setting) { void Config::ReadBasicSetting(Settings::Setting<Type, ranged>& setting) {
const QString name = QString::fromStdString(setting.GetLabel()); const QString name = QString::fromStdString(setting.GetLabel());
const Type default_value = setting.GetDefault(); const Type default_value = setting.GetDefault();
if (qt_config->value(name + QStringLiteral("/default"), false).toBool()) { if (qt_config->value(name + QStringLiteral("/default"), false).toBool()) {
@ -164,16 +164,16 @@ void Config::WriteBasicSetting(const Settings::Setting<std::string>& setting) {
qt_config->setValue(name, QString::fromStdString(value)); qt_config->setValue(name, QString::fromStdString(value));
} }
template <typename Type> template <typename Type, bool ranged>
void Config::WriteBasicSetting(const Settings::Setting<Type>& setting) { void Config::WriteBasicSetting(const Settings::Setting<Type, ranged>& setting) {
const QString name = QString::fromStdString(setting.GetLabel()); const QString name = QString::fromStdString(setting.GetLabel());
const Type value = setting.GetValue(); const Type value = setting.GetValue();
qt_config->setValue(name + QStringLiteral("/default"), value == setting.GetDefault()); qt_config->setValue(name + QStringLiteral("/default"), value == setting.GetDefault());
qt_config->setValue(name, value); qt_config->setValue(name, value);
} }
template <typename Type> template <typename Type, bool ranged>
void Config::WriteGlobalSetting(const Settings::SwitchableSetting<Type>& setting) { void Config::WriteGlobalSetting(const Settings::SwitchableSetting<Type, ranged>& setting) {
const QString name = QString::fromStdString(setting.GetLabel()); const QString name = QString::fromStdString(setting.GetLabel());
const Type& value = setting.GetValue(global); const Type& value = setting.GetValue(global);
if (!global) { if (!global) {
@ -1421,8 +1421,8 @@ QVariant Config::ReadSetting(const QString& name, const QVariant& default_value)
return result; return result;
} }
template <typename Type> template <typename Type, bool ranged>
void Config::ReadGlobalSetting(Settings::SwitchableSetting<Type>& setting) { void Config::ReadGlobalSetting(Settings::SwitchableSetting<Type, ranged>& setting) {
QString name = QString::fromStdString(setting.GetLabel()); QString name = QString::fromStdString(setting.GetLabel());
const bool use_global = qt_config->value(name + QStringLiteral("/use_global"), true).toBool(); const bool use_global = qt_config->value(name + QStringLiteral("/use_global"), true).toBool();
setting.SetGlobal(use_global); setting.SetGlobal(use_global);

View File

@ -159,8 +159,8 @@ private:
* *
* @param The setting * @param The setting
*/ */
template <typename Type> template <typename Type, bool ranged>
void ReadGlobalSetting(Settings::SwitchableSetting<Type>& setting); void ReadGlobalSetting(Settings::SwitchableSetting<Type, ranged>& setting);
/** /**
* Sets a value to the qt_config using the setting's label and default value. If the config is a * Sets a value to the qt_config using the setting's label and default value. If the config is a
@ -168,8 +168,8 @@ private:
* *
* @param The setting * @param The setting
*/ */
template <typename Type> template <typename Type, bool ranged>
void WriteGlobalSetting(const Settings::SwitchableSetting<Type>& setting); void WriteGlobalSetting(const Settings::SwitchableSetting<Type, ranged>& setting);
/** /**
* Reads a value from the qt_config using the setting's label and default value and applies the * Reads a value from the qt_config using the setting's label and default value and applies the
@ -177,15 +177,15 @@ private:
* *
* @param The setting * @param The setting
*/ */
template <typename Type> template <typename Type, bool ranged>
void ReadBasicSetting(Settings::Setting<Type>& setting); void ReadBasicSetting(Settings::Setting<Type, ranged>& setting);
/** Sets a value from the setting in the qt_config using the setting's label and default value. /** Sets a value from the setting in the qt_config using the setting's label and default value.
* *
* @param The setting * @param The setting
*/ */
template <typename Type> template <typename Type, bool ranged>
void WriteBasicSetting(const Settings::Setting<Type>& setting); void WriteBasicSetting(const Settings::Setting<Type, ranged>& setting);
ConfigType type; ConfigType type;
std::unique_ptr<QSettings> qt_config; std::unique_ptr<QSettings> qt_config;

View File

@ -27,8 +27,9 @@ enum class CheckState {
// ApplyPerGameSetting, given a Settings::Setting and a Qt UI element, properly applies a Setting // ApplyPerGameSetting, given a Settings::Setting and a Qt UI element, properly applies a Setting
void ApplyPerGameSetting(Settings::SwitchableSetting<bool>* setting, const QCheckBox* checkbox, void ApplyPerGameSetting(Settings::SwitchableSetting<bool>* setting, const QCheckBox* checkbox,
const CheckState& tracker); const CheckState& tracker);
template <typename Type> template <typename Type, bool ranged>
void ApplyPerGameSetting(Settings::SwitchableSetting<Type>* setting, const QComboBox* combobox) { void ApplyPerGameSetting(Settings::SwitchableSetting<Type, ranged>* setting,
const QComboBox* combobox) {
if (Settings::IsConfiguringGlobal() && setting->UsingGlobal()) { if (Settings::IsConfiguringGlobal() && setting->UsingGlobal()) {
setting->SetValue(static_cast<Type>(combobox->currentIndex())); setting->SetValue(static_cast<Type>(combobox->currentIndex()));
} else if (!Settings::IsConfiguringGlobal()) { } else if (!Settings::IsConfiguringGlobal()) {
@ -45,8 +46,9 @@ void ApplyPerGameSetting(Settings::SwitchableSetting<Type>* setting, const QComb
// Sets a Qt UI element given a Settings::Setting // Sets a Qt UI element given a Settings::Setting
void SetPerGameSetting(QCheckBox* checkbox, const Settings::SwitchableSetting<bool>* setting); void SetPerGameSetting(QCheckBox* checkbox, const Settings::SwitchableSetting<bool>* setting);
template <typename Type> template <typename Type, bool ranged>
void SetPerGameSetting(QComboBox* combobox, const Settings::SwitchableSetting<Type>* setting) { void SetPerGameSetting(QComboBox* combobox,
const Settings::SwitchableSetting<Type, ranged>* setting) {
combobox->setCurrentIndex(setting->UsingGlobal() ? ConfigurationShared::USE_GLOBAL_INDEX combobox->setCurrentIndex(setting->UsingGlobal() ? ConfigurationShared::USE_GLOBAL_INDEX
: static_cast<int>(setting->GetValue()) + : static_cast<int>(setting->GetValue()) +
ConfigurationShared::USE_GLOBAL_OFFSET); ConfigurationShared::USE_GLOBAL_OFFSET);

View File

@ -99,8 +99,8 @@ void Config::ReadSetting(const std::string& group, Settings::Setting<bool>& sett
setting = sdl2_config->GetBoolean(group, setting.GetLabel(), setting.GetDefault()); setting = sdl2_config->GetBoolean(group, setting.GetLabel(), setting.GetDefault());
} }
template <typename Type> template <typename Type, bool ranged>
void Config::ReadSetting(const std::string& group, Settings::Setting<Type>& setting) { void Config::ReadSetting(const std::string& group, Settings::Setting<Type, ranged>& setting) {
setting = static_cast<Type>(sdl2_config->GetInteger(group, setting.GetLabel(), setting = static_cast<Type>(sdl2_config->GetInteger(group, setting.GetLabel(),
static_cast<long>(setting.GetDefault()))); static_cast<long>(setting.GetDefault())));
} }

View File

@ -33,6 +33,6 @@ private:
* @param group The name of the INI group * @param group The name of the INI group
* @param setting The yuzu setting to modify * @param setting The yuzu setting to modify
*/ */
template <typename Type> template <typename Type, bool ranged>
void ReadSetting(const std::string& group, Settings::Setting<Type>& setting); void ReadSetting(const std::string& group, Settings::Setting<Type, ranged>& setting);
}; };