aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/enum.h
blob: 894364aa43b779df776ce8e29791e269bfda56e0 (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
#pragma once

#include "preprocessor.h"

#include <util/generic/strbuf.h>

#include <stdexcept>
#include <type_traits>
#include <array>
#include <vector>

#include <library/cpp/yt/exception/exception.h>

namespace NYT {

////////////////////////////////////////////////////////////////////////////////
/*
 * Smart enumerations augment C++ enum classes with a bunch of reflection
 * capabilities accessible via TEnumTraits class specialization.
 *
 * Please refer to the unit test for an actual example of usage
 * (unittests/enum_ut.cpp).
 */

// Actual overload must be provided with defines DEFINE_ENUM_XXX (see below).
template <class T>
void GetEnumTraitsImpl(T);

template <
    class T,
    bool = std::is_enum<T>::value &&
        !std::is_convertible<T, int>::value &&
        !std::is_same<void, decltype(GetEnumTraitsImpl(T()))>::value
>
struct TEnumTraits
{
    static constexpr bool IsEnum = false;
    static constexpr bool IsBitEnum = false;
    static constexpr bool IsStringSerializableEnum = false;
};

template <class T>
struct TEnumTraits<T, true>
{
    using TImpl = decltype(GetEnumTraitsImpl(T()));
    using TType = T;
    using TUnderlying = typename TImpl::TUnderlying;

    static constexpr bool IsEnum = true;
    static constexpr bool IsBitEnum = TImpl::IsBitEnum;
    static constexpr bool IsStringSerializableEnum = TImpl::IsStringSerializableEnum;

    static constexpr int DomainSize = TImpl::DomainSize;

    static TStringBuf GetTypeName();

    static const TStringBuf* FindLiteralByValue(TType value);
    static bool FindValueByLiteral(TStringBuf literal, TType* result);

    static const std::array<TStringBuf, DomainSize>& GetDomainNames();
    static const std::array<TType, DomainSize>& GetDomainValues();

    static TType FromString(TStringBuf str);
    static TString ToString(TType value);

    // For non-bit enums only.
    static constexpr TType GetMinValue();
    static constexpr TType GetMaxValue();

    // For bit enums only.
    static std::vector<TType> Decompose(TType value);

    // LLVM SmallDenseMap interop.
    // This should only be used for enums whose underlying type has big enough range
    // (see getEmptyKey and getTombstoneKey functions).
    struct TDenseMapInfo
    {
        static inline TType getEmptyKey()
        {
            return static_cast<TType>(-1);
        }

        static inline TType getTombstoneKey()
        {
            return static_cast<TType>(-2);
        }

        static unsigned getHashValue(const TType& key)
        {
            return static_cast<unsigned>(key) * 37U;
        }

        static bool isEqual(const TType& lhs, const TType& rhs)
        {
            return lhs == rhs;
        }
    };
};

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

//! Defines a smart enumeration with a specific underlying type.
/*!
 * \param name Enumeration name.
 * \param seq Enumeration domain encoded as a <em>sequence</em>.
 * \param underlyingType Underlying type.
 */
#define DEFINE_ENUM_WITH_UNDERLYING_TYPE(name, underlyingType, seq) \
    ENUM__CLASS(name, underlyingType, seq) \
    ENUM__BEGIN_TRAITS(name, underlyingType, false, false, seq) \
    ENUM__MINMAX \
    ENUM__VALIDATE_UNIQUE(name) \
    ENUM__END_TRAITS(name)

//! Defines a smart enumeration with a specific underlying type.
//! Duplicate enumeration values are allowed.
#define DEFINE_AMBIGUOUS_ENUM_WITH_UNDERLYING_TYPE(name, underlyingType, seq) \
    ENUM__CLASS(name, underlyingType, seq) \
    ENUM__BEGIN_TRAITS(name, underlyingType, false, false, seq) \
    ENUM__MINMAX \
    ENUM__END_TRAITS(name)

//! Defines a smart enumeration with the default |int| underlying type.
#define DEFINE_ENUM(name, seq) \
    DEFINE_ENUM_WITH_UNDERLYING_TYPE(name, int, seq)

//! Defines a smart enumeration with a specific underlying type.
/*!
 * \param name Enumeration name.
 * \param seq Enumeration domain encoded as a <em>sequence</em>.
 * \param underlyingType Underlying type.
 */
#define DEFINE_BIT_ENUM_WITH_UNDERLYING_TYPE(name, underlyingType, seq) \
    ENUM__CLASS(name, underlyingType, seq) \
    ENUM__BEGIN_TRAITS(name, underlyingType, true, false, seq) \
    ENUM__DECOMPOSE \
    ENUM__VALIDATE_UNIQUE(name) \
    ENUM__END_TRAITS(name) \
    ENUM__BITWISE_OPS(name)

//! Defines a smart enumeration with a specific underlying type.
//! Duplicate enumeration values are allowed.
/*!
 * \param name Enumeration name.
 * \param seq Enumeration domain encoded as a <em>sequence</em>.
 * \param underlyingType Underlying type.
 */
#define DEFINE_AMBIGUOUS_BIT_ENUM_WITH_UNDERLYING_TYPE(name, underlyingType, seq) \
    ENUM__CLASS(name, underlyingType, seq) \
    ENUM__BEGIN_TRAITS(name, underlyingType, true, false, seq) \
    ENUM__DECOMPOSE \
    ENUM__END_TRAITS(name) \
    ENUM__BITWISE_OPS(name)

//! Defines a smart enumeration with the default |unsigned| underlying type.
/*!
 * \param name Enumeration name.
 * \param seq Enumeration domain encoded as a <em>sequence</em>.
 */
#define DEFINE_BIT_ENUM(name, seq) \
    DEFINE_BIT_ENUM_WITH_UNDERLYING_TYPE(name, unsigned, seq)

//! Defines a smart enumeration with a specific underlying type and IsStringSerializable attribute.
/*!
 * \param name Enumeration name.
 * \param seq Enumeration domain encoded as a <em>sequence</em>.
 * \param underlyingType Underlying type.
 */
#define DEFINE_STRING_SERIALIZABLE_ENUM_WITH_UNDERLYING_TYPE(name, underlyingType, seq) \
    ENUM__CLASS(name, underlyingType, seq) \
    ENUM__BEGIN_TRAITS(name, underlyingType, false, true, seq) \
    ENUM__MINMAX \
    ENUM__VALIDATE_UNIQUE(name) \
    ENUM__END_TRAITS(name) \

//! Defines a smart enumeration with a specific underlying type and IsStringSerializable attribute.
//! Duplicate enumeration values are allowed.
#define DEFINE_AMBIGUOUS_STRING_SERIALIZABLE_ENUM_WITH_UNDERLYING_TYPE(name, underlyingType, seq) \
    ENUM__CLASS(name, underlyingType, seq) \
    ENUM__BEGIN_TRAITS(name, underlyingType, false, true, seq) \
    ENUM__MINMAX \
    ENUM__END_TRAITS(name)

//! Defines a smart enumeration with the default |int| underlying type and IsStringSerializable attribute.
#define DEFINE_STRING_SERIALIZABLE_ENUM(name, seq) \
    DEFINE_STRING_SERIALIZABLE_ENUM_WITH_UNDERLYING_TYPE(name, int, seq)

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

//! A statically sized vector with elements of type |T| indexed by
//! the items of enumeration type |E|.
/*!
 *  Items are value-initialized on construction.
 */
template <
    class E,
    class T,
    E Min = TEnumTraits<E>::GetMinValue(),
    E Max = TEnumTraits<E>::GetMaxValue()
>
class TEnumIndexedVector
{
public:
    using TIndex = E;
    using TValue = T;

    TEnumIndexedVector();
    TEnumIndexedVector(std::initializer_list<T> elements);

    T& operator[] (E index);
    const T& operator[] (E index) const;

    // STL interop.
    T* begin();
    const T* begin() const;
    T* end();
    const T* end() const;

    static bool IsDomainValue(E value);

private:
    using TUnderlying = typename TEnumTraits<E>::TUnderlying;
    static constexpr int N = static_cast<TUnderlying>(Max) - static_cast<TUnderlying>(Min) + 1;
    std::array<T, N> Items_;
};

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

//! Returns |true| iff the enumeration value is not bitwise zero.
template <typename E, typename = std::enable_if_t<NYT::TEnumTraits<E>::IsBitEnum, E>>
bool Any(E value);

//! Returns |true| iff the enumeration value is bitwise zero.
template <typename E, typename = std::enable_if_t<NYT::TEnumTraits<E>::IsBitEnum, E>>
bool None(E value);

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

} // namespace NYT

#define ENUM_INL_H_
#include "enum-inl.h"
#undef ENUM_INL_H_