YSTest: cstdint.hpp 源文件

YSTest PreAlpha_b700_20160611 The YSLib Test Project

- 首页
- 相关页面
- 模块
- 命名空间
- 类
- 文件

- 文件列表
- 文件成员

cstdint.hpp
浏览该文件的文档.
1 /*
2 © 2012-2013, 2015-2016 FrankHB.
3
4 This file is part of the YSLib project, and may only be used,
5 modified, and distributed under the terms of the YSLib project
6 license, LICENSE.TXT. By continuing to use, modify, or distribute
7 this file you indicate that you have read the license and
8 understand and accept it fully.
9 */
10
28 #ifndef YB_INC_ystdex_cstdint_hpp_
29 #define YB_INC_ystdex_cstdint_hpp_ 1
30
31 #include "iterator_op.hpp" // for CHAR_BIT, integral_constant, size_t,
32 // make_signed_t, make_unsigned_t, std::int64_t, std::uint64_t, yconstraint,
33 // is_undereferenceable, ystdex::make_reverse_iterator;
34 #include <limits>
35 #include <numeric> // for std::accumulate;
36
37 namespace ystdex
38 {
39
41
42
46 enum class byte_order
47 {
48 unknown = 0,
49 neutral = 1,
50 little = 2,
51 big = 3,
52 PDP = 4
53 };
54
55
57
58 namespace details
59 {
60
61 struct bit_order_tester
62 {
63 unsigned char le : 4, : CHAR_BIT - 4;
64 };
65
66 union byte_order_tester
67 {
68 std::uint_least32_t n;
69 byte p[4];
70 };
71
72 } // namespace details;
73
75 yconstfn_relaxed YB_STATELESS byte_order
76 native_byte_order()
77 {
78 yconstexpr const details::byte_order_tester x = {0x01020304};
79
80 return x.p[0] == 4 ? byte_order::little : (x.p[0] == 1 ? byte_order::big
81 : (x.p[0] == 2 ? byte_order::PDP : byte_order::unknown));
82 }
83
85 yconstfn YB_STATELESS bool
86 native_little_bit_order()
87 {
88 return bool(details::bit_order_tester{1}.le & 1);
89 }
91
92
93
99 template<typename _tInt>
100 struct integer_width : integral_constant<size_t, sizeof(_tInt) * CHAR_BIT>
101 {};
102
103
110 template<typename _type, bool>
111 struct make_signed_c
112 {
113 using type = make_signed_t<_type>;
114 };
115
116 template<typename _type>
117 struct make_signed_c<_type, false>
118 {
119 using type = make_unsigned_t<_type>;
120 };
122
123
131 template<size_t _vWidth>
132 struct make_width_int
133 {
134 static_assert(_vWidth <= 64, "Width too large found.");
135
136 using fast_type = typename make_width_int<(_vWidth <= 8U ? 8U
137 : (_vWidth <= 16U ? 16U : (_vWidth <= 32U ? 32U : 64U)))>::fast_type;
138 using unsigned_fast_type = typename make_width_int<(_vWidth <= 8U ? 8U
139 : (_vWidth <= 16U ? 16U : (_vWidth <= 32U ? 32U : 64U)))>
140 ::unsigned_fast_type;
141 using least_type = typename make_width_int<(_vWidth <= 8U ? 8U
142 : (_vWidth <= 16U ? 16U : (_vWidth <= 32U ? 32U : 64U)))>::least_type;
143 using unsigned_least_type = typename make_width_int<(_vWidth <= 8U ? 8U
144 : (_vWidth <= 16U ? 16U : (_vWidth <= 32U ? 32U : 64U)))>
145 ::unsigned_least_type;
146 };
147
148 template<>
149 struct make_width_int<8U>
150 {
151 using type = std::int8_t;
152 using unsigned_type = std::uint8_t;
153 using fast_type = std::int_fast8_t;
154 using unsigned_fast_type = std::uint_fast8_t;
155 using least_type = std::int_least8_t;
156 using unsigned_least_type = std::uint_least8_t;
157 };
158
159 template<>
160 struct make_width_int<16U>
161 {
162 using type = std::int16_t;
163 using unsigned_type = std::uint16_t;
164 using fast_type = std::int_fast16_t;
165 using unsigned_fast_type = std::uint_fast16_t;
166 using least_type = std::int_least16_t;
167 using unsigned_least_type = std::uint_least16_t;
168 };
169
170 template<>
171 struct make_width_int<32U>
172 {
173 using type = std::int32_t;
174 using unsigned_type = std::uint32_t;
175 using fast_type = std::int_fast32_t;
176 using unsigned_fast_type = std::uint_fast32_t;
177 using least_type = std::int_least32_t;
178 using unsigned_least_type = std::uint_least32_t;
179 };
180
181 template<>
182 struct make_width_int<64U>
183 {
184 using type = std::int64_t;
185 using unsigned_type = std::uint64_t;
186 using fast_type = std::int_fast64_t;
187 using unsigned_fast_type = std::uint_fast64_t;
188 using least_type = std::int_least64_t;
189 using unsigned_least_type = std::uint_least64_t;
190 };
192
193
202 template<typename _type, bool _bSigned = is_signed<_type>::value>
203 struct make_widen_int
204 {
205 using type = typename make_signed_c<typename
206 make_width_int<integer_width<_type>::value << 1>::type, _bSigned>::type;
207 };
208
209 template<bool _bSigned>
210 struct make_widen_int<std::int64_t, _bSigned>
211 {
212 using type = std::int64_t;
213 };
214
215 template<bool _bSigned>
216 struct make_widen_int<std::uint64_t, _bSigned>
217 {
218 using type = std::uint64_t;
219 };
221
222
231 template<typename _type>
232 struct modular_arithmetic
233 {
234 static yconstexpr const _type value = is_unsigned<_type>::value
235 ? std::numeric_limits<_type>::max() : _type(0);
236 };
237
238
244 template<typename _type1, typename _type2>
245 struct have_same_modulo : bool_constant<uintmax_t(modular_arithmetic<
246 _type1>::value) != 0 && uintmax_t(modular_arithmetic<_type1>::value)
247 == uintmax_t(modular_arithmetic<_type2>::value)>
248 {};
249
250
252
253
258 template<size_t _vWidth, typename _tIn>
259 typename make_width_int<_vWidth>::unsigned_type
260 pack_uint(_tIn first, _tIn last) ynothrowv
261 {
262 static_assert(_vWidth != 0 && _vWidth % std::numeric_limits<byte>::digits
263 == 0, "Invalid integer width found.");
264 using utype = typename make_width_int<_vWidth>::unsigned_type;
265
266 yconstraint(!is_undereferenceable(first));
267 return std::accumulate(first, last, utype(), [](utype x, byte y){
268 return utype(x << std::numeric_limits<byte>::digits | y);
269 });
270 }
271
277 template<size_t _vWidth, typename _tOut>
278 void
279 unpack_uint(typename ystdex::make_width_int<_vWidth>::unsigned_type value,
280 _tOut result) ynothrow
281 {
282 static_assert(_vWidth != 0 && _vWidth % std::numeric_limits<byte>::digits
283 == 0, "Invalid integer width found.");
284 auto n(_vWidth);
285
286 while(!(_vWidth < (n -= std::numeric_limits<byte>::digits)))
287 {
288 yconstraint(!is_undereferenceable(result));
289 *result = byte(value >> n);
290 ++result;
291 }
292 }
294
296
297
301 template<size_t _vWidth>
302 inline YB_NONNULL(1) typename make_width_int<_vWidth>::unsigned_type
303 read_uint_be(const byte* buf) ynothrowv
304 {
305 yconstraint(buf);
306 return ystdex::pack_uint<_vWidth>(buf,
307 buf + _vWidth / std::numeric_limits<byte>::digits);
308 }
309
311
312 template<size_t _vWidth>
314 inline YB_NONNULL(1) typename make_width_int<_vWidth>::unsigned_type
315 read_uint_le(const byte* buf) ynothrowv
316 {
317 yconstraint(buf);
318 return ystdex::pack_uint<_vWidth>(ystdex::make_reverse_iterator(
319 buf + _vWidth / std::numeric_limits<byte>::digits),
320 ystdex::make_reverse_iterator(buf));
321 }
322
327 template<size_t _vWidth>
328 inline YB_NONNULL(1) void
329 write_uint_be(byte* buf, typename make_width_int<_vWidth>::unsigned_type
330 val) ynothrowv
331 {
332 yconstraint(buf);
333 ystdex::unpack_uint<_vWidth>(val, buf);
334 }
335
337 template<size_t _vWidth>
338 inline YB_NONNULL(1) void
339 write_uint_le(byte* buf, typename make_width_int<_vWidth>::unsigned_type
340 val) ynothrowv
341 {
342 yconstraint(buf);
343 ystdex::unpack_uint<_vWidth>(val, ystdex::make_reverse_iterator(buf
344 + _vWidth / std::numeric_limits<byte>::digits));
345 }
347
348
349 } // namespace ystdex;
350
351 #endif
352
ystdex::have_same_modulo
判断两个类型是否具有相同的模值。
Definition: cstdint.hpp:245
ystdex::make_width_int< 64U >::fast_type
std::int_fast64_t fast_type
Definition: cstdint.hpp:186
ystdex::details::byte_order_tester
Definition: cstdint.hpp:66
ystdex::make_width_int< 32U >::fast_type
std::int_fast32_t fast_type
Definition: cstdint.hpp:175
YSLib::Drawing::x
The boundary is out of the const Rect SDst SPos x
Definition: YDraw.h:127
ystdex::make_reverse_iterator
std::reverse_iterator< _tIter > make_reverse_iterator(_tIter i)
构造反向迭代器。
Definition: iterator_op.hpp:107
YB_STATELESS
#define YB_STATELESS
指示函数或函数模板实例为无状态函数。
Definition: ydef.h:522
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:653
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:740
ystdex::byte_order::unknown
ystdex::byte_order::neutral
yconstraint
#define yconstraint(_expr)
约束：接口语义。
Definition: cassert.h:82
YSLib::Text::buf
const char * buf
Definition: TextFile.h:77
ystdex::details::bit_order_tester
Definition: cstdint.hpp:61
ystdex::make_width_int< 8U >::type
std::int8_t type
Definition: cstdint.hpp:151
ystdex::make_width_int< 32U >::type
std::int32_t type
Definition: cstdint.hpp:173
ystdex::unpack_uint
void unpack_uint(typename ystdex::make_width_int< _vWidth >::unsigned_type value, _tOut result)
分解无符号整数到迭代器对指定的字节范围。
Definition: cstdint.hpp:279
ystdex::write_uint_le
void write_uint_le(byte *buf, typename make_width_int< _vWidth >::unsigned_type val)
向字节缓冲区写入指定宽的小端序无符号整数。
Definition: cstdint.hpp:339
ystdex::byte_order::PDP
ystdex::make_width_int::fast_type
typename make_width_int<(_vWidth<=8U?8U:(_vWidth<=16U?16U:(_vWidth<=32U?32U:64U)))>::fast_type fast_type
Definition: cstdint.hpp:137
ystdex::make_width_int< 8U >::unsigned_fast_type
std::uint_fast8_t unsigned_fast_type
Definition: cstdint.hpp:154
ystdex::native_little_bit_order
bool native_little_bit_order()
测试本机位序。
Definition: cstdint.hpp:86
ystdex::make_width_int< 16U >::unsigned_fast_type
std::uint_fast16_t unsigned_fast_type
Definition: cstdint.hpp:165
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:760
ystdex::integer_width
取指定整数类型的位宽度。
Definition: cstdint.hpp:100
ystdex::make_signed_c
取指定整数类型和条件表达式对应的有符号或无符号整数类型。
Definition: cstdint.hpp:111
ystdex::bool_constant
integral_constant< bool, _b > bool_constant
bool 常量。
Definition: type_traits.hpp:382
ystdex::make_signed_c::type
make_signed_t< _type > type
Definition: cstdint.hpp:113
ystdex::make_width_int
取按指定宽度的整数类型。
Definition: cstdint.hpp:132
ystdex::byte_order::big
ystdex::make_width_int< 32U >::least_type
std::int_least32_t least_type
Definition: cstdint.hpp:177
ystdex::modular_arithmetic::value
static const _type value
Definition: cstdint.hpp:234
ystdex::make_widen_int< std::int64_t, _bSigned >::type
std::int64_t type
Definition: cstdint.hpp:212
ystdex::make_width_int::unsigned_least_type
typename make_width_int<(_vWidth<=8U?8U:(_vWidth<=16U?16U:(_vWidth<=32U?32U:64U)))>::unsigned_least_type unsigned_least_type
Definition: cstdint.hpp:145
ystdex::make_width_int< 8U >::least_type
std::int_least8_t least_type
Definition: cstdint.hpp:155
ystdex::make_width_int< 16U >::fast_type
std::int_fast16_t fast_type
Definition: cstdint.hpp:164
ystdex::make_width_int< 8U >::fast_type
std::int_fast8_t fast_type
Definition: cstdint.hpp:153
ystdex::make_width_int< 64U >::unsigned_least_type
std::uint_least64_t unsigned_least_type
Definition: cstdint.hpp:189
ystdex::make_widen_int< std::uint64_t, _bSigned >::type
std::uint64_t type
Definition: cstdint.hpp:218
yconstfn_relaxed
#define yconstfn_relaxed
指定编译时没有 C++11 限制和隐式成员 const 的常量函数。
Definition: ydef.h:666
std
Definition: memory.hpp:1100
ystdex::byte_order::little
ystdex::make_width_int< 8U >::unsigned_least_type
std::uint_least8_t unsigned_least_type
Definition: cstdint.hpp:156
ystdex::pack_uint
make_width_int< _vWidth >::unsigned_type pack_uint(_tIn first, _tIn last)
使用迭代器对指定的范围中的字节表示的序列构造无符号整数。
Definition: cstdint.hpp:260
ystdex::make_width_int::unsigned_fast_type
typename make_width_int<(_vWidth<=8U?8U:(_vWidth<=16U?16U:(_vWidth<=32U?32U:64U)))>::unsigned_fast_type unsigned_fast_type
Definition: cstdint.hpp:140
ystdex::byte_order
byte_order
字节序。
Definition: cstdint.hpp:46
ystdex::make_width_int< 64U >::type
std::int64_t type
Definition: cstdint.hpp:184
ystdex
特性测试宏。
Definition: ydef.h:796
ystdex::max
const _type & max(const _type &a, const _type &b, _fComp comp=less< _type >())
取较大的元素。
Definition: algorithm.hpp:543
ystdex::make_width_int< 16U >::unsigned_least_type
std::uint_least16_t unsigned_least_type
Definition: cstdint.hpp:167
ystdex::make_width_int< 16U >::type
std::int16_t type
Definition: cstdint.hpp:162
ystdex::make_width_int< 64U >::least_type
std::int_least64_t least_type
Definition: cstdint.hpp:188
ystdex::write_uint_be
void write_uint_be(byte *buf, typename make_width_int< _vWidth >::unsigned_type val)
向字节缓冲区写入指定宽的大端序无符号整数。
Definition: cstdint.hpp:329
ystdex::details::byte_order_tester::n
std::uint_least32_t n
Definition: cstdint.hpp:68
ystdex::details::bit_order_tester::le
unsigned char le
Definition: cstdint.hpp:63
ystdex::make_width_int::least_type
typename make_width_int<(_vWidth<=8U?8U:(_vWidth<=16U?16U:(_vWidth<=32U?32U:64U)))>::least_type least_type
Definition: cstdint.hpp:142
ystdex::make_width_int< 64U >::unsigned_type
std::uint64_t unsigned_type
Definition: cstdint.hpp:185
ystdex::is_undereferenceable
bool is_undereferenceable(const _tIter &)
Definition: deref_op.hpp:45
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
ystdex::read_uint_le
make_width_int< _vWidth >::unsigned_type read_uint_le(const byte *buf)
从字节缓冲区读取指定宽的小端序无符号整数。
Definition: cstdint.hpp:315
iterator_op.hpp
迭代器操作。
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:808
ystdex::make_width_int< 8U >::unsigned_type
std::uint8_t unsigned_type
Definition: cstdint.hpp:152
ystdex::make_width_int< 16U >::least_type
std::int_least16_t least_type
Definition: cstdint.hpp:166
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:654
ystdex::details::byte_order_tester::p
byte p[4]
Definition: cstdint.hpp:69
ystdex::make_width_int< 16U >::unsigned_type
std::uint16_t unsigned_type
Definition: cstdint.hpp:163
ystdex::make_widen_int::type
typename make_signed_c< typename make_width_int< integer_width< _type >::value<< 1 >::type, _bSigned >::type type
Definition: cstdint.hpp:206
ystdex::native_byte_order
byte_order native_byte_order()
测试本机字节序。
Definition: cstdint.hpp:76
ystdex::make_width_int< 32U >::unsigned_type
std::uint32_t unsigned_type
Definition: cstdint.hpp:174
ystdex::make_widen_int
位加倍扩展。
Definition: cstdint.hpp:203
ystdex::make_width_int< 32U >::unsigned_least_type
std::uint_least32_t unsigned_least_type
Definition: cstdint.hpp:178
ystdex::modular_arithmetic
模算术特性：取得不超过模值的最大值。
Definition: cstdint.hpp:232
ystdex::make_signed_c< _type, false >::type
make_unsigned_t< _type > type
Definition: cstdint.hpp:119
ystdex::read_uint_be
make_width_int< _vWidth >::unsigned_type read_uint_be(const byte *buf)
从字节缓冲区读取指定宽的大端序无符号整数。
Definition: cstdint.hpp:303
ystdex::make_width_int< 64U >::unsigned_fast_type
std::uint_fast64_t unsigned_fast_type
Definition: cstdint.hpp:187
platform::bool
std::FILE *fp bool
Definition: FileIO.h:1187
platform::n
size_t n
Definition: YCLib/FileSystem.cpp:321
ystdex::make_width_int< 32U >::unsigned_fast_type
std::uint_fast32_t unsigned_fast_type
Definition: cstdint.hpp:176