Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace BYTE_TYPE with a solution based on namespaces #1201

Merged
merged 1 commit into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions include/gsl/byte
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@
#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 <cstddef>
#endif
Expand All @@ -88,6 +82,12 @@ namespace gsl
{
#if GSL_USE_STD_BYTE

namespace impl {
// 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.
// Users of GSL should only use gsl::byte, not gsl::impl::byte.
using byte = std::byte;
}

using byte GSL_DEPRECATED("Use std::byte instead.") = std::byte;

using std::to_integer;
Expand All @@ -100,6 +100,12 @@ enum class byte_may_alias byte : unsigned char
{
};

namespace impl {
// 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.
// Users of GSL should only use gsl::byte, not gsl::impl::byte.
using byte = gsl::byte;
}

template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept
{
Expand Down Expand Up @@ -168,20 +174,20 @@ constexpr IntegerType to_integer(byte b) noexcept
template <typename T>
// 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_TYPE to_byte(T t) noexcept
constexpr gsl::impl::byte to_byte(T t) noexcept
{
static_assert(std::is_same<T, unsigned char>::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<t>() version.");
return BYTE_TYPE(t);
return gsl::impl::byte(t);
}

template <int I>
constexpr BYTE_TYPE to_byte() noexcept
constexpr gsl::impl::byte 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<BYTE_TYPE>(I);
return static_cast<gsl::impl::byte>(I);
}

} // namespace gsl
Expand Down
14 changes: 7 additions & 7 deletions include/gsl/span
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define GSL_SPAN_H

#include "./assert" // for Expects
#include "./byte" // for BYTE_TYPE
#include "./byte" // for gsl::impl::byte
#include "./span_ext" // for span specialization of gsl::at and other span-related extensions
#include "./util" // for narrow_cast

Expand Down Expand Up @@ -824,28 +824,28 @@ namespace details

// [span.objectrep], views of object representation
template <class ElementType, std::size_t Extent>
span<const BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>
span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>
as_bytes(span<ElementType, Extent> s) noexcept
{
using type = span<const BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>;
using type = span<const gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;

// clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format on
return type{reinterpret_cast<const BYTE_TYPE*>(s.data()), s.size_bytes()};
return type{reinterpret_cast<const gsl::impl::byte*>(s.data()), s.size_bytes()};
}

template <class ElementType, std::size_t Extent,
std::enable_if_t<!std::is_const<ElementType>::value, int> = 0>
span<BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>
span<gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>
as_writable_bytes(span<ElementType, Extent> s) noexcept
{
using type = span<BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>;
using type = span<gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;

// clang-format off
GSL_SUPPRESS(type.1) // NO-FORMAT: attribute
// clang-format on
return type{reinterpret_cast<BYTE_TYPE*>(s.data()), s.size_bytes()};
return type{reinterpret_cast<gsl::impl::byte*>(s.data()), s.size_bytes()};
}

} // namespace gsl
Expand Down