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 //
0021 // make suppress attributes work for some compilers
0022 // Hopefully temporary until suppression standardization occurs
0023 //
0024 #if defined(__clang__)
0025 #define GSL_SUPPRESS(x) [[gsl::suppress("x")]]
0026 #else
0027 #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
0028 #define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
0029 #else
0030 #define GSL_SUPPRESS(x)
0031 #endif // _MSC_VER
0032 #endif // __clang__
0033
0034 #include <type_traits>
0035
0036 // VS2017 15.8 added support for the __cpp_lib_byte definition
0037 // To do: drop _HAS_STD_BYTE when support for pre 15.8 expires
0038 #ifdef _MSC_VER
0039
0040 #pragma warning(push)
0041
0042 // Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
0043 #pragma warning(disable : 26493) // don't use c-style casts // TODO: MSVC suppression in templates
0044 // does not always work
0045
0046 #ifndef GSL_USE_STD_BYTE
0047 // this tests if we are under MSVC and the standard lib has std::byte and it is enabled
0048 #if (defined(_HAS_STD_BYTE) && _HAS_STD_BYTE) || \
0049 (defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603)
0050
0051 #define GSL_USE_STD_BYTE 1
0052
0053 #else // (defined(_HAS_STD_BYTE) && _HAS_STD_BYTE) || (defined(__cpp_lib_byte) && __cpp_lib_byte >=
0054 // 201603)
0055
0056 #define GSL_USE_STD_BYTE 0
0057
0058 #endif // (defined(_HAS_STD_BYTE) && _HAS_STD_BYTE) || (defined(__cpp_lib_byte) && __cpp_lib_byte >=
0059 // 201603)
0060 #endif // GSL_USE_STD_BYTE
0061
0062 #else // _MSC_VER
0063
0064 #ifndef GSL_USE_STD_BYTE
0065 #include <cstddef> /* __cpp_lib_byte */
0066 // this tests if we are under GCC or Clang with enough -std=c++1z power to get us std::byte
0067 // also check if libc++ version is sufficient (> 5.0) or libstdc++ actually contains std::byte
0068 #if defined(__cplusplus) && (__cplusplus >= 201703L) && \
0069 (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) || \
0070 defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
0071
0072 #define GSL_USE_STD_BYTE 1
0073
0074 #else // defined(__cplusplus) && (__cplusplus >= 201703L) &&
0075 // (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
0076 // defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
0077
0078 #define GSL_USE_STD_BYTE 0
0079
0080 #endif // defined(__cplusplus) && (__cplusplus >= 201703L) &&
0081 // (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
0082 // defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
0083 #endif // GSL_USE_STD_BYTE
0084
0085 #endif // _MSC_VER
0086
0087 // Use __may_alias__ attribute on gcc and clang
0088 #if defined __clang__ || (defined(__GNUC__) && __GNUC__ > 5)
0089 #define byte_may_alias __attribute__((__may_alias__))
0090 #else // defined __clang__ || defined __GNUC__
0091 #define byte_may_alias
0092 #endif // defined __clang__ || defined __GNUC__
0093
0094 #if GSL_USE_STD_BYTE
0095 #include <cstddef>
0096 #endif
0097
0098 namespace gsl
0099 {
0100 #if GSL_USE_STD_BYTE
0101
0102 using std::byte;
0103 using std::to_integer;
0104
0105 #else // GSL_USE_STD_BYTE
0106
0107 // This is a simple definition for now that allows
0108 // use of byte within span<> to be standards-compliant
0109 enum class byte_may_alias byte : unsigned char
0110 {
0111 };
0112
0113 template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
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, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
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, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
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, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
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, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
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 <bool E, typename T>
0178 constexpr byte to_byte_impl(T t) noexcept
0179 {
0180 static_assert(
0181 E, "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
0182 "If you are calling to_byte with an integer contant use: gsl::to_byte<t>() version.");
0183 return static_cast<byte>(t);
0184 }
0185 template <>
0186 // NOTE: need suppression since c++14 does not allow "return {t}"
0187 // GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
0188 constexpr byte to_byte_impl<true, unsigned char>(unsigned char t) noexcept
0189 {
0190 return byte(t);
0191 }
0192
0193 template <typename T>
0194 constexpr byte to_byte(T t) noexcept
0195 {
0196 return to_byte_impl<std::is_same<T, unsigned char>::value, T>(t);
0197 }
0198
0199 template <int I>
0200 constexpr byte to_byte() noexcept
0201 {
0202 static_assert(I >= 0 && I <= 255,
0203 "gsl::byte only has 8 bits of storage, values must be in range 0-255");
0204 return static_cast<byte>(I);
0205 }
0206
0207 } // namespace gsl
0208
0209 #ifdef _MSC_VER
0210 #pragma warning(pop)
0211 #endif // _MSC_VER
0212
0213 #endif // GSL_BYTE_H