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 // 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.
0087 // Users of GSL should only use gsl::byte, not gsl::impl::byte.
0088 using byte = std::byte;
0089 }
0090
0091 using byte GSL_DEPRECATED("Use std::byte instead.") = std::byte;
0092
0093 using std::to_integer;
0094
0095 #else // GSL_USE_STD_BYTE
0096
0097 // This is a simple definition for now that allows
0098 // use of byte within span<> to be standards-compliant
0099 enum class byte_may_alias byte : unsigned char
0100 {
0101 };
0102
0103 namespace impl {
0104 // 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.
0105 // Users of GSL should only use gsl::byte, not gsl::impl::byte.
0106 using byte = gsl::byte;
0107 }
0108
0109 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
0110 constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept
0111 {
0112 return b = byte(static_cast<unsigned char>(b) << shift);
0113 }
0114
0115 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
0116 constexpr byte operator<<(byte b, IntegerType shift) noexcept
0117 {
0118 return byte(static_cast<unsigned char>(b) << shift);
0119 }
0120
0121 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
0122 constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept
0123 {
0124 return b = byte(static_cast<unsigned char>(b) >> shift);
0125 }
0126
0127 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
0128 constexpr byte operator>>(byte b, IntegerType shift) noexcept
0129 {
0130 return byte(static_cast<unsigned char>(b) >> shift);
0131 }
0132
0133 constexpr byte& operator|=(byte& l, byte r) noexcept
0134 {
0135 return l = byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
0136 }
0137
0138 constexpr byte operator|(byte l, byte r) noexcept
0139 {
0140 return byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
0141 }
0142
0143 constexpr byte& operator&=(byte& l, byte r) noexcept
0144 {
0145 return l = byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
0146 }
0147
0148 constexpr byte operator&(byte l, byte r) noexcept
0149 {
0150 return byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
0151 }
0152
0153 constexpr byte& operator^=(byte& l, byte r) noexcept
0154 {
0155 return l = byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
0156 }
0157
0158 constexpr byte operator^(byte l, byte r) noexcept
0159 {
0160 return byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
0161 }
0162
0163 constexpr byte operator~(byte b) noexcept { return byte(~static_cast<unsigned char>(b)); }
0164
0165 template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
0166 constexpr IntegerType to_integer(byte b) noexcept
0167 {
0168 return static_cast<IntegerType>(b);
0169 }
0170
0171 #endif // GSL_USE_STD_BYTE
0172
0173
0174 template <typename T>
0175 // NOTE: need suppression since c++14 does not allow "return {t}"
0176 // GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
0177 constexpr gsl::impl::byte to_byte(T t) noexcept
0178 {
0179 static_assert(std::is_same<T, unsigned char>::value,
0180 "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
0181 "If you are calling to_byte with an integer constant use: gsl::to_byte<t>() version.");
0182 return gsl::impl::byte(t);
0183 }
0184
0185 template <int I>
0186 constexpr gsl::impl::byte to_byte() noexcept
0187 {
0188 static_assert(I >= 0 && I <= 255,
0189 "gsl::byte only has 8 bits of storage, values must be in range 0-255");
0190 return static_cast<gsl::impl::byte>(I);
0191 }
0192
0193 } // namespace gsl
0194
0195 #ifdef _MSC_VER
0196 #pragma warning(pop)
0197 #endif // _MSC_VER
0198
0199 #endif // GSL_BYTE_H