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
|
/* SPDX-License-Identifier: BSL-1.0 OR BSD-3-Clause */
#ifndef MPT_BASE_UTILITY_HPP
#define MPT_BASE_UTILITY_HPP
#include "mpt/base/detect_compiler.hpp"
#include "mpt/base/namespace.hpp"
#if MPT_CXX_BEFORE(20)
#include "mpt/base/saturate_cast.hpp"
#endif
#include <type_traits>
#include <utility>
#include <cstddef>
namespace mpt {
inline namespace MPT_INLINE_NS {
#if MPT_CXX_AT_LEAST(20) && !MPT_CLANG_BEFORE(13, 0, 0)
using std::in_range;
#else
// Returns true iff Tdst can represent the value val.
// Use as if(mpt::in_range<uint8>(-1)).
template <typename Tdst, typename Tsrc>
constexpr bool in_range(Tsrc val) {
return (static_cast<Tsrc>(mpt::saturate_cast<Tdst>(val)) == val);
}
#endif
#if MPT_CXX_AT_LEAST(23)
using std::to_underlying;
#else
template <typename T>
constexpr std::underlying_type_t<T> to_underlying(T value) noexcept {
return static_cast<typename std::underlying_type<T>::type>(value);
}
#endif
template <typename T>
struct value_initializer {
inline void operator()(T & x) {
x = T{};
}
};
template <typename T, std::size_t N>
struct value_initializer<T[N]> {
inline void operator()(T (&a)[N]) {
for (auto & e : a)
{
value_initializer<T>{}(e);
}
}
};
template <typename T>
inline void reset(T & x) {
value_initializer<T>{}(x);
}
#if MPT_CXX_AT_LEAST(20) && !MPT_CLANG_BEFORE(13, 0, 0)
using std::cmp_equal;
using std::cmp_greater;
using std::cmp_greater_equal;
using std::cmp_less;
using std::cmp_less_equal;
using std::cmp_not_equal;
#else
template <typename Ta, typename Tb>
constexpr bool cmp_equal(Ta a, Tb b) noexcept {
using UTa = typename std::make_unsigned<Ta>::type;
using UTb = typename std::make_unsigned<Tb>::type;
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) {
return a == b;
} else if constexpr (std::is_signed<Ta>::value) {
return (a < 0) ? false : static_cast<UTa>(a) == b;
} else {
return (b < 0) ? false : a == static_cast<UTb>(b);
}
}
template <typename Ta, typename Tb>
constexpr bool cmp_not_equal(Ta a, Tb b) noexcept {
using UTa = typename std::make_unsigned<Ta>::type;
using UTb = typename std::make_unsigned<Tb>::type;
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) {
return a != b;
} else if constexpr (std::is_signed<Ta>::value) {
return (a < 0) ? true : static_cast<UTa>(a) != b;
} else {
return (b < 0) ? true : a != static_cast<UTb>(b);
}
}
template <typename Ta, typename Tb>
constexpr bool cmp_less(Ta a, Tb b) noexcept {
using UTa = typename std::make_unsigned<Ta>::type;
using UTb = typename std::make_unsigned<Tb>::type;
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) {
return a < b;
} else if constexpr (std::is_signed<Ta>::value) {
return (a < 0) ? true : static_cast<UTa>(a) < b;
} else {
return (b < 0) ? false : a < static_cast<UTb>(b);
}
}
template <typename Ta, typename Tb>
constexpr bool cmp_greater(Ta a, Tb b) noexcept {
using UTa = typename std::make_unsigned<Ta>::type;
using UTb = typename std::make_unsigned<Tb>::type;
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) {
return a > b;
} else if constexpr (std::is_signed<Ta>::value) {
return (a < 0) ? false : static_cast<UTa>(a) > b;
} else {
return (b < 0) ? true : a > static_cast<UTb>(b);
}
}
template <typename Ta, typename Tb>
constexpr bool cmp_less_equal(Ta a, Tb b) noexcept {
using UTa = typename std::make_unsigned<Ta>::type;
using UTb = typename std::make_unsigned<Tb>::type;
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) {
return a <= b;
} else if constexpr (std::is_signed<Ta>::value) {
return (a < 0) ? true : static_cast<UTa>(a) <= b;
} else {
return (b < 0) ? false : a <= static_cast<UTb>(b);
}
}
template <typename Ta, typename Tb>
constexpr bool cmp_greater_equal(Ta a, Tb b) noexcept {
using UTa = typename std::make_unsigned<Ta>::type;
using UTb = typename std::make_unsigned<Tb>::type;
if constexpr (std::is_signed<Ta>::value == std::is_signed<Tb>::value) {
return a >= b;
} else if constexpr (std::is_signed<Ta>::value) {
return (a < 0) ? false : static_cast<UTa>(a) >= b;
} else {
return (b < 0) ? true : a >= static_cast<UTb>(b);
}
}
#endif
} // namespace MPT_INLINE_NS
} // namespace mpt
#endif // MPT_BASE_UTILITY_HPP
|