1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
#ifndef ENUM_INL_H_
#error "Direct inclusion of this file is not allowed, include enum.h"
// For the sake of sane code completion.
#include "enum.h"
#endif
#include "format.h"
#include <library/cpp/yt/exception/exception.h>
#include <util/string/printf.h>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
namespace NDetail {
[[noreturn]]
void ThrowMalformedEnumValueException(
TStringBuf typeName,
TStringBuf value);
void FormatUnknownEnumValue(
TStringBuilderBase* builder,
TStringBuf name,
i64 value);
} // namespace NDetail
template <class T>
std::optional<T> TryParseEnum(TStringBuf value)
{
auto tryFromString = [] (TStringBuf value) -> std::optional<T> {
if (auto decodedValue = TryDecodeEnumValue(value)) {
return TEnumTraits<T>::FindValueByLiteral(*decodedValue);
}
auto reportError = [value] () {
throw TSimpleException(Format("Enum value %Qv is neither in a proper underscore case nor in a format \"%v(123)\"",
value,
TEnumTraits<T>::GetTypeName()));
};
TStringBuf typeName;
auto isTypeNameCorrect = value.NextTok('(', typeName) && typeName == TEnumTraits<T>::GetTypeName();
if (!isTypeNameCorrect) {
reportError();
}
TStringBuf enumValue;
std::underlying_type_t<T> underlyingValue = 0;
auto isEnumValueCorrect = value.NextTok(')', enumValue) && TryFromString(enumValue, underlyingValue);
if (!isEnumValueCorrect) {
reportError();
}
auto isParsingComplete = value.empty();
if (!isParsingComplete) {
reportError();
}
return static_cast<T>(underlyingValue);
};
if constexpr (TEnumTraits<T>::IsBitEnum) {
T result{};
TStringBuf token;
while (value.NextTok('|', token)) {
if (auto scalar = tryFromString(StripString(token))) {
result |= *scalar;
} else {
return {};
}
}
return result;
} else {
return tryFromString(value);
}
}
template <class T>
T ParseEnum(TStringBuf value)
{
if (auto optionalResult = TryParseEnum<T>(value)) {
return *optionalResult;
}
NYT::NDetail::ThrowMalformedEnumValueException(TEnumTraits<T>::GetTypeName(), value);
}
template <class T>
void FormatEnum(TStringBuilderBase* builder, T value, bool lowerCase)
{
auto formatScalarValue = [builder, lowerCase] (T value) {
auto optionalLiteral = TEnumTraits<T>::FindLiteralByValue(value);
if (!optionalLiteral) {
NYT::NDetail::FormatUnknownEnumValue(
builder,
TEnumTraits<T>::GetTypeName(),
ToUnderlying(value));
return;
}
if (lowerCase) {
CamelCaseToUnderscoreCase(builder, *optionalLiteral);
} else {
builder->AppendString(*optionalLiteral);
}
};
if constexpr (TEnumTraits<T>::IsBitEnum) {
if (TEnumTraits<T>::FindLiteralByValue(value)) {
formatScalarValue(value);
return;
}
auto first = true;
for (auto scalarValue : TEnumTraits<T>::GetDomainValues()) {
if (Any(value & scalarValue)) {
if (!first) {
builder->AppendString(TStringBuf(" | "));
}
first = false;
formatScalarValue(scalarValue);
}
}
} else {
formatScalarValue(value);
}
}
template <class T>
TString FormatEnum(T value)
{
TStringBuilder builder;
FormatEnum(&builder, value, /*lowerCase*/ true);
return builder.Flush();
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|