18#ifndef MBED_PLATFORM_SPAN_H_
19#define MBED_PLATFORM_SPAN_H_
26#include "platform/mbed_assert.h"
40namespace span_detail {
44template<
typename From,
typename To>
49 struct false_type { };
51 static const From &generator();
52 static true_type sink(
const To &);
53 static false_type sink(...);
56 static const bool value =
sizeof(true_type) ==
sizeof(sink(generator()));
61#if defined(DOXYGEN_ONLY)
69const ptrdiff_t SPAN_DYNAMIC_EXTENT = -1;
71#define SPAN_DYNAMIC_EXTENT -1
214template<
typename ElementType, ptrdiff_t Extent = SPAN_DYNAMIC_EXTENT>
252 static_assert(Extent >= 0,
"Invalid extent for a Span");
267 "Cannot default construct a static-extent Span (unless Extent is 0)"
332 template<
typename OtherElementType>
338 "OtherElementType(*)[] should be convertible to ElementType (*)[]"
380 return _data + Extent;
440 template<ptrdiff_t Count>
444 (0 <= Count) && (Count <= Extent),
445 "Invalid subspan extent"
459 template<ptrdiff_t Count>
463 (0 <= Count) && (Count <= Extent),
464 "Invalid subspan extent"
483 template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
488 0 <= Offset && Offset <= Extent,
489 "Invalid subspan offset"
493 (0 <= Count && (Count + Offset) <= Extent),
494 "Invalid subspan count"
527 _data + (Extent - count),
551 (0 <= count && (count + offset) <= Extent)
566template<
typename ElementType>
567struct Span<ElementType, SPAN_DYNAMIC_EXTENT> {
612 _data(NULL), _size(0) { }
627 _data(ptr), _size(count)
664 template<
size_t Count>
666 _data(elements), _size(Count) { }
677 template<
typename OtherElementType, ptrdiff_t OtherExtent>
679 _data(other.
data()), _size(other.
size())
683 "OtherElementType(*)[] should be convertible to ElementType (*)[]"
725 return _data + _size;
784 template<ptrdiff_t Count>
800 template<ptrdiff_t Count>
819 template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
826 (0 <= Count && (Count + Offset) <= _size)
858 _data + (_size - count),
882 (0 <= count && (count + offset) <= _size)
906template<
typename T,
typename U, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
931template<
typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
946template<
typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
963template<
typename T,
typename U, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
966 return !(lhs == rhs);
978template<
typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
993template<
typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
1014template<
typename T,
size_t Size>
1033template<ptrdiff_t Extent,
typename T>
1057 return Span<T>(array_ptr, array_size);
1072template<
typename T,
size_t Extent>
1093template<
size_t Extent,
typename T>
void operator!=(const SafeBool< T > &lhs, const SafeBool< U > &rhs)
Avoid conversion to bool between different classes.
void operator==(const SafeBool< T > &lhs, const SafeBool< U > &rhs)
Avoid conversion to bool between different classes.
Span(const Span< OtherElementType, OtherExtent > &other)
Construct a Span object from another Span.
Span()
Construct an empty Span.
iterator begin() const
Return an iterator to the first element of the sequence.
Span(pointer ptr, index_type count)
Construct a Span from a pointer to a buffer and its size.
Span< element_type, SPAN_DYNAMIC_EXTENT > last(index_type count) const
Create a new Span over the last count elements of the existing view.
Span(pointer first, pointer last)
Construct a Span from the range [first, last).
Span(element_type(&elements)[Count])
Construct a Span from the reference to an array.
Span< element_type, SPAN_DYNAMIC_EXTENT > subspan(index_type offset, index_type count=SPAN_DYNAMIC_EXTENT) const
Create a subspan that is a view of other count elements; the view starts at element offset.
Span< element_type, Count > first() const
Create a new Span over the first Count elements of the existing view.
pointer iterator
Iterator to an ElementType.
Span< element_type, Count > last() const
Create a new Span over the last Count elements of the existing view.
reverse_iterator rbegin() const
Return a reverse_iterator to the first element of the reversed sequence.
ElementType element_type
Type of the element contained.
reference operator[](index_type index) const
Access to an element of the sequence.
bool empty() const
Return if the sequence viewed is empty or not.
iterator end() const
Return an iterator to the element following the last element of the sequence.
Span< element_type, Count > subspan() const
Create a subspan that is a view other Count elements; the view starts at element Offset.
ptrdiff_t index_type
Type of the index.
element_type * pointer
Pointer to an ElementType.
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator to an ElementType.
Span< element_type, SPAN_DYNAMIC_EXTENT > first(index_type count) const
Create a new Span over the first count elements of the existing view.
reverse_iterator rend() const
Return a reverse_iterator to the element following the last element of the reversed sequence.
pointer data() const
Get the raw pointer to the sequence viewed.
element_type & reference
Reference to an ElementType.
index_type size() const
Return the size of the array viewed.
Nonowning view to a sequence of contiguous elements.
Span()
Construct an empty Span.
iterator begin() const
Return an iterator to the first element of the sequence.
Span(pointer ptr, index_type count)
Construct a Span from a pointer to a buffer and its size.
Span(const Span< OtherElementType, Extent > &other)
Construct a Span object from another Span of the same size.
Span< element_type, SPAN_DYNAMIC_EXTENT > last(index_type count) const
Create a new Span over the last count elements of the existing view.
Span(pointer first, pointer last)
Construct a Span from the range [first, last).
Span< element_type, Count==SPAN_DYNAMIC_EXTENT ? Extent - Offset :Count > subspan() const
Create a subspan that is a view of other Count elements; the view starts at element Offset.
Span< element_type, SPAN_DYNAMIC_EXTENT > subspan(index_type offset, index_type count=SPAN_DYNAMIC_EXTENT) const
Create a subspan that is a view of other count elements; the view starts at element offset.
Span< element_type, Count > first() const
Create a new Span over the first Count elements of the existing view.
static const index_type extent
Size of the Extent; -1 if dynamic.
pointer iterator
Iterator to an ElementType.
Span< element_type, Count > last() const
Create a new Span over the last Count elements of the existing view.
reverse_iterator rbegin() const
Return a reverse_iterator to the first element of the reversed sequence.
ElementType element_type
Type of the element contained.
reference operator[](index_type index) const
Returns a reference to the element at position index.
bool empty() const
Return if the sequence is empty or not.
iterator end() const
Return an iterator to the element following the last element of the sequence.
ptrdiff_t index_type
Type of the index.
element_type * pointer
Pointer to an ElementType.
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator to an ElementType.
Span< element_type, SPAN_DYNAMIC_EXTENT > first(index_type count) const
Create a new Span over the first count elements of the existing view.
reverse_iterator rend() const
Return a reverse_iterator to the element following the last element of the reversed sequence.
pointer data() const
Return a pointer to the first element of the sequence or NULL if the Span is empty().
element_type & reference
Reference to an ElementType.
index_type size() const
Return the size of the sequence viewed.
Span(element_type(&elements)[Extent])
Construct a Span from the reference to an array.