AuroraEnum/Include/AuroraEnum.hpp

87 lines
3.4 KiB
C++
Raw Normal View History

2021-10-28 20:35:48 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: AuroraEnum.hpp
Date: 2021-10-28
Author: Reece
***/
#pragma once
2021-10-28 21:18:32 +00:00
///////////////////////////////////////////////////////////////////////////////////////////
/// Configuration
///////////////////////////////////////////////////////////////////////////////////////////
#if defined(AUE_OUT_OF_ECOSYSTEM)
#include <unordered_map>
#include <string>
#include <functional>
#if !defined(AU_AuHashMap)
#define AU_AuHashMap
template<typename T, typename Z>
using AuHashMap = std::unordered_map<T, Z>;
#endif
#if !defined(AU_AuString)
#define AU_AuString
using AuString = std::string;
#endif
2021-10-28 21:17:52 +00:00
#if !defined(AU_UInt)
#define AU_UInt
using AuUInt = size_t;
#endif
2021-10-28 21:49:27 +00:00
#if !defined(AU_Consumer)
#define AU_Consumer
template<typename ...T>
using AuConsumer = std::function<void(T...)>;
#endif
#endif
2021-10-28 20:35:48 +00:00
///////////////////////////////////////////////////////////////////////////////////////////
/// 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,
2021-10-28 20:35:48 +00:00
/// @hideinitializer
#define AUE_EMIT_D(name, val) name::val,
2021-10-28 20:35:48 +00:00
/// @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<AuUInt>(name::kEnumCount)] = {AU_FOR_EACH(AUE_EMIT_A, AUE_STRIP_BRACKETS(vals)) }; \
static inline const name k ## name ## Values[static_cast<AuUInt>(name::kEnumCount)] = {AU_FOR_EACH_THAT(AUE_EMIT_D, name, AUE_STRIP_BRACKETS(vals)) }; \
2021-10-28 21:43:28 +00:00
inline const AuHashMap<AuString, name> k ## name ## FromString = { AU_FOR_EACH_THAT(AUE_EMIT_PAIRNAME, name, AUE_STRIP_BRACKETS(vals)) {"kEnumCount", name::kEnumCount} }; \
2021-10-28 20:35:48 +00:00
static inline name name ## FromString (const AuString &codename) { AU_FOR_EACH_THAT(AUE_EMIT_CHECK, name, AUE_STRIP_BRACKETS(vals)) return name::kEnumCount; } \
2021-10-28 21:43:28 +00:00
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; } \
2021-10-28 21:54:58 +00:00
static inline const AuString & name ## ToString(name val) { static const AuString invalid = "kEnumCount"; if (static_cast<AuUInt>(val) >= static_cast<AuUInt>(name::kEnumCount)) return invalid; return k ## name ## ToString[static_cast<AuUInt>(val)]; } \
2021-10-28 20:35:48 +00:00
static inline void name ## ForEach(const AuConsumer<name> &consumer) { AU_FOR_EACH_THAT(AUE_EMIT_CALL, name, AUE_STRIP_BRACKETS(vals)) }