From 4286e7aa02e777268b25d8a5bcc1ee87f7b16c4d Mon Sep 17 00:00:00 2001 From: Peter Keresztes Schmidt Date: Thu, 20 Aug 2020 00:46:52 +0200 Subject: Core/ChatCommands: Check whether a passed numeric enum value is valid (#25285) --- src/common/Utilities/SmartEnum.h | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'src/common/Utilities/SmartEnum.h') 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::ToString(value); } template static Enum FromIndex(size_t index) { return Trinity::Impl::EnumUtils::FromIndex(index); } + template + static uint32 ToIndex(Enum value) { return Trinity::Impl::EnumUtils::ToIndex(value);} + + template + static bool IsValid(Enum value) + { + try + { + Trinity::Impl::EnumUtils::ToIndex(value); + return true; + } catch (...) + { + return false; + } + } + + template + static bool IsValid(std::underlying_type_t value) { return IsValid(static_cast(value)); } template class Iterator -- cgit v1.2.3