-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric_conversions.h
More file actions
325 lines (290 loc) · 9.92 KB
/
numeric_conversions.h
File metadata and controls
325 lines (290 loc) · 9.92 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#pragma once
#ifndef XNR_CONVERSIONS_H
#define XNR_CONVERSIONS_H
#ifndef XNR_CONVERSION_OVERFLOW_CHECKING
#ifdef _DEBUG
#define XNR_CONVERSION_OVERFLOW_CHECKING 1
#else
#define XNR_CONVERSION_OVERFLOW_CHECKING 0
#endif
#endif
/***************************************************************************/
// to_signed
/***************************************************************************/
template <class FixDest, class FixSrc = std::false_type, class DeducedSrc,
class = std::enable_if_t<
(std::is_same_v<FixSrc, std::false_type>
|| std::is_same_v<FixSrc, DeducedSrc>)
&& std::is_signed_v<FixDest> && std::is_unsigned_v<DeducedSrc>
>
>
constexpr auto to_signed(DeducedSrc arg)
{
const FixDest ts = static_cast<FixDest>(arg);
#if XNR_CONVERSION_OVERFLOW_CHECKING > 0
if(ts!=arg)
throw std::overflow_error("conversion to signed overflow");
#endif
return ts;
}
template <int = 0, class DeducedSrc,
class = std::enable_if_t<std::is_unsigned_v<DeducedSrc>>
>
constexpr auto to_signed(DeducedSrc arg)
{
const auto ts = static_cast<std::make_signed_t<DeducedSrc>>(arg);
#if XNR_CONVERSION_OVERFLOW_CHECKING > 0
if (ts < 0)
throw std::overflow_error("conversion to signed overflow");
#endif
return ts;
}
/***************************************************************************/
// to_signed_cast
/***************************************************************************/
template <class FixDest, class FixSrc = std::false_type, class DeducedSrc,
class = std::enable_if_t<
(std::is_same_v<FixSrc, std::false_type>
|| std::is_same_v<FixSrc, DeducedSrc>)
&& std::is_signed_v<FixDest>&& std::is_unsigned_v<DeducedSrc>
>
>
constexpr auto to_signed_cast(DeducedSrc arg)
{
return static_cast<FixDest>(arg);
}
template <int = 0, class DeducedSrc,
class = std::enable_if_t<std::is_unsigned_v<DeducedSrc>>
>
constexpr auto to_signed_cast(DeducedSrc arg)
{
return static_cast<std::make_signed_t<DeducedSrc>>(arg);
}
/***************************************************************************/
// to_unsigned
/***************************************************************************/
template <class FixDest, class FixSrc = std::false_type, class DeducedSrc,
class = std::enable_if_t<
(std::is_same_v<FixSrc, std::false_type>
|| std::is_same_v<FixSrc, DeducedSrc>)
&& std::is_signed_v<DeducedSrc>
>
>
constexpr auto to_unsigned(DeducedSrc arg)
{
FixDest tus = static_cast<std::make_unsigned_t<FixDest>>(arg);
#if XNR_CONVERSION_OVERFLOW_CHECKING > 0
if(tus != arg)
throw std::overflow_error("signed to unsigned overflow");
#endif
return tus;
}
template <int = 0, class DeducedSrc,
class = std::enable_if_t<std::is_signed_v<DeducedSrc>>
>
constexpr auto to_unsigned(DeducedSrc arg)
{
#if XNR_CONVERSION_OVERFLOW_CHECKING > 0
if(arg<0)
throw std::overflow_error("signed to unsigned overflow");
#endif
return static_cast<std::make_unsigned_t<DeducedSrc>>(arg);
}
/***************************************************************************/
// to_unsigned_cast
/***************************************************************************/
template <class FixDest, class FixSrc = std::false_type, class DeducedSrc,
class = std::enable_if_t<
(std::is_same_v<FixSrc, std::false_type>
|| std::is_same_v<FixSrc, DeducedSrc>)
&& std::is_signed_v<DeducedSrc>
>
>
constexpr auto to_unsigned_cast(DeducedSrc arg)
{
return static_cast<std::make_unsigned_t<FixDest>>(arg);
}
template <int = 0, class DeducedSrc,
class = std::enable_if_t<std::is_signed_v<DeducedSrc>>
>
constexpr auto to_unsigned_cast(DeducedSrc arg)
{
return static_cast<std::make_unsigned_t<DeducedSrc>>(arg);
}
/***************************************************************************/
// narrow_to
/***************************************************************************/
template <class Dest, class FixSrc = std::false_type,
class DeducedSrc, class = std::enable_if_t<
(std::is_same_v<FixSrc, std::false_type>
|| std::is_same_v<FixSrc, DeducedSrc>)
&& std::is_integral_v<Dest>
&& std::is_integral_v<DeducedSrc>
&& std::is_signed_v<Dest> == std::is_signed_v<DeducedSrc>
&& (sizeof(Dest) < sizeof(DeducedSrc))>
>
constexpr Dest narrow_to(DeducedSrc arg)
{
Dest nt = static_cast<Dest>(arg);
#if XNR_CONVERSION_OVERFLOW_CHECKING > 0
if (nt !=arg)
throw std::overflow_error("integer narrowing overflow");
#endif
return nt;
}
/***************************************************************************/
// narrow_cast_to
/***************************************************************************/
template <class Dest, class FixSrc = std::false_type,
class DeducedSrc, class = std::enable_if_t<
(std::is_same_v<FixSrc, std::false_type>
|| std::is_same_v<FixSrc, DeducedSrc>)
&& std::is_integral_v<Dest>
&& std::is_integral_v<DeducedSrc>
&& (sizeof(Dest) < sizeof(DeducedSrc))>
>
constexpr Dest narrow_cast_to(DeducedSrc arg)
{
return static_cast<Dest>(arg);
}
/***************************************************************************/
// truncate_to
/***************************************************************************/
template <class Dest = decltype(1), class FixSrc = std::false_type,
class DeducedSrc, class = std::enable_if_t<
(std::is_same_v<FixSrc, std::false_type>
|| std::is_same_v<FixSrc, DeducedSrc>)
&& std::is_floating_point_v<DeducedSrc>
&& !std::is_floating_point_v<Dest>>
>
constexpr Dest truncate_to(DeducedSrc arg)
{
#if XNR_CONVERSION_OVERFLOW_CHECKING > 0
if (
std::numeric_limits<Dest>::max() < arg
||
std::numeric_limits<Dest>::min() > arg)
throw std::overflow_error
("number too large for conversion");
#endif
return static_cast<Dest>(arg);
}
/***************************************************************************/
// round_to
/***************************************************************************/
template <class Dest = decltype(1), class FixSrc = std::false_type,
class DeducedSrc, class = std::enable_if_t<
(std::is_same_v<FixSrc, std::false_type>
|| std::is_same_v<FixSrc, DeducedSrc>)
&& std::is_floating_point_v<DeducedSrc>
&& !std::is_floating_point_v<Dest>>
>
constexpr Dest round_to(DeducedSrc arg)
{
#if XNR_CONVERSION_OVERFLOW_CHECKING > 0
if (
std::numeric_limits<Dest>::max() < arg
||
std::numeric_limits<Dest>::min() > arg
)
throw std::overflow_error("number too large for conversion");
#endif
return static_cast<const Dest>((std::is_signed_v<Dest> && arg < 0) ?
arg - 0.5 : arg + 0.5);
}
/***************************************************************************/
// approx_to
/***************************************************************************/
template <class Dest, class FixSrc = std::false_type,
class DeducedSrc, class = std::enable_if_t<
(std::is_same_v<FixSrc, std::false_type> || std::is_same_v<FixSrc, DeducedSrc>)
&& std::is_floating_point_v<Dest>
&&
(
(std::is_floating_point_v<DeducedSrc> && (sizeof(Dest) < sizeof(DeducedSrc)))
||
(std::is_integral_v<DeducedSrc> && (sizeof(Dest) <= sizeof(DeducedSrc)))
)
>
>
constexpr Dest approx_to(DeducedSrc arg)
{
return static_cast<Dest>(arg);
}
/***************************************************************************/
// promote_to
/***************************************************************************/
template <class Dest, class FixSrc = std::false_type,
class DeducedSrc, class = std::enable_if_t<
(std::is_same_v<FixSrc, std::false_type> || std::is_same_v<FixSrc, DeducedSrc>)
&& (sizeof(Dest) > sizeof(DeducedSrc))
&&
(
(std::is_floating_point_v<Dest> && std::is_arithmetic_v<DeducedSrc> )
||
(std::is_integral_v<Dest> && std::is_signed_v<Dest> && std::is_integral_v<DeducedSrc>)
||
(std::is_unsigned_v<Dest> && std::is_unsigned_v<DeducedSrc>)
)
>
>
constexpr Dest promote_to(DeducedSrc arg)
{
return static_cast<Dest>(arg);
}
/***************************************************************************/
// promote_here
/***************************************************************************/
template <class Dest, class FixSrc = std::false_type,
class = std::enable_if_t<std::is_arithmetic_v<Dest>>
>
class promote_here
{
Dest v;
public:
template <class DeducedSrc, class = std::enable_if_t<
(std::is_same_v<FixSrc, std::false_type> || std::is_same_v<FixSrc, DeducedSrc>)
&& (sizeof(Dest) > sizeof(DeducedSrc) || std::is_same_v<Dest , DeducedSrc>)
&&
(
std::is_same_v<Dest, DeducedSrc>
||
(std::is_floating_point_v<Dest>&& std::is_arithmetic_v<DeducedSrc>)
||
(std::is_integral_v<Dest> && std::is_signed_v<Dest> && std::is_integral_v<DeducedSrc>)
||
(std::is_unsigned_v<Dest> && std::is_unsigned_v<DeducedSrc>)
)
>
>
explicit inline promote_here(DeducedSrc uv) : v(static_cast<Dest>(uv))
{}
//Disallowing copying will stop some unintended use
inline promote_here(promote_here const& _v) = delete;
inline promote_here(promote_here const&& _v) = delete;
inline auto operator = (promote_here const& _v) = delete;
inline auto operator = (promote_here const&& _v) = delete;
inline promote_here() = delete;
inline operator Dest() const {
return v;
}
inline Dest operator ()() const {
return v;
}
//This will catch attempts to initialise wrong numerical types
//and not alow them to compile
template<class T,
class = std::enable_if_t<std::is_arithmetic_v<T>>>
inline operator const T() const {
const int f = 0;
f = 6; //intentionally prevents compilation
return v;
}
};
/***************************************************************************/
// aliases using promote_here
/***************************************************************************/
using double_here = promote_here<double>;
using long_double_here = promote_here<long double>;
using long_long_here = promote_here<long long>;
#endif //XNR_CONVERSIONS_H