mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-10 23:44:29 +00:00
add constexpr containers (#187)
This commit is contained in:
parent
2a7658d084
commit
533c9509ef
10 changed files with 2609 additions and 7 deletions
|
|
@ -38,8 +38,8 @@
|
|||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <limits>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
|
@ -281,6 +281,7 @@ constexpr string_view pretty_name(string_view name) noexcept {
|
|||
return {}; // Invalid name.
|
||||
}
|
||||
|
||||
template<typename Op = std::equal_to<>>
|
||||
class case_insensitive {
|
||||
static constexpr char to_lower(char c) noexcept {
|
||||
return (c >= 'A' && c <= 'Z') ? static_cast<char>(c + ('a' - 'A')) : c;
|
||||
|
|
@ -293,7 +294,7 @@ class case_insensitive {
|
|||
static_assert(always_false_v<L, R>, "magic_enum::case_insensitive not supported Non-ASCII feature.");
|
||||
return false;
|
||||
#else
|
||||
return to_lower(lhs) == to_lower(rhs);
|
||||
return Op{}(to_lower(lhs), to_lower(rhs));
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
|
@ -1107,7 +1108,7 @@ template <typename E>
|
|||
// Obtains index in enum values from enum value.
|
||||
// Returns optional with index.
|
||||
template <typename E>
|
||||
[[nodiscard]] constexpr auto enum_index(E value) noexcept -> detail::enable_if_t<E, optional<std::size_t>> {
|
||||
[[nodiscard]] constexpr auto enum_index([[maybe_unused]] E value) noexcept -> detail::enable_if_t<E, optional<std::size_t>> {
|
||||
using D = std::decay_t<E>;
|
||||
using U = underlying_type_t<D>;
|
||||
|
||||
|
|
@ -1216,7 +1217,7 @@ template <detail::value_type VT, typename E>
|
|||
// Returns name from enum-flags value.
|
||||
// If enum-flags value does not have name or value out of range, returns empty string.
|
||||
template <typename E>
|
||||
[[nodiscard]] auto enum_flags_name(E value) -> detail::enable_if_t<E, string> {
|
||||
[[nodiscard]] auto enum_flags_name(E value, [[maybe_unused]] char sep = '|') -> detail::enable_if_t<E, string> {
|
||||
using D = std::decay_t<E>;
|
||||
static_assert(detail::is_flags_v<D>, "magic_enum::enum_flags_name requires enum-flags type.");
|
||||
|
||||
|
|
@ -1236,7 +1237,7 @@ template <typename E>
|
|||
}
|
||||
|
||||
// Allows you to write magic_enum::enum_cast<foo>("bar", magic_enum::case_insensitive);
|
||||
inline constexpr auto case_insensitive = detail::case_insensitive{};
|
||||
inline constexpr auto case_insensitive = detail::case_insensitive<>{};
|
||||
|
||||
// Obtains enum value from integer value.
|
||||
// Returns optional with enum value.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue