Skip to content

Commit 2828399

Browse files
beinhaerterWerner Henze
andauthored
replace BYTE_TYPE with a solution based on namespaces (#1201)
- A macro with the very generic name `BYTE_TYPE` is likely to collide with existing code, so get rid of the macro. - The new solution is to provide a non-deprecated `byte` in the namespace `gsl::impl`. - Users of GSL should use `gsl::byte`, which is still deprecated when mapped to a `std::std::byte`. - GSL types and functions need to use `gsl::impl::byte` so they do not trigger the deprecation warning. - The `gsl::impl::byte` return type in an exported function is not nice, it might mislead users to use that type in their own declarations. But the `BYTE_TYPE` solution is not better in this respect. Co-authored-by: Werner Henze <w.henze@avm.de>
1 parent c5fbb81 commit 2828399

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

include/gsl/byte

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,6 @@
7474
#define byte_may_alias
7575
#endif // defined __clang__ || defined __GNUC__
7676

77-
#if GSL_USE_STD_BYTE
78-
#define BYTE_TYPE std::byte
79-
#else // !GSL_USE_STD_BYTE
80-
#define BYTE_TYPE byte
81-
#endif // GSL_USE_STD_BYTE
82-
8377
#if GSL_USE_STD_BYTE
8478
#include <cstddef>
8579
#endif
@@ -88,6 +82,12 @@ namespace gsl
8882
{
8983
#if GSL_USE_STD_BYTE
9084

85+
namespace impl {
86+
// 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.
87+
// Users of GSL should only use gsl::byte, not gsl::impl::byte.
88+
using byte = std::byte;
89+
}
90+
9191
using byte GSL_DEPRECATED("Use std::byte instead.") = std::byte;
9292

9393
using std::to_integer;
@@ -100,6 +100,12 @@ enum class byte_may_alias byte : unsigned char
100100
{
101101
};
102102

103+
namespace impl {
104+
// 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.
105+
// Users of GSL should only use gsl::byte, not gsl::impl::byte.
106+
using byte = gsl::byte;
107+
}
108+
103109
template <class IntegerType, std::enable_if_t<std::is_integral<IntegerType>::value, bool> = true>
104110
constexpr byte& operator<<=(byte& b, IntegerType shift) noexcept
105111
{
@@ -168,20 +174,20 @@ constexpr IntegerType to_integer(byte b) noexcept
168174
template <typename T>
169175
// NOTE: need suppression since c++14 does not allow "return {t}"
170176
// GSL_SUPPRESS(type.4) // NO-FORMAT: attribute // TODO: suppression does not work
171-
constexpr BYTE_TYPE to_byte(T t) noexcept
177+
constexpr gsl::impl::byte to_byte(T t) noexcept
172178
{
173179
static_assert(std::is_same<T, unsigned char>::value,
174180
"gsl::to_byte(t) must be provided an unsigned char, otherwise data loss may occur. "
175181
"If you are calling to_byte with an integer constant use: gsl::to_byte<t>() version.");
176-
return BYTE_TYPE(t);
182+
return gsl::impl::byte(t);
177183
}
178184

179185
template <int I>
180-
constexpr BYTE_TYPE to_byte() noexcept
186+
constexpr gsl::impl::byte to_byte() noexcept
181187
{
182188
static_assert(I >= 0 && I <= 255,
183189
"gsl::byte only has 8 bits of storage, values must be in range 0-255");
184-
return static_cast<BYTE_TYPE>(I);
190+
return static_cast<gsl::impl::byte>(I);
185191
}
186192

187193
} // namespace gsl

include/gsl/span

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#define GSL_SPAN_H
1919

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

@@ -824,28 +824,28 @@ namespace details
824824

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

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

838838
template <class ElementType, std::size_t Extent,
839839
std::enable_if_t<!std::is_const<ElementType>::value, int> = 0>
840-
span<BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>
840+
span<gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>
841841
as_writable_bytes(span<ElementType, Extent> s) noexcept
842842
{
843-
using type = span<BYTE_TYPE, details::calculate_byte_size<ElementType, Extent>::value>;
843+
using type = span<gsl::impl::byte, details::calculate_byte_size<ElementType, Extent>::value>;
844844

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

851851
} // namespace gsl

0 commit comments

Comments
 (0)