diff options
| -rw-r--r-- | src/common/settings_common.h | 5 | ||||
| -rw-r--r-- | src/common/settings_enums.h | 28 | ||||
| -rw-r--r-- | src/common/settings_setting.h | 9 |
3 files changed, 36 insertions, 6 deletions
diff --git a/src/common/settings_common.h b/src/common/settings_common.h index 669d32204..b355384a4 100644 --- a/src/common/settings_common.h +++ b/src/common/settings_common.h | |||
| @@ -220,6 +220,11 @@ public: | |||
| 220 | */ | 220 | */ |
| 221 | [[nodiscard]] virtual constexpr bool Ranged() const = 0; | 221 | [[nodiscard]] virtual constexpr bool Ranged() const = 0; |
| 222 | 222 | ||
| 223 | /** | ||
| 224 | * @returns The index of the enum if the underlying setting type is an enum, else max of u32. | ||
| 225 | */ | ||
| 226 | [[nodiscard]] virtual constexpr u32 EnumIndex() const = 0; | ||
| 227 | |||
| 223 | /* | 228 | /* |
| 224 | * Switchable settings | 229 | * Switchable settings |
| 225 | */ | 230 | */ |
diff --git a/src/common/settings_enums.h b/src/common/settings_enums.h index a5f87c956..f9bb75840 100644 --- a/src/common/settings_enums.h +++ b/src/common/settings_enums.h | |||
| @@ -11,8 +11,9 @@ | |||
| 11 | namespace Settings { | 11 | namespace Settings { |
| 12 | 12 | ||
| 13 | template <typename T> | 13 | template <typename T> |
| 14 | struct Canonicalization { | 14 | struct EnumMetadata { |
| 15 | static constexpr std::vector<std::pair<std::string, T>> Get(); | 15 | static constexpr std::vector<std::pair<std::string, T>> Canonicalizations(); |
| 16 | static constexpr u32 Index(); | ||
| 16 | }; | 17 | }; |
| 17 | 18 | ||
| 18 | #define PAIR_45(N, X, ...) {#X, N::X} __VA_OPT__(, PAIR_46(N, __VA_ARGS__)) | 19 | #define PAIR_45(N, X, ...) {#X, N::X} __VA_OPT__(, PAIR_46(N, __VA_ARGS__)) |
| @@ -65,10 +66,17 @@ struct Canonicalization { | |||
| 65 | #define ENUM(NAME, ...) \ | 66 | #define ENUM(NAME, ...) \ |
| 66 | enum class NAME : u32 { __VA_ARGS__ }; \ | 67 | enum class NAME : u32 { __VA_ARGS__ }; \ |
| 67 | template <> \ | 68 | template <> \ |
| 68 | constexpr std::vector<std::pair<std::string, NAME>> Canonicalization<NAME>::Get() { \ | 69 | constexpr std::vector<std::pair<std::string, NAME>> EnumMetadata<NAME>::Canonicalizations() { \ |
| 69 | return {PAIR(NAME, __VA_ARGS__)}; \ | 70 | return {PAIR(NAME, __VA_ARGS__)}; \ |
| 71 | } \ | ||
| 72 | template <> \ | ||
| 73 | constexpr u32 EnumMetadata<NAME>::Index() { \ | ||
| 74 | return __COUNTER__; \ | ||
| 70 | } | 75 | } |
| 71 | 76 | ||
| 77 | // AudioEngine must be specified discretely due to having existing but slightly different | ||
| 78 | // canonicalizations | ||
| 79 | // TODO (lat9nq): Remove explicit definition of AudioEngine/sink_id | ||
| 72 | enum class AudioEngine : u32 { | 80 | enum class AudioEngine : u32 { |
| 73 | Auto, | 81 | Auto, |
| 74 | Cubeb, | 82 | Cubeb, |
| @@ -77,7 +85,8 @@ enum class AudioEngine : u32 { | |||
| 77 | }; | 85 | }; |
| 78 | 86 | ||
| 79 | template <> | 87 | template <> |
| 80 | constexpr std::vector<std::pair<std::string, AudioEngine>> Canonicalization<AudioEngine>::Get() { | 88 | constexpr std::vector<std::pair<std::string, AudioEngine>> |
| 89 | EnumMetadata<AudioEngine>::Canonicalizations() { | ||
| 81 | return { | 90 | return { |
| 82 | {"auto", AudioEngine::Auto}, | 91 | {"auto", AudioEngine::Auto}, |
| 83 | {"cubeb", AudioEngine::Cubeb}, | 92 | {"cubeb", AudioEngine::Cubeb}, |
| @@ -86,6 +95,13 @@ constexpr std::vector<std::pair<std::string, AudioEngine>> Canonicalization<Audi | |||
| 86 | }; | 95 | }; |
| 87 | } | 96 | } |
| 88 | 97 | ||
| 98 | template <> | ||
| 99 | constexpr u32 EnumMetadata<AudioEngine>::Index() { | ||
| 100 | // This is just a sufficiently large number that is more than the number of other enums declared | ||
| 101 | // here | ||
| 102 | return 100; | ||
| 103 | } | ||
| 104 | |||
| 89 | ENUM(AudioMode, Mono, Stereo, Surround); | 105 | ENUM(AudioMode, Mono, Stereo, Surround); |
| 90 | 106 | ||
| 91 | ENUM(Language, Japanese, EnglishAmerican, French, German, Italian, Spanish, Chinese, Korean, Dutch, | 107 | ENUM(Language, Japanese, EnglishAmerican, French, German, Italian, Spanish, Chinese, Korean, Dutch, |
| @@ -130,7 +146,7 @@ ENUM(AspectRatio, R16_9, R4_3, R21_9, R16_10, Stretch); | |||
| 130 | 146 | ||
| 131 | template <typename Type> | 147 | template <typename Type> |
| 132 | constexpr std::string CanonicalizeEnum(Type id) { | 148 | constexpr std::string CanonicalizeEnum(Type id) { |
| 133 | const auto group = Canonicalization<Type>::Get(); | 149 | const auto group = EnumMetadata<Type>::Canonicalizations(); |
| 134 | for (auto& [name, value] : group) { | 150 | for (auto& [name, value] : group) { |
| 135 | if (value == id) { | 151 | if (value == id) { |
| 136 | return name; | 152 | return name; |
| @@ -141,7 +157,7 @@ constexpr std::string CanonicalizeEnum(Type id) { | |||
| 141 | 157 | ||
| 142 | template <typename Type> | 158 | template <typename Type> |
| 143 | constexpr Type ToEnum(const std::string& canonicalization) { | 159 | constexpr Type ToEnum(const std::string& canonicalization) { |
| 144 | const auto group = Canonicalization<Type>::Get(); | 160 | const auto group = EnumMetadata<Type>::Canonicalizations(); |
| 145 | for (auto& [name, value] : group) { | 161 | for (auto& [name, value] : group) { |
| 146 | if (name == canonicalization) { | 162 | if (name == canonicalization) { |
| 147 | return value; | 163 | return value; |
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 @@ | |||
| 3 | 3 | ||
| 4 | #pragma once | 4 | #pragma once |
| 5 | 5 | ||
| 6 | #include <limits> | ||
| 6 | #include <map> | 7 | #include <map> |
| 7 | #include <optional> | 8 | #include <optional> |
| 8 | #include <stdexcept> | 9 | #include <stdexcept> |
| @@ -197,6 +198,14 @@ public: | |||
| 197 | return std::type_index(typeid(Type)); | 198 | return std::type_index(typeid(Type)); |
| 198 | } | 199 | } |
| 199 | 200 | ||
| 201 | constexpr u32 EnumIndex() const override { | ||
| 202 | if constexpr (std::is_enum<Type>()) { | ||
| 203 | return EnumMetadata<Type>::Index(); | ||
| 204 | } else { | ||
| 205 | return std::numeric_limits<u32>::max(); | ||
| 206 | } | ||
| 207 | } | ||
| 208 | |||
| 200 | virtual std::string MinVal() const override { | 209 | virtual std::string MinVal() const override { |
| 201 | return this->ToString(minimum); | 210 | return this->ToString(minimum); |
| 202 | } | 211 | } |