/*** Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved. File: AuroraEnum.hpp Date: 2021-10-28 Author: Reece ***/ #pragma once /////////////////////////////////////////////////////////////////////////////////////////// /// Configuration /////////////////////////////////////////////////////////////////////////////////////////// #if defined(AUE_OUT_OF_ECOSYSTEM) #include #include #include #if !defined(AU_AuHashMap) #define AU_AuHashMap template using AuHashMap = std::unordered_map; #endif #if !defined(AU_AuString) #define AU_AuString using AuString = std::string; #endif #if !defined(AU_UInt) #define AU_UInt using AuUInt = size_t; #endif #if !defined(AU_Consumer) #define AU_Consumer template using AuConsumer = std::function; #endif #endif /////////////////////////////////////////////////////////////////////////////////////////// /// Utilities /////////////////////////////////////////////////////////////////////////////////////////// /// @hideinitializer #define AUE_BRACKET_SCOPE(...) __VA_ARGS__ /// @hideinitializer #define AUE_STRIP_BRACKETS_(X) X /// @hideinitializer #define AUE_STRIP_BRACKETS(X) AUE_STRIP_BRACKETS_(AUE_BRACKET_SCOPE X) /// @hideinitializer #define AUE_EMIT_ARRAY(val) val, /// @hideinitializer #define AUE_EMIT_A(val) #val, /// @hideinitializer #define AUE_EMIT_D(name, val) name::val, /// @hideinitializer #define AUE_EMIT_PAIRNAME(name, val) {#val, name::val}, /// @hideinitializer #define AUE_EMIT_CALL(name, val) consumer(name::val); /// @hideinitializer #define AUE_EMIT_CHECK(name, val) if (codename == #val) {return name::val;} /////////////////////////////////////////////////////////////////////////////////////////// // API /////////////////////////////////////////////////////////////////////////////////////////// #define AUE_DEFINE(name, vals) enum class name { AU_FOR_EACH(AUE_EMIT_ARRAY, AUE_STRIP_BRACKETS(vals)) kEnumCount }; \ static inline const AuString k ## name ## ToString[static_cast(name::kEnumCount)] = {AU_FOR_EACH(AUE_EMIT_A, AUE_STRIP_BRACKETS(vals)) }; \ static inline const name k ## name ## Values[static_cast(name::kEnumCount)] = {AU_FOR_EACH_THAT(AUE_EMIT_D, name, AUE_STRIP_BRACKETS(vals)) }; \ inline const AuHashMap k ## name ## FromString = { AU_FOR_EACH_THAT(AUE_EMIT_PAIRNAME, name, AUE_STRIP_BRACKETS(vals)) {"kEnumCount", name::kEnumCount} }; \ static inline name name ## FromString (const AuString &codename) { AU_FOR_EACH_THAT(AUE_EMIT_CHECK, name, AUE_STRIP_BRACKETS(vals)) return name::kEnumCount; } \ static inline name name ## FromHashString(const AuString &codename) { auto itr = k ## name ## FromString.find(codename); if (itr == k ## name ## FromString.end()) return name::kEnumCount; return itr->second; } \ static inline const AuString & name ## ToString(name val) { static const AuString invalid = "kEnumCount"; if (static_cast(val) >= static_cast(name::kEnumCount)) return invalid; return k ## name ## ToString[static_cast(val)]; } \ static inline void name ## ForEach(const AuConsumer &consumer) { AU_FOR_EACH_THAT(AUE_EMIT_CALL, name, AUE_STRIP_BRACKETS(vals)) } \ static const AuUInt k ## name ## Count = static_cast(name::kEnumCount); \ static const name k ## name ## Invalid = name::kEnumCount; \ static const name k ## name ## MaxLegal = static_cast(k ## name ## Count - 1); \ static const name k ## name ## MinLegal = static_cast(0); #define AUE_ITERATE(name, index) name index = static_cast(0); static_cast(index) < static_cast(name::kEnumCount); index = static_cast(static_cast(index) + 1)