AuroraEnum/Include/AuroraEnum.hpp

113 lines
6.3 KiB
C++

/***
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)
#if !defined(AUE_NOCPPSTL)
#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
#if !defined(AU_AuConsumer)
#define AU_AuConsumer
template<typename ... T>
using AuConsumer = std::function<void(T...)>;
#endif
#endif
#if !defined(AU_AuUInt)
#define AU_AuUInt
using AuUInt = size_t;
#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_VA(name, ...) AUE_DEFINE(name, (__VA_ARGS__))
#define AUE_DEFINE(name, vals) enum class name { AU_FOR_EACH(_AUE_EMIT_ARRAY, _AUE_STRIP_BRACKETS(vals)) kEnumCount, kEnumInvalid = -1, eEnumInvalid = -1, eEnumCount = 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)) }; \
inline const AuHashMap<AuString, name> 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<AuUInt>(val) >= static_cast<AuUInt>(name::kEnumCount)) return invalid; return k ## name ## ToString[static_cast<AuUInt>(val)]; } \
static inline bool name ## IsValid(name val) { return static_cast<AuUInt>(val) < static_cast<AuUInt>(name::kEnumCount); } \
static inline void name ## ForEach(const AuConsumer<name> &consumer) { AU_FOR_EACH_THAT(_AUE_EMIT_CALL, name, _AUE_STRIP_BRACKETS(vals)) } \
static const AuUInt k ## name ## Count = static_cast<AuUInt>(name::kEnumCount); \
static const name k ## name ## Invalid = name::kEnumCount; \
static const name k ## name ## MaxLegal = static_cast<name>(k ## name ## Count - 1); \
static const name k ## name ## MinLegal = static_cast<name>(0);
#define AUE_ITERATE(name, index) name index = static_cast<name>(0); static_cast<AuUInt>(index) < static_cast<AuUInt>(name::kEnumCount); index = static_cast<name>(static_cast<AuUInt>(index) + 1)
///////////////////////////////////////////////////////////////////////////////////////////
// Limited support for when there is no STL or STL-like library available
///////////////////////////////////////////////////////////////////////////////////////////
#if defined(AUE_NOCPPSTL)
#undef AUE_DEFINE
#define AUE_DEFINE(name, vals) enum class name { AU_FOR_EACH(_AUE_EMIT_ARRAY, _AUE_STRIP_BRACKETS(vals)) kEnumCount, kEnumInvalid = -1, eEnumInvalid = -1, eEnumCount = kEnumCount }; \
static inline const char * 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)) }; \
static inline const char * name ## ToString(name val) { static const char * invalid = "kEnumCount"; if (static_cast<AuUInt>(val) >= static_cast<AuUInt>(name::kEnumCount)) return invalid; return k ## name ## ToString[static_cast<AuUInt>(val)]; } \
static inline bool name ## IsValid(name val) { return static_cast<AuUInt>(val) < static_cast<AuUInt>(name::kEnumCount); } \
static const AuUInt k ## name ## Count = static_cast<AuUInt>(name::kEnumCount); \
static const name k ## name ## Invalid = name::kEnumCount; \
static const name k ## name ## MaxLegal = static_cast<name>(k ## name ## Count - 1); \
static const name k ## name ## MinLegal = static_cast<name>(0);
#endif