18 #ifndef MBED_PLATFORM_SPAN_H_ 19 #define MBED_PLATFORM_SPAN_H_ 25 #include "platform/mbed_assert.h" 39 namespace span_detail {
43 template<
typename From,
typename To>
48 struct false_type { };
50 static const From &generator();
51 static true_type sink(
const To &);
52 static false_type sink(...);
55 static const bool value =
sizeof(true_type) ==
sizeof(sink(generator()));
60 #if defined(DOXYGEN_ONLY) 68 const ptrdiff_t SPAN_DYNAMIC_EXTENT = -1;
70 #define SPAN_DYNAMIC_EXTENT -1 213 template<
typename ElementType, ptrdiff_t Extent = SPAN_DYNAMIC_EXTENT>
239 static const index_type extent = Extent;
256 "Cannot default construct a static-extent Span (unless Extent is 0)" 272 Span(pointer ptr, index_type count) :
291 Span(pointer first, pointer last) :
309 Span(element_type (&elements)[Extent]):
321 template<
typename OtherElementType>
327 "OtherElementType(*)[] should be convertible to ElementType (*)[]" 389 template<ptrdiff_t Count>
393 (0 <= Count) && (Count <= Extent),
394 "Invalid subspan extent" 408 template<ptrdiff_t Count>
412 (0 <= Count) && (Count <= Extent),
413 "Invalid subspan extent" 432 template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
433 Span<element_type, Count == SPAN_DYNAMIC_EXTENT ? Extent - Offset : Count>
437 0 <= Offset && Offset <= Extent,
438 "Invalid subspan offset" 441 (Count == SPAN_DYNAMIC_EXTENT) ||
442 (0 <= Count && (Count + Offset) <= Extent),
443 "Invalid subspan count" 445 return Span<element_type, Count == SPAN_DYNAMIC_EXTENT ? Extent - Offset : Count>(
447 Count == SPAN_DYNAMIC_EXTENT ? Extent - Offset : Count
476 _data + (Extent - count),
494 index_type offset, index_type count = SPAN_DYNAMIC_EXTENT
499 (count == SPAN_DYNAMIC_EXTENT) ||
500 (0 <= count && (count + offset) <= Extent)
504 count == SPAN_DYNAMIC_EXTENT ? Extent - offset : count
515 template<
typename ElementType>
516 struct Span<ElementType, SPAN_DYNAMIC_EXTENT> {
540 static const index_type extent = SPAN_DYNAMIC_EXTENT;
551 _data(NULL), _size(0) { }
565 Span(pointer ptr, index_type count) :
566 _data(ptr), _size(count)
584 Span(pointer first, pointer last) :
585 _data(first), _size(last - first)
603 template<
size_t Count>
604 Span(element_type (&elements)[Count]):
605 _data(elements), _size(Count) { }
616 template<
typename OtherElementType, ptrdiff_t OtherExtent>
618 _data(other.data()), _size(other.size())
622 "OtherElementType(*)[] should be convertible to ElementType (*)[]" 683 template<ptrdiff_t Count>
699 template<ptrdiff_t Count>
718 template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
724 (Count == SPAN_DYNAMIC_EXTENT) ||
725 (0 <= Count && (Count + Offset) <= _size)
729 Count == SPAN_DYNAMIC_EXTENT ? _size - Offset : Count
757 _data + (_size - count),
775 index_type offset, index_type count = SPAN_DYNAMIC_EXTENT
780 (count == SPAN_DYNAMIC_EXTENT) ||
781 (0 <= count && (count + offset) <= _size)
785 count == SPAN_DYNAMIC_EXTENT ? _size - offset : count
805 template<
typename T,
typename U, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
830 template<
typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
845 template<
typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
862 template<
typename T,
typename U, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
865 return !(lhs == rhs);
877 template<
typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
892 template<
typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
913 template<
typename T,
size_t Size>
932 template<ptrdiff_t Extent,
typename T>
956 return Span<T>(array_ptr, array_size);
971 template<
typename T,
size_t Extent>
992 template<
size_t Extent,
typename T>
1014 template<
typename T>
Span< element_type, Count > last() const
Create a new Span over the last Count elements of the existing view.
ElementType element_type
Type of the element contained.
index_type size() const
Return the size of the sequence viewed.
index_type size() const
Return the size of the array viewed.
Span()
Construct an empty Span.
Span< element_type, SPAN_DYNAMIC_EXTENT > first(index_type count) const
Create a new Span over the first count elements of the existing view.
pointer data() const
Get the raw pointer to the sequence viewed.
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< element_type, Count > first() const
Create a new Span over the first Count elements of the existing view.
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==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...
ptrdiff_t index_type
Type of the index.
Span< element_type, Count > first() const
Create a new Span over the first Count elements of the existing view.
reference operator[](index_type index) const
Access to an element of the sequence.
element_type & reference
Reference to an ElementType.
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 ptr, index_type count)
Construct a Span from a pointer to a buffer and its size.
Span()
Construct an empty Span.
element_type * pointer
Pointer to an ElementType.
ptrdiff_t index_type
Type of the index.
element_type & reference
Reference to an ElementType.
Span< element_type, Count > subspan() const
Create a subspan that is a view other Count elements; the view starts at element Offset.
Span(pointer first, pointer last)
Construct a Span from the range [first, last).
element_type * pointer
Pointer to an ElementType.
ElementType element_type
Type of the element contained.
Span(pointer first, pointer last)
Construct a Span from the range [first, last).
Span< element_type, Count > last() const
Create a new Span over the last Count elements of the existing view.
Nonowning view to a sequence of contiguous elements.
Span< element_type, SPAN_DYNAMIC_EXTENT > first(index_type count) const
Create a new Span over the first count elements of the existing view.
Span(const Span< OtherElementType, OtherExtent > &other)
Construct a Span object from another Span.
void operator!=(const SafeBool< T > &lhs, const SafeBool< U > &rhs)
Avoid conversion to bool between different classes.
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...
void operator==(const SafeBool< T > &lhs, const SafeBool< U > &rhs)
Avoid conversion to bool between different classes.
bool empty() const
Return if the sequence viewed is empty or not.
Span(pointer ptr, index_type count)
Construct a Span from a pointer to a buffer and its size.
bool empty() const
Return if the sequence is empty or not.
pointer data() const
Return a pointer to the first element of the sequence or NULL if the Span is empty().
Span(element_type(&elements)[Extent])
Construct a Span from the reference to an array.
Span(element_type(&elements)[Count])
Construct a Span from the reference to an array.
reference operator[](index_type index) const
Returns a reference to the element at position index.
Span(const Span< OtherElementType, Extent > &other)
Construct a Span object from another Span of the same size.