4#ifndef _Stroika_Foundation_Common_Enumeration_h_
5#define _Stroika_Foundation_Common_Enumeration_h_ 1
7#include "Stroika/Foundation/StroikaPreComp.h"
15#include "Stroika/Foundation/Common/Common.h"
36#ifndef qCANNOT_FIGURE_OUT_HOW_TO_INIT_STD_ARRAY_FROM_STD_INITIALIZER_
37#define qCANNOT_FIGURE_OUT_HOW_TO_INIT_STD_ARRAY_FROM_STD_INITIALIZER_ 1
46 template <
typename ENUM>
47 constexpr ENUM
Inc (ENUM e);
61 template <
typename ENUM>
62 constexpr underlying_type_t<ENUM> ToInt (ENUM e);
77 template <
typename ENUM>
88 template <
typename ENUM>
89 constexpr ENUM
ToEnum (underlying_type_t<ENUM> e);
99 template <
typename ENUM>
100 constexpr make_unsigned_t<underlying_type_t<ENUM>>
OffsetFromStart (ENUM e);
109#define Stroika_Define_Enum_Bounds(FIRST_ITEM, LAST_ITEM) \
110 eSTART = FIRST_ITEM, eEND = LAST_ITEM + 1, eLAST = LAST_ITEM, eCOUNT = eEND - eSTART,
115 template <
typename T>
125 template <
typename ENUM_TYPE>
126 using EnumName = pair<ENUM_TYPE, const wchar_t*>;
145 template <
typename ENUM_TYPE>
148 DISABLE_COMPILER_MSC_WARNING_START (4996);
149 DISABLE_COMPILER_GCC_WARNING_START (
"GCC diagnostic ignored \"-Wdeprecated-declarations\"");
150 DISABLE_COMPILER_CLANG_WARNING_START (
"clang diagnostic ignored \"-Wdeprecated-declarations\"");
151 static_assert (is_enum_v<
decltype (ENUM_TYPE::eCOUNT)>,
152 "Missing eCOUNT - typically Use Stroika_Define_Enum_Bounds inside the enum");
153 static_assert (is_enum_v<
decltype (ENUM_TYPE::eSTART)>,
154 "Missing eSTART - typically Use Stroika_Define_Enum_Bounds inside the enum");
155 static_assert (is_enum_v<
decltype (ENUM_TYPE::eEND)>,
"Missing eEND - typically Use Stroika_Define_Enum_Bounds inside the enum");
158 using EnumNamesHolderType_ = array<EnumName<ENUM_TYPE>,
static_cast<size_t> (ENUM_TYPE::eCOUNT)>;
161 using BasicArrayInitializer = array<EnumName<ENUM_TYPE>,
static_cast<size_t> (ENUM_TYPE::eCOUNT)>;
163 DISABLE_COMPILER_MSC_WARNING_END (4996);
164 DISABLE_COMPILER_GCC_WARNING_END (
"GCC diagnostic ignored \"-Wdeprecated-declarations\"");
165 DISABLE_COMPILER_CLANG_WARNING_END (
"clang diagnostic ignored \"-Wdeprecated-declarations\"");
168 using const_iterator =
typename EnumNamesHolderType_::const_iterator;
171 using value_type =
typename EnumNamesHolderType_::value_type;
176#if qCANNOT_FIGURE_OUT_HOW_TO_INIT_STD_ARRAY_FROM_STD_INITIALIZER_
177 EnumNames () =
default;
179 EnumNames () =
delete;
182 constexpr EnumNames (EnumNames&& src) =
default;
183 constexpr EnumNames (
const EnumNames& src) =
default;
184 constexpr EnumNames (
const BasicArrayInitializer& init);
185 EnumNames (
const initializer_list<EnumName<ENUM_TYPE>>& origEnumNames);
187 constexpr EnumNames (
const EnumName<ENUM_TYPE> origEnumNames[N]);
192 nonvirtual EnumNames& operator= (EnumNames&& rhs) =
default;
193 nonvirtual EnumNames& operator= (
const EnumNames& rhs) =
default;
198 explicit operator initializer_list<EnumName<ENUM_TYPE>> ()
const;
203 nonvirtual const_iterator begin ()
const;
208 nonvirtual const_iterator end ()
const;
213 nonvirtual
constexpr size_t size ()
const;
222 nonvirtual
constexpr const wchar_t* PeekName (ENUM_TYPE e)
const;
233 nonvirtual
const wchar_t* GetName (ENUM_TYPE e)
const;
239 nonvirtual optional<ENUM_TYPE> PeekValue (
const wchar_t* name)
const;
246 nonvirtual ENUM_TYPE GetValue (
const wchar_t* name)
const;
247 template <
typename NOT_FOUND_EXCEPTION>
248 nonvirtual ENUM_TYPE GetValue (
const wchar_t* name,
const NOT_FOUND_EXCEPTION& notFoundException)
const;
251 nonvirtual
constexpr void RequireItemsOrderedByEnumValue_ ()
const;
257 EnumNamesHolderType_ fEnumNames_;
287 template <
typename ENUM_TYPE>
289 static const EnumNames<ENUM_TYPE> k;
300#include "Enumeration.inl"
constexpr make_unsigned_t< underlying_type_t< ENUM > > OffsetFromStart(ENUM e)
offset of given enum from ENUM::eSTART
constexpr make_unsigned_t< underlying_type_t< ENUM > > GetDistanceSpanned(ENUM e)
return the distance spanned by an enum, e.g. for use in an array
constexpr ENUM ToEnum(underlying_type_t< ENUM > e)
Cast the given int to the given ENUM type - (like static_cast<int>()) - but check range.
constexpr ENUM Inc(ENUM e)
Increment the given enumeration safely, without a bunch of casts.