Debian Codegraph @

src/3rdparty/gsl/byte

raw
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4 //
5 // This code is licensed under the MIT License (MIT).
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13 // THE SOFTWARE.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #ifndef GSL_BYTE_H
18 #define GSL_BYTE_H
19
20 #include "./util" // for GSL_DEPRECATED
21
22 #include <type_traits>
23
24 #ifdef _MSC_VER
25
26 #pragma warning(push)
27
28 // Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
29 #pragma warning(disable : 26493) // don't use c-style casts // TODO: MSVC suppression in templates
30 // does not always work
31
32 #ifndef GSL_USE_STD_BYTE
33 // this tests if we are under MSVC and the standard lib has std::byte and it is enabled
34 #if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603
35
36 #define GSL_USE_STD_BYTE 1
37
38 #else // defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603
39
40 #define GSL_USE_STD_BYTE 0
41
42 #endif // defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603
43 #endif // GSL_USE_STD_BYTE
44
45 #else // _MSC_VER
46
47 #ifndef GSL_USE_STD_BYTE
48 #include <cstddef> /* __cpp_lib_byte */
49 // this tests if we are under GCC or Clang with enough -std=c++1z power to get us std::byte
50 // also check if libc++ version is sufficient (> 5.0) or libstdc++ actually contains std::byte
51 #if defined(__cplusplus) && (__cplusplus >= 201703L) && \
52 (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) || \
53 defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
54
55 #define GSL_USE_STD_BYTE 1
56
57 #else // defined(__cplusplus) && (__cplusplus >= 201703L) &&
58 // (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
59 // defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
60
61 #define GSL_USE_STD_BYTE 0
62
63 #endif // defined(__cplusplus) && (__cplusplus >= 201703L) &&
64 // (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
65 // defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
66 #endif // GSL_USE_STD_BYTE
67
68 #endif // _MSC_VER
69
70 // Use __may_alias__ attribute on gcc and clang
71 #if defined __clang__ || (defined(__GNUC__) && __GNUC__ > 5)
72 #define byte_may_alias __attribute__((__may_alias__))
73 #else // defined __clang__ || defined __GNUC__
74 #define byte_may_alias
75 #endif // defined __clang__ || defined __GNUC__
76
77 #if GSL_USE_STD_BYTE
78 #include <cstddef>
79 #endif
80
81 namespace gsl
82 {
83 #if GSL_USE_STD_BYTE
84
85 namespace impl {
86 // impl::byte is used by gsl::as_bytes so our own code does not trigger a deprecation warning as would be the case when we used gsl::byte.
87 // Users of GSL should only use gsl::byte, not gsl::impl::byte.
88 using byte = std::byte;
89 }
90
91 using byte GSL_DEPRECATED("Use std::byte instead.") = std::byte;
92
93 using std::to_integer;
94
95 #else // GSL_USE_STD_BYTE
96
97 // This is a simple definition for now that allows
98 // use of byte within span<> to be standards-compliant
99 enum class byte_may_alias byte : unsigned char
100 {
101 };
102
103 namespace impl {
104 // impl::byte is used by gsl::as_bytes so our own code does not trigger a deprecation warning as would be the case when we used gsl::byte.
105 // Users of GSL should only use gsl::byte, not gsl::impl::byte.
106 using byte = gsl::byte;
107 }
108
109 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
110 constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept
111 {
112 return b = byte(static_cast<unsigned char>(b) << shift);
113 }
114
115 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
116 constexpr byte operator<<(byte b, IntegerType shift) noexcept
117 {
118 return byte(static_cast<unsigned char>(b) << shift);
119 }
120
121 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
122 constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept
123 {
124 return b = byte(static_cast<unsigned char>(b) >> shift);
125 }
126
127 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
128 constexpr byte operator>>(byte b, IntegerType shift) noexcept
129 {
130 return byte(static_cast<unsigned char>(b) >> shift);
131 }
132
133 constexpr byte& operator|=(byte& l, byte r) noexcept
134 {
135 return l = byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
136 }
137
138 constexpr byte operator|(byte l, byte r) noexcept
139 {
140 return byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
141 }
142
143 constexpr byte& operator&=(byte& l, byte r) noexcept
144 {
145 return l = byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
146 }
147
148 constexpr byte operator&(byte l, byte r) noexcept
149 {
150 return byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
151 }
152
153 constexpr byte& operator^=(byte& l, byte r) noexcept
154 {
155 return l = byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
156 }
157
158 constexpr byte operator^(byte l, byte r) noexcept
159 {
160 return byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
161 }
162
163 constexpr byte operator~(byte b) noexcept { return byte(~static_cast<unsigned char>(b)); }
164
165 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
166 constexpr IntegerType to_integer(byte b) noexcept
167 {
168 return static_cast<IntegerType>(b);
169 }
170
171 #endif // GSL_USE_STD_BYTE
172
173
174 template <typename T>
175 // NOTE: need suppression since c++14 does not allow "return {t}"
176 // GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
177 constexpr gsl::impl::byte to_byte(T t) noexcept
178 {
179 static_assert(std::is_same<T, unsigned char>::value,
180 "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
181 "If you are calling to_byte with an integer constant use: gsl::to_byte<t>() version.");
182 return gsl::impl::byte(t);
183 }
184
185 template <int I>
186 constexpr gsl::impl::byte to_byte() noexcept
187 {
188 static_assert(I >= 0 && I <= 255,
189 "gsl::byte only has 8 bits of storage, values must be in range 0-255");
190 return static_cast<gsl::impl::byte>(I);
191 }
192
193 } // namespace gsl
194
195 #ifdef _MSC_VER
196 #pragma warning(pop)
197 #endif // _MSC_VER
198
199 #endif // GSL_BYTE_H