-
Notifications
You must be signed in to change notification settings - Fork 0
/
byte.hpp
116 lines (100 loc) · 3.21 KB
/
byte.hpp
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
/**
* \file byte.hpp
*
* \brief This header is mostly taken from Microsoft's implementation of
* the GSL byte-type.
*
* \author Matthew Rodusek ([email protected])
*/
#ifndef BIT_STL_UTILITIES_BYTE_HPP
#define BIT_STL_UTILITIES_BYTE_HPP
#include <type_traits> // std::is_integral, std::enable_if_t, etc
namespace bit {
namespace stl {
/// \brief Unsigned byte type
///
/// \note Due to a restriction in pre-C++-17, a conversion to a pointer of
/// byte types results in undefined behavior due to a violation of
/// strict-aliasing.
enum class byte : unsigned char{};
//-------------------------------------------------------------------------
// Byte Operators
//-------------------------------------------------------------------------
#ifndef BIT_DOXYGEN_BUILD
template<typename IntT, typename = std::enable_if_t<std::is_integral<IntT>::value>>
#else
template<typename IntT>
#endif
inline constexpr byte& operator<<=(byte& lhs, IntT shift)
noexcept
{
return lhs = byte(static_cast<unsigned char>(lhs) << shift);
}
#ifndef BIT_DOXYGEN_BUILD
template<typename IntT, typename = std::enable_if_t<std::is_integral<IntT>::value>>
#else
template<typename IntT>
#endif
inline constexpr byte operator<<(byte lhs, IntT shift)
noexcept
{
return byte(static_cast<unsigned char>(lhs) << shift);
}
#ifndef BIT_DOXYGEN_BUILD
template<typename IntT, typename = std::enable_if_t<std::is_integral<IntT>::value>>
#else
template<typename IntT>
#endif
inline constexpr byte& operator>>=(byte& lhs, IntT shift)
noexcept
{
return lhs = byte(static_cast<unsigned char>(lhs) >> shift);
}
#ifndef BIT_DOXYGEN_BUILD
template<typename IntT, typename = std::enable_if_t<std::is_integral<IntT>::value>>
#else
template<typename IntT>
#endif
inline constexpr byte operator>>(byte lhs, IntT shift)
noexcept
{
return byte(static_cast<unsigned char>(lhs) >> shift);
}
inline byte& operator|=(byte& lhs, byte rhs)
noexcept
{
return lhs = byte(static_cast<unsigned char>(lhs) | static_cast<unsigned char>(rhs));
}
inline constexpr byte operator|(byte lhs, byte rhs)
noexcept
{
return byte(static_cast<unsigned char>(lhs) | static_cast<unsigned char>(rhs));
}
inline byte& operator&=(byte& lhs, byte rhs)
noexcept
{
return lhs = byte(static_cast<unsigned char>(lhs) & static_cast<unsigned char>(rhs));
}
inline constexpr byte operator&(byte lhs, byte rhs)
noexcept
{
return byte(static_cast<unsigned char>(lhs) & static_cast<unsigned char>(rhs));
}
inline byte& operator^=(byte& lhs, byte rhs)
noexcept
{
return lhs = byte(static_cast<unsigned char>(lhs) ^ static_cast<unsigned char>(rhs));
}
inline constexpr byte operator^(byte lhs, byte rhs)
noexcept
{
return byte(static_cast<unsigned char>(lhs) ^ static_cast<unsigned char>(rhs));
}
inline constexpr byte operator~(byte lhs)
noexcept
{
return byte(~static_cast<unsigned char>(lhs));
}
} // namespace stl
} // namespace bit
#endif /* BIT_STL_UTILITIES_BYTE_HPP */