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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
#ifndef STRONG_TYPEDEF_INL_H_
#error "Direct inclusion of this file is not allowed, include strong_typedef.h"
// For the sake of sane code completion.
#include "strong_typedef.h"
#endif
#include "wrapper_traits.h"
#include <util/generic/strbuf.h>
#include <functional>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
template <class T, class TTag>
constexpr TStrongTypedef<T, TTag>::TStrongTypedef()
requires std::is_default_constructible_v<T>
: Underlying_{}
{ }
template <class T, class TTag>
constexpr TStrongTypedef<T, TTag>::TStrongTypedef(const T& underlying)
requires std::is_copy_constructible_v<T>
: Underlying_(underlying)
{ }
template <class T, class TTag>
constexpr TStrongTypedef<T, TTag>::TStrongTypedef(T&& underlying)
requires std::is_move_constructible_v<T>
: Underlying_(std::move(underlying))
{ }
template <class T, class TTag>
constexpr TStrongTypedef<T, TTag>::operator const T&() const
{
return Underlying_;
}
template <class T, class TTag>
constexpr TStrongTypedef<T, TTag>::operator T&()
{
return Underlying_;
}
template <class T, class TTag>
constexpr T& TStrongTypedef<T, TTag>::Underlying() &
{
return Underlying_;
}
template <class T, class TTag>
constexpr const T& TStrongTypedef<T, TTag>::Underlying() const&
{
return Underlying_;
}
template <class T, class TTag>
constexpr T&& TStrongTypedef<T, TTag>::Underlying() &&
{
return std::move(Underlying_);
}
#define XX(op, defaultValue) \
template <class T, class TTag> \
constexpr auto TStrongTypedef<T, TTag>::operator op(const TStrongTypedef& rhs) const \
noexcept(noexcept(Underlying_ op rhs.Underlying_)) \
requires requires (T lhs, T rhs) {lhs op rhs; } \
{ \
if constexpr (std::same_as<T, void>) { \
return defaultValue; \
} \
return Underlying_ op rhs.Underlying_; \
}
XX(<, false)
XX(>, false)
XX(<=, true)
XX(>=, true)
XX(==, true)
XX(!=, false)
XX(<=>, std::strong_ordering::equal)
#undef XX
template <class T, class TTag>
TStrongTypedef<T, TTag>::operator bool() const
noexcept(noexcept(static_cast<bool>(Underlying_)))
{
return static_cast<bool>(Underlying_);
}
////////////////////////////////////////////////////////////////////////////////
template <class T>
struct TStrongTypedefTraits
{
constexpr static bool IsStrongTypedef = false;
};
template <class T, class TTag>
struct TStrongTypedefTraits<TStrongTypedef<T, TTag>>
{
constexpr static bool IsStrongTypedef = true;
using TUnderlying = T;
};
////////////////////////////////////////////////////////////////////////////////
template <class T, class TChar>
requires CStrongTypedef<T>
bool TryFromStringImpl(const TChar* data, size_t size, T& value)
{
return TryFromString(data, size, value.Underlying());
}
////////////////////////////////////////////////////////////////////////////////
class TStringBuilderBase;
template <class T, class TTag>
void FormatValue(TStringBuilderBase* builder, const TStrongTypedef<T, TTag>& value, TStringBuf format)
noexcept(noexcept(FormatValue(builder, value.Underlying(), format)))
{
FormatValue(builder, value.Underlying(), format);
}
////////////////////////////////////////////////////////////////////////////////
template <class T, class TTag>
struct TBasicWrapperTraits<TStrongTypedef<T, TTag>>
{
static constexpr bool IsTrivialWrapper = false;
using TUnwrapped = T;
static constexpr bool HasValue(const TStrongTypedef<T, TTag>&) noexcept
{
return true;
}
template <class U>
requires std::same_as<std::remove_cvref_t<U>, TStrongTypedef<T, TTag>>
static constexpr decltype(auto) Unwrap(U&& wrapper) noexcept
{
return std::forward<U>(wrapper).Underlying();
}
};
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
namespace std {
////////////////////////////////////////////////////////////////////////////////
template <class T, class TTag>
struct hash<NYT::TStrongTypedef<T, TTag>>
{
size_t operator()(const NYT::TStrongTypedef<T, TTag>& value) const
{
return std::hash<T>()(value.Underlying());
}
};
////////////////////////////////////////////////////////////////////////////////
template <class T, class TTag>
requires std::numeric_limits<T>::is_specialized
class numeric_limits<NYT::TStrongTypedef<T, TTag>>
{
public:
#define XX(name) \
static constexpr decltype(numeric_limits<T>::name) name = numeric_limits<T>::name;
XX(is_specialized)
XX(is_signed)
XX(digits)
XX(digits10)
XX(max_digits10)
XX(is_integer)
XX(is_exact)
XX(radix)
XX(min_exponent)
XX(min_exponent10)
XX(max_exponent)
XX(max_exponent10)
XX(has_infinity)
XX(has_quiet_NaN)
XX(has_signaling_NaN)
XX(has_denorm)
XX(has_denorm_loss)
XX(is_iec559)
XX(is_bounded)
XX(is_modulo)
XX(traps)
XX(tinyness_before)
XX(round_style)
#undef XX
#define XX(name) \
static constexpr NYT::TStrongTypedef<T, TTag> name() noexcept \
{ \
return NYT::TStrongTypedef<T, TTag>(numeric_limits<T>::name()); \
}
XX(min)
XX(max)
XX(lowest)
XX(epsilon)
XX(round_error)
XX(infinity)
XX(quiet_NaN)
XX(signaling_NaN)
XX(denorm_min)
#undef XX
};
////////////////////////////////////////////////////////////////////////////////
} // namespace std
////////////////////////////////////////////////////////////////////////////////
template <class T, class TTag>
struct THash<NYT::TStrongTypedef<T, TTag>>
{
size_t operator()(const NYT::TStrongTypedef<T, TTag>& value) const
{
static constexpr bool IsHashable = requires (T value) {
{ THash<T>()(value) } -> std::same_as<size_t>;
};
if constexpr (IsHashable) {
return THash<T>()(value.Underlying());
} else {
return std::hash<T>()(value.Underlying());
}
}
};
////////////////////////////////////////////////////////////////////////////////
template <class T, class TTag>
IOutputStream& operator<<(IOutputStream& out, const NYT::TStrongTypedef<T, TTag>& value)
{
return out << value.Underlying();
}
////////////////////////////////////////////////////////////////////////////////
|