aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/unified_agent_client/enum.h
blob: 1eef79081986670a7e7ee79a2075e344f6a04bbe (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
#pragma once

#include <util/generic/serialized_enum.h>
#include <util/generic/vector.h>

namespace NUnifiedAgent {
    namespace NPrivate {
        using TEnumNames = TVector<const TString*>;

        template <typename TEnum>
        TEnumNames* BuildEnumNames() {
            const auto names = GetEnumNames<TEnum>();
            TEnumNames* result = new TEnumNames(names.size());
            size_t index = 0;
            for (const auto& p: names) {
                Y_ABORT_UNLESS(static_cast<size_t>(p.first) == index);
                (*result)[index++] = &p.second;
            }
            return result;
        }

        template <typename TEnum>
        const TEnumNames& EnumNamesVec() {
            static const TEnumNames* EnumNames = BuildEnumNames<TEnum>();
            return *EnumNames;
        }
    }

    template <typename TEnum, typename = std::enable_if_t<std::is_enum_v<TEnum>>>
    inline const TString& NameOf(TEnum val) noexcept {
        return *NPrivate::EnumNamesVec<TEnum>()[static_cast<size_t>(val)];
    }
}