18 #ifndef MBED_PLATFORM_SPAN_H_ 19 #define MBED_PLATFORM_SPAN_H_ 25 #include "platform/mbed_assert.h" 38 namespace span_detail {
42 template<
typename From,
typename To>
47 struct false_type { };
49 static const From &generator();
50 static true_type sink(
const To &);
51 static false_type sink(...);
54 static const bool value =
sizeof(true_type) ==
sizeof(sink(generator()));
59 #if defined(DOXYGEN_ONLY) 67 const ptrdiff_t SPAN_DYNAMIC_EXTENT = -1;
69 #define SPAN_DYNAMIC_EXTENT -1 212 template<
typename ElementType, ptrdiff_t Extent = SPAN_DYNAMIC_EXTENT>
238 static const index_type extent = Extent;
255 "Cannot default construct a static-extent Span (unless Extent is 0)" 271 Span(pointer ptr, index_type count) :
290 Span(pointer first, pointer last) :
308 Span(element_type (&elements)[Extent]):
320 template<
typename OtherElementType>
326 "OtherElementType(*)[] should be convertible to ElementType (*)[]" 388 template<ptrdiff_t Count>
392 (0 <= Count) && (Count <= Extent),
393 "Invalid subspan extent" 407 template<ptrdiff_t Count>
411 (0 <= Count) && (Count <= Extent),
412 "Invalid subspan extent" 431 template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
432 Span<element_type, Count == SPAN_DYNAMIC_EXTENT ? Extent - Offset : Count>
436 0 <= Offset && Offset <= Extent,
437 "Invalid subspan offset" 440 (Count == SPAN_DYNAMIC_EXTENT) ||
441 (0 <= Count && (Count + Offset) <= Extent),
442 "Invalid subspan count" 444 return Span<element_type, Count == SPAN_DYNAMIC_EXTENT ? Extent - Offset : Count>(
446 Count == SPAN_DYNAMIC_EXTENT ? Extent - Offset : Count
475 _data + (Extent - count),
493 index_type offset, index_type count = SPAN_DYNAMIC_EXTENT
498 (count == SPAN_DYNAMIC_EXTENT) ||
499 (0 <= count && (count + offset) <= Extent)
503 count == SPAN_DYNAMIC_EXTENT ? Extent - offset : count
514 template<
typename ElementType>
515 struct Span<ElementType, SPAN_DYNAMIC_EXTENT> {
539 static const index_type extent = SPAN_DYNAMIC_EXTENT;
550 _data(NULL), _size(0) { }
564 Span(pointer ptr, index_type count) :
565 _data(ptr), _size(count)
583 Span(pointer first, pointer last) :
584 _data(first), _size(last - first)
602 template<
size_t Count>
603 Span(element_type (&elements)[Count]):
604 _data(elements), _size(Count) { }
615 template<
typename OtherElementType, ptrdiff_t OtherExtent>
617 _data(other.data()), _size(other.size())
621 "OtherElementType(*)[] should be convertible to ElementType (*)[]" 682 template<ptrdiff_t Count>
698 template<ptrdiff_t Count>
717 template<std::ptrdiff_t Offset, std::ptrdiff_t Count>
723 (Count == SPAN_DYNAMIC_EXTENT) ||
724 (0 <= Count && (Count + Offset) <= _size)
728 Count == SPAN_DYNAMIC_EXTENT ? _size - Offset : Count
756 _data + (_size - count),
774 index_type offset, index_type count = SPAN_DYNAMIC_EXTENT
779 (count == SPAN_DYNAMIC_EXTENT) ||
780 (0 <= count && (count + offset) <= _size)
784 count == SPAN_DYNAMIC_EXTENT ? _size - offset : count
804 template<
typename T,
typename U, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
829 template<
typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
844 template<
typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
861 template<
typename T,
typename U, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
864 return !(lhs == rhs);
876 template<
typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
891 template<
typename T, ptrdiff_t LhsExtent, ptrdiff_t RhsExtent>
912 template<
typename T,
size_t Size>
931 template<ptrdiff_t Extent,
typename T>
955 return Span<T>(array_ptr, array_size);
970 template<
typename T,
size_t Extent>
991 template<
size_t Extent,
typename T>
1013 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.