forked from z1dev/zkanji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checked_cast.h
297 lines (246 loc) · 13.2 KB
/
checked_cast.h
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
/*
** Copyright 2007-2013, 2017-2018 Sólyom Zoltán
** This file is part of zkanji, a free software released under the terms of the
** GNU General Public License version 3. See the file LICENSE for details.
**/
#ifndef CHECKED_CAST_H
#define CHECKED_CAST_H
#include <type_traits>
#include <limits>
#ifndef NDEBUG
#ifndef _DEBUG
#define NDEBUG
#define NDEBUG_ADDED_SEPARATELY
#endif
#endif
#include <cassert>
/*
// Converts T value to RESULT_TYPE, which must be a signed number. RESULT_TYPE is by default
// int32_t. The conversion asserts that the value is not out of limits of the result type.
template<typename T, typename RESULT_TYPE = int32_t>
typename std::enable_if<std::is_signed<RESULT_TYPE>::value && std::is_unsigned<T>::value && (sizeof(T) >= sizeof(RESULT_TYPE)), RESULT_TYPE>::type tosigned(T val)
{
assert(val <= static_cast<typename std::make_unsigned<RESULT_TYPE>::type>(std::numeric_limits<RESULT_TYPE>::max()));
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, which must be a signed number. RESULT_TYPE is by default
// int32_t. The conversion asserts that the value is not out of limits of the result type.
template<typename T, typename RESULT_TYPE = int32_t>
typename std::enable_if<std::is_signed<RESULT_TYPE>::value && std::is_signed<T>::value && (sizeof(T) > sizeof(RESULT_TYPE)), RESULT_TYPE>::type tosigned(T val)
{
assert(val <= std::numeric_limits<RESULT_TYPE>::max() && val >= std::numeric_limits<RESULT_TYPE>::min());
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, which must be a signed number. RESULT_TYPE is by default
// int32_t. The conversion asserts that the value is not out of limits of the result type.
template<typename T, typename RESULT_TYPE = int32_t>
typename std::enable_if<std::is_signed<RESULT_TYPE>::value && (sizeof(T) < sizeof(RESULT_TYPE) || (std::is_signed<T>::value && sizeof(T) == sizeof(RESULT_TYPE))), RESULT_TYPE>::type tosigned(T val)
{
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, which must be an unsigned number. RESULT_TYPE is by
// default uint32_t. The conversion asserts that the value is not out of limits of the
// result type.
template<typename T, typename RESULT_TYPE = uint32_t>
typename std::enable_if<std::is_unsigned<RESULT_TYPE>::value && std::is_signed<T>::value && (sizeof(T) >= sizeof(RESULT_TYPE)), RESULT_TYPE>::type tounsigned(T val)
{
assert(val >= 0 && static_cast<typename std::make_unsigned<T>::type>(val) <= std::numeric_limits<RESULT_TYPE>::max());
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, which must be an unsigned number. RESULT_TYPE is by
// default uint32_t. The conversion asserts that the value is not out of limits of the
// result type.
template<typename T, typename RESULT_TYPE = uint32_t>
typename std::enable_if<std::is_unsigned<RESULT_TYPE>::value && std::is_signed<T>::value && (sizeof(T) < sizeof(RESULT_TYPE)), RESULT_TYPE>::type tounsigned(T val)
{
assert(val >= 0);
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, which must be an unsigned number. RESULT_TYPE is by
// default uint32_t. The conversion asserts that the value is not out of limits of the
// result type.
template<typename T, typename RESULT_TYPE = uint32_t>
typename std::enable_if<std::is_unsigned<RESULT_TYPE>::value && std::is_unsigned<T>::value && (sizeof(T) <= sizeof(RESULT_TYPE)), RESULT_TYPE>::type tounsigned(T val)
{
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, which must be an unsigned number. RESULT_TYPE is by
// default uint32_t. The conversion asserts that the value is not out of limits of the
// result type.
template<typename T, typename RESULT_TYPE = uint32_t>
typename std::enable_if<std::is_unsigned<RESULT_TYPE>::value && std::is_unsigned<T>::value && (sizeof(T) > sizeof(RESULT_TYPE)), RESULT_TYPE>::type tounsigned(T val)
{
assert(val < std::numeric_limits<RESULT_TYPE>::max());
return static_cast<RESULT_TYPE>(val);
}
*/
/*
inline int tosigned(size_t val)
{
assert(val <= std::numeric_limits<int>::max());
return static_cast<int>(val);
}
inline unsigned int tounsigned(size_t val)
{
assert(val <= std::numeric_limits<unsigned int>::max());
return static_cast<unsigned int>(val);
}
// Make value signed. The value is checked for being too large, in which case the assert
// fails. Types smaller than int are converted to int.
template<typename T>
typename std::enable_if<(sizeof(T) < sizeof(int)), int>::type tosigned(T val)
{
return (int)val;
}
// Make value unsigned. The value is checked for being negative, in which case the assert
// fails. Types smaller than unsigned int are converted to unsigned int.
template<typename T>
typename std::enable_if<(sizeof(T) < sizeof(unsigned int)), unsigned int>::type tounsigned(T val)
{
return (unsigned int)val;
}
// Make value signed. The value is checked for being too large, in which case the assert
// fails. Types smaller than int are converted to int.
template<typename T>
typename std::enable_if<std::is_unsigned<T>::value && (sizeof(T) >= sizeof(int)), typename std::make_signed<T>::type>::type tosigned(T val)
{
assert(val <= static_cast<T>(std::numeric_limits<typename std::make_signed<T>::type>::max()));
return static_cast<typename std::make_signed<T>::type>(val);
}
// Make value signed. The value is checked for being too large, in which case the assert
// fails. Types smaller than int are converted to int.
template<typename T>
typename std::enable_if<std::is_signed<T>::value && (sizeof(T) >= sizeof(int)), T>::type tosigned(T val)
{
return val;
}
// Make value unsigned. The value is checked for being negative, in which case the assert
// fails. Types smaller than unsigned int are converted to unsigned int.
template<typename T>
typename std::enable_if<std::is_signed<T>::value && (sizeof(T) >= sizeof(int)), typename std::make_unsigned<T>::type>::type tounsigned(T val)
{
assert(val >= 0);
return static_cast<typename std::make_unsigned<T>::type>(val);
}
// Make value unsigned. The value is checked for being negative, in which case the assert
// fails. Types smaller than unsigned int are converted to unsigned int.
template<typename T>
typename std::enable_if<std::is_unsigned<T>::value && (sizeof(T) >= sizeof(int)), T>::type tounsigned(T val)
{
return val;
}
// Makes a value signed or unsigned to match the first template parameter's signedness. If the
// converted type is smaller than the first parameter, it is converted to the first parameter
// type. If it is larger, but smaller than an int, it's converted to an int type.
template<typename T, typename S>
typename std::enable_if<(sizeof(T) > sizeof(S)), T>::type tosignedness(S val)
{
return (T)val;
}
// Makes a value signed or unsigned to match the first template parameter's signedness. If the
// converted type is smaller than the first parameter, it is converted to the first parameter
// type. If it is larger, but smaller than an int, it's converted to an int type.
template<typename T, typename S>
typename std::enable_if<std::is_signed<T>::value == std::is_signed<S>::value && (sizeof(T) <= sizeof(S)), typename std::conditional<(sizeof(S) >= sizeof(int)), S, typename std::conditional<std::is_signed<S>::value, int, unsigned int>::type >::type>::type tosignedness(S val)
{
return static_cast<typename std::conditional<(sizeof(S) >= sizeof(int)), S, typename std::conditional<std::is_signed<S>::value, int, unsigned int>::type >::type>(val);
}
// Makes a value signed or unsigned to match the first template parameter's signedness. If the
// converted type is smaller than the first parameter, it is converted to the first parameter
// type. If it is larger, but smaller than an int, it's converted to an int type.
template<typename T, typename S>
typename std::enable_if<std::is_signed<T>::value && std::is_unsigned<S>::value && (sizeof(T) <= sizeof(S) && sizeof(S) >= sizeof(int)), typename std::make_signed<S>::type>::type tosignedness(S val)
{
return tosigned(val);
}
// Makes a value signed or unsigned to match the first template parameter's signedness. If the
// converted type is smaller than the first parameter, it is converted to the first parameter
// type. If it is larger, but smaller than an int, it's converted to an int type.
template<typename T, typename S>
typename std::enable_if<std::is_signed<T>::value && std::is_unsigned<S>::value && (sizeof(T) <= sizeof(S) && sizeof(S) < sizeof(int)), int>::type tosignedness(S val)
{
return static_cast<int>(val);
}
// Makes a value signed or unsigned to match the first template parameter's signedness. If the
// converted type is smaller than the first parameter, it is converted to the first parameter
// type. If it is larger, but smaller than an int, it's converted to an int type.
template<typename T, typename S>
typename std::enable_if<std::is_unsigned<T>::value && std::is_signed<S>::value && (sizeof(T) <= sizeof(S) && sizeof(S) >= sizeof(unsigned int)), typename std::make_unsigned<S>::type>::type tosignedness(S val)
{
return tounsigned(val);
}
// Makes a value signed or unsigned to match the first template parameter's signedness. If the
// converted type is smaller than the first parameter, it is converted to the first parameter
// type. If it is larger, but smaller than an int, it's converted to an int type.
template<typename T, typename S>
typename std::enable_if<std::is_unsigned<T>::value && std::is_signed<S>::value && (sizeof(T) <= sizeof(S) && sizeof(S) < sizeof(unsigned int)), unsigned int>::type tosignedness(S val)
{
return static_cast<unsigned int>(val);
}
*/
// Converts T value to RESULT_TYPE, while asserting that it stays within conversion limits.
template<typename RESULT_TYPE, typename T>
typename std::enable_if<std::is_signed<RESULT_TYPE>::value && std::is_unsigned<T>::value && (sizeof(T) >= sizeof(RESULT_TYPE)), RESULT_TYPE>::type tosignedness(T val)
{
assert(val <= static_cast<typename std::make_unsigned<RESULT_TYPE>::type>(std::numeric_limits<RESULT_TYPE>::max()));
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, while asserting that it stays within conversion limits.
template<typename RESULT_TYPE, typename T>
typename std::enable_if<std::is_signed<RESULT_TYPE>::value && std::is_signed<T>::value && (sizeof(T) > sizeof(RESULT_TYPE)), RESULT_TYPE>::type tosignedness(T val)
{
assert(val <= std::numeric_limits<RESULT_TYPE>::max() && val >= std::numeric_limits<RESULT_TYPE>::min());
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, while asserting that it stays within conversion limits.
template<typename RESULT_TYPE, typename T>
typename std::enable_if<std::is_signed<RESULT_TYPE>::value && (sizeof(T) < sizeof(RESULT_TYPE) || (std::is_signed<T>::value && sizeof(T) == sizeof(RESULT_TYPE))), RESULT_TYPE>::type tosignedness(T val)
{
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, while asserting that it stays within conversion limits.
template<typename RESULT_TYPE, typename T>
typename std::enable_if<std::is_unsigned<RESULT_TYPE>::value && std::is_signed<T>::value && (sizeof(T) >= sizeof(RESULT_TYPE)), RESULT_TYPE>::type tosignedness(T val)
{
assert(val >= 0 && static_cast<typename std::make_unsigned<T>::type>(val) <= std::numeric_limits<RESULT_TYPE>::max());
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, while asserting that it stays within conversion limits.
template<typename RESULT_TYPE, typename T>
typename std::enable_if<std::is_unsigned<RESULT_TYPE>::value && std::is_signed<T>::value && (sizeof(T) < sizeof(RESULT_TYPE)), RESULT_TYPE>::type tosignedness(T val)
{
assert(val >= 0);
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, while asserting that it stays within conversion limits.
template<typename RESULT_TYPE, typename T>
typename std::enable_if<std::is_unsigned<RESULT_TYPE>::value && std::is_unsigned<T>::value && (sizeof(T) <= sizeof(RESULT_TYPE)), RESULT_TYPE>::type tosignedness(T val)
{
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, while asserting that it stays within conversion limits.
template<typename RESULT_TYPE, typename T>
typename std::enable_if<std::is_unsigned<RESULT_TYPE>::value && std::is_unsigned<T>::value && (sizeof(T) > sizeof(RESULT_TYPE)), RESULT_TYPE>::type tosignedness(T val)
{
assert(val < std::numeric_limits<RESULT_TYPE>::max());
return static_cast<RESULT_TYPE>(val);
}
// Converts T value to RESULT_TYPE, which must be a signed type. RESULT_TYPE is by default
// int32_t. The conversion asserts that the value is not out of limits of the result type.
template<typename RESULT_TYPE = int32_t, typename T = uint32_t>
typename std::enable_if<std::is_signed<RESULT_TYPE>::value, RESULT_TYPE>::type tosigned(T val)
{
return tosignedness<RESULT_TYPE, T>(val);
}
// Converts T value to RESULT_TYPE, which must be an unsigned type. RESULT_TYPE is by default
// uint32_t. The conversion asserts that the value is not out of limits of the result type.
template<typename RESULT_TYPE = uint32_t, typename T = int32_t>
typename std::enable_if<std::is_unsigned<RESULT_TYPE>::value, RESULT_TYPE>::type tounsigned(T val)
{
return tosignedness<RESULT_TYPE, T>(val);
}
#ifdef NDEBUG_ADDED_SEPARATELY
#undef NDEBUG_ADDED_SEPARATELY
#undef NDEBUG
#endif
#endif // CHECKED_CAST_H