From 04d4b6ab802a1238eaca6c0a16d1a739503b81d9 Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Sat, 10 Jun 2023 23:40:39 -0400 Subject: (ui,)settings: Use explicit instantiation Reduces compile times a tad on clang. --- src/common/settings_setting.h | 413 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 413 insertions(+) create mode 100644 src/common/settings_setting.h (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h new file mode 100644 index 000000000..f226e38d4 --- /dev/null +++ b/src/common/settings_setting.h @@ -0,0 +1,413 @@ +#pragma once + +#include +#include +#include +#include +#include +#include "common/common_types.h" +#include "common/settings_common.h" +#include "common/settings_enums.h" + +namespace Settings { + +/** The Setting class is a simple resource manager. It defines a label and default value + * alongside the actual value of the setting for simpler and less-error prone use with frontend + * configurations. Specifying a default value and label is required. A minimum and maximum range + * can be specified for sanitization. + */ +template +class Setting : public BasicSetting { +protected: + Setting() = default; + + /** + * Only sets the setting to the given initializer, leaving the other members to their default + * initializers. + * + * @param global_val Initial value of the setting + */ + explicit Setting(const Type& val) + : value{val}, + default_value{}, maximum{}, minimum{}, label{}, category{Category::Miscellaneous}, id{} {} + +public: + /** + * Sets a default value, label, and setting value. + * + * @param linkage Setting registry + * @param default_val Initial value of the setting, and default value of the setting + * @param name Label for the setting + * @param category_ Category of the setting AKA INI group + */ + explicit Setting(Linkage& linkage, const Type& default_val, const std::string& name, + enum Category category_, bool save_ = true, bool runtime_modifiable_ = false) + requires(!ranged) + : value{default_val}, default_value{default_val}, label{name}, category{category_}, + id{linkage.count}, save{save_}, runtime_modifiable{runtime_modifiable_} { + linkage.by_category[category].push_front(this); + linkage.count++; + } + virtual ~Setting() = default; + + /** + * Sets a default value, minimum value, maximum value, and label. + * + * @param linkage Setting registry + * @param default_val Initial value of the setting, and default value of the setting + * @param min_val Sets the minimum allowed value of the setting + * @param max_val Sets the maximum allowed value of the setting + * @param name Label for the setting + * @param category_ Category of the setting AKA INI group + */ + explicit Setting(Linkage& linkage, const Type& default_val, const Type& min_val, + const Type& max_val, const std::string& name, enum Category category_, + bool save_ = true, bool runtime_modifiable_ = false) + requires(ranged) + : value{default_val}, default_value{default_val}, maximum{max_val}, minimum{min_val}, + label{name}, category{category_}, id{linkage.count}, save{save_}, + runtime_modifiable{runtime_modifiable_} { + linkage.by_category[category].push_front(this); + linkage.count++; + } + + /** + * Returns a reference to the setting's value. + * + * @returns A reference to the setting + */ + [[nodiscard]] virtual const Type& GetValue() const { + return value; + } + + /** + * Sets the setting to the given value. + * + * @param val The desired value + */ + virtual void SetValue(const Type& val) { + Type temp{ranged ? std::clamp(val, minimum, maximum) : val}; + std::swap(value, temp); + } + + /** + * Returns the value that this setting was created with. + * + * @returns A reference to the default value + */ + [[nodiscard]] const Type& GetDefault() const { + return default_value; + } + + /** + * Returns the label this setting was created with. + * + * @returns A reference to the label + */ + [[nodiscard]] const std::string& GetLabel() const override { + return label; + } + + /** + * Returns the setting's category AKA INI group. + * + * @returns The setting's category + */ + [[nodiscard]] enum Category Category() const override { + return category; + } + + [[nodiscard]] bool RuntimeModfiable() const override { + return runtime_modifiable; + } + + [[nodiscard]] constexpr bool IsEnum() const override { + return std::is_enum::value; + } + + /** + * Returns whether the current setting is Switchable. + * + * @returns If the setting is a SwitchableSetting + */ + [[nodiscard]] virtual constexpr bool Switchable() const override { + return false; + } + +protected: + std::string ToString(const Type& value_) const { + if constexpr (std::is_same()) { + return value_; + } else if constexpr (std::is_same>()) { + return value_.has_value() ? std::to_string(*value_) : "none"; + } else if constexpr (std::is_same()) { + return value_ ? "true" : "false"; + } else if (std::is_same()) { + return CanonicalizeEnum(value_); + } else { + return std::to_string(static_cast(value_)); + } + } + +public: + /** + * Converts the value of the setting to a std::string. Respects the global state if the setting + * has one. + * + * @returns The current setting as a std::string + */ + std::string ToString() const override { + return ToString(this->GetValue()); + } + + /** + * Returns the default value of the setting as a std::string. + * + * @returns The default value as a string. + */ + std::string DefaultToString() const override { + return ToString(default_value); + } + + /** + * Assigns a value to the setting. + * + * @param val The desired setting value + * + * @returns A reference to the setting + */ + virtual const Type& operator=(const Type& val) { + Type temp{ranged ? std::clamp(val, minimum, maximum) : val}; + std::swap(value, temp); + return value; + } + + /** + * Returns a reference to the setting. + * + * @returns A reference to the setting + */ + explicit virtual operator const Type&() const { + return value; + } + + /** + * Converts the given value to the Setting's type of value. Uses SetValue to enter the setting, + * thus respecting its constraints. + * + * @param input The desired value + */ + void LoadString(const std::string& input) override { + if (input.empty()) { + this->SetValue(this->GetDefault()); + return; + } + try { + if constexpr (std::is_same()) { + this->SetValue(input); + } else if constexpr (std::is_same>()) { + this->SetValue(static_cast(std::stoul(input))); + } else if constexpr (std::is_same()) { + this->SetValue(input == "true"); + } else if constexpr (std::is_same()) { + this->SetValue(ToEnum(input)); + } else { + this->SetValue(static_cast(std::stoll(input))); + } + } catch (std::invalid_argument) { + this->SetValue(this->GetDefault()); + } + } + + [[nodiscard]] std::string constexpr Canonicalize() const override { + if constexpr (std::is_enum::value) { + return CanonicalizeEnum(this->GetValue()); + } + return ToString(this->GetValue()); + } + + /** + * Returns the save preference of the setting i.e. when saving or reading the setting from a + * frontend, whether this setting should be skipped. + * + * @returns The save preference + */ + virtual bool Save() const override { + return save; + } + + /** + * Gives us another way to identify the setting without having to go through a string. + * + * @returns the type_index of the setting's type + */ + virtual std::type_index TypeId() const override { + return std::type_index(typeid(Type)); + } + + virtual constexpr u32 Id() const override { + return id; + } + + virtual std::string MinVal() const override { + return this->ToString(minimum); + } + virtual std::string MaxVal() const override { + return this->ToString(maximum); + } + +protected: + Type value{}; ///< The setting + const Type default_value{}; ///< The default value + const Type maximum{}; ///< Maximum allowed value of the setting + const Type minimum{}; ///< Minimum allowed value of the setting + const std::string label{}; ///< The setting's label + const enum Category category; ///< The setting's category AKA INI group + const u32 id; + bool save; + bool runtime_modifiable; +}; + +/** + * The SwitchableSetting class is a slightly more complex version of the Setting class. This adds a + * custom setting to switch to when a guest application specifically requires it. The effect is that + * other components of the emulator can access the setting's intended value without any need for the + * component to ask whether the custom or global setting is needed at the moment. + * + * By default, the global setting is used. + */ +template +class SwitchableSetting : virtual public Setting { +public: + /** + * Sets a default value, label, and setting value. + * + * @param linkage Setting registry + * @param default_val Initial value of the setting, and default value of the setting + * @param name Label for the setting + * @param category_ Category of the setting AKA INI group + */ + explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const std::string& name, + Category category, bool save = true, bool runtime_modifiable = false) + requires(!ranged) + : Setting{linkage, default_val, name, category, save, runtime_modifiable} { + linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); }); + } + virtual ~SwitchableSetting() = default; + + /** + * Sets a default value, minimum value, maximum value, and label. + * + * @param linkage Setting registry + * @param default_val Initial value of the setting, and default value of the setting + * @param min_val Sets the minimum allowed value of the setting + * @param max_val Sets the maximum allowed value of the setting + * @param name Label for the setting + * @param category_ Category of the setting AKA INI group + */ + explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const Type& min_val, + const Type& max_val, const std::string& name, Category category, + bool save = true, bool runtime_modifiable = false) + requires(ranged) + : Setting{linkage, default_val, min_val, max_val, + name, category, save, runtime_modifiable} { + linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); }); + } + + /** + * Tells this setting to represent either the global or custom setting when other member + * functions are used. + * + * @param to_global Whether to use the global or custom setting. + */ + void SetGlobal(bool to_global) override { + use_global = to_global; + } + + /** + * Returns whether this setting is using the global setting or not. + * + * @returns The global state + */ + [[nodiscard]] bool UsingGlobal() const override { + return use_global; + } + + /** + * Returns either the global or custom setting depending on the values of this setting's global + * state or if the global value was specifically requested. + * + * @param need_global Request global value regardless of setting's state; defaults to false + * + * @returns The required value of the setting + */ + [[nodiscard]] virtual const Type& GetValue() const override { + if (use_global) { + return this->value; + } + return custom; + } + [[nodiscard]] virtual const Type& GetValue(bool need_global) const { + if (use_global || need_global) { + return this->value; + } + return custom; + } + + /** + * Sets the current setting value depending on the global state. + * + * @param val The new value + */ + void SetValue(const Type& val) override { + Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val}; + if (use_global) { + std::swap(this->value, temp); + } else { + std::swap(custom, temp); + } + } + + [[nodiscard]] virtual constexpr bool Switchable() const override { + return true; + } + + [[nodiscard]] virtual std::string ToStringGlobal() const override { + return this->ToString(this->value); + } + + /** + * Assigns the current setting value depending on the global state. + * + * @param val The new value + * + * @returns A reference to the current setting value + */ + const Type& operator=(const Type& val) override { + Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val}; + if (use_global) { + std::swap(this->value, temp); + return this->value; + } + std::swap(custom, temp); + return custom; + } + + /** + * Returns the current setting value depending on the global state. + * + * @returns A reference to the current setting value + */ + virtual explicit operator const Type&() const override { + if (use_global) { + return this->value; + } + return custom; + } + +protected: + bool use_global{true}; ///< The setting's global state + Type custom{}; ///< The custom value of the setting +}; + +} // namespace Settings -- cgit v1.2.3 From 4903f40efe7b0c193309708ea01bd6abea8bac16 Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Sun, 11 Jun 2023 00:41:14 -0400 Subject: settings_setting: Fix errors ToString didn't have a constexpr if statement where needed. Canonicalize missed an else, causing unreachable code error on MSVC. --- src/common/settings_setting.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index f226e38d4..99a4bad01 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -142,7 +142,7 @@ protected: return value_.has_value() ? std::to_string(*value_) : "none"; } else if constexpr (std::is_same()) { return value_ ? "true" : "false"; - } else if (std::is_same()) { + } else if constexpr (std::is_same()) { return CanonicalizeEnum(value_); } else { return std::to_string(static_cast(value_)); @@ -222,8 +222,9 @@ public: [[nodiscard]] std::string constexpr Canonicalize() const override { if constexpr (std::is_enum::value) { return CanonicalizeEnum(this->GetValue()); + } else { + return ToString(this->GetValue()); } - return ToString(this->GetValue()); } /** -- cgit v1.2.3 From 11e7e1b8cec5a665bdc6c9e702f83ad6ea35dd6b Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Mon, 12 Jun 2023 17:05:30 -0400 Subject: settings: Move some simple data to BasicSetting Reduces the need for the compiler to duplicate this code, by about 100KB executable size. --- src/common/settings_setting.h | 86 ++++++------------------------------------- 1 file changed, 11 insertions(+), 75 deletions(-) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index 99a4bad01..1ca3acf18 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -1,3 +1,6 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + #pragma once #include @@ -21,16 +24,6 @@ class Setting : public BasicSetting { protected: Setting() = default; - /** - * Only sets the setting to the given initializer, leaving the other members to their default - * initializers. - * - * @param global_val Initial value of the setting - */ - explicit Setting(const Type& val) - : value{val}, - default_value{}, maximum{}, minimum{}, label{}, category{Category::Miscellaneous}, id{} {} - public: /** * Sets a default value, label, and setting value. @@ -43,11 +36,8 @@ public: explicit Setting(Linkage& linkage, const Type& default_val, const std::string& name, enum Category category_, bool save_ = true, bool runtime_modifiable_ = false) requires(!ranged) - : value{default_val}, default_value{default_val}, label{name}, category{category_}, - id{linkage.count}, save{save_}, runtime_modifiable{runtime_modifiable_} { - linkage.by_category[category].push_front(this); - linkage.count++; - } + : BasicSetting(linkage, name, category_, save_, runtime_modifiable_), value{default_val}, + default_value{default_val} {} virtual ~Setting() = default; /** @@ -64,12 +54,8 @@ public: const Type& max_val, const std::string& name, enum Category category_, bool save_ = true, bool runtime_modifiable_ = false) requires(ranged) - : value{default_val}, default_value{default_val}, maximum{max_val}, minimum{min_val}, - label{name}, category{category_}, id{linkage.count}, save{save_}, - runtime_modifiable{runtime_modifiable_} { - linkage.by_category[category].push_front(this); - linkage.count++; - } + : BasicSetting(linkage, name, category_, save_, runtime_modifiable_), value{default_val}, + default_value{default_val}, maximum{max_val}, minimum{min_val} {} /** * Returns a reference to the setting's value. @@ -99,41 +85,10 @@ public: return default_value; } - /** - * Returns the label this setting was created with. - * - * @returns A reference to the label - */ - [[nodiscard]] const std::string& GetLabel() const override { - return label; - } - - /** - * Returns the setting's category AKA INI group. - * - * @returns The setting's category - */ - [[nodiscard]] enum Category Category() const override { - return category; - } - - [[nodiscard]] bool RuntimeModfiable() const override { - return runtime_modifiable; - } - [[nodiscard]] constexpr bool IsEnum() const override { return std::is_enum::value; } - /** - * Returns whether the current setting is Switchable. - * - * @returns If the setting is a SwitchableSetting - */ - [[nodiscard]] virtual constexpr bool Switchable() const override { - return false; - } - protected: std::string ToString(const Type& value_) const { if constexpr (std::is_same()) { @@ -227,16 +182,6 @@ public: } } - /** - * Returns the save preference of the setting i.e. when saving or reading the setting from a - * frontend, whether this setting should be skipped. - * - * @returns The save preference - */ - virtual bool Save() const override { - return save; - } - /** * Gives us another way to identify the setting without having to go through a string. * @@ -246,10 +191,6 @@ public: return std::type_index(typeid(Type)); } - virtual constexpr u32 Id() const override { - return id; - } - virtual std::string MinVal() const override { return this->ToString(minimum); } @@ -258,15 +199,10 @@ public: } protected: - Type value{}; ///< The setting - const Type default_value{}; ///< The default value - const Type maximum{}; ///< Maximum allowed value of the setting - const Type minimum{}; ///< Minimum allowed value of the setting - const std::string label{}; ///< The setting's label - const enum Category category; ///< The setting's category AKA INI group - const u32 id; - bool save; - bool runtime_modifiable; + Type value{}; ///< The setting + const Type default_value{}; ///< The default value + const Type maximum{}; ///< Maximum allowed value of the setting + const Type minimum{}; ///< Minimum allowed value of the setting }; /** -- cgit v1.2.3 From 27e53990ed7100159cc08fd8470a9faecd011cbe Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Tue, 13 Jun 2023 19:37:54 -0400 Subject: settings: Document BasicSetting, add Ranged --- src/common/settings_setting.h | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index 1ca3acf18..658b6328d 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -198,6 +198,10 @@ public: return this->ToString(maximum); } + constexpr bool Ranged() const override { + return ranged; + } + protected: Type value{}; ///< The setting const Type default_value{}; ///< The default value -- cgit v1.2.3 From ee32b177823b9b8499c9fd188a571884f00cf655 Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Sun, 18 Jun 2023 03:52:41 -0400 Subject: common,yuzu-qt: GCC warning silences Fixes -Wshadow, -Wdeprecated, and catch by copy rather than by ref. --- src/common/settings_setting.h | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index 658b6328d..f803e4e6e 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -169,7 +170,7 @@ public: } else { this->SetValue(static_cast(std::stoll(input))); } - } catch (std::invalid_argument) { + } catch (std::invalid_argument& e) { this->SetValue(this->GetDefault()); } } @@ -229,9 +230,10 @@ public: * @param category_ Category of the setting AKA INI group */ explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const std::string& name, - Category category, bool save = true, bool runtime_modifiable = false) + Category category_, bool save_ = true, + bool runtime_modifiable_ = false) requires(!ranged) - : Setting{linkage, default_val, name, category, save, runtime_modifiable} { + : Setting{linkage, default_val, name, category_, save_, runtime_modifiable_} { linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); }); } virtual ~SwitchableSetting() = default; @@ -247,11 +249,11 @@ public: * @param category_ Category of the setting AKA INI group */ explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const Type& min_val, - const Type& max_val, const std::string& name, Category category, - bool save = true, bool runtime_modifiable = false) + const Type& max_val, const std::string& name, Category category_, + bool save_ = true, bool runtime_modifiable_ = false) requires(ranged) : Setting{linkage, default_val, min_val, max_val, - name, category, save, runtime_modifiable} { + name, category_, save_, runtime_modifiable_} { linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); }); } -- cgit v1.2.3 From c97cbd089b371ed07234c66633f105862462826a Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Sun, 18 Jun 2023 17:58:07 -0400 Subject: settings_setting: Fix MSVC error --- src/common/settings_setting.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index f803e4e6e..a0a05da54 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -170,7 +170,7 @@ public: } else { this->SetValue(static_cast(std::stoll(input))); } - } catch (std::invalid_argument& e) { + } catch (std::invalid_argument&) { this->SetValue(this->GetDefault()); } } -- cgit v1.2.3 From b2438f1fb7d083ffe8c8afdc30e9c612631d6ace Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Wed, 21 Jun 2023 03:23:36 -0400 Subject: settings: Define specializations for settings Suggests to a frontend how to represent each setting. --- src/common/settings_setting.h | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index a0a05da54..9805a5b5d 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -35,10 +35,12 @@ public: * @param category_ Category of the setting AKA INI group */ explicit Setting(Linkage& linkage, const Type& default_val, const std::string& name, - enum Category category_, bool save_ = true, bool runtime_modifiable_ = false) + enum Category category_, + enum Specialization specialization = Specialization::Default, + bool save_ = true, bool runtime_modifiable_ = false) requires(!ranged) - : BasicSetting(linkage, name, category_, save_, runtime_modifiable_), value{default_val}, - default_value{default_val} {} + : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization), + value{default_val}, default_value{default_val} {} virtual ~Setting() = default; /** @@ -53,10 +55,11 @@ public: */ explicit Setting(Linkage& linkage, const Type& default_val, const Type& min_val, const Type& max_val, const std::string& name, enum Category category_, + enum Specialization specialization = Specialization::Default, bool save_ = true, bool runtime_modifiable_ = false) requires(ranged) - : BasicSetting(linkage, name, category_, save_, runtime_modifiable_), value{default_val}, - default_value{default_val}, maximum{max_val}, minimum{min_val} {} + : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization), + value{default_val}, default_value{default_val}, maximum{max_val}, minimum{min_val} {} /** * Returns a reference to the setting's value. @@ -230,10 +233,12 @@ public: * @param category_ Category of the setting AKA INI group */ explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const std::string& name, - Category category_, bool save_ = true, - bool runtime_modifiable_ = false) + Category category_, + enum Specialization specialization = Specialization::Default, + bool save_ = true, bool runtime_modifiable_ = false) requires(!ranged) - : Setting{linkage, default_val, name, category_, save_, runtime_modifiable_} { + : Setting{linkage, default_val, name, category_, specialization, + save_, runtime_modifiable_} { linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); }); } virtual ~SwitchableSetting() = default; @@ -250,10 +255,12 @@ public: */ explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const Type& min_val, const Type& max_val, const std::string& name, Category category_, + enum Specialization specialization = Specialization::Default, bool save_ = true, bool runtime_modifiable_ = false) requires(ranged) - : Setting{linkage, default_val, min_val, max_val, - name, category_, save_, runtime_modifiable_} { + : Setting{linkage, default_val, min_val, + max_val, name, category_, + specialization, save_, runtime_modifiable_} { linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); }); } -- cgit v1.2.3 From 7f708e8d77fb6237407f49246622dbe1b445d536 Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Wed, 21 Jun 2023 04:32:13 -0400 Subject: settings: Define paired settings settings_common: Remove unused optional --- src/common/settings_setting.h | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index 9805a5b5d..dd91250a1 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -37,9 +37,11 @@ public: explicit Setting(Linkage& linkage, const Type& default_val, const std::string& name, enum Category category_, enum Specialization specialization = Specialization::Default, - bool save_ = true, bool runtime_modifiable_ = false) + bool save_ = true, bool runtime_modifiable_ = false, + BasicSetting* other_setting = nullptr) requires(!ranged) - : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization), + : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization, + other_setting), value{default_val}, default_value{default_val} {} virtual ~Setting() = default; @@ -56,9 +58,11 @@ public: explicit Setting(Linkage& linkage, const Type& default_val, const Type& min_val, const Type& max_val, const std::string& name, enum Category category_, enum Specialization specialization = Specialization::Default, - bool save_ = true, bool runtime_modifiable_ = false) + bool save_ = true, bool runtime_modifiable_ = false, + BasicSetting* other_setting = nullptr) requires(ranged) - : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization), + : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization, + other_setting), value{default_val}, default_value{default_val}, maximum{max_val}, minimum{min_val} {} /** @@ -235,10 +239,12 @@ public: explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const std::string& name, Category category_, enum Specialization specialization = Specialization::Default, - bool save_ = true, bool runtime_modifiable_ = false) + bool save_ = true, bool runtime_modifiable_ = false, + BasicSetting* other_setting = nullptr) requires(!ranged) - : Setting{linkage, default_val, name, category_, specialization, - save_, runtime_modifiable_} { + : Setting{ + linkage, default_val, name, category_, specialization, + save_, runtime_modifiable_, other_setting} { linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); }); } virtual ~SwitchableSetting() = default; @@ -256,11 +262,12 @@ public: explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const Type& min_val, const Type& max_val, const std::string& name, Category category_, enum Specialization specialization = Specialization::Default, - bool save_ = true, bool runtime_modifiable_ = false) + bool save_ = true, bool runtime_modifiable_ = false, + BasicSetting* other_setting = nullptr) requires(ranged) - : Setting{linkage, default_val, min_val, - max_val, name, category_, - specialization, save_, runtime_modifiable_} { + : Setting{ + linkage, default_val, min_val, max_val, name, category_, specialization, + save_, runtime_modifiable_, other_setting} { linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); }); } -- cgit v1.2.3 From 926f3e3d3e6ff57633d2d44085f02754ffe1c988 Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Wed, 21 Jun 2023 05:04:21 -0400 Subject: settings,configuration: Add a default suffix --- src/common/settings_setting.h | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index dd91250a1..d1915cb43 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -35,8 +35,7 @@ public: * @param category_ Category of the setting AKA INI group */ explicit Setting(Linkage& linkage, const Type& default_val, const std::string& name, - enum Category category_, - enum Specialization specialization = Specialization::Default, + enum Category category_, u32 specialization = Specialization::Default, bool save_ = true, bool runtime_modifiable_ = false, BasicSetting* other_setting = nullptr) requires(!ranged) @@ -57,9 +56,8 @@ public: */ explicit Setting(Linkage& linkage, const Type& default_val, const Type& min_val, const Type& max_val, const std::string& name, enum Category category_, - enum Specialization specialization = Specialization::Default, - bool save_ = true, bool runtime_modifiable_ = false, - BasicSetting* other_setting = nullptr) + u32 specialization = Specialization::Default, bool save_ = true, + bool runtime_modifiable_ = false, BasicSetting* other_setting = nullptr) requires(ranged) : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization, other_setting), @@ -237,8 +235,7 @@ public: * @param category_ Category of the setting AKA INI group */ explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const std::string& name, - Category category_, - enum Specialization specialization = Specialization::Default, + Category category_, u32 specialization = Specialization::Default, bool save_ = true, bool runtime_modifiable_ = false, BasicSetting* other_setting = nullptr) requires(!ranged) @@ -261,8 +258,8 @@ public: */ explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const Type& min_val, const Type& max_val, const std::string& name, Category category_, - enum Specialization specialization = Specialization::Default, - bool save_ = true, bool runtime_modifiable_ = false, + u32 specialization = Specialization::Default, bool save_ = true, + bool runtime_modifiable_ = false, BasicSetting* other_setting = nullptr) requires(ranged) : Setting{ -- cgit v1.2.3 From 54d58130a0818efc4d70d72624f734f2b6f0a6d3 Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Wed, 21 Jun 2023 15:38:30 -0400 Subject: settings_setting: Silence shadowing warnings --- src/common/settings_setting.h | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index d1915cb43..eb46b2b6d 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -35,12 +35,12 @@ public: * @param category_ Category of the setting AKA INI group */ explicit Setting(Linkage& linkage, const Type& default_val, const std::string& name, - enum Category category_, u32 specialization = Specialization::Default, + enum Category category_, u32 specialization_ = Specialization::Default, bool save_ = true, bool runtime_modifiable_ = false, - BasicSetting* other_setting = nullptr) + BasicSetting* other_setting_ = nullptr) requires(!ranged) - : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization, - other_setting), + : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization_, + other_setting_), value{default_val}, default_value{default_val} {} virtual ~Setting() = default; @@ -56,11 +56,11 @@ public: */ explicit Setting(Linkage& linkage, const Type& default_val, const Type& min_val, const Type& max_val, const std::string& name, enum Category category_, - u32 specialization = Specialization::Default, bool save_ = true, - bool runtime_modifiable_ = false, BasicSetting* other_setting = nullptr) + u32 specialization_ = Specialization::Default, bool save_ = true, + bool runtime_modifiable_ = false, BasicSetting* other_setting_ = nullptr) requires(ranged) - : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization, - other_setting), + : BasicSetting(linkage, name, category_, save_, runtime_modifiable_, specialization_, + other_setting_), value{default_val}, default_value{default_val}, maximum{max_val}, minimum{min_val} {} /** @@ -235,13 +235,13 @@ public: * @param category_ Category of the setting AKA INI group */ explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const std::string& name, - Category category_, u32 specialization = Specialization::Default, + Category category_, u32 specialization_ = Specialization::Default, bool save_ = true, bool runtime_modifiable_ = false, - BasicSetting* other_setting = nullptr) + BasicSetting* other_setting_ = nullptr) requires(!ranged) : Setting{ - linkage, default_val, name, category_, specialization, - save_, runtime_modifiable_, other_setting} { + linkage, default_val, name, category_, specialization_, + save_, runtime_modifiable_, other_setting_} { linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); }); } virtual ~SwitchableSetting() = default; @@ -258,13 +258,14 @@ public: */ explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const Type& min_val, const Type& max_val, const std::string& name, Category category_, - u32 specialization = Specialization::Default, bool save_ = true, + u32 specialization_ = Specialization::Default, bool save_ = true, bool runtime_modifiable_ = false, - BasicSetting* other_setting = nullptr) + BasicSetting* other_setting_ = nullptr) requires(ranged) - : Setting{ - linkage, default_val, min_val, max_val, name, category_, specialization, - save_, runtime_modifiable_, other_setting} { + : Setting{linkage, default_val, min_val, + max_val, name, category_, + specialization_, save_, runtime_modifiable_, + other_setting_} { linkage.restore_functions.emplace_back([this]() { this->SetGlobal(true); }); } -- cgit v1.2.3 From e7f01128f181a7b754f6d5f94122cde059c7ed1a Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Wed, 21 Jun 2023 21:41:06 -0400 Subject: settings: Give indices to enums --- src/common/settings_setting.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index eb46b2b6d..2e708fa0d 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -3,6 +3,7 @@ #pragma once +#include #include #include #include @@ -197,6 +198,14 @@ public: return std::type_index(typeid(Type)); } + constexpr u32 EnumIndex() const override { + if constexpr (std::is_enum()) { + return EnumMetadata::Index(); + } else { + return std::numeric_limits::max(); + } + } + virtual std::string MinVal() const override { return this->ToString(minimum); } -- cgit v1.2.3 From 267f3c7905972ad78713cd92f96e3073ea1c8996 Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Tue, 18 Jul 2023 15:36:20 -0400 Subject: settings: Cleanup Addresses review feedback Co-authored-by: Morph <39850852+Morph1984@users.noreply.github.com> --- src/common/settings_setting.h | 77 ++++++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 30 deletions(-) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index 2e708fa0d..959b4f3f9 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -34,6 +34,10 @@ public: * @param default_val Initial value of the setting, and default value of the setting * @param name Label for the setting * @param category_ Category of the setting AKA INI group + * @param specialization_ Suggestion for how frontend implemetations represent this in a config + * @param save_ Suggests that this should or should not be saved to a frontend config file + * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded + * @param other_setting_ A second Setting to associate to this one in metadata */ explicit Setting(Linkage& linkage, const Type& default_val, const std::string& name, enum Category category_, u32 specialization_ = Specialization::Default, @@ -54,6 +58,10 @@ public: * @param max_val Sets the maximum allowed value of the setting * @param name Label for the setting * @param category_ Category of the setting AKA INI group + * @param specialization_ Suggestion for how frontend implemetations represent this in a config + * @param save_ Suggests that this should or should not be saved to a frontend config file + * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded + * @param other_setting_ A second Setting to associate to this one in metadata */ explicit Setting(Linkage& linkage, const Type& default_val, const Type& min_val, const Type& max_val, const std::string& name, enum Category category_, @@ -93,18 +101,19 @@ public: } [[nodiscard]] constexpr bool IsEnum() const override { - return std::is_enum::value; + return std::is_enum_v; } protected: - std::string ToString(const Type& value_) const { - if constexpr (std::is_same()) { + [[nodiscard]] std::string ToString(const Type& value_) const { + if constexpr (std::is_same_v) { return value_; - } else if constexpr (std::is_same>()) { + } else if constexpr (std::is_same_v>) { return value_.has_value() ? std::to_string(*value_) : "none"; - } else if constexpr (std::is_same()) { + } else if constexpr (std::is_same_v) { return value_ ? "true" : "false"; - } else if constexpr (std::is_same()) { + } else if constexpr (std::is_same_v) { + // Compatibility with old AudioEngine setting being a string return CanonicalizeEnum(value_); } else { return std::to_string(static_cast(value_)); @@ -118,7 +127,7 @@ public: * * @returns The current setting as a std::string */ - std::string ToString() const override { + [[nodiscard]] std::string ToString() const override { return ToString(this->GetValue()); } @@ -127,7 +136,7 @@ public: * * @returns The default value as a string. */ - std::string DefaultToString() const override { + [[nodiscard]] std::string DefaultToString() const override { return ToString(default_value); } @@ -159,19 +168,19 @@ public: * * @param input The desired value */ - void LoadString(const std::string& input) override { + void LoadString(const std::string& input) override final { if (input.empty()) { this->SetValue(this->GetDefault()); return; } try { - if constexpr (std::is_same()) { + if constexpr (std::is_same_v) { this->SetValue(input); - } else if constexpr (std::is_same>()) { + } else if constexpr (std::is_same_v>) { this->SetValue(static_cast(std::stoul(input))); - } else if constexpr (std::is_same()) { + } else if constexpr (std::is_same_v) { this->SetValue(input == "true"); - } else if constexpr (std::is_same()) { + } else if constexpr (std::is_same_v) { this->SetValue(ToEnum(input)); } else { this->SetValue(static_cast(std::stoll(input))); @@ -181,8 +190,8 @@ public: } } - [[nodiscard]] std::string constexpr Canonicalize() const override { - if constexpr (std::is_enum::value) { + [[nodiscard]] std::string constexpr Canonicalize() const override final { + if constexpr (std::is_enum_v) { return CanonicalizeEnum(this->GetValue()); } else { return ToString(this->GetValue()); @@ -194,26 +203,26 @@ public: * * @returns the type_index of the setting's type */ - virtual std::type_index TypeId() const override { + [[nodiscard]] std::type_index TypeId() const override final { return std::type_index(typeid(Type)); } - constexpr u32 EnumIndex() const override { - if constexpr (std::is_enum()) { + [[nodiscard]] constexpr u32 EnumIndex() const override final { + if constexpr (std::is_enum_v) { return EnumMetadata::Index(); } else { return std::numeric_limits::max(); } } - virtual std::string MinVal() const override { + [[nodiscard]] std::string MinVal() const override final { return this->ToString(minimum); } - virtual std::string MaxVal() const override { + [[nodiscard]] std::string MaxVal() const override final { return this->ToString(maximum); } - constexpr bool Ranged() const override { + [[nodiscard]] constexpr bool Ranged() const override { return ranged; } @@ -242,6 +251,10 @@ public: * @param default_val Initial value of the setting, and default value of the setting * @param name Label for the setting * @param category_ Category of the setting AKA INI group + * @param specialization_ Suggestion for how frontend implemetations represent this in a config + * @param save_ Suggests that this should or should not be saved to a frontend config file + * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded + * @param other_setting_ A second Setting to associate to this one in metadata */ explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const std::string& name, Category category_, u32 specialization_ = Specialization::Default, @@ -264,6 +277,10 @@ public: * @param max_val Sets the maximum allowed value of the setting * @param name Label for the setting * @param category_ Category of the setting AKA INI group + * @param specialization_ Suggestion for how frontend implemetations represent this in a config + * @param save_ Suggests that this should or should not be saved to a frontend config file + * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded + * @param other_setting_ A second Setting to associate to this one in metadata */ explicit SwitchableSetting(Linkage& linkage, const Type& default_val, const Type& min_val, const Type& max_val, const std::string& name, Category category_, @@ -284,7 +301,7 @@ public: * * @param to_global Whether to use the global or custom setting. */ - void SetGlobal(bool to_global) override { + void SetGlobal(bool to_global) override final { use_global = to_global; } @@ -293,7 +310,7 @@ public: * * @returns The global state */ - [[nodiscard]] bool UsingGlobal() const override { + [[nodiscard]] bool UsingGlobal() const override final { return use_global; } @@ -305,13 +322,13 @@ public: * * @returns The required value of the setting */ - [[nodiscard]] virtual const Type& GetValue() const override { + [[nodiscard]] const Type& GetValue() const override final { if (use_global) { return this->value; } return custom; } - [[nodiscard]] virtual const Type& GetValue(bool need_global) const { + [[nodiscard]] const Type& GetValue(bool need_global) const { if (use_global || need_global) { return this->value; } @@ -323,7 +340,7 @@ public: * * @param val The new value */ - void SetValue(const Type& val) override { + void SetValue(const Type& val) override final { Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val}; if (use_global) { std::swap(this->value, temp); @@ -332,11 +349,11 @@ public: } } - [[nodiscard]] virtual constexpr bool Switchable() const override { + [[nodiscard]] constexpr bool Switchable() const override final { return true; } - [[nodiscard]] virtual std::string ToStringGlobal() const override { + [[nodiscard]] std::string ToStringGlobal() const override final { return this->ToString(this->value); } @@ -347,7 +364,7 @@ public: * * @returns A reference to the current setting value */ - const Type& operator=(const Type& val) override { + const Type& operator=(const Type& val) override final { Type temp{ranged ? std::clamp(val, this->minimum, this->maximum) : val}; if (use_global) { std::swap(this->value, temp); @@ -362,7 +379,7 @@ public: * * @returns A reference to the current setting value */ - virtual explicit operator const Type&() const override { + explicit operator const Type&() const override final { if (use_global) { return this->value; } -- cgit v1.2.3 From 1e093767a85ee0fdce6f1619e967a6560963dcf3 Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Tue, 18 Jul 2023 15:42:59 -0400 Subject: common,configure_system: Rename method to GetCategory Fixes essentially a shadowing issue. --- src/common/settings_setting.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index 959b4f3f9..55280fec4 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -40,7 +40,7 @@ public: * @param other_setting_ A second Setting to associate to this one in metadata */ explicit Setting(Linkage& linkage, const Type& default_val, const std::string& name, - enum Category category_, u32 specialization_ = Specialization::Default, + Category category_, u32 specialization_ = Specialization::Default, bool save_ = true, bool runtime_modifiable_ = false, BasicSetting* other_setting_ = nullptr) requires(!ranged) @@ -64,7 +64,7 @@ public: * @param other_setting_ A second Setting to associate to this one in metadata */ explicit Setting(Linkage& linkage, const Type& default_val, const Type& min_val, - const Type& max_val, const std::string& name, enum Category category_, + const Type& max_val, const std::string& name, Category category_, u32 specialization_ = Specialization::Default, bool save_ = true, bool runtime_modifiable_ = false, BasicSetting* other_setting_ = nullptr) requires(ranged) -- cgit v1.2.3 From b02e7eea78cf5d5f3631b11a58f28d0888948fc0 Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Tue, 18 Jul 2023 15:44:27 -0400 Subject: settings_setting: Fix typo --- src/common/settings_setting.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/common/settings_setting.h') diff --git a/src/common/settings_setting.h b/src/common/settings_setting.h index 55280fec4..a8beb06e9 100644 --- a/src/common/settings_setting.h +++ b/src/common/settings_setting.h @@ -34,7 +34,7 @@ public: * @param default_val Initial value of the setting, and default value of the setting * @param name Label for the setting * @param category_ Category of the setting AKA INI group - * @param specialization_ Suggestion for how frontend implemetations represent this in a config + * @param specialization_ Suggestion for how frontend implementations represent this in a config * @param save_ Suggests that this should or should not be saved to a frontend config file * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded * @param other_setting_ A second Setting to associate to this one in metadata @@ -58,7 +58,7 @@ public: * @param max_val Sets the maximum allowed value of the setting * @param name Label for the setting * @param category_ Category of the setting AKA INI group - * @param specialization_ Suggestion for how frontend implemetations represent this in a config + * @param specialization_ Suggestion for how frontend implementations represent this in a config * @param save_ Suggests that this should or should not be saved to a frontend config file * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded * @param other_setting_ A second Setting to associate to this one in metadata @@ -251,7 +251,7 @@ public: * @param default_val Initial value of the setting, and default value of the setting * @param name Label for the setting * @param category_ Category of the setting AKA INI group - * @param specialization_ Suggestion for how frontend implemetations represent this in a config + * @param specialization_ Suggestion for how frontend implementations represent this in a config * @param save_ Suggests that this should or should not be saved to a frontend config file * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded * @param other_setting_ A second Setting to associate to this one in metadata @@ -277,7 +277,7 @@ public: * @param max_val Sets the maximum allowed value of the setting * @param name Label for the setting * @param category_ Category of the setting AKA INI group - * @param specialization_ Suggestion for how frontend implemetations represent this in a config + * @param specialization_ Suggestion for how frontend implementations represent this in a config * @param save_ Suggests that this should or should not be saved to a frontend config file * @param runtime_modifiable_ Suggests whether this is modifiable while a guest is loaded * @param other_setting_ A second Setting to associate to this one in metadata -- cgit v1.2.3