di 0.1.0
Loading...
Searching...
No Matches
enum_to_string.h
Go to the documentation of this file.
1#pragma once
2
7
8namespace di::reflection {
9namespace detail {
10 struct EnumToStringFunction {
11 template<concepts::ReflectableToEnumerators T>
12 requires(!concepts::BitwiseEnum<T>)
13 constexpr auto operator()(T value) const {
14 auto result = "[<Invalid Enum Value>]"_sv;
16 [&](auto enumerator) {
17 if (enumerator.value == value) {
18 // NOTE: the strings in this library are compile-time values (with fixed length), so we need to
19 // convert them to a normal string view.
21 }
22 },
23 reflection::reflect(value));
24 return result;
25 }
26
27 template<concepts::ReflectableToEnumerators T>
28 requires(concepts::BitwiseEnum<T>)
29 constexpr auto operator()(T value) const {
30 auto result = ""_s;
31 auto matched = false;
32 // First check for exact matches.
34 [&](auto enumerator) {
35 if (enumerator.value == value) {
37 matched = true;
38 value = T(0);
39 }
40 },
41 reflection::reflect(value));
42
43 // Now, if we didn't match exactly, try each enum 1 by 1 in reverse. By iterating
44 // in reverse we ensure composite enums are matched first, if the user declared things
45 // correctly.
46 if (value != T(0)) {
48 [&](auto enumerator) {
49 if ((enumerator.value & value) != T(0)) {
50 if (!result.empty()) {
51 result.push_back('|');
52 }
54 matched = true;
55 value &= ~enumerator.value;
56 }
57 },
58 reflection::reflect(value));
59 auto new_result = reverse(result) | di::to<String>();
60 result = di::move(new_result);
61 }
62
63 // If we matched nothing or have extraneous extra values, the enum is invalid.
64 if (!matched || value != T(0)) {
65 result = "[<Invalid Enum Value>]"_s;
66 }
67 return result;
68 }
69 };
70}
71
72constexpr inline auto enum_to_string = detail::EnumToStringFunction {};
73}
74
75namespace di {
77}
constexpr auto reverse
Definition reverse.h:28
constexpr auto fixed_string_to_utf8_string_view
Definition fixed_string_to_utf8_string_view.h:32
constexpr auto value
Definition value.h:34
Definition atom.h:10
constexpr auto enumerator
Definition enumerator.h:40
constexpr auto enum_to_string
Definition enum_to_string.h:72
constexpr auto reflect
Definition reflect.h:47
Definition any_storable.h:9
constexpr void tuple_for_each_reverse(F &&function, Tup &&tuple)
Definition tuple_for_each.h:32
constexpr auto to_owned
Definition to_owned.h:26
constexpr void tuple_for_each(F &&function, Tup &&tuple)
Definition tuple_for_each.h:22
constexpr auto to(Con &&container, Args &&... args)
Definition to.h:25