134 lines
3.9 KiB
C++
134 lines
3.9 KiB
C++
#pragma once
|
|
|
|
#include "detail/magic_enum.h"
|
|
|
|
namespace tl
|
|
{
|
|
|
|
// Returns type name of enum.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_type_name() noexcept
|
|
{
|
|
return magic_enum::enum_type_name<E>();
|
|
}
|
|
|
|
// Returns number of enum values.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_count() noexcept
|
|
{
|
|
return magic_enum::enum_count<E>();
|
|
}
|
|
|
|
// Returns enum value at specified index.
|
|
// No bounds checking is performed: the behavior is undefined if index >= number of enum values.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_value(std::size_t index) noexcept
|
|
{
|
|
return magic_enum::enum_value<E>(index);
|
|
}
|
|
|
|
// Returns std::array with enum values, sorted by enum value.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_values() noexcept
|
|
{
|
|
return magic_enum::enum_values<E>();
|
|
}
|
|
|
|
// Returns name from static storage enum variable.
|
|
// This version is much lighter on the compile times and is not restricted to the enum_range limitation.
|
|
template <auto V>
|
|
[[nodiscard]] constexpr auto enum_name() noexcept
|
|
{
|
|
return magic_enum::enum_name<V>();
|
|
}
|
|
|
|
// Returns name from enum value.
|
|
// If enum value does not have name or value out of range, returns empty string.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_name(E value) noexcept
|
|
{
|
|
return magic_enum::enum_name(value);
|
|
}
|
|
|
|
// Returns std::array with names, sorted by enum value.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_names() noexcept
|
|
{
|
|
return magic_enum::enum_names<E>();
|
|
}
|
|
|
|
// Returns std::array with pairs (value, name), sorted by enum value.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_entries() noexcept
|
|
{
|
|
return magic_enum::enum_entries<E>();
|
|
}
|
|
|
|
// Obtains enum value from integer value.
|
|
// Returns optional with enum value.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_cast(std::underlying_type_t<E> value) noexcept
|
|
{
|
|
return magic_enum::enum_cast<E>(value);
|
|
}
|
|
|
|
// Obtains enum value from name.
|
|
// Returns optional with enum value.
|
|
template <typename E, typename BinaryPredicate>
|
|
[[nodiscard]] constexpr auto enum_cast(std::string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>)
|
|
{
|
|
return magic_enum::enum_cast<E, BinaryPredicate>(value, p);
|
|
}
|
|
|
|
// Obtains enum value from name.
|
|
// Returns optional with enum value.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_cast(std::string_view value) noexcept
|
|
{
|
|
return magic_enum::enum_cast<E>(value);
|
|
}
|
|
|
|
// Returns integer value from enum value.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_integer(E value) noexcept
|
|
{
|
|
return magic_enum::enum_integer<E>(value);
|
|
}
|
|
|
|
// Obtains index in enum values from enum value.
|
|
// Returns optional with index.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_index(E value) noexcept
|
|
{
|
|
return magic_enum::enum_index<E>(value);
|
|
}
|
|
|
|
// Checks whether enum contains enumerator with such enum value.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_contains(E value) noexcept
|
|
{
|
|
return magic_enum::enum_contains<E>(value);
|
|
}
|
|
|
|
// Checks whether enum contains enumerator with such integer value.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_contains(std::underlying_type_t<E> value) noexcept
|
|
{
|
|
return magic_enum::enum_contains<E>(value);
|
|
}
|
|
|
|
// Checks whether enum contains enumerator with such name.
|
|
template <typename E, typename BinaryPredicate>
|
|
[[nodiscard]] constexpr auto enum_contains(std::string_view value, BinaryPredicate p) noexcept(std::is_nothrow_invocable_r_v<bool, BinaryPredicate, char, char>)
|
|
{
|
|
return magic_enum::enum_names<E, BinaryPredicate>(value, p);
|
|
}
|
|
|
|
// Checks whether enum contains enumerator with such name.
|
|
template <typename E>
|
|
[[nodiscard]] constexpr auto enum_contains(std::string_view value) noexcept
|
|
{
|
|
return magic_enum::enum_contains<E>(value);
|
|
}
|
|
|
|
} |