Skip to content

Commit aa605bb

Browse files
authored
Merge pull request #13252 from marcemmers/span-add-begin-end
Implements iterators for the Span class to be compatible with std::span
2 parents 947eb7a + a20ff63 commit aa605bb

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

platform/Span.h

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#define MBED_PLATFORM_SPAN_H_
2020

2121
#include <algorithm>
22+
#include <iterator>
2223
#include <stddef.h>
2324
#include <stdint.h>
2425

@@ -233,6 +234,16 @@ struct Span {
233234
*/
234235
typedef element_type &reference;
235236

237+
/**
238+
* Iterator to an ElementType
239+
*/
240+
typedef pointer iterator;
241+
242+
/**
243+
* Reverse iterator to an ElementType
244+
*/
245+
typedef std::reverse_iterator<iterator> reverse_iterator;
246+
236247
/**
237248
* Size of the Extent; -1 if dynamic.
238249
*/
@@ -349,6 +360,46 @@ struct Span {
349360
return size() == 0;
350361
}
351362

363+
/**
364+
* Return an iterator to the first element of the sequence.
365+
*
366+
* @return An iterator to the first element of the sequence.
367+
*/
368+
iterator begin() const
369+
{
370+
return _data;
371+
}
372+
373+
/**
374+
* Return an iterator to the element following the last element of the sequence.
375+
*
376+
* @return An iterator to the element following the last element of the sequence.
377+
*/
378+
iterator end() const
379+
{
380+
return _data + Extent;
381+
}
382+
383+
/**
384+
* Return a reverse_iterator to the first element of the reversed sequence.
385+
*
386+
* @return A reverse_iterator to the first element of the reversed sequence.
387+
*/
388+
reverse_iterator rbegin() const
389+
{
390+
return reverse_iterator(end());
391+
}
392+
393+
/**
394+
* Return a reverse_iterator to the element following the last element of the reversed sequence.
395+
*
396+
* @return A reverse_iterator to the element following the last element of the reversed sequence.
397+
*/
398+
reverse_iterator rend() const
399+
{
400+
return reverse_iterator(begin());
401+
}
402+
352403
/**
353404
* Returns a reference to the element at position @p index.
354405
*
@@ -534,6 +585,16 @@ struct Span<ElementType, SPAN_DYNAMIC_EXTENT> {
534585
*/
535586
typedef element_type &reference;
536587

588+
/**
589+
* Iterator to an ElementType
590+
*/
591+
typedef pointer iterator;
592+
593+
/**
594+
* Reverse iterator to an ElementType
595+
*/
596+
typedef std::reverse_iterator<iterator> reverse_iterator;
597+
537598
/**
538599
* Size of the Extent; -1 if dynamic.
539600
*/
@@ -644,6 +705,46 @@ struct Span<ElementType, SPAN_DYNAMIC_EXTENT> {
644705
return size() == 0;
645706
}
646707

708+
/**
709+
* Return an iterator to the first element of the sequence.
710+
*
711+
* @return An iterator to the first element of the sequence.
712+
*/
713+
iterator begin() const
714+
{
715+
return _data;
716+
}
717+
718+
/**
719+
* Return an iterator to the element following the last element of the sequence.
720+
*
721+
* @return An iterator to the element following the last element of the sequence.
722+
*/
723+
iterator end() const
724+
{
725+
return _data + _size;
726+
}
727+
728+
/**
729+
* Return a reverse_iterator to the first element of the reversed sequence.
730+
*
731+
* @return A reverse_iterator to the first element of the reversed sequence.
732+
*/
733+
reverse_iterator rbegin() const
734+
{
735+
return reverse_iterator(end());
736+
}
737+
738+
/**
739+
* Return a reverse_iterator to the element following the last element of the reversed sequence.
740+
*
741+
* @return A reverse_iterator to the element following the last element of the reversed sequence.
742+
*/
743+
reverse_iterator rend() const
744+
{
745+
return reverse_iterator(begin());
746+
}
747+
647748
/**
648749
* Access to an element of the sequence.
649750
*

0 commit comments

Comments
 (0)