aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/enum-inl.h
blob: c7e0abdab21ffdc3914763be186eb77e595e9df4 (plain) (blame)
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#pragma once
#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 <util/string/printf.h>
#include <util/string/cast.h>

#include <algorithm>
#include <stdexcept>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////

#define ENUM__CLASS(enumType, underlyingType, seq) \
    enum class enumType : underlyingType \
    { \
        PP_FOR_EACH(ENUM__DOMAIN_ITEM, seq) \
    };

#define ENUM__DOMAIN_ITEM(item) \
    PP_IF( \
        PP_IS_SEQUENCE(item), \
        ENUM__DOMAIN_ITEM_SEQ, \
        ENUM__DOMAIN_ITEM_ATOMIC \
    )(item)()

#define ENUM__DOMAIN_ITEM_ATOMIC(item) \
    item PP_COMMA

#define ENUM__DOMAIN_ITEM_SEQ(seq) \
    PP_ELEMENT(seq, 0) = PP_ELEMENT(seq, 1) PP_COMMA

////////////////////////////////////////////////////////////////////////////////

namespace NDetail {

template <typename TValues>
constexpr bool CheckValuesMonotonic(const TValues& values)
{
    return std::adjacent_find(values.begin(), values.end(), std::greater_equal<>()) == values.end();
}

template <typename TValues>
constexpr bool CheckValuesUnique(const TValues& values)
{
    if (CheckValuesMonotonic(values)) {
        return true;
    }
    for (size_t i = 0; i < std::size(values); ++i) {
        for (size_t j = i + 1; j < std::size(values); ++j) {
            if (values[i] == values[j]) {
                return false;
            }
        }
    }
    return true;
}

template <typename TNames>
constexpr bool CheckDomainNames(const TNames& names)
{
    for (size_t i = 0; i < std::size(names); ++i) {
        if (std::size(names[i]) == 0) {
            return false;
        }
        for (size_t j = 1; j < std::size(names[i]); ++j) {
            // If name does not start with a capital letter, all the others must be in lowercase.
            if (('A' <= names[i][j] && names[i][j] <= 'Z') && ('A' > names[i][0] || names[i][0] > 'Z')) {
                return false;
            }
        }
    }
    return true;
}

} // namespace NDetail

////////////////////////////////////////////////////////////////////////////////

#define ENUM__BEGIN_TRAITS(enumType, underlyingType, isBit, isStringSerializable, seq) \
    struct TEnumTraitsImpl_##enumType \
    { \
        using T = enumType; \
        \
        [[maybe_unused]] static constexpr bool IsBitEnum = isBit; \
        [[maybe_unused]] static constexpr bool IsStringSerializableEnum = isStringSerializable; \
        static constexpr int DomainSize = PP_COUNT(seq); \
        \
        static constexpr int GetDomainSize() \
        { \
            return DomainSize; \
        } \
        \
        static constexpr std::array<TStringBuf, DomainSize> Names{{ \
            PP_FOR_EACH(ENUM__GET_DOMAIN_NAMES_ITEM, seq) \
        }}; \
        static_assert(::NYT::NDetail::CheckDomainNames(Names), \
            "Enumeration " #enumType " contains names in wrong format"); \
        static constexpr std::array<T, DomainSize> Values{{ \
            PP_FOR_EACH(ENUM__GET_DOMAIN_VALUES_ITEM, seq) \
        }}; \
        \
        static TStringBuf GetTypeName() \
        { \
            static constexpr TStringBuf Result = PP_STRINGIZE(enumType); \
            return Result; \
        } \
        \
        static const std::optional<TStringBuf> FindLiteralByValue(T value) \
        { \
            for (int i = 0; i < GetDomainSize(); ++i) { \
                if (Values[i] == value) { \
                    return Names[i]; \
                } \
            } \
            return std::nullopt; \
        } \
        \
        static std::optional<T> FindValueByLiteral(TStringBuf literal) \
        { \
            for (int i = 0; i < GetDomainSize(); ++i) { \
                if (Names[i] == literal) { \
                    return Values[i]; \
                } \
            } \
            return std::nullopt; \
        } \
        \
        static constexpr const std::array<TStringBuf, DomainSize>& GetDomainNames() \
        { \
            return Names; \
        } \
        \
        static constexpr const std::array<T, DomainSize>& GetDomainValues() \
        { \
            return Values; \
        }

#define ENUM__GET_DOMAIN_VALUES_ITEM(item) \
    PP_IF( \
        PP_IS_SEQUENCE(item), \
        ENUM__GET_DOMAIN_VALUES_ITEM_SEQ, \
        ENUM__GET_DOMAIN_VALUES_ITEM_ATOMIC \
    )(item)

#define ENUM__GET_DOMAIN_VALUES_ITEM_SEQ(seq) \
    ENUM__GET_DOMAIN_VALUES_ITEM_ATOMIC(PP_ELEMENT(seq, 0))

#define ENUM__GET_DOMAIN_VALUES_ITEM_ATOMIC(item) \
    T::item,

#define ENUM__GET_DOMAIN_NAMES_ITEM(item) \
    PP_IF( \
        PP_IS_SEQUENCE(item), \
        ENUM__GET_DOMAIN_NAMES_ITEM_SEQ, \
        ENUM__GET_DOMAIN_NAMES_ITEM_ATOMIC \
    )(item)

#define ENUM__GET_DOMAIN_NAMES_ITEM_SEQ(seq) \
    PP_IF( \
       ENUM__ITEM_SEQ_HAS_DOMAIN_NAME(seq), \
       ENUM__GET_DOMAIN_NAMES_ITEM_SEQ_CUSTOM, \
       ENUM__GET_DOMAIN_NAMES_ITEM_SEQ_AUTO \
    )(seq)

#define ENUM__ITEM_SEQ_HAS_DOMAIN_NAME(seq) \
    PP_CONCAT(ENUM__ITEM_SEQ_HAS_DOMAIN_NAME_, PP_COUNT(seq))

#define ENUM__ITEM_SEQ_HAS_DOMAIN_NAME_2 PP_FALSE
#define ENUM__ITEM_SEQ_HAS_DOMAIN_NAME_3 PP_TRUE

#define ENUM__GET_DOMAIN_NAMES_ITEM_SEQ_CUSTOM(seq) \
    TStringBuf(PP_ELEMENT(seq, 2)),

#define ENUM__GET_DOMAIN_NAMES_ITEM_SEQ_AUTO(seq) \
    ENUM__GET_DOMAIN_NAMES_ITEM_ATOMIC(PP_ELEMENT(seq, 0))

#define ENUM__GET_DOMAIN_NAMES_ITEM_ATOMIC(item) \
    TStringBuf(PP_STRINGIZE(item)),

#define ENUM__VALIDATE_UNIQUE(enumType) \
    static_assert(::NYT::NDetail::CheckValuesUnique(Values), \
        "Enumeration " #enumType " contains duplicate values");

#define ENUM__END_TRAITS(enumType) \
    }; \
    \
    [[maybe_unused]] inline TEnumTraitsImpl_##enumType GetEnumTraitsImpl(enumType) \
    { \
        return {}; \
    } \
    \
    using ::ToString; \
    [[maybe_unused]] inline TString ToString(enumType value) \
    { \
        return ::NYT::TEnumTraits<enumType>::ToString(value); \
    }

////////////////////////////////////////////////////////////////////////////////

template <class T>
constexpr int TEnumTraitsWithKnownDomain<T, true>::GetDomainSize()
{
    return TEnumTraitsImpl<T>::GetDomainSize();
}

template <class T>
constexpr auto TEnumTraitsWithKnownDomain<T, true>::GetDomainNames() -> const std::array<TStringBuf, GetDomainSize()>&
{
    return TEnumTraitsImpl<T>::GetDomainNames();
}

template <class T>
constexpr auto TEnumTraitsWithKnownDomain<T, true>::GetDomainValues() -> const std::array<T, GetDomainSize()>&
{
    return TEnumTraitsImpl<T>::GetDomainValues();
}

template <class T>
constexpr T TEnumTraitsWithKnownDomain<T, true>::GetMinValue()
    requires (!TEnumTraitsImpl<T>::IsBitEnum)
{
    const auto& values = GetDomainValues();
    static_assert(!values.empty()); \
    return *std::min_element(std::begin(values), std::end(values));
}

template <class T>
constexpr T TEnumTraitsWithKnownDomain<T, true>::GetMaxValue()
    requires (!TEnumTraitsImpl<T>::IsBitEnum)
{
    const auto& values = GetDomainValues();
    static_assert(!values.empty()); \
    return *std::max_element(std::begin(values), std::end(values));
}

template <class T>
std::vector<T> TEnumTraitsWithKnownDomain<T, true>::Decompose(T value)
    requires (TEnumTraitsImpl<T>::IsBitEnum)
{
    std::vector<T> result;
    for (auto domainValue : GetDomainValues()) {
        if (Any(value & domainValue)) {
            result.push_back(domainValue);
        }
    }
    return result;
}

////////////////////////////////////////////////////////////////////////////////

template <class T>
TStringBuf TEnumTraits<T, true>::GetTypeName()
{
    return TEnumTraitsImpl<T>::GetTypeName();
}

template <class T>
std::optional<T> TEnumTraits<T, true>::FindValueByLiteral(TStringBuf literal)
{
    return TEnumTraitsImpl<T>::FindValueByLiteral(literal);
}

template <class T>
std::optional<TStringBuf> TEnumTraits<T, true>::FindLiteralByValue(T value)
{
    return TEnumTraitsImpl<T>::FindLiteralByValue(value);
}

template <class T>
TString TEnumTraits<T, true>::ToString(T value)
{
    using ::ToString;
    if (auto optionalLiteral = TEnumTraits<T>::FindLiteralByValue(value)) {
        return ToString(*optionalLiteral);
    }
    TString result;
    result = TEnumTraits<T>::GetTypeName();
    result += "(";
    result += ToString(ToUnderlying(value));
    result += ")";
    return result;
}

template <class T>
T TEnumTraits<T, true>::FromString(TStringBuf literal)
{
    auto optionalValue = FindValueByLiteral(literal);
    if (!optionalValue) {
        throw ::NYT::TSimpleException(Sprintf("Error parsing %s value %s",
            GetTypeName().data(),
            TString(literal).Quote().c_str()).c_str());
    }
    return *optionalValue;
}

////////////////////////////////////////////////////////////////////////////////

template <class E, class T, E Min, E Max>
constexpr TEnumIndexedVector<E, T, Min, Max>::TEnumIndexedVector()
    : Items_{}
{ }

template <class E, class T, E Min, E Max>
constexpr TEnumIndexedVector<E, T, Min, Max>::TEnumIndexedVector(std::initializer_list<T> elements)
    : Items_{}
{
    Y_ASSERT(std::distance(elements.begin(), elements.end()) <= N);
    size_t index = 0;
    for (const auto& element : elements) {
        Items_[index++] = element;
    }
}

template <class E, class T, E Min, E Max>
T& TEnumIndexedVector<E, T, Min, Max>::operator[] (E index)
{
    Y_ASSERT(index >= Min && index <= Max);
    return Items_[ToUnderlying(index) - ToUnderlying(Min)];
}

template <class E, class T, E Min, E Max>
const T& TEnumIndexedVector<E, T, Min, Max>::operator[] (E index) const
{
    return const_cast<TEnumIndexedVector&>(*this)[index];
}

template <class E, class T, E Min, E Max>
T* TEnumIndexedVector<E, T, Min, Max>::begin()
{
    return Items_.data();
}

template <class E, class T, E Min, E Max>
const T* TEnumIndexedVector<E, T, Min, Max>::begin() const
{
    return Items_.data();
}

template <class E, class T, E Min, E Max>
T* TEnumIndexedVector<E, T, Min, Max>::end()
{
    return begin() + N;
}

template <class E, class T, E Min, E Max>
const T* TEnumIndexedVector<E, T, Min, Max>::end() const
{
    return begin() + N;
}

template <class E, class T, E Min, E Max>
bool TEnumIndexedVector<E, T, Min, Max>::IsDomainValue(E value)
{
    return value >= Min && value <= Max;
}

////////////////////////////////////////////////////////////////////////////////

#define ENUM__BINARY_BITWISE_OPERATOR(T, assignOp, op) \
    [[maybe_unused]] inline constexpr T operator op (T lhs, T rhs) \
    { \
        return T(ToUnderlying(lhs) op ToUnderlying(rhs)); \
    } \
    \
    [[maybe_unused]] inline T& operator assignOp (T& lhs, T rhs) \
    { \
        lhs = T(ToUnderlying(lhs) op ToUnderlying(rhs)); \
        return lhs; \
    }

#define ENUM__UNARY_BITWISE_OPERATOR(T, op) \
    [[maybe_unused]] inline constexpr T operator op (T value) \
    { \
        return T(op ToUnderlying(value)); \
    }

#define ENUM__BIT_SHIFT_OPERATOR(T, assignOp, op) \
    [[maybe_unused]] inline constexpr T operator op (T lhs, size_t rhs) \
    { \
        return T(ToUnderlying(lhs) op rhs); \
    } \
    \
    [[maybe_unused]] inline T& operator assignOp (T& lhs, size_t rhs) \
    { \
        lhs = T(ToUnderlying(lhs) op rhs); \
        return lhs; \
    }

#define ENUM__BITWISE_OPS(enumType) \
    ENUM__BINARY_BITWISE_OPERATOR(enumType, &=, &)  \
    ENUM__BINARY_BITWISE_OPERATOR(enumType, |=, | ) \
    ENUM__BINARY_BITWISE_OPERATOR(enumType, ^=, ^)  \
    ENUM__UNARY_BITWISE_OPERATOR(enumType, ~)       \
    ENUM__BIT_SHIFT_OPERATOR(enumType, <<=, << )    \
    ENUM__BIT_SHIFT_OPERATOR(enumType, >>=, >> )

////////////////////////////////////////////////////////////////////////////////

template <typename E>
    requires std::is_enum_v<E>
constexpr std::underlying_type_t<E> ToUnderlying(E value) noexcept
{
    return static_cast<std::underlying_type_t<E>>(value);
}

template <typename E>
    requires TEnumTraits<E>::IsBitEnum
constexpr bool Any(E value) noexcept
{
    return ToUnderlying(value) != 0;
}

template <typename E>
    requires TEnumTraits<E>::IsBitEnum
constexpr bool None(E value) noexcept
{
    return ToUnderlying(value) == 0;
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT