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

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

#include <optional>
#include <type_traits>
#include <typeinfo>

namespace NPrivate {

    [[noreturn]] void OnSafeCastToEnumUnexpectedValue(const std::type_info& valueTypeInfo);

} // namespace NPrivate

/**
 * Safely cast an integer value to the enum value.
 * @throw yexception is case of unknown enum underlying type value
 *
 * @tparam TEnum     enum type
 */
template <typename TEnum, typename TInteger, typename = std::enable_if_t<std::is_enum_v<TEnum>>>
TEnum SafeCastToEnum(TInteger integerValue) {
    using TUnderlyingEnumType = std::underlying_type_t<TEnum>;

    std::optional<TUnderlyingEnumType> value;
    try {
        value = SafeIntegerCast<TUnderlyingEnumType>(integerValue);
    } catch (const TBadCastException&) {
        // SafeIntegerCast throws TBadCastException when TInteger cannot be cast
        // to TUnderlyingEnumType but the exception message is about integer
        // value cast being unsafe.
        // SafeCastToEnum must throw TBadCastException with its own exception
        // message even if integer cast fails.
    }

    if (value.has_value()) {
        for (TEnum enumValue : GetEnumAllValues<TEnum>()) {
            if (static_cast<TUnderlyingEnumType>(enumValue) == *value) {
                return enumValue;
            }
        }
    }

    NPrivate::OnSafeCastToEnumUnexpectedValue(typeid(TEnum));
}