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

#include "enum.h"

#include <util/string/cast.h>
#include <util/string/printf.h>

#include <type_traits>

namespace NYT {

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

namespace NDetail {

template <class T, class S>
bool IsInIntegralRange(S value)
    requires std::is_signed_v<T> && std::is_signed_v<S>
{
    return value >= std::numeric_limits<T>::min() && value <= std::numeric_limits<T>::max();
}

template <class T, class S>
bool IsInIntegralRange(S value)
    requires std::is_signed_v<T> && std::is_unsigned_v<S>
{
    return value <= static_cast<typename std::make_unsigned<T>::type>(std::numeric_limits<T>::max());
}

template <class T, class S>
bool IsInIntegralRange(S value)
    requires std::is_unsigned_v<T> && std::is_signed_v<S>
{
    return value >= 0 && static_cast<typename std::make_unsigned<S>::type>(value) <= std::numeric_limits<T>::max();
}

template <class T, class S>
bool IsInIntegralRange(S value)
    requires std::is_unsigned_v<T> && std::is_unsigned_v<S>
{
    return value <= std::numeric_limits<T>::max();
}

template <class T, class S>
bool IsInIntegralRange(S value)
    requires std::is_enum_v<S>
{
    return IsInIntegralRange<T>(static_cast<std::underlying_type_t<S>>(value));
}

template <class T>
TString FormatInvalidCastValue(T value)
{
    return ::ToString(value);
}

inline TString FormatInvalidCastValue(signed char value)
{
    return TString("'") + value + TString("'");
}

inline TString FormatInvalidCastValue(unsigned char value)
{
    return TString("'") + value + TString("'");
}

#ifdef __cpp_char8_t
inline TString FormatInvalidCastValue(char8_t value)
{
    return FormatInvalidCastValue(static_cast<unsigned char>(value));
}
#endif

} // namespace NDetail

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

template <class T, class S>
bool TryIntegralCast(S value, T* result)
{
    if (!NYT::NDetail::IsInIntegralRange<T>(value)) {
        return false;
    }
    *result = static_cast<T>(value);
    return true;
}

template <class T, class S>
T CheckedIntegralCast(S value)
{
    T result;
    if (!TryIntegralCast<T>(value, &result)) {
        throw TSimpleException(Sprintf("Error casting %s value \"%s\" to %s: value is out of expected range [%s; %s]",
            TypeName<S>().c_str(),
            NYT::NDetail::FormatInvalidCastValue(value).c_str(),
            TypeName<T>().c_str(),
            ::ToString(std::numeric_limits<T>::min()).c_str(),
            ::ToString(std::numeric_limits<T>::max()).c_str()));
    }
    return result;
}

template <class T, class S>
bool TryEnumCast(S value, T* result)
{
    std::underlying_type_t<T> underlying;
    if (!TryIntegralCast<std::underlying_type_t<T>>(value, &underlying)) {
        return false;
    }
    auto candidate = static_cast<T>(underlying);
    if (!TEnumTraits<T>::FindLiteralByValue(candidate)) {
        return false;
    }
    *result = candidate;
    return true;
}

template <class T, class S>
T CheckedEnumCast(S value)
{
    T result;
    if (!TryEnumCast<T>(value, &result)) {
        throw TSimpleException(Sprintf("Error casting %s value \"%d\" to enum %s",
            TypeName<S>().c_str(),
            static_cast<int>(value),
            TEnumTraits<T>::GetTypeName().data()));
    }
    return result;
}

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

} // namespace NYT