4#include "../Foundation/InitializerList.h"
5#include "../Foundation/TypeTraits.h"
9template <
typename Type>
18template <
typename Type>
22 using VoidType =
typename TypeTraits::SameConstnessAs<Type, void>::type;
25 constexpr Span(Type (&_items)[N]) : items(_items), sizeElements(N)
29 constexpr Span() : items(nullptr), sizeElements(0) {}
38 constexpr Span(Type& type) : items(&type), sizeElements(1) {}
42 constexpr Span(std::initializer_list<Type> list) : items(nullptr), sizeElements(0)
46 sizeElements = list.size();
59 return {
reinterpret_cast<Type*
>(&value),
sizeof(T) /
sizeof(Type)};
68 return Span(
reinterpret_cast<Type*
>(rawMemory),
sizeInBytes /
sizeof(Type));
80 [[nodiscard]]
constexpr const Type*
begin()
const {
return items; }
84 [[nodiscard]]
constexpr const Type*
end()
const {
return items + sizeElements; }
88 [[nodiscard]]
constexpr const Type*
data()
const {
return items; }
92 [[nodiscard]]
constexpr Type*
begin() {
return items; }
96 [[nodiscard]]
constexpr Type*
end() {
return items + sizeElements; }
100 [[nodiscard]]
constexpr Type*
data() {
return items; }
108 [[nodiscard]]
constexpr SizeType
sizeInBytes()
const {
return sizeElements *
sizeof(Type); }
116 [[nodiscard]]
constexpr bool sliceStart(SizeType offsetInElements,
Span& destination)
const
133 [[nodiscard]]
constexpr bool sliceStartLength(SizeType offsetInElements, SizeType lengthInElements,
134 Span& destination)
const
138 destination =
Span(items + offsetInElements, lengthInElements);
146 [[nodiscard]]
constexpr bool empty()
const {
return sizeElements == 0; }
148 [[nodiscard]]
constexpr bool contains(
const Type& type, SizeType* index =
nullptr)
const
150 for (SizeType idx = 0; idx < sizeElements; ++idx)
152 if (items[idx] == type)
164 Type& operator[](SizeType idx) {
return items[idx]; }
166 const Type& operator[](SizeType idx)
const {
return items[idx]; }
169 template <
typename IntType>
172 if (idx >= 0 and idx <
static_cast<IntType
>(sizeElements))
178 template <
typename IntType>
179 const Type*
get(IntType idx)
const
181 if (idx >= 0 and idx <
static_cast<IntType
>(sizeElements))
188 SizeType sizeElements;
unsigned long size_t
Platform independent unsigned size type.
Definition: PrimitiveTypes.h:56
View over a contiguous sequence of items (pointer + size in elements).
Definition: Span.h:20
static Span< Type > reinterpret_object(T &value)
Constructs a Span reinterpreting memory pointed by object of type T as a type Type
Definition: Span.h:57
constexpr const Type * data() const
Returns pointer to first element of the span.
Definition: Span.h:88
static Span< Type > reinterpret_bytes(VoidType *rawMemory, SizeType sizeInBytes)
Construct a span reinterpreting raw memory (void* or const void*) to Type or const Type
Definition: Span.h:66
const Type * get(IntType idx) const
Gets the item at given index or nullptr if index is negative or bigger than size.
Definition: Span.h:179
constexpr SizeType sizeInBytes() const
Size of Span in bytes.
Definition: Span.h:108
constexpr Span(std::initializer_list< Type > list)
Span specialized constructor (mainly used for converting const char* to StringView)
Definition: Span.h:42
constexpr bool sliceStart(SizeType offsetInElements, Span &destination) const
Creates another Span, starting at an offset in elements from current Span, until end.
Definition: Span.h:116
constexpr Span()
Builds an empty Span.
Definition: Span.h:29
constexpr Type * begin()
Returns pointer to first element of the span.
Definition: Span.h:92
constexpr Type * end()
Returns pointer to one after the last element of the span.
Definition: Span.h:96
constexpr const Type * begin() const
Returns pointer to first element of the span.
Definition: Span.h:80
Type * get(IntType idx)
Gets the item at given index or nullptr if index is negative or bigger than size.
Definition: Span.h:170
constexpr bool sliceStartLength(SizeType offsetInElements, SizeType lengthInElements, Span &destination) const
Creates another Span, starting at an offset in elements from current Span of specified length.
Definition: Span.h:133
constexpr Span(Type *items, SizeType sizeInElements)
Builds a Span from an array.
Definition: Span.h:34
Span< const T > reinterpret_as_array_of() const
Reinterprets the current span as an array of the specified type.
Definition: Span.h:73
constexpr Type * data()
Returns pointer to first element of the span.
Definition: Span.h:100
constexpr bool empty() const
Check if Span is empty.
Definition: Span.h:146
constexpr SizeType sizeInElements() const
Size of Span in elements.
Definition: Span.h:104
constexpr const Type * end() const
Returns pointer to one after the last element of the span.
Definition: Span.h:84
constexpr Span(Type &type)
Builds a Span from a single object.
Definition: Span.h:38