byte 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include <type_traits>
  19. #ifdef _MSC_VER
  20. #pragma warning(push)
  21. // Turn MSVC /analyze rules that generate too much noise. TODO: fix in the tool.
  22. #pragma warning(disable : 26493) // don't use c-style casts // TODO: MSVC suppression in templates
  23. // does not always work
  24. #ifndef GSL_USE_STD_BYTE
  25. // this tests if we are under MSVC and the standard lib has std::byte and it is enabled
  26. #if defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603
  27. #define GSL_USE_STD_BYTE 1
  28. #else // defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603
  29. #define GSL_USE_STD_BYTE 0
  30. #endif // defined(__cpp_lib_byte) && __cpp_lib_byte >= 201603
  31. #endif // GSL_USE_STD_BYTE
  32. #else // _MSC_VER
  33. #ifndef GSL_USE_STD_BYTE
  34. #include <cstddef> /* __cpp_lib_byte */
  35. // this tests if we are under GCC or Clang with enough -std=c++1z power to get us std::byte
  36. // also check if libc++ version is sufficient (> 5.0) or libstdc++ actually contains std::byte
  37. #if defined(__cplusplus) && (__cplusplus >= 201703L) && \
  38. (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) || \
  39. defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
  40. #define GSL_USE_STD_BYTE 1
  41. #else // defined(__cplusplus) && (__cplusplus >= 201703L) &&
  42. // (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
  43. // defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
  44. #define GSL_USE_STD_BYTE 0
  45. #endif // defined(__cplusplus) && (__cplusplus >= 201703L) &&
  46. // (defined(__cpp_lib_byte) && (__cpp_lib_byte >= 201603) ||
  47. // defined(_LIBCPP_VERSION) && (_LIBCPP_VERSION >= 5000))
  48. #endif // GSL_USE_STD_BYTE
  49. #endif // _MSC_VER
  50. // Use __may_alias__ attribute on gcc and clang
  51. #if defined __clang__ || (defined(__GNUC__) && __GNUC__ > 5)
  52. #define byte_may_alias __attribute__((__may_alias__))
  53. #else // defined __clang__ || defined __GNUC__
  54. #define byte_may_alias
  55. #endif // defined __clang__ || defined __GNUC__
  56. #if GSL_USE_STD_BYTE
  57. #include <cstddef>
  58. #endif
  59. namespace gsl
  60. {
  61. #if GSL_USE_STD_BYTE
  62. using std::byte;
  63. using std::to_integer;
  64. #else // GSL_USE_STD_BYTE
  65. // This is a simple definition for now that allows
  66. // use of byte within span<> to be standards-compliant
  67. enum class byte_may_alias byte : unsigned char
  68. {
  69. };
  70. template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
  71. constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept
  72. {
  73. return b = byte(static_cast<unsigned char>(b) << shift);
  74. }
  75. template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
  76. constexpr byte operator<<(byte b, IntegerType shift) noexcept
  77. {
  78. return byte(static_cast<unsigned char>(b) << shift);
  79. }
  80. template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
  81. constexpr byte& operator>>=(byte& b, IntegerType shift) noexcept
  82. {
  83. return b = byte(static_cast<unsigned char>(b) >> shift);
  84. }
  85. template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
  86. constexpr byte operator>>(byte b, IntegerType shift) noexcept
  87. {
  88. return byte(static_cast<unsigned char>(b) >> shift);
  89. }
  90. constexpr byte& operator|=(byte& l, byte r) noexcept
  91. {
  92. return l = byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
  93. }
  94. constexpr byte operator|(byte l, byte r) noexcept
  95. {
  96. return byte(static_cast<unsigned char>(l) | static_cast<unsigned char>(r));
  97. }
  98. constexpr byte& operator&=(byte& l, byte r) noexcept
  99. {
  100. return l = byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
  101. }
  102. constexpr byte operator&(byte l, byte r) noexcept
  103. {
  104. return byte(static_cast<unsigned char>(l) & static_cast<unsigned char>(r));
  105. }
  106. constexpr byte& operator^=(byte& l, byte r) noexcept
  107. {
  108. return l = byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
  109. }
  110. constexpr byte operator^(byte l, byte r) noexcept
  111. {
  112. return byte(static_cast<unsigned char>(l) ^ static_cast<unsigned char>(r));
  113. }
  114. constexpr byte operator~(byte b) noexcept { return byte(~static_cast<unsigned char>(b)); }
  115. template <class IntegerType, class = std::enable_if_t<std::is_integral<IntegerType>::value>>
  116. constexpr IntegerType to_integer(byte b) noexcept
  117. {
  118. return static_cast<IntegerType>(b);
  119. }
  120. #endif // GSL_USE_STD_BYTE
  121. template <typename T>
  122. // NOTE: need suppression since c++14 does not allow "return {t}"
  123. // GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
  124. constexpr byte to_byte(T t) noexcept
  125. {
  126. static_assert(std::is_same<T, unsigned char>::value,
  127. "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
  128. "If you are calling to_byte with an integer constant use: gsl::to_byte<t>() version.");
  129. return byte(t);
  130. }
  131. template <int I>
  132. constexpr byte to_byte() noexcept
  133. {
  134. static_assert(I >= 0 && I <= 255,
  135. "gsl::byte only has 8 bits of storage, values must be in range 0-255");
  136. return static_cast<byte>(I);
  137. }
  138. } // namespace gsl
  139. #ifdef _MSC_VER
  140. #pragma warning(pop)
  141. #endif // _MSC_VER
  142. #endif // GSL_BYTE_H