diff options
author | Peter Keresztes Schmidt <carbenium@outlook.com> | 2020-08-20 00:46:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-20 00:46:52 +0200 |
commit | 4286e7aa02e777268b25d8a5bcc1ee87f7b16c4d (patch) | |
tree | 13c9a4c4be3ae749a0301f0e397051a98a10f339 /src/common/Utilities/SmartEnum.h | |
parent | 9c9e8c7d58cd0e939330089cfb555945ebbf73eb (diff) |
Core/ChatCommands: Check whether a passed numeric enum value is valid (#25285)
Diffstat (limited to 'src/common/Utilities/SmartEnum.h')
-rw-r--r-- | src/common/Utilities/SmartEnum.h | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/src/common/Utilities/SmartEnum.h b/src/common/Utilities/SmartEnum.h index 0966469f454..8c9a873dd9c 100644 --- a/src/common/Utilities/SmartEnum.h +++ b/src/common/Utilities/SmartEnum.h @@ -23,7 +23,7 @@ struct EnumText { - EnumText(char const* c, char const* t, char const* d) : Constant(c), Title(t), Description(d) {} + EnumText(char const* c, char const* t, char const* d) : Constant(c), Title(t), Description(d) { } // Enum constant of the value char const* const Constant; // Human-readable title of the value @@ -42,6 +42,7 @@ namespace Trinity static size_t Count(); static EnumText ToString(Enum value); static Enum FromIndex(size_t index); + static size_t ToIndex(Enum index); }; } } @@ -55,6 +56,24 @@ class EnumUtils static EnumText ToString(Enum value) { return Trinity::Impl::EnumUtils<Enum>::ToString(value); } template <typename Enum> static Enum FromIndex(size_t index) { return Trinity::Impl::EnumUtils<Enum>::FromIndex(index); } + template <typename Enum> + static uint32 ToIndex(Enum value) { return Trinity::Impl::EnumUtils<Enum>::ToIndex(value);} + + template<typename Enum> + static bool IsValid(Enum value) + { + try + { + Trinity::Impl::EnumUtils<Enum>::ToIndex(value); + return true; + } catch (...) + { + return false; + } + } + + template<typename Enum> + static bool IsValid(std::underlying_type_t<Enum> value) { return IsValid(static_cast<Enum>(value)); } template <typename Enum> class Iterator |