Warning, /include/gsl/byte is written in an unsupported language. File is not indexed.
0001 ///////////////////////////////////////////////////////////////////////////////
0002 //
0003 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
0004 //
0005 // This code is licensed under the MIT License (MIT).
0006 //
0007 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0008 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0009 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0010 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0011 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0012 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0013 // THE SOFTWARE.
0014 //
0015 ///////////////////////////////////////////////////////////////////////////////
0016
0017 #ifndef GSL_BYTE_H
0018 #define GSL_BYTE_H
0019
0020 #include "./util" // for GSL_DEPRECATED
0021
0022 #include <type_traits>
0023
0024 #ifdef _MSC_VER
0025
0026 #pragma warning(push)
0027
0028 // Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
0029 #pragma warning(disable : 26493) // don't use c-style casts // TODO: MSVC suppression in templates
0030 // does not always work
0031
0032 #ifndef GSL_USE_STD_BYTE
0033 // this tests if we are under MSVC and the standard lib has std::byte and it is enabled
0034 #if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603
0035
0036 #define GSL_USE_STD_BYTE 1
0037
0038 #else // defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603
0039
0040 #define GSL_USE_STD_BYTE 0
0041
0042 #endif // defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603
0043 #endif // GSL_USE_STD_BYTE
0044
0045 #else // _MSC_VER
0046
0047 #ifndef GSL_USE_STD_BYTE
0048 #include <cstddef> /* __cpp_lib_byte */
0049 // this tests if we are under GCC or Clang with enough -std=c++1z power to get us std::byte
0050 // also check if libc++ version is sufficient (> 5.0) or libstdc++ actually contains std::byte
0051 #if defined(__cplusplus) && (__cplusplus >= 201703L) && \
0052 (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) || \
0053 defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
0054
0055 #define GSL_USE_STD_BYTE 1
0056
0057 #else // defined(__cplusplus) && (__cplusplus >= 201703L) &&
0058 // (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
0059 // defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
0060
0061 #define GSL_USE_STD_BYTE 0
0062
0063 #endif // defined(__cplusplus) && (__cplusplus >= 201703L) &&
0064 // (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
0065 // defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
0066 #endif // GSL_USE_STD_BYTE
0067
0068 #endif // _MSC_VER
0069
0070 // Use __may_alias__ attribute on gcc and clang
0071 #if defined __clang__ || (defined(__GNUC__) && __GNUC__ > 5)
0072 #define byte_may_alias __attribute__((__may_alias__))
0073 #else // defined __clang__ || defined __GNUC__
0074 #define byte_may_alias
0075 #endif // defined __clang__ || defined __GNUC__
0076
0077 #if GSL_USE_STD_BYTE
0078 #include <cstddef>
0079 #endif
0080
0081 namespace gsl
0082 {
0083 #if GSL_USE_STD_BYTE
0084
0085 namespace impl
0086 {
0087 // impl::byte is used by gsl::as_bytes so our own code does not trigger a deprecation warning as
0088 // would be the case when we used gsl::byte. Users of GSL should only use gsl::byte, not
0089 // gsl::impl::byte.
0090 using byte = std::byte;
0091 } // namespace impl
0092
0093 using byte GSL_DEPRECATED("Use std::byte instead.") = std::byte;
0094
0095 using std::to_integer;
0096
0097 #else // GSL_USE_STD_BYTE
0098
0099 // This is a simple definition for now that allows
0100 // use of byte within span<> to be standards-compliant
0101 enum class byte_may_alias byte : unsigned char
0102 {
0103 };
0104
0105 namespace impl
0106 {
0107 // impl::byte is used by gsl::as_bytes so our own code does not trigger a deprecation warning as
0108 // would be the case when we used gsl::byte. Users of GSL should only use gsl::byte, not
0109 // gsl::impl::byte.
0110 using byte = gsl::byte;
0111 } // namespace impl
0112
0113 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
0114 constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept
0115 {
0116 return b = byte(static_cast<unsigned char>(b) << shift);
0117 }
0118
0119 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
0120 constexpr byte operator<<(byte b, IntegerType shift) noexcept
0121 {
0122 return byte(static_cast<unsigned char>(b) << shift);
0123 }
0124
0125 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
0126 constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept
0127 {
0128 return b = byte(static_cast<unsigned char>(b) >> shift);
0129 }
0130
0131 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
0132 constexpr byte operator>>(byte b, IntegerType shift) noexcept
0133 {
0134 return byte(static_cast<unsigned char>(b) >> shift);
0135 }
0136
0137 constexpr byte& operator|=(byte& l, byte r) noexcept
0138 {
0139 return l = byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
0140 }
0141
0142 constexpr byte operator|(byte l, byte r) noexcept
0143 {
0144 return byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
0145 }
0146
0147 constexpr byte& operator&=(byte& l, byte r) noexcept
0148 {
0149 return l = byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
0150 }
0151
0152 constexpr byte operator&(byte l, byte r) noexcept
0153 {
0154 return byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
0155 }
0156
0157 constexpr byte& operator^=(byte& l, byte r) noexcept
0158 {
0159 return l = byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
0160 }
0161
0162 constexpr byte operator^(byte l, byte r) noexcept
0163 {
0164 return byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
0165 }
0166
0167 constexpr byte operator~(byte b) noexcept { return byte(~static_cast<unsigned char>(b)); }
0168
0169 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
0170 constexpr IntegerType to_integer(byte b) noexcept
0171 {
0172 return static_cast<IntegerType>(b);
0173 }
0174
0175 #endif // GSL_USE_STD_BYTE
0176
0177 template <typename T>
0178 constexpr gsl::impl::byte to_byte(T t) noexcept
0179 {
0180 static_assert(
0181 std::is_same<T, unsigned char>::value,
0182 "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
0183 "If you are calling to_byte with an integer constant use: gsl::to_byte<t>() version.");
0184 return gsl::impl::byte(t);
0185 }
0186
0187 template <int I>
0188 constexpr gsl::impl::byte to_byte() noexcept
0189 {
0190 static_assert(I >= 0 && I <= 255,
0191 "gsl::byte only has 8 bits of storage, values must be in range 0-255");
0192 return static_cast<gsl::impl::byte>(I);
0193 }
0194
0195 } // namespace gsl
0196
0197 #ifdef _MSC_VER
0198 #pragma warning(pop)
0199 #endif // _MSC_VER
0200
0201 #endif // GSL_BYTE_H