aboutsummaryrefslogtreecommitdiffstats
path: root/util/generic/serialized_enum.h
blob: fb847701b0fce916c298ede750491c318dbf5aff (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
#pragma once

#include <util/generic/fwd.h>
#include <util/generic/vector.h>
#include <util/generic/map.h>

#include <cstddef>
#include <type_traits>

/*

A file with declarations of enumeration-related functions.
It doesn't contains definitions. To generate them you have to add

    GENERATE_ENUM_SERIALIZATION_WITH_HEADER(your_header_with_your_enum.h)
or
    GENERATE_ENUM_SERIALIZATION(your_header_with_your_enum.h)

in your ya.make

@see https://st.yandex-team.ru/IGNIETFERRO-333
@see https://wiki.yandex-team.ru/PoiskovajaPlatforma/Build/WritingCmakefiles/#generate-enum-with-header

*/

/**
 * Returns number of distinct items in enum or enum class
 *
 * @tparam EnumT     enum type
 */
template <typename EnumT>
Y_CONST_FUNCTION constexpr size_t GetEnumItemsCount(); 

namespace NEnumSerializationRuntime {
    namespace NDetail {
        template <typename EEnum>
        struct TSelectEnumRepresentationType;

        template <typename TEnumType, typename TRepresentationType, class TStorage = TVector<TRepresentationType>>
        class TMappedArrayView;

        template <typename TEnumType, typename TRepresentationType, typename TValueType, class TStorage = TMap<TRepresentationType, TValueType>>
        class TMappedDictView;
    }

    /// Class with behaviour similar to TMap<EnumT, TValueType>
    template <typename EnumT, typename TValueType>
    using TMappedDictView = NDetail::TMappedDictView<EnumT, typename NDetail::TSelectEnumRepresentationType<EnumT>::TType, TValueType>;

    /// Class with behaviour similar to TVector<EnumT>
    template <typename EnumT>
    using TMappedArrayView = NDetail::TMappedArrayView<EnumT, typename NDetail::TSelectEnumRepresentationType<EnumT>::TType>;

    /**
     * Returns names for items in enum or enum class
     *
     * @tparam EnumT     enum type
     */
    template <typename EnumT>
    TMappedDictView<EnumT, TString> GetEnumNamesImpl();
    /**
     * Returns unique items in enum or enum class
     *
     * @tparam EnumT     enum type
     */
    template <typename EnumT>
    ::NEnumSerializationRuntime::TMappedArrayView<EnumT> GetEnumAllValuesImpl();

    /**
     * Returns human-readable comma-separated list of names in enum or enum class
     *
     * @tparam EnumT     enum type
     */
    template <typename EnumT>
    const TString& GetEnumAllNamesImpl();

    /**
     * Returns C++ identifiers for items in enum or enum class
     *
     * @tparam EnumT     enum type
     */
    template <typename EnumT>
    const TVector<TString>& GetEnumAllCppNamesImpl();
}

/**
 * Returns names for items in enum or enum class
 *
 * @tparam EnumT     enum type
 */
template <typename EnumT>
Y_CONST_FUNCTION ::NEnumSerializationRuntime::TMappedDictView<EnumT, TString> GetEnumNames() { 
    return ::NEnumSerializationRuntime::GetEnumNamesImpl<EnumT>();
}

/**
 * Returns unique items in enum or enum class
 *
 * @tparam EnumT     enum type
 */
template <typename EnumT>
Y_CONST_FUNCTION ::NEnumSerializationRuntime::TMappedArrayView<EnumT> GetEnumAllValues() { 
    return ::NEnumSerializationRuntime::GetEnumAllValuesImpl<EnumT>();
}

/**
 * Returns human-readable comma-separated list of names in enum or enum class
 *
 * @tparam EnumT     enum type
 */
template <typename EnumT>
Y_CONST_FUNCTION const TString& GetEnumAllNames() { 
    return ::NEnumSerializationRuntime::GetEnumAllNamesImpl<EnumT>();
}

/**
 * Returns C++ identifiers for items in enum or enum class
 *
 * @tparam EnumT     enum type
 */
template <typename EnumT>
Y_CONST_FUNCTION const TVector<TString>& GetEnumAllCppNames() { 
    return ::NEnumSerializationRuntime::GetEnumAllCppNamesImpl<EnumT>();
}

namespace NEnumSerializationRuntime {
    namespace NDetail {
        /// Checks that the `From` type can be promoted up to the `To` type without losses
        template <typename From, typename To>
        struct TIsPromotable: public std::is_same<std::common_type_t<From, To>, To> {
            static_assert(std::is_integral<From>::value, "`From` type has to be an integer");
            static_assert(std::is_integral<To>::value, "`To` type has to be an integer");
        };

        /// Selects enum representation type. Works like std::underlying_type_t<>, but promotes small types up to `int`
        template <typename EEnum>
        struct TSelectEnumRepresentationType {
            using TUnderlyingType = std::underlying_type_t<EEnum>;
            using TIsSigned = std::is_signed<TUnderlyingType>;
            using TRepresentationType = std::conditional_t<
                TIsSigned::value,
                std::conditional_t<
                    TIsPromotable<TUnderlyingType, int>::value,
                    int,
                    long long>,
                std::conditional_t<
                    TIsPromotable<TUnderlyingType, unsigned>::value,
                    unsigned,
                    unsigned long long>>;
            using TType = TRepresentationType;
            static_assert(sizeof(TUnderlyingType) <= sizeof(TType), "size of `TType` is not smaller than the size of `TUnderlyingType`");
        };

        template <typename TEnumType, typename TRepresentationType>
        class TMappedViewBase {
            static_assert(sizeof(std::underlying_type_t<TEnumType>) <= sizeof(TRepresentationType), "Internal type is probably too small to represent all possible values");

        public:
            static constexpr TEnumType CastFromRepresentationType(const TRepresentationType key) noexcept {
                return static_cast<TEnumType>(key);
            }

            static constexpr TRepresentationType CastToRepresentationType(const TEnumType key) noexcept {
                return static_cast<TRepresentationType>(key);
            }
        };

        /// Wrapper class with behaviour similar to TVector<EnumT>
        ///
        /// @tparam TEnumType            enum type at the external interface
        /// @tparam TRepresentationType  designated underlying type of enum
        /// @tparam TStorage             internal container type
        template <typename TEnumType, typename TRepresentationType, class TStorage>
        class TMappedArrayView: public TMappedViewBase<TEnumType, TRepresentationType> {
        public:
            using value_type = TEnumType;

        public:
            TMappedArrayView(const TStorage& a) noexcept
                : Ref(a)
            {
            }

            class TIterator {
            public:
                using TSlaveIteratorType = typename TStorage::const_iterator;

                using difference_type = std::ptrdiff_t;
                using value_type = TEnumType;
                using pointer = const TEnumType*;
                using reference = const TEnumType&;
                using iterator_category = std::bidirectional_iterator_tag;

            public:
                TIterator(TSlaveIteratorType it)
                    : Slave(std::move(it))
                {
                }

                bool operator==(const TIterator& it) const {
                    return Slave == it.Slave;
                }

                bool operator!=(const TIterator& it) const {
                    return !(*this == it);
                }

                TEnumType operator*() const {
                    return TMappedArrayView::CastFromRepresentationType(*Slave);
                }

                TIterator& operator++() {
                    ++Slave;
                    return *this;
                }

                TIterator& operator--() {
                    --Slave;
                    return *this;
                }

                TIterator operator++(int) {
                    auto temp = Slave;
                    ++Slave;
                    return temp;
                }

                TIterator operator--(int) {
                    auto temp = Slave;
                    --Slave;
                    return temp;
                }

            private:
                TSlaveIteratorType Slave;
            };

            TIterator begin() const {
                return Ref.begin();
            }

            TIterator end() const {
                return Ref.end();
            }

            size_t size() const {
                return Ref.size();
            }

            Y_PURE_FUNCTION bool empty() const { 
                return Ref.empty();
            }

            TEnumType at(size_t index) const {
                return this->CastFromRepresentationType(Ref.at(index));
            }

            TEnumType operator[](size_t index) const {
                return this->CastFromRepresentationType(Ref[index]);
            }

            // Allocate container and copy view's content into it
            template <template <class...> class TContainer = TVector>
            TContainer<TEnumType> Materialize() const {
                return {begin(), end()};
            }

        private:
            const TStorage& Ref;
        };

        /// Wrapper class with behaviour similar to TMap<EnumT, TValueType>
        ///
        /// @tparam TEnumType            enum type at the external interface
        /// @tparam TRepresentationType  designated underlying type of enum
        /// @tparam TValueType           mapped value
        /// @tparam TStorage             internal container type
        template <typename TEnumType, typename TRepresentationType, typename TValueType, class TStorage>
        class TMappedDictView: public TMappedViewBase<TEnumType, TRepresentationType> {
        public:
            using TMappedItemType = std::pair<const TEnumType, const TValueType&>;

            class TDereferenceResultHolder {
            public:
                TDereferenceResultHolder(const TRepresentationType enumValue, const TValueType& payload) noexcept
                    : Data(TMappedDictView::CastFromRepresentationType(enumValue), payload)
                {
                }

                const TMappedItemType* operator->() const noexcept {
                    return &Data;
                }

            private:
                TMappedItemType Data;
            };

            TMappedDictView(const TStorage& m) noexcept
                : Ref(m)
            {
            }

            class TIterator {
            public:
                using TSlaveIteratorType = typename TStorage::const_iterator;

                using difference_type = std::ptrdiff_t;
                using value_type = TMappedItemType;
                using pointer = const TMappedItemType*;
                using reference = const TMappedItemType&;
                using iterator_category = std::bidirectional_iterator_tag;

            public:
                TIterator(TSlaveIteratorType it)
                    : Slave(std::move(it))
                {
                }

                bool operator==(const TIterator& it) const {
                    return Slave == it.Slave;
                }

                bool operator!=(const TIterator& it) const {
                    return !(*this == it);
                }

                TDereferenceResultHolder operator->() const {
                    return {Slave->first, Slave->second};
                }

                TMappedItemType operator*() const {
                    return {TMappedDictView::CastFromRepresentationType(Slave->first), Slave->second};
                }

                TIterator& operator++() {
                    ++Slave;
                    return *this;
                }

                TIterator& operator--() {
                    --Slave;
                    return *this;
                }

                TIterator operator++(int) {
                    auto temp = Slave;
                    ++Slave;
                    return temp;
                }

                TIterator operator--(int) {
                    auto temp = Slave;
                    --Slave;
                    return temp;
                }

            private:
                TSlaveIteratorType Slave;
            };

            TIterator begin() const {
                return Ref.begin();
            }

            TIterator end() const {
                return Ref.end();
            }

            size_t size() const {
                return Ref.size();
            }

            Y_PURE_FUNCTION bool empty() const { 
                return Ref.empty();
            }

            bool contains(const TEnumType key) const {
                return Ref.contains(this->CastToRepresentationType(key));
            }

            TIterator find(const TEnumType key) const {
                return Ref.find(this->CastToRepresentationType(key));
            }

            const TValueType& at(const TEnumType key) const {
                return Ref.at(this->CastToRepresentationType(key));
            }

            // Allocate container and copy view's content into it
            template <template <class...> class TContainer = TMap>
            TContainer<TEnumType, TValueType> Materialize() const {
                return {begin(), end()};
            }

        private:
            const TStorage& Ref;
        };
    }
}