1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-09 23:34:23 +00:00

update doc

This commit is contained in:
neargye 2023-08-14 18:41:40 +04:00
parent 4904822db8
commit 45c8b5d92c
3 changed files with 17 additions and 1 deletions

View file

@ -92,6 +92,13 @@ constexpr optional<E> enum_cast(string_view value, BinaryPredicate p) noexcept(i
// color.value() -> Color::GREEN
}
// case insensitive enum_cast
auto color = magic_enum::enum_cast<Color>(value, magic_enum::case_insensitive);
// enum_cast with BinaryPredicate
auto color = magic_enum::enum_cast<Color>(value, [](char lhs, char rhs) { return std::tolower(lhs) == std::tolower(rhs); }
// enum_cast with default
auto color_or_default = magic_enum::enum_cast<Color>(value).value_or(Color::NONE);
```

View file

@ -54,6 +54,12 @@ int main() {
std::cout << "BLUE = " << to_integer(c2.value()) << std::endl; // BLUE = 0
}
// Case insensitive enum_cast.
c2 = magic_enum::enum_cast<Color>("blue", magic_enum::case_insensitive);
if (c2.has_value()) {
std::cout << "BLUE = " << to_integer(c2.value()) << std::endl; // BLUE = 0
}
// Integer value to enum value.
auto c3 = magic_enum::enum_cast<Color>(10);
if (c3.has_value()) {

View file

@ -301,7 +301,7 @@ class case_insensitive {
public:
template <typename L, typename R>
constexpr auto operator()(L lhs,R rhs) const noexcept -> std::enable_if_t<std::is_same_v<std::decay_t<L>, char_type> && std::is_same_v<std::decay_t<R>, char_type>, bool> {
constexpr auto operator()(L lhs, R rhs) const noexcept -> std::enable_if_t<std::is_same_v<std::decay_t<L>, char_type> && std::is_same_v<std::decay_t<R>, char_type>, bool> {
return Op{}(to_lower(lhs), to_lower(rhs));
}
};
@ -434,6 +434,7 @@ constexpr auto n() noexcept {
name.str_ += 37;
}
#elif defined(_MSC_VER)
// CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284).
str_view name;
name.str_ = __FUNCSIG__;
name.str_ += 40;
@ -511,6 +512,7 @@ constexpr auto n() noexcept {
#elif defined(_MSC_VER)
str_view name;
if ((__FUNCSIG__[5] == '_' && __FUNCSIG__[35] != '(') || (__FUNCSIG__[5] == 'c' && __FUNCSIG__[41] != '(')) {
// CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284).
name.str_ = __FUNCSIG__;
name.str_ += 35;
name.size_ = sizeof(__FUNCSIG__) - 52;
@ -548,6 +550,7 @@ constexpr auto n() noexcept {
constexpr auto name_ptr = MAGIC_ENUM_GET_ENUM_NAME_BUILTIN(V);
auto name = name_ptr ? str_view{name_ptr, std::char_traits<char>::length(name_ptr)} : str_view{};
# else
// CLI/C++ workaround (see https://github.com/Neargye/magic_enum/issues/284).
str_view name;
name.str_ = __FUNCSIG__;
name.size_ = sizeof(__FUNCSIG__) - 17;