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: [PATCH] settings: Give indices to enums --- src/common/settings_common.h | 5 +++++ src/common/settings_enums.h | 28 ++++++++++++++++++++++------ 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: */ [[nodiscard]] virtual constexpr bool Ranged() const = 0; + /** + * @returns The index of the enum if the underlying setting type is an enum, else max of u32. + */ + [[nodiscard]] virtual constexpr u32 EnumIndex() const = 0; + /* * Switchable settings */ 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 @@ namespace Settings { template -struct Canonicalization { - static constexpr std::vector> Get(); +struct EnumMetadata { + static constexpr std::vector> Canonicalizations(); + static constexpr u32 Index(); }; #define PAIR_45(N, X, ...) {#X, N::X} __VA_OPT__(, PAIR_46(N, __VA_ARGS__)) @@ -65,10 +66,17 @@ struct Canonicalization { #define ENUM(NAME, ...) \ enum class NAME : u32 { __VA_ARGS__ }; \ template <> \ - constexpr std::vector> Canonicalization::Get() { \ + constexpr std::vector> EnumMetadata::Canonicalizations() { \ return {PAIR(NAME, __VA_ARGS__)}; \ + } \ + template <> \ + constexpr u32 EnumMetadata::Index() { \ + return __COUNTER__; \ } +// AudioEngine must be specified discretely due to having existing but slightly different +// canonicalizations +// TODO (lat9nq): Remove explicit definition of AudioEngine/sink_id enum class AudioEngine : u32 { Auto, Cubeb, @@ -77,7 +85,8 @@ enum class AudioEngine : u32 { }; template <> -constexpr std::vector> Canonicalization::Get() { +constexpr std::vector> +EnumMetadata::Canonicalizations() { return { {"auto", AudioEngine::Auto}, {"cubeb", AudioEngine::Cubeb}, @@ -86,6 +95,13 @@ constexpr std::vector> Canonicalization +constexpr u32 EnumMetadata::Index() { + // This is just a sufficiently large number that is more than the number of other enums declared + // here + return 100; +} + ENUM(AudioMode, Mono, Stereo, Surround); 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); template constexpr std::string CanonicalizeEnum(Type id) { - const auto group = Canonicalization::Get(); + const auto group = EnumMetadata::Canonicalizations(); for (auto& [name, value] : group) { if (value == id) { return name; @@ -141,7 +157,7 @@ constexpr std::string CanonicalizeEnum(Type id) { template constexpr Type ToEnum(const std::string& canonicalization) { - const auto group = Canonicalization::Get(); + const auto group = EnumMetadata::Canonicalizations(); for (auto& [name, value] : group) { if (name == canonicalization) { 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 @@ #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); }