YSTest: cstdint.hpp 源文件

YSTest PreAlpha_b800_20170810 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, size_t_, size_t, make_signed_t,
32 // make_unsigned_t, std::int64_t, std::uint64_t, is_signed, _t, common_type,
33 // cond_t, and_, is_unsigned, bool_, yconstraint, is_undereferenceable,
34 // ystdex::make_reverse_iterator;
35 #include <limits>
36 #include <numeric> // for std::accumulate;
37
38 namespace ystdex
39 {
40
42
43
47 enum class byte_order
48 {
49 unknown = 0,
50 neutral = 1,
51 little = 2,
52 big = 3,
53 PDP = 4
54 };
55
56
58
59 namespace details
60 {
61
62 struct bit_order_tester
63 {
64 unsigned char le : 4, : CHAR_BIT - 4;
65 };
66
67 union byte_order_tester
68 {
69 std::uint_least32_t n;
70 byte p[4];
71 };
72
73 } // namespace details;
74
76 yconstfn_relaxed YB_STATELESS byte_order
77 native_byte_order()
78 {
79 yconstexpr const details::byte_order_tester x = {0x01020304};
80
81 return x.p[0] == 4 ? byte_order::little : (x.p[0] == 1 ? byte_order::big
82 : (x.p[0] == 2 ? byte_order::PDP : byte_order::unknown));
83 }
84
86 yconstfn YB_STATELESS bool
87 native_little_bit_order()
88 {
89 return bool(details::bit_order_tester{1}.le & 1);
90 }
92
93
94
100 template<typename _tInt>
101 struct integer_width : size_t_<sizeof(_tInt) * CHAR_BIT>
102 {};
103
104
111 template<typename _type, bool>
112 struct make_signed_c : make_signed<_type>
113 {};
114
115 template<typename _type>
116 struct make_signed_c<_type, false> : make_unsigned<_type>
117 {};
119
120
128 template<size_t _vWidth>
129 struct make_width_int
130 {
131 static_assert(_vWidth <= 64, "Width too large found.");
132
133 using fast_type = typename make_width_int<(_vWidth <= 8U ? 8U
134 : (_vWidth <= 16U ? 16U : (_vWidth <= 32U ? 32U : 64U)))>::fast_type;
135 using unsigned_fast_type = typename make_width_int<(_vWidth <= 8U ? 8U
136 : (_vWidth <= 16U ? 16U : (_vWidth <= 32U ? 32U : 64U)))>
137 ::unsigned_fast_type;
138 using least_type = typename make_width_int<(_vWidth <= 8U ? 8U
139 : (_vWidth <= 16U ? 16U : (_vWidth <= 32U ? 32U : 64U)))>::least_type;
140 using unsigned_least_type = typename make_width_int<(_vWidth <= 8U ? 8U
141 : (_vWidth <= 16U ? 16U : (_vWidth <= 32U ? 32U : 64U)))>
142 ::unsigned_least_type;
143 };
144
145 template<>
146 struct make_width_int<8U>
147 {
148 using type = std::int8_t;
149 using unsigned_type = std::uint8_t;
150 using fast_type = std::int_fast8_t;
151 using unsigned_fast_type = std::uint_fast8_t;
152 using least_type = std::int_least8_t;
153 using unsigned_least_type = std::uint_least8_t;
154 };
155
156 template<>
157 struct make_width_int<16U>
158 {
159 using type = std::int16_t;
160 using unsigned_type = std::uint16_t;
161 using fast_type = std::int_fast16_t;
162 using unsigned_fast_type = std::uint_fast16_t;
163 using least_type = std::int_least16_t;
164 using unsigned_least_type = std::uint_least16_t;
165 };
166
167 template<>
168 struct make_width_int<32U>
169 {
170 using type = std::int32_t;
171 using unsigned_type = std::uint32_t;
172 using fast_type = std::int_fast32_t;
173 using unsigned_fast_type = std::uint_fast32_t;
174 using least_type = std::int_least32_t;
175 using unsigned_least_type = std::uint_least32_t;
176 };
177
178 template<>
179 struct make_width_int<64U>
180 {
181 using type = std::int64_t;
182 using unsigned_type = std::uint64_t;
183 using fast_type = std::int_fast64_t;
184 using unsigned_fast_type = std::uint_fast64_t;
185 using least_type = std::int_least64_t;
186 using unsigned_least_type = std::uint_least64_t;
187 };
189
190
192
193
199 template<typename _type, bool _bSigned = is_signed<_type>::value>
200 struct make_widen_int
201 {
202 private:
203 using width = integer_width<_type>;
204
205 public:
206 using type = _t<make_signed_c<_t<make_width_int<(width::value << 1) <= 64
207 ? width::value << 1 : width::value>>, _bSigned>>;
208 };
209
210
217 template<typename... _types>
218 struct common_int_type : common_type<_types...>
219 {};
220
221 template<typename _type1, typename _type2, typename... _types>
222 struct common_int_type<_type1, _type2, _types...>
223 {
224 private:
225 using common_t = common_type_t<_type1, _type2>;
226
227 public:
228 using type = typename common_int_type<cond_t<
229 and_<is_unsigned<common_t>, or_<is_signed<_type1>, is_signed<_type2>>>,
230 _t<make_widen_int<common_t, true>>, common_t>, _types...>::type;
231 };
233
234
235
244 template<typename _type>
245 struct modular_arithmetic
246 {
247 static yconstexpr const _type value = is_unsigned<_type>::value
248 ? std::numeric_limits<_type>::max() : _type(0);
249 };
250
251
257 template<typename _type1, typename _type2>
258 struct have_same_modulo : bool_<uintmax_t(modular_arithmetic<
259 _type1>::value) != 0 && uintmax_t(modular_arithmetic<_type1>::value)
260 == uintmax_t(modular_arithmetic<_type2>::value)>
261 {};
262
263
265
266
271 template<size_t _vWidth, typename _tIn>
272 typename make_width_int<_vWidth>::unsigned_type
273 pack_uint(_tIn first, _tIn last) ynothrowv
274 {
275 static_assert(_vWidth != 0 && _vWidth % std::numeric_limits<byte>::digits
276 == 0, "Invalid integer width found.");
277 using utype = typename make_width_int<_vWidth>::unsigned_type;
278
279 yconstraint(!is_undereferenceable(first));
280 return std::accumulate(first, last, utype(), [](utype x, byte y){
281 return utype(x << std::numeric_limits<byte>::digits | y);
282 });
283 }
284
290 template<size_t _vWidth, typename _tOut>
291 void
292 unpack_uint(typename ystdex::make_width_int<_vWidth>::unsigned_type value,
293 _tOut result) ynothrow
294 {
295 static_assert(_vWidth != 0 && _vWidth % std::numeric_limits<byte>::digits
296 == 0, "Invalid integer width found.");
297 auto n(_vWidth);
298
299 while(!(_vWidth < (n -= std::numeric_limits<byte>::digits)))
300 {
301 yconstraint(!is_undereferenceable(result));
302 *result = byte(value >> n);
303 ++result;
304 }
305 }
307
309
310
314 template<size_t _vWidth>
315 inline YB_NONNULL(1) typename make_width_int<_vWidth>::unsigned_type
316 read_uint_be(const byte* buf) ynothrowv
317 {
318 yconstraint(buf);
319 return ystdex::pack_uint<_vWidth>(buf,
320 buf + _vWidth / std::numeric_limits<byte>::digits);
321 }
322
324
325 template<size_t _vWidth>
327 inline YB_NONNULL(1) typename make_width_int<_vWidth>::unsigned_type
328 read_uint_le(const byte* buf) ynothrowv
329 {
330 yconstraint(buf);
331 return ystdex::pack_uint<_vWidth>(ystdex::make_reverse_iterator(
332 buf + _vWidth / std::numeric_limits<byte>::digits),
333 ystdex::make_reverse_iterator(buf));
334 }
335
340 template<size_t _vWidth>
341 inline YB_NONNULL(1) void
342 write_uint_be(byte* buf, typename make_width_int<_vWidth>::unsigned_type
343 val) ynothrowv
344 {
345 yconstraint(buf);
346 ystdex::unpack_uint<_vWidth>(val, buf);
347 }
348
350 template<size_t _vWidth>
351 inline YB_NONNULL(1) void
352 write_uint_le(byte* buf, typename make_width_int<_vWidth>::unsigned_type
353 val) ynothrowv
354 {
355 yconstraint(buf);
356 ystdex::unpack_uint<_vWidth>(val, ystdex::make_reverse_iterator(buf
357 + _vWidth / std::numeric_limits<byte>::digits));
358 }
360
361
362 } // namespace ystdex;
363
364 #endif
365
ystdex::make_width_int< 64U >::type
std::int64_t type
Definition: cstdint.hpp:181
ystdex::cond_t
conditional_t< _tCond::value, _tThen, _tElse > cond_t
条件判断。
Definition: type_traits.hpp:738
ystdex::make_width_int< 8U >::type
std::int8_t type
Definition: cstdint.hpp:148
ystdex::details::byte_order_tester::n
std::uint_least32_t n
Definition: cstdint.hpp:69
yconstfn
#define yconstfn
指定编译时常量函数。
Definition: ydef.h:680
ystdex::make_width_int< 32U >::type
std::int32_t type
Definition: cstdint.hpp:170
ystdex::make_width_int< 64U >::unsigned_type
std::uint64_t unsigned_type
Definition: cstdint.hpp:182
ystdex::make_width_int< 16U >::fast_type
std::int_fast16_t fast_type
Definition: cstdint.hpp:161
ystdex::make_width_int< 32U >::least_type
std::int_least32_t least_type
Definition: cstdint.hpp:174
ystdex::make_width_int< 64U >::unsigned_least_type
std::uint_least64_t unsigned_least_type
Definition: cstdint.hpp:186
ystdex::make_width_int< 16U >::type
std::int16_t type
Definition: cstdint.hpp:159
ystdex::make_width_int< 32U >::unsigned_fast_type
std::uint_fast32_t unsigned_fast_type
Definition: cstdint.hpp:173
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:137
ystdex::pack_uint
make_width_int< _vWidth >::unsigned_type pack_uint(_tIn first, _tIn last)
使用迭代器对指定的范围中的字节表示的序列构造无符号整数。
Definition: cstdint.hpp:273
ystdex::have_same_modulo
判断两个类型是否具有相同的模值。
Definition: cstdint.hpp:258
YSLib::UI::y
SPos SPos y
Definition: YWidget.cpp:44
NPL::x
const TermNode &term ynothrow const TermNode &term ynothrow const TermNode &term ynothrow TermNode &term const TermNode &term ValueNode &node const ValueNode &node const _type & x
Definition: SContext.h:94
ystdex::common_int_type< _type1, _type2, _types... >::type
typename common_int_type< cond_t< and_< is_unsigned< common_t >, or_< is_signed< _type1 >, is_signed< _type2 >>>, _t< make_widen_int< common_t, true >>, common_t >, _types... >::type type
Definition: cstdint.hpp:230
ystdex::byte_order::unknown
ystdex::modular_arithmetic::value
static const _type value
Definition: cstdint.hpp:247
ystdex::or_
Definition: type_traits.hpp:438
ystdex::integer_width
取指定整数类型的位宽度。
Definition: cstdint.hpp:101
platform::bool
std::FILE *fp bool
Definition: FileIO.h:1280
ystdex::make_width_int< 8U >::fast_type
std::int_fast8_t fast_type
Definition: cstdint.hpp:150
ystdex::make_width_int< 16U >::least_type
std::int_least16_t least_type
Definition: cstdint.hpp:163
yconstfn_relaxed
#define yconstfn_relaxed
指定编译时没有 C++11 限制和隐式成员 const 的常量函数。
Definition: ydef.h:692
ystdex::make_signed_c
取指定整数类型和条件表达式对应的有符号或无符号整数类型。
Definition: cstdint.hpp:112
yconstexpr
#define yconstexpr
指定编译时常量表达式。
Definition: ydef.h:679
ystdex::details::byte_order_tester::p
byte p[4]
Definition: cstdint.hpp:70
YSLib::Text::buf
const char * buf
Definition: TextFile.h:81
ystdex::is_undereferenceable
bool is_undereferenceable(const _tIter &)
Definition: deref_op.hpp:45
ystdex::native_little_bit_order
bool native_little_bit_order()
测试本机位序。
Definition: cstdint.hpp:87
ystdex::common_int_type< _type1, _type2, _types... >::common_t
common_type_t< _type1, _type2 > common_t
Definition: cstdint.hpp:225
ystdex::max
const _type & max(const _type &a, const _type &b, _fComp comp=less< _type >())
取较大的元素。
Definition: algorithm.hpp:549
ystdex::make_width_int
取按指定宽度的整数类型。
Definition: cstdint.hpp:129
ystdex::make_reverse_iterator
std::reverse_iterator< _tIter > make_reverse_iterator(_tIter i)
构造反向迭代器。
Definition: iterator_op.hpp:107
ystdex::byte_order::big
ynothrow
#define ynothrow
YSLib 无异常抛出保证：若支持 noexcept 关键字， 指定特定的 noexcept 异常规范。
Definition: ydef.h:766
ystdex::make_width_int< 32U >::unsigned_least_type
std::uint_least32_t unsigned_least_type
Definition: cstdint.hpp:175
ystdex::read_uint_be
make_width_int< _vWidth >::unsigned_type read_uint_be(const byte *buf)
从字节缓冲区读取指定宽的大端序无符号整数。
Definition: cstdint.hpp:316
ystdex
特性测试宏。
Definition: ydef.h:822
ystdex::native_byte_order
byte_order native_byte_order()
测试本机字节序。
Definition: cstdint.hpp:77
ystdex::make_width_int< 64U >::unsigned_fast_type
std::uint_fast64_t unsigned_fast_type
Definition: cstdint.hpp:184
ystdex::size_t_
std::integral_constant< size_t, _vInt > size_t_
Definition: type_traits.hpp:385
ystdex::details::byte_order_tester
Definition: cstdint.hpp:67
ystdex::make_width_int< 32U >::fast_type
std::int_fast32_t fast_type
Definition: cstdint.hpp:172
ystdex::read_uint_le
make_width_int< _vWidth >::unsigned_type read_uint_le(const byte *buf)
从字节缓冲区读取指定宽的小端序无符号整数。
Definition: cstdint.hpp:328
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:139
ystdex::details::bit_order_tester
Definition: cstdint.hpp:62
ystdex::common_int_type
公共整数类型。
Definition: cstdint.hpp:218
ystdex::_t
typename _type::type _t
嵌套类型别名。
Definition: type_traits.hpp:97
ynothrowv
#define ynothrowv
YSLib 无异常抛出保证验证：有条件地使用无异常抛出规范。
Definition: ydef.h:786
ystdex::write_uint_be
void write_uint_be(byte *buf, typename make_width_int< _vWidth >::unsigned_type val)
向字节缓冲区写入指定宽的大端序无符号整数。
Definition: cstdint.hpp:342
ystdex::unpack_uint
void unpack_uint(typename ystdex::make_width_int< _vWidth >::unsigned_type value, _tOut result)
分解无符号整数到迭代器对指定的字节范围。
Definition: cstdint.hpp:292
iterator_op.hpp
迭代器操作。
ystdex::byte
unsigned char byte
字节类型。
Definition: ydef.h:834
ystdex::make_width_int< 8U >::unsigned_type
std::uint8_t unsigned_type
Definition: cstdint.hpp:149
ystdex::byte_order
byte_order
字节序。
Definition: cstdint.hpp:47
ystdex::make_width_int< 32U >::unsigned_type
std::uint32_t unsigned_type
Definition: cstdint.hpp:171
ystdex::make_width_int< 64U >::fast_type
std::int_fast64_t fast_type
Definition: cstdint.hpp:183
ystdex::make_width_int< 8U >::unsigned_fast_type
std::uint_fast8_t unsigned_fast_type
Definition: cstdint.hpp:151
ystdex::byte_order::little
ystdex::make_width_int< 64U >::least_type
std::int_least64_t least_type
Definition: cstdint.hpp:185
ystdex::byte_order::neutral
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:134
ystdex::make_width_int< 8U >::unsigned_least_type
std::uint_least8_t unsigned_least_type
Definition: cstdint.hpp:153
ystdex::modular_arithmetic
模算术特性：取得不超过模值的最大值。
Definition: cstdint.hpp:245
ystdex::make_widen_int::type
_t< make_signed_c< _t< make_width_int<(width::value<< 1)<=64?width::value<< 1:width::value >>, _bSigned >> type
Definition: cstdint.hpp:207
ystdex::write_uint_le
void write_uint_le(byte *buf, typename make_width_int< _vWidth >::unsigned_type val)
向字节缓冲区写入指定宽的小端序无符号整数。
Definition: cstdint.hpp:352
ystdex::make_widen_int
位加倍扩展。
Definition: cstdint.hpp:200
ystdex::make_width_int< 16U >::unsigned_least_type
std::uint_least16_t unsigned_least_type
Definition: cstdint.hpp:164
ystdex::byte_order::PDP
yconstraint
#define yconstraint(_expr)
约束：接口语义。
Definition: cassert.h:88
ystdex::and_
逻辑操作元函数。
Definition: type_traits.hpp:420
ystdex::make_width_int< 8U >::least_type
std::int_least8_t least_type
Definition: cstdint.hpp:152
ystdex::details::bit_order_tester::le
unsigned char le
Definition: cstdint.hpp:64
ystdex::make_width_int< 16U >::unsigned_type
std::uint16_t unsigned_type
Definition: cstdint.hpp:160
ystdex::make_width_int< 16U >::unsigned_fast_type
std::uint_fast16_t unsigned_fast_type
Definition: cstdint.hpp:162
ystdex::bool_
std::integral_constant< bool, _vInt > bool_
Definition: type_traits.hpp:375
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:142
YB_STATELESS
#define YB_STATELESS
指示函数或函数模板实例为无状态函数。
Definition: ydef.h:548