gsl_byte 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #ifndef GSL_BYTE_H
  17. #define GSL_BYTE_H
  18. //
  19. // make suppress attributes work for some compilers
  20. // Hopefully temporary until suppression standardization occurs
  21. //
  22. #if defined(__clang__)
  23. #define GSL_SUPPRESS(x) [[gsl::suppress("x")]]
  24. #else
  25. #if defined(_MSC_VER)
  26. #define GSL_SUPPRESS(x) [[gsl::suppress(x)]]
  27. #else
  28. #define GSL_SUPPRESS(x)
  29. #endif // _MSC_VER
  30. #endif // __clang__
  31. #include <type_traits>
  32. // VS2017 15.8 added support for the __cpp_lib_byte definition
  33. // To do: drop _HAS_STD_BYTE when support for pre 15.8 expires
  34. #ifdef _MSC_VER
  35. #pragma warning(push)
  36. // Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
  37. #pragma warning(disable : 26493) // don't use c-style casts // TODO: MSVC suppression in templates does not always work
  38. #ifndef GSL_USE_STD_BYTE
  39. // this tests if we are under MSVC and the standard lib has std::byte and it is enabled
  40. #if (defined(_HAS_STD_BYTE) && _HAS_STD_BYTE) || (defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603)
  41. #define GSL_USE_STD_BYTE 1
  42. #else // (defined(_HAS_STD_BYTE) && _HAS_STD_BYTE) || (defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603)
  43. #define GSL_USE_STD_BYTE 0
  44. #endif // (defined(_HAS_STD_BYTE) && _HAS_STD_BYTE) || (defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603)
  45. #endif // GSL_USE_STD_BYTE
  46. #else // _MSC_VER
  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 libstc++ 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. #define GSL_USE_STD_BYTE 1
  55. #else // defined(__cplusplus) && (__cplusplus >= 201703L) &&
  56. // (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
  57. // defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
  58. #define GSL_USE_STD_BYTE 0
  59. #endif //defined(__cplusplus) && (__cplusplus >= 201703L) &&
  60. // (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
  61. // defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
  62. #endif // GSL_USE_STD_BYTE
  63. #endif // _MSC_VER
  64. // Use __may_alias__ attribute on gcc and clang
  65. #if defined __clang__ || (defined(__GNUC__) && __GNUC__ > 5)
  66. #define byte_may_alias __attribute__((__may_alias__))
  67. #else // defined __clang__ || defined __GNUC__
  68. #define byte_may_alias
  69. #endif // defined __clang__ || defined __GNUC__
  70. #if GSL_USE_STD_BYTE
  71. #include <cstddef>
  72. #endif
  73. namespace gsl
  74. {
  75. #if GSL_USE_STD_BYTE
  76. using std::byte;
  77. using std::to_integer;
  78. #else // GSL_USE_STD_BYTE
  79. // This is a simple definition for now that allows
  80. // use of byte within span<> to be standards-compliant
  81. enum class byte_may_alias byte : unsigned char
  82. {
  83. };
  84. template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
  85. constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept
  86. {
  87. return b = byte(static_cast<unsigned char>(b) << shift);
  88. }
  89. template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
  90. constexpr byte operator<<(byte b, IntegerType shift) noexcept
  91. {
  92. return byte(static_cast<unsigned char>(b) << shift);
  93. }
  94. template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
  95. constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept
  96. {
  97. return b = byte(static_cast<unsigned char>(b) >> shift);
  98. }
  99. template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
  100. constexpr byte operator>>(byte b, IntegerType shift) noexcept
  101. {
  102. return byte(static_cast<unsigned char>(b) >> shift);
  103. }
  104. constexpr byte& operator|=(byte& l, byte r) noexcept
  105. {
  106. return l = byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
  107. }
  108. constexpr byte operator|(byte l, byte r) noexcept
  109. {
  110. return byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
  111. }
  112. constexpr byte& operator&=(byte& l, byte r) noexcept
  113. {
  114. return l = byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
  115. }
  116. constexpr byte operator&(byte l, byte r) noexcept
  117. {
  118. return byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
  119. }
  120. constexpr byte& operator^=(byte& l, byte r) noexcept
  121. {
  122. return l = byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
  123. }
  124. constexpr byte operator^(byte l, byte r) noexcept
  125. {
  126. return byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
  127. }
  128. constexpr byte operator~(byte b) noexcept { return byte(~static_cast<unsigned char>(b)); }
  129. template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
  130. constexpr IntegerType to_integer(byte b) noexcept
  131. {
  132. return static_cast<IntegerType>(b);
  133. }
  134. #endif // GSL_USE_STD_BYTE
  135. template <bool E, typename T>
  136. constexpr byte to_byte_impl(T t) noexcept
  137. {
  138. static_assert(
  139. E, "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
  140. "If you are calling to_byte with an integer contant use: gsl::to_byte<t>() version.");
  141. return static_cast<byte>(t);
  142. }
  143. template <>
  144. // NOTE: need suppression since c++14 does not allow "return {t}"
  145. // GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
  146. constexpr byte to_byte_impl<true, unsigned char>(unsigned char t) noexcept
  147. {
  148. return byte(t);
  149. }
  150. template <typename T>
  151. constexpr byte to_byte(T t) noexcept
  152. {
  153. return to_byte_impl<std::is_same<T, unsigned char>::value, T>(t);
  154. }
  155. template <int I>
  156. constexpr byte to_byte() noexcept
  157. {
  158. static_assert(I >= 0 && I <= 255,
  159. "gsl::byte only has 8 bits of storage, values must be in range 0-255");
  160. return static_cast<byte>(I);
  161. }
  162. } // namespace gsl
  163. #ifdef _MSC_VER
  164. #pragma warning(pop)
  165. #endif // _MSC_VER
  166. #endif // GSL_BYTE_H