1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-10 23:44:29 +00:00
This commit is contained in:
neargye 2020-12-29 18:56:51 +02:00
parent 6654d24a82
commit 806f2530ef
3 changed files with 27 additions and 25 deletions

View file

@ -50,7 +50,7 @@
Banana = 3, Banana = 3,
COUNT = 4 COUNT = 4
}; };
// magic_enum::enum_cast<ShapeKind>("Box") -> std::nullopt // magic_enum::enum_cast<ShapeKind>("Box") -> nullopt
// magic_enum::enum_name(ShapeKind::Box) -> "ConvexBegin" // magic_enum::enum_name(ShapeKind::Box) -> "ConvexBegin"
``` ```
@ -76,7 +76,7 @@
// magic_enum::enum_name(ShapeKind::Box) -> "Box" // magic_enum::enum_name(ShapeKind::Box) -> "Box"
// Non-reflected aliases. // Non-reflected aliases.
// magic_enum::enum_cast<ShapeKind>("ConvexBegin") -> std::nullopt // magic_enum::enum_cast<ShapeKind>("ConvexBegin") -> nullopt
// magic_enum::enum_name(ShapeKind::ConvexBegin) -> "Box" // magic_enum::enum_name(ShapeKind::ConvexBegin) -> "Box"
``` ```
@ -87,9 +87,9 @@
one = 1, one = 1,
ONE = 1 ONE = 1
}; };
// magic_enum::enum_cast<Number>("one") -> std::nullopt // magic_enum::enum_cast<Number>("one") -> nullopt
// magic_enum::enum_name(Number::one) -> "" // magic_enum::enum_name(Number::one) -> ""
// magic_enum::enum_cast<Number>("ONE") -> std::nullopt // magic_enum::enum_cast<Number>("ONE") -> nullopt
// magic_enum::enum_name(Number::ONE) -> "" // magic_enum::enum_name(Number::ONE) -> ""
``` ```

View file

@ -13,7 +13,7 @@
* [`enum_type_name` returns type name of enum.](#enum_type_name) * [`enum_type_name` returns type name of enum.](#enum_type_name)
* [`is_unscoped_enum` checks whether type is an Unscoped enumeration.](#is_unscoped_enum) * [`is_unscoped_enum` checks whether type is an Unscoped enumeration.](#is_unscoped_enum)
* [`is_scoped_enum` checks whether type is an Scoped enumeration.](#is_scoped_enum) * [`is_scoped_enum` checks whether type is an Scoped enumeration.](#is_scoped_enum)
* [`underlying_type` improved UB-free "SFINAE-friendly" std::underlying_type.](#underlying_type) * [`underlying_type` improved UB-free "SFINAE-friendly" underlying_type.](#underlying_type)
* [`ostream_operators` ostream operators for enums.](#ostream_operators) * [`ostream_operators` ostream operators for enums.](#ostream_operators)
* [`bitwise_operators` bitwise operators for enums.](#bitwise_operators) * [`bitwise_operators` bitwise operators for enums.](#bitwise_operators)
@ -41,9 +41,9 @@ constexpr optional<E> enum_cast(string_view value, BinaryPredicate p) noexcept(i
* Obtains enum value from string or integer. * Obtains enum value from string or integer.
* Returns `std::optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value. * Returns `optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value.
* If argument does not enum value, returns empty `std::optional`. * If argument does not enum value, returns empty `optional`.
* Examples * Examples
@ -93,7 +93,7 @@ template <typename E>
constexpr array<E, N> enum_values() noexcept; constexpr array<E, N> enum_values() noexcept;
``` ```
* Returns `std::array<E, N>` with all enum values where `N = number of enum values`, sorted by enum value. * Returns `array<E, N>` with all enum values where `N = number of enum values`, sorted by enum value.
* Examples * Examples
@ -146,7 +146,7 @@ template <auto V>
constexpr string_view enum_name() noexcept; constexpr string_view enum_name() noexcept;
``` ```
* Returns name from enum value as `std::string_view` with null-terminated string. * Returns name from enum value as `string_view` with null-terminated string.
* If enum value does not have name or [out of range](limitations.md), `enum_name(value)` returns empty string. * If enum value does not have name or [out of range](limitations.md), `enum_name(value)` returns empty string.
* If enum value does not have name, `enum_name<value>()` occurs the compilation error `"Enum value does not have a name."`. * If enum value does not have name, `enum_name<value>()` occurs the compilation error `"Enum value does not have a name."`.
@ -177,7 +177,7 @@ template <typename E>
constexpr array<string_view, N> enum_names() noexcept; constexpr array<string_view, N> enum_names() noexcept;
``` ```
* Returns `std::array<std::string_view, N>` with all names where `N = number of enum values`, sorted by enum value. * Returns `array<string_view, N>` with all names where `N = number of enum values`, sorted by enum value.
* Examples * Examples
@ -194,7 +194,7 @@ template <typename E>
constexpr array<pair<E, string_view>, N> enum_entries() noexcept; constexpr array<pair<E, string_view>, N> enum_entries() noexcept;
``` ```
* Returns `std::array<std::pair<E, std::string_view>, N>` with all pairs (value, name) where `N = number of enum values`, sorted by enum value. * Returns `array<pair<E, string_view>, N>` with all pairs (value, name) where `N = number of enum values`, sorted by enum value.
* Examples * Examples
@ -214,7 +214,7 @@ constexpr optional<size_t> enum_index() noexcept;
* Obtains index in enum values from enum value. * Obtains index in enum values from enum value.
* Returns `std::optional<std::size_t>` with index. * Returns `optional<size_t>` with index.
* Examples * Examples
@ -261,7 +261,7 @@ template <typename E>
constexpr string_view enum_type_name() noexcept; constexpr string_view enum_type_name() noexcept;
``` ```
* Returns type name of enum as `std::string_view` null-terminated string. * Returns type name of enum as `string_view` null-terminated string.
* Examples * Examples
@ -331,7 +331,7 @@ template <typename T>
using underlying_type_t = typename underlying_type<T>::type; using underlying_type_t = typename underlying_type<T>::type;
``` ```
* Improved UB-free "SFINAE-friendly" [std::underlying_type](https://en.cppreference.com/w/cpp/types/underlying_type). * Improved UB-free "SFINAE-friendly" [underlying_type](https://en.cppreference.com/w/cpp/types/underlying_type).
* If T is a complete enumeration type, provides a member typedef type that names the underlying type of T.</br> * If T is a complete enumeration type, provides a member typedef type that names the underlying type of T.</br>
Otherwise, if T is not an enumeration type, there is no member type.</br> Otherwise, if T is not an enumeration type, there is no member type.</br>

View file

@ -154,13 +154,20 @@ struct supported
: std::false_type {}; : std::false_type {};
#endif #endif
struct char_equal_to {
constexpr bool operator()(char lhs, char rhs) const noexcept {
return lhs == rhs;
}
};
template <std::size_t N> template <std::size_t N>
struct static_string { class static_string {
public:
constexpr explicit static_string(string_view str) noexcept : static_string{str, std::make_index_sequence<N>{}} { constexpr explicit static_string(string_view str) noexcept : static_string{str, std::make_index_sequence<N>{}} {
assert(str.size() == N); assert(str.size() == N);
} }
constexpr const char* data() const noexcept { return chars.data(); } constexpr const char* data() const noexcept { return chars_.data(); }
constexpr std::size_t size() const noexcept { return N; } constexpr std::size_t size() const noexcept { return N; }
@ -168,13 +175,14 @@ struct static_string {
private: private:
template <std::size_t... I> template <std::size_t... I>
constexpr static_string(string_view str, std::index_sequence<I...>) noexcept : chars{{str[I]..., '\0'}} {} constexpr static_string(string_view str, std::index_sequence<I...>) noexcept : chars_{{str[I]..., '\0'}} {}
const std::array<char, N + 1> chars; const std::array<char, N + 1> chars_;
}; };
template <> template <>
struct static_string<0> { class static_string<0> {
public:
constexpr explicit static_string(string_view) noexcept {} constexpr explicit static_string(string_view) noexcept {}
constexpr const char* data() const noexcept { return nullptr; } constexpr const char* data() const noexcept { return nullptr; }
@ -184,12 +192,6 @@ struct static_string<0> {
constexpr operator string_view() const noexcept { return {}; } constexpr operator string_view() const noexcept { return {}; }
}; };
struct char_equal_to {
constexpr bool operator()(char lhs, char rhs) const noexcept {
return lhs == rhs;
}
};
constexpr string_view pretty_name(string_view name) noexcept { constexpr string_view pretty_name(string_view name) noexcept {
for (std::size_t i = name.size(); i > 0; --i) { for (std::size_t i = name.size(); i > 0; --i) {
if (!((name[i - 1] >= '0' && name[i - 1] <= '9') || if (!((name[i - 1] >= '0' && name[i - 1] <= '9') ||