MCPcopy Create free account
hub / github.com/Neargye/magic_enum / enum_flags_name

Function enum_flags_name

include/magic_enum/magic_enum_flags.hpp:67–93  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

65// If enum-flags value does not have name or value out of range, returns empty string.
66template <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.

Callers 4

mainFunction · 0.85
test_flags.cppFile · 0.85
test_nonascii.cppFile · 0.85
test_aliases.cppFile · 0.85

Calls 4

appendMethod · 0.80
emptyMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected