4#include "../Foundation/InitializerList.h"
5#include "../Foundation/LibC.h"
6#include "../Foundation/TypeTraits.h"
10template <
typename Type>
22template <
typename Type>
26 using VoidType =
typename TypeTraits::SameConstnessAs<Type, void>::type;
29 constexpr Span(Type (&_items)[N]) : items(_items), sizeElements(N)
33 constexpr Span() : items(nullptr), sizeElements(0) {}
42 constexpr Span(Type& type) : items(&type), sizeElements(1) {}
46 constexpr Span(std::initializer_list<Type> list) : items(nullptr), sizeElements(0)
50 sizeElements = list.size();
63 return {
reinterpret_cast<Type*
>(&value),
sizeof(T) /
sizeof(Type)};
72 return Span(
reinterpret_cast<Type*
>(rawMemory),
sizeInBytes /
sizeof(Type));
91 [[nodiscard]]
constexpr const Type*
begin()
const {
return items; }
95 [[nodiscard]]
constexpr const Type*
end()
const {
return items + sizeElements; }
99 [[nodiscard]]
constexpr const Type*
data()
const {
return items; }
103 [[nodiscard]]
constexpr Type*
begin() {
return items; }
107 [[nodiscard]]
constexpr Type*
end() {
return items + sizeElements; }
111 [[nodiscard]]
constexpr Type*
data() {
return items; }
119 [[nodiscard]]
constexpr SizeType
sizeInBytes()
const {
return sizeElements *
sizeof(Type); }
127 [[nodiscard]]
constexpr bool sliceStart(SizeType offsetInElements,
Span& destination)
const
144 [[nodiscard]]
constexpr bool sliceStartLength(SizeType offsetInElements, SizeType lengthInElements,
145 Span& destination)
const
149 destination =
Span(items + offsetInElements, lengthInElements);
160 const auto diff = other.items - items;
161 if (diff < 0 or
static_cast<SizeType
>(diff) >
sizeInBytes())
167 output =
Span(items,
static_cast<SizeType
>(diff) /
sizeof(Type));
174 [[nodiscard]]
constexpr bool empty()
const {
return sizeElements == 0; }
176 [[nodiscard]]
constexpr bool contains(
const Type& type, SizeType* index =
nullptr)
const
178 for (SizeType idx = 0; idx < sizeElements; ++idx)
180 if (items[idx] == type)
192 Type& operator[](SizeType idx) {
return items[idx]; }
194 const Type& operator[](SizeType idx)
const {
return items[idx]; }
197 template <
typename IntType>
200 if (idx >= 0 and idx <
static_cast<IntType
>(sizeElements))
206 template <
typename IntType>
207 const Type*
get(IntType idx)
const
209 if (idx >= 0 and idx <
static_cast<IntType
>(sizeElements))
215 template <
typename U>
226 template <
typename U>
238 SizeType sizeElements;
241#if SC_PLATFORM_WINDOWS
242#define SC_NATIVE_STR(str) L##str
244#define SC_NATIVE_STR(str) str
251 constexpr SpanStringView(
const char*
string,
size_t stringLength) : text(
string, stringLength) {}
253 constexpr SpanStringView(
const char (&charLiteral)[N]) : text(charLiteral)
260 if (N < text.sizeInElements() + 1)
262 ::memcpy(&buffer[0], text.data(), text.sizeInElements());
274 constexpr SpanString(
char (&buffer)[N]) : text(buffer)
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:24
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:61
constexpr const Type * data() const
Returns pointer to first element of the span.
Definition: Span.h:99
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:70
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:207
constexpr SizeType sizeInBytes() const
Size of Span in bytes.
Definition: Span.h:119
bool memcmpWith(const Span< U > other) const
Compares this span with another one byte by byte.
Definition: Span.h:216
constexpr Span(std::initializer_list< Type > list)
Span specialized constructor (mainly used for converting const char* to StringView)
Definition: Span.h:46
Span< T > reinterpret_as_array_of()
Reinterprets the current span as an array of the specified type.
Definition: Span.h:84
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:127
constexpr Span()
Builds an empty Span.
Definition: Span.h:33
constexpr Type * begin()
Returns pointer to first element of the span.
Definition: Span.h:103
constexpr Type * end()
Returns pointer to one after the last element of the span.
Definition: Span.h:107
constexpr const Type * begin() const
Returns pointer to first element of the span.
Definition: Span.h:91
Type * get(IntType idx)
Gets the item at given index or nullptr if index is negative or bigger than size.
Definition: Span.h:198
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:144
bool memcpyTo(Span< U > &other) const
Bitwise copies contents of this Span over another (non-overlapping)
Definition: Span.h:227
constexpr Span(Type *items, SizeType sizeInElements)
Builds a Span from an array.
Definition: Span.h:38
Span< const T > reinterpret_as_array_of() const
Reinterprets the current span as an array of the specified type.
Definition: Span.h:77
constexpr Type * data()
Returns pointer to first element of the span.
Definition: Span.h:111
constexpr bool empty() const
Check if Span is empty.
Definition: Span.h:174
constexpr SizeType sizeInElements() const
Size of Span in elements.
Definition: Span.h:115
constexpr const Type * end() const
Returns pointer to one after the last element of the span.
Definition: Span.h:95
constexpr Span(Type &type)
Builds a Span from a single object.
Definition: Span.h:42
const bool sliceFromStartUntil(Span other, Span &output) const
Creates another Span shorter or equal than the current one such that its end equals other....
Definition: Span.h:158
An writable view over an ASCII string (to avoid including Strings library)
Definition: Span.h:272
An read-only view over an ASCII string (to avoid including Strings library)
Definition: Span.h:249
bool writeNullTerminated(char(&buffer)[N]) const
Writes current string view over a sized char array buffer, adding a null terminator.
Definition: Span.h:258