diff options
author | Peter Keresztes Schmidt <carbenium@outlook.com> | 2020-08-20 00:46:52 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2022-02-03 00:18:48 +0100 |
commit | 3c82863c528fa9ea1bd5eb75fa596a99266431b7 (patch) | |
tree | 7434bb682d3ff216357055500947c5a1fb8237f6 /src/common/Utilities/SmartEnum.h | |
parent | 3ba767c438f2def1d619eda34c6b843c29ebc518 (diff) |
Core/ChatCommands: Check whether a passed numeric enum value is valid (#25285)
(cherry picked from commit 4286e7aa02e777268b25d8a5bcc1ee87f7b16c4d)
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 |