| 65 | // If enum-flags value does not have name or value out of range, returns empty string. |
| 66 | template <typename E> |
| 67 | [[nodiscard]] auto enum_flags_name(E value, char_type sep = static_cast<char_type>('|')) -> detail::enable_if_t<E, string> { |
| 68 | using D = std::decay_t<E>; |
| 69 | using U = underlying_type_t<D>; |
| 70 | constexpr auto S = detail::enum_subtype::flags; |
| 71 | static_assert(detail::is_reflected_v<D, S>, "magic_enum requires enum implementation and valid max and min."); |
| 72 | |
| 73 | string name; |
| 74 | auto check_value = U{0}; |
| 75 | for (std::size_t i = 0; i < detail::count_v<D, S>; ++i) { |
| 76 | if (const auto v = static_cast<U>(enum_value<D, S>(i)); (static_cast<U>(value) & v) != 0) { |
| 77 | if (const auto n = detail::names_v<D, S>[i]; !n.empty()) { |
| 78 | check_value |= v; |
| 79 | if (!name.empty()) { |
| 80 | name.append(1, sep); |
| 81 | } |
| 82 | name.append(n.data(), n.size()); |
| 83 | } else { |
| 84 | return {}; // Value out of range. |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (check_value != 0 && check_value == static_cast<U>(value)) { |
| 90 | return name; |
| 91 | } |
| 92 | return {}; // Invalid value or out of range. |
| 93 | } |
| 94 | |
| 95 | // Returns enum-flags value from integer value. |
| 96 | // Returns optional with enum-flags value. |
no test coverage detected