Backport
cstddef.hpp
Go to the documentation of this file.
1 
7 /*
8  The MIT License (MIT)
9 
10  Copyright (c) 2020 Matthew Rodusek All rights reserved.
11 
12  Permission is hereby granted, free of charge, to any person obtaining a copy
13  of this software and associated documentation files (the "Software"), to deal
14  in the Software without restriction, including without limitation the rights
15  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  copies of the Software, and to permit persons to whom the Software is
17  furnished to do so, subject to the following conditions:
18 
19  The above copyright notice and this permission notice shall be included in
20  all copies or substantial portions of the Software.
21 
22  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  SOFTWARE.
29 */
30 #ifndef BPSTD_CSTDDEF_HPP
31 #define BPSTD_CSTDDEF_HPP
32 
33 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
34 # pragma once
35 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
36 
37 #include "detail/config.hpp"
38 #include "type_traits.hpp"
39 
40 #include <type_traits> // std::is_integral
41 #include <cstddef> // __cpp_lib_byte, and to proxy API
42 
43 BPSTD_COMPILER_DIAGNOSTIC_PREAMBLE
44 
45 // The below implementation is based on GSL's implementation of 'gsl::byte'
46 
47 // VS2017 15.8 added support for the __cpp_lib_byte definition
48 // To do: drop _HAS_STD_BYTE when support for pre 15.8 expires
49 #if defined(_MSC_VER)
50 
51 // Turn MSVC /analyze rules that generate too much noise.
52 # pragma warning(push)
53 # pragma warning(disable : 26493) // don't use c-style cast
54 
55 # if !defined(BPSTD_USE_STD_BYTE)
56 // this tests if we are under MSVC and the standard lib has std::byte and it is
57 // enabled
58 # if defined(_HAS_STD_BYTE) && _HAS_STD_BYTE
59 # define BPSTD_USE_STD_BYTE 1
60 # elif defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603
61 # define BPSTD_USE_STD_BYTE 1
62 # else
63 # define BPSTD_USE_STD_BYTE 0
64 # endif
65 # endif // BPSTD_USE_STD_BYTE
66 #endif // _MSC_VER
67 
68 #if !defined(BPSTD_USE_STD_BYTE)
69 
70 // this tests if we are under GCC or Clang with enough -std:c++1z power to get us std::byte
71 // also check if libc++ version is sufficient (> 5.0) or libstc++ actually contains std::byte
72 # if defined(__cplusplus) && (__cplusplus >= 201703L) && \
73  (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) || \
74  defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
75 # define BPSTD_USE_STD_BYTE 1
76 # else
77 # define BPSTD_USE_STD_BYTE 0
78 # endif
79 #endif // BPSTD_USE_STD_BYTE
80 
81 namespace bpstd {
82 
83 #if BPSTD_USE_STD_BYTE
84  using std::byte;
85 #else
86  enum class BPSTD_MAY_ALIAS byte : unsigned char {};
87 
88  //============================================================================
89  // non-member functions : enum lass : byte
90  //============================================================================
91 
92  //----------------------------------------------------------------------------
93  // Bitwise operators
94  //----------------------------------------------------------------------------
95 
96  template <typename Integer,
97  typename = enable_if_t<std::is_integral<Integer>::value>>
98  constexpr byte operator<<(byte b, Integer shift) noexcept;
99  template <typename Integer,
100  typename = enable_if_t<std::is_integral<Integer>::value>>
101  constexpr byte operator>>(byte b, Integer shift) noexcept;
102 
103  constexpr byte operator|(byte lhs, byte rhs) noexcept;
104  constexpr byte operator&(byte lhs, byte rhs) noexcept;
105  constexpr byte operator^(byte lhs, byte rhs) noexcept;
106  constexpr byte operator~(byte b) noexcept;
107 
108  //----------------------------------------------------------------------------
109  // Compound Bitwise Operators
110  //----------------------------------------------------------------------------
111 
112  template <typename Integer,
113  typename = enable_if_t<std::is_integral<Integer>::value>>
114  BPSTD_CPP14_CONSTEXPR byte& operator<<=(byte& b, Integer shift) noexcept;
115  template <typename Integer,
116  typename = enable_if_t<std::is_integral<Integer>::value>>
117  BPSTD_CPP14_CONSTEXPR byte& operator>>=(byte& b, Integer shift) noexcept;
118  BPSTD_CPP14_CONSTEXPR byte& operator|=(byte& lhs, byte rhs) noexcept;
119  BPSTD_CPP14_CONSTEXPR byte& operator&=(byte& lhs, byte rhs) noexcept;
120 
121  BPSTD_CPP14_CONSTEXPR byte& operator^=(byte& lhs, byte rhs) noexcept;
122 
123 #endif
124 
125  //----------------------------------------------------------------------------
126  // Utilities
127  //----------------------------------------------------------------------------
128 
129  template <typename Integer,
130  typename = enable_if_t<std::is_integral<Integer>::value>>
131  constexpr Integer to_integer(byte b) noexcept;
132 
133 } // namespace bpstd
134 
135 //==============================================================================
136 // definitions : non-member functions : enum class : byte
137 //==============================================================================
138 
139 #if !BPSTD_USE_STD_BYTE
140 
141 //------------------------------------------------------------------------------
142 // Bitwise Operators
143 //------------------------------------------------------------------------------
144 
145 template <typename Integer, typename>
146 inline BPSTD_INLINE_VISIBILITY constexpr
147 bpstd::byte bpstd::operator<<(byte b, Integer shift)
148  noexcept
149 {
150  return static_cast<byte>(static_cast<unsigned char>(b) << shift);
151 }
152 
153 template <typename Integer, typename>
154 inline BPSTD_INLINE_VISIBILITY constexpr
155 bpstd::byte bpstd::operator>>(byte b, Integer shift)
156  noexcept
157 {
158  return static_cast<byte>(static_cast<unsigned char>(b) >> shift);
159 }
160 
161 inline BPSTD_INLINE_VISIBILITY constexpr
162 bpstd::byte bpstd::operator|(byte lhs, byte rhs)
163  noexcept
164 {
165  return static_cast<byte>(
166  static_cast<unsigned char>(lhs) | static_cast<unsigned char>(rhs)
167  );
168 }
169 
170 inline BPSTD_INLINE_VISIBILITY constexpr
171 bpstd::byte bpstd::operator&(byte lhs, byte rhs)
172  noexcept
173 {
174  return static_cast<byte>(
175  static_cast<unsigned char>(lhs) & static_cast<unsigned char>(rhs)
176  );
177 }
178 
179 inline BPSTD_INLINE_VISIBILITY constexpr
180 bpstd::byte bpstd::operator^(byte lhs, byte rhs)
181  noexcept
182 {
183  return static_cast<byte>(
184  static_cast<unsigned char>(lhs) ^ static_cast<unsigned char>(rhs)
185  );
186 }
187 
188 inline BPSTD_INLINE_VISIBILITY constexpr
189 bpstd::byte bpstd::operator~(byte b)
190  noexcept
191 {
192  return static_cast<byte>(~static_cast<unsigned char>(b));
193 }
194 
195 //------------------------------------------------------------------------------
196 // Compound Bitwise Operators
197 //------------------------------------------------------------------------------
198 
199 template <typename Integer, typename>
200 inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR
201 bpstd::byte& bpstd::operator<<=(byte& b, Integer shift)
202  noexcept
203 {
204  return b = static_cast<byte>(static_cast<unsigned char>(b) << shift);
205 }
206 
207 template <typename Integer, typename>
208 inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR
209 bpstd::byte& bpstd::operator>>=(byte& b, Integer shift)
210  noexcept
211 {
212  return b = static_cast<byte>(static_cast<unsigned char>(b) >> shift);
213 }
214 
215 inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR
216 bpstd::byte& bpstd::operator|=(byte& lhs, byte rhs)
217  noexcept
218 {
219  return lhs = static_cast<byte>(
220  static_cast<unsigned char>(lhs) | static_cast<unsigned char>(rhs)
221  );
222 }
223 
224 inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR
225 bpstd::byte& bpstd::operator&=(byte& lhs, byte rhs)
226  noexcept
227 {
228  return lhs = static_cast<byte>(
229  static_cast<unsigned char>(lhs) & static_cast<unsigned char>(rhs)
230  );
231 }
232 
233 inline BPSTD_INLINE_VISIBILITY BPSTD_CPP14_CONSTEXPR
234 bpstd::byte& bpstd::operator^=(byte& lhs, byte rhs)
235  noexcept
236 {
237  return lhs = static_cast<byte>(
238  static_cast<unsigned char>(lhs) ^ static_cast<unsigned char>(rhs)
239  );
240 }
241 
242 #endif // BPSTD_USE_STD_BYTE
243 
244 //------------------------------------------------------------------------------
245 // Utilities
246 //------------------------------------------------------------------------------
247 
248 template <typename Integer, typename>
249 inline BPSTD_INLINE_VISIBILITY constexpr
250 Integer bpstd::to_integer(byte b)
251  noexcept
252 {
253  return static_cast<Integer>(b);
254 }
255 
256 BPSTD_COMPILER_DIAGNOSTIC_POSTAMBLE
257 
258 #endif /* BPSTD_CSTDDEF_HPP */
type_traits.hpp
This header provides definitions from the C++ header <type_traits>
bpstd::operator<<
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &o, const basic_string_view< CharT, Traits > &str)
Overload for ostream output of basic_string_view.