From 7b3165edbe1a96863f9369d50a9778bd810c431f Mon Sep 17 00:00:00 2001 From: Carson Radtke Date: Thu, 13 Feb 2025 21:57:00 -0600 Subject: [PATCH] do not use gsl::byte if it is deprecated --- include/gsl/byte | 15 +++++++++++---- include/gsl/span | 14 +++++++------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/include/gsl/byte b/include/gsl/byte index dcef27a..7e4c622 100644 --- a/include/gsl/byte +++ b/include/gsl/byte @@ -74,6 +74,12 @@ #define byte_may_alias #endif // defined __clang__ || defined __GNUC__ +#if GSL_USE_STD_BYTE +#define BYTE_TYPE std::byte +#else // !GSL_USE_STD_BYTE +#define BYTE_TYPE byte +#endif // GSL_USE_STD_BYTE + #if GSL_USE_STD_BYTE #include #endif @@ -158,23 +164,24 @@ constexpr IntegerType to_integer(byte b) noexcept #endif // GSL_USE_STD_BYTE + template // NOTE: need suppression since c++14 does not allow "return {t}" // GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work -constexpr byte to_byte(T t) noexcept +constexpr BYTE_TYPE to_byte(T t) noexcept { static_assert(std::is_same::value, "gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. " "If you are calling to_byte with an integer constant use: gsl::to_byte() version."); - return byte(t); + return BYTE_TYPE(t); } template -constexpr byte to_byte() noexcept +constexpr BYTE_TYPE to_byte() noexcept { static_assert(I >= 0 && I <= 255, "gsl::byte only has 8 bits of storage, values must be in range 0-255"); - return static_cast(I); + return static_cast(I); } } // namespace gsl diff --git a/include/gsl/span b/include/gsl/span index 03f4aaf..42b083c 100644 --- a/include/gsl/span +++ b/include/gsl/span @@ -18,7 +18,7 @@ #define GSL_SPAN_H #include "./assert" // for Expects -#include "./byte" // for byte +#include "./byte" // for BYTE_TYPE #include "./span_ext" // for span specialization of gsl::at and other span-related extensions #include "./util" // for narrow_cast @@ -824,28 +824,28 @@ namespace details // [span.objectrep], views of object representation template -span::value> +span::value> as_bytes(span s) noexcept { - using type = span::value>; + using type = span::value>; // clang-format off GSL_SUPPRESS(type.1) // NO-FORMAT: attribute // clang-format on - return type{reinterpret_cast(s.data()), s.size_bytes()}; + return type{reinterpret_cast(s.data()), s.size_bytes()}; } template ::value, int> = 0> -span::value> +span::value> as_writable_bytes(span s) noexcept { - using type = span::value>; + using type = span::value>; // clang-format off GSL_SUPPRESS(type.1) // NO-FORMAT: attribute // clang-format on - return type{reinterpret_cast(s.data()), s.size_bytes()}; + return type{reinterpret_cast(s.data()), s.size_bytes()}; } } // namespace gsl