15#define CONSTEXPR_IF_NOT_DEBUG
16#define ASSERT_IF_DEBUG(x) assert((x))
18#define CONSTEXPR_IF_NOT_DEBUG constexpr
19#define ASSERT_IF_DEBUG(x)
23#if __has_attribute(lifetimebound)
24#define SPAN_ATTR_LIFETIMEBOUND [[clang::lifetimebound]]
26#define SPAN_ATTR_LIFETIMEBOUND
29#define SPAN_ATTR_LIFETIMEBOUND
93template <
typename C>
class Span {
97 template <
class T>
struct is_Span_int :
public std::false_type {};
111 template <
typename T,
112 typename std::enable_if<
113 std::is_convertible<T (*)[], C (*)[]>::value,
int>::type = 0>
123 template <
typename T,
124 typename std::enable_if<
125 std::is_convertible<T (*)[], C (*)[]>::value,
int>::type = 0>
142 template <
typename O,
143 typename std::enable_if<
144 std::is_convertible<O (*)[], C (*)[]>::value,
int>::type = 0>
170 template <
typename V>
173 typename std::enable_if<
176 typename std::remove_pointer<
177 decltype(std::declval<V &>().
data())>::type (*)[],
179 std::is_convertible<
decltype(std::declval<V &>().
size()),
181 std::nullptr_t>::type =
nullptr)
184 template <
typename V>
187 typename std::enable_if<
190 typename std::remove_pointer<
191 decltype(std::declval<const V &>().
data())>::type (*)[],
193 std::is_convertible<
decltype(std::declval<const V &>().
size()),
195 std::nullptr_t>::type =
nullptr)
209 constexpr std::size_t
size() const noexcept {
return m_size; }
211 return sizeof(C) *
m_size;
213 constexpr bool empty() const noexcept {
return size() == 0; }
223 std::size_t
count)
const noexcept {
237 return a.size() == b.size() &&
238 std::equal(a.begin(), a.end(), b.begin());
244 return std::lexicographical_compare(a.begin(), a.end(), b.begin(),
257 template <
typename O>
friend class Span;
262template <
typename T,
typename EndOrSize>
Span(T *, EndOrSize) ->
Span<T>;
269 !std::is_lvalue_reference_v<T>,
270 const std::remove_pointer_t<decltype(std::declval<T &&>().
data())>>>;
287 return reinterpret_cast<const std::byte *
>(
data);
290 return reinterpret_cast<std::byte *
>(
data);
295 return {
BytePtr(s.data()), s.size_bytes()};
298 return {
BytePtr(s.data()), s.size_bytes()};
322 return reinterpret_cast<const uint8_t *
>(c);
328 ->
Span<
typename std::remove_pointer<
decltype(
UCharCast(s.data()))>::type> {
A Span is an object that can refer to a contiguous sequence of objects.
CONSTEXPR_IF_NOT_DEBUG Span< C > last(std::size_t count) const noexcept
CONSTEXPR_IF_NOT_DEBUG C & back() const noexcept
constexpr std::size_t size() const noexcept
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset) const noexcept
constexpr Span(C(&a)[N]) noexcept
Construct a Span from an array.
constexpr Span() noexcept
CONSTEXPR_IF_NOT_DEBUG C & operator[](std::size_t pos) const noexcept
constexpr Span(const V &other SPAN_ATTR_LIFETIMEBOUND, typename std::enable_if< !is_Span< V >::value &&std::is_convertible< typename std::remove_pointer< decltype(std::declval< const V & >().data())>::type(*)[], C(*)[]>::value &&std::is_convertible< decltype(std::declval< const V & >().size()), std::size_t >::value, std::nullptr_t >::type=nullptr)
friend constexpr bool operator<(const Span &a, const Span &b) noexcept
CONSTEXPR_IF_NOT_DEBUG Span< C > first(std::size_t count) const noexcept
constexpr C * data() const noexcept
friend constexpr bool operator<=(const Span &a, const Span &b) noexcept
constexpr Span(T *begin, std::size_t size) noexcept
Construct a span from a begin pointer and a size.
CONSTEXPR_IF_NOT_DEBUG Span(T *begin, T *end) noexcept
Construct a span from a begin and end pointer.
friend constexpr bool operator>(const Span &a, const Span &b) noexcept
friend constexpr bool operator==(const Span &a, const Span &b) noexcept
constexpr Span(const Span &) noexcept=default
Default copy constructor.
constexpr std::size_t size_bytes() const noexcept
Span & operator=(const Span &other) noexcept=default
Default assignment operator.
constexpr C * begin() const noexcept
friend constexpr bool operator!=(const Span &a, const Span &b) noexcept
constexpr bool empty() const noexcept
constexpr C * end() const noexcept
friend constexpr bool operator>=(const Span &a, const Span &b) noexcept
CONSTEXPR_IF_NOT_DEBUG C & front() const noexcept
CONSTEXPR_IF_NOT_DEBUG Span< C > subspan(std::size_t offset, std::size_t count) const noexcept
constexpr Span(const Span< O > &other) noexcept
Implicit conversion of spans between compatible types.
constexpr Span(V &other SPAN_ATTR_LIFETIMEBOUND, typename std::enable_if< !is_Span< V >::value &&std::is_convertible< typename std::remove_pointer< decltype(std::declval< V & >().data())>::type(*)[], C(*)[]>::value &&std::is_convertible< decltype(std::declval< V & >().size()), std::size_t >::value, std::nullptr_t >::type=nullptr)
Construct a Span for objects with .data() and .size() (std::string, std::array, std::vector,...
Span< std::byte > AsWritableBytes(Span< T > s) noexcept
#define ASSERT_IF_DEBUG(x)
const std::byte * BytePtr(const void *data)
Convert a data pointer to a std::byte data pointer.
Span< const std::byte > MakeByteSpan(V &&v) noexcept
#define SPAN_ATTR_LIFETIMEBOUND
uint8_t * UCharCast(char *c)
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(Span{std::forward< V >(v)}))
Like the Span constructor, but for (const) uint8_t member types only.
T & SpanPopBack(Span< T > &span)
Pop the last element off a span, and return a reference to that element.
Span(T *, EndOrSize) -> Span< T >
Span< const std::byte > AsBytes(Span< T > s) noexcept
constexpr auto UCharSpanCast(Span< T > s) -> Span< typename std::remove_pointer< decltype(UCharCast(s.data()))>::type >
#define CONSTEXPR_IF_NOT_DEBUG
Span< std::byte > MakeWritableByteSpan(V &&v) noexcept