Files
Library/include/Void/Detail/Enum.hpp
T
faumarayandClaude Sonnet 4.6 a09e000f0f Add Hash, Random, Uuid, Encoding, Bits, Math modules + make Testing deps optional
New header-only modules:
- Void::Hash — FNV-1a 32/64, CRC-32, DJB2, MurmurHash3-32, UDL literals
- Void::Random — Xoshiro256** PRNG, uniform_int/real, coin_flip, shuffle, random_string
- Void::Uuid — v4 (random), v5_lite (name-based), parse, RFC 4122 namespaces
- Void::Encoding — Base64 (standard + URL-safe), Hex encode/decode
- Void::Bits — popcount, bit extract/deposit, rotation, align, byteswap, masks
- Void::Math — Vec2/Vec3/Vec4, Mat4 (identity, translation, scale, rotation)

Testing module: move Stacktrace from INTERNAL_DEPS to OPTIONAL_DEPS. Added
Detail/Color.hpp shim that delegates to Stacktrace::Color when present or
provides inline ANSI fallback otherwise. All Stacktrace calls in SignalHandler,
ConsoleReporter, and Runner are gated on VOID_HAS_STACKTRACE.

Fix Repr.hpp reflect_struct to handle template specializations that lack
std::meta::identifier_of (now guarded by has_identifier check).

33/33 tests pass.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-14 21:52:22 +03:00

69 lines
1.9 KiB
C++

#pragma once
#include <optional>
#include <string_view>
#include <type_traits>
#if __has_include(<meta>)
#include <meta>
#define VOID_HAS_ENUM_REFLECTION 1
#endif
namespace Void::Detail
{
#ifdef VOID_HAS_ENUM_REFLECTION
/// Return the identifier string for an enum value, or "?" if not found.
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] std::string_view enum_name(E value) noexcept
{
static constexpr auto enumerators =
std::define_static_array(std::meta::enumerators_of(^^E));
template for (constexpr auto e : enumerators) {
if (value == [:e:]) {
return std::meta::identifier_of(e);
}
}
return "?";
}
/// Cast an enum value to a different enum type by matching identifier names.
/// Returns the first enumerator of Target whose identifier equals that of
/// the source value, or static_cast<Target>(0) if no match is found.
template <typename Target, typename Source>
requires std::is_enum_v<Target> && std::is_enum_v<Source>
[[nodiscard]] Target enum_cast(Source value) noexcept
{
const auto src_name = enum_name(value);
static constexpr auto target_enumerators =
std::define_static_array(std::meta::enumerators_of(^^Target));
template for (constexpr auto e : target_enumerators) {
if (std::meta::identifier_of(e) == src_name) {
return [:e:];
}
}
return static_cast<Target>(0);
}
/// Parse a string into an enum value by matching enumerator identifier names.
/// Returns std::nullopt if no enumerator name matches.
template <typename E>
requires std::is_enum_v<E>
[[nodiscard]] std::optional<E> enum_parse(std::string_view name) noexcept
{
static constexpr auto enumerators =
std::define_static_array(std::meta::enumerators_of(^^E));
template for (constexpr auto e : enumerators) {
if (std::meta::identifier_of(e) == name) {
return [:e:];
}
}
return std::nullopt;
}
#endif // VOID_HAS_ENUM_REFLECTION
} // namespace Void::Detail