aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/arcadia_enum-inl.h
blob: 1df8b2c46479200c2056cd55a50805ecee721856 (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
#pragma once
#ifndef ARCADIA_ENUM_INL_H_
#error "Direct inclusion of this file is not allowed, include arcadia_enum.h"
// For the sake of sane code completion.
#include "arcadia_enum.h"
#endif

#include <util/system/type_name.h>

namespace NYT::NDetail {

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

template <class T>
struct TArcadiaEnumTraitsImpl
{
    static constexpr bool IsBitEnum = false;
    static constexpr bool IsStringSerializableEnum = false;

    static TStringBuf GetTypeName()
    {
        static const auto Result = TypeName<T>();
        return Result;
    }

    static std::optional<TStringBuf> FindLiteralByValue(T value)
    {
        auto names = GetEnumNames<T>();
        auto it = names.find(value);
        return it == names.end() ? std::nullopt : std::make_optional(TStringBuf(it->second));
    }

    static std::optional<T> FindValueByLiteral(TStringBuf literal)
    {
        static const auto LiteralToValue = [] {
            THashMap<TString, T> result;
            for (const auto& [value, name] : GetEnumNames<T>()) {
                result.emplace(name, value);
            }
            return result;
        }();
        auto it = LiteralToValue.find(literal);
        return it == LiteralToValue.end() ? std::nullopt : std::make_optional(it->second);
    }

    static bool IsKnownValue(T value)
    {
        static const auto Values = [] {
            THashSet<T> result;
            for (const auto& [value, _] : GetEnumNames<T>()) {
                result.insert(value);
            }
            return result;
        }();
        return Values.contains(value);
    }
};

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

} // namespace NYT::NDetail