MIP_SDK  v3.0.0
MicroStrain Communications Library for embedded systems
span.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef MICROSTRAIN_USE_STD_SPAN
4 
5 #include <span>
6 
7 namespace microstrain
8 {
9 
10  static inline constexpr size_t DYNAMIC_EXTENT = std::dynamic_extent;
11 
12  template<class T, size_t Size=DYNAMIC_EXTENT>
13  using Span = std::span<T, Size>;
14 
15 } // namespace microstrain
16 
17 #else // MICROSTRAIN_USE_STD_SPAN
18 
19 #include <type_traits>
20 #include <stddef.h>
21 
22 namespace microstrain
23 {
24 
25 
26 static constexpr size_t DYNAMIC_EXTENT = -1;
27 
39 template<class T, size_t Extent=DYNAMIC_EXTENT>
40 struct Span
41 {
42  static constexpr size_t extent = Extent;
43 
44  using element_type = T;
45  using value_type = typename std::remove_cv<T>::type;
46  using difference_type = ptrdiff_t;
47  using pointer = T*;
48  using reference = T&;
49  using const_pointer = const T*;
50  using const_reference = const T&;
51 
52  constexpr Span(pointer ptr) : m_ptr(ptr) {}
53 
54  constexpr pointer begin() const noexcept { return m_ptr; }
55  constexpr pointer end() const noexcept { return m_ptr+extent; }
56 
57  constexpr element_type front() const noexcept { return *m_ptr; }
58  constexpr element_type back() const noexcept { return *m_ptr[extent-1]; }
59 
60  constexpr reference operator[](size_t idx) noexcept { return m_ptr[idx]; }
61  constexpr const_reference operator[](size_t idx) const noexcept { return m_ptr[idx]; }
62 
63  constexpr pointer data() const noexcept { return m_ptr; }
64 
65  [[nodiscard]] constexpr size_t size() const noexcept { return extent; }
66  [[nodiscard]] constexpr bool empty() const noexcept { return extent == 0; }
67 
68  [[nodiscard]] constexpr Span<T, DYNAMIC_EXTENT> subspan(size_t index, size_t length) const { return {m_ptr+index, length}; }
69  [[nodiscard]] constexpr Span<T, DYNAMIC_EXTENT> subspan(size_t index) const { return {m_ptr+index, extent-index}; }
70  template<size_t Offset, size_t Count = DYNAMIC_EXTENT>
71  [[nodiscard]] constexpr Span<T, Count == DYNAMIC_EXTENT ? DYNAMIC_EXTENT : Extent-Count> subspan() const { return {m_ptr+Offset}; }
72 
73 private:
74  pointer m_ptr = nullptr;
75 };
76 
77 
78 template<class T>
79 struct Span<T, DYNAMIC_EXTENT>
80 {
81  using element_type = T;
82  using value_type = typename std::remove_cv<T>::type;
83  using difference_type = ptrdiff_t;
84  using pointer = T*;
85  using reference = T&;
86  using const_pointer = const T*;
87  using const_reference = const T&;
88 
89  constexpr Span() = default;
90  constexpr Span(pointer ptr, size_t cnt) : m_ptr(ptr), m_cnt(cnt) {}
91  template<size_t N>
92  constexpr Span(const T (&arr)[N]) : m_ptr(arr), m_cnt(N) {}
93 
94  constexpr pointer begin() const noexcept { return m_ptr; }
95  constexpr pointer end() const noexcept { return m_ptr+m_cnt; }
96 
97  constexpr element_type front() const noexcept { return *m_ptr; }
98  constexpr element_type back() const noexcept { return *m_ptr[m_cnt-1]; }
99 
100  constexpr reference operator[](size_t idx) noexcept { return m_ptr[idx]; }
101  constexpr const_reference operator[](size_t idx) const noexcept { return m_ptr[idx]; }
102 
103  constexpr pointer data() const noexcept { return m_ptr; }
104 
105  [[nodiscard]] constexpr size_t size() const noexcept { return m_cnt; }
106  [[nodiscard]] constexpr bool empty() const noexcept { return m_cnt == 0; }
107 
108  [[nodiscard]] constexpr Span<T, DYNAMIC_EXTENT> subspan(size_t index, size_t length) const { return {m_ptr+index, length}; }
109  [[nodiscard]] constexpr Span<T, DYNAMIC_EXTENT> subspan(size_t index) const { return {m_ptr+index, m_cnt-index}; }
110  template<size_t Offset, size_t Count = DYNAMIC_EXTENT>
111  [[nodiscard]] constexpr Span<T, Count> subspan() const { return {m_ptr+Offset, Count}; }
112 
113 private:
114  pointer m_ptr = nullptr;
115  size_t m_cnt = 0;
116 };
117 
118 
119 } // namespace microstrain
120 
121 #endif // MICROSTRAIN_USE_STD_SPAN
microstrain::Span< T, DYNAMIC_EXTENT >
Definition: span.hpp:79
microstrain::Span::data
constexpr pointer data() const noexcept
Definition: span.hpp:63
microstrain::Span::subspan
constexpr Span< T, DYNAMIC_EXTENT > subspan(size_t index) const
Definition: span.hpp:69
microstrain::Span::Span
constexpr Span(pointer ptr)
Definition: span.hpp:52
microstrain::Span::pointer
T * pointer
Definition: span.hpp:47
microstrain::Span< T, DYNAMIC_EXTENT >::element_type
T element_type
Definition: span.hpp:81
microstrain::Span< T, DYNAMIC_EXTENT >::subspan
constexpr Span< T, DYNAMIC_EXTENT > subspan(size_t index, size_t length) const
Definition: span.hpp:108
microstrain::Span::front
constexpr element_type front() const noexcept
Definition: span.hpp:57
microstrain::Span::subspan
constexpr Span< T, DYNAMIC_EXTENT > subspan(size_t index, size_t length) const
Definition: span.hpp:68
microstrain::Span< T, DYNAMIC_EXTENT >::front
constexpr element_type front() const noexcept
Definition: span.hpp:97
microstrain::Span::size
constexpr size_t size() const noexcept
Definition: span.hpp:65
microstrain::Span< T, DYNAMIC_EXTENT >::const_pointer
const T * const_pointer
Definition: span.hpp:86
microstrain::Span::operator[]
constexpr const_reference operator[](size_t idx) const noexcept
Definition: span.hpp:61
microstrain::Span< T, DYNAMIC_EXTENT >::end
constexpr pointer end() const noexcept
Definition: span.hpp:95
microstrain::Span< T, DYNAMIC_EXTENT >::difference_type
ptrdiff_t difference_type
Definition: span.hpp:83
microstrain::Span
Implementation of std::span from C++20.
Definition: span.hpp:40
microstrain::Span< T, DYNAMIC_EXTENT >::empty
constexpr bool empty() const noexcept
Definition: span.hpp:106
microstrain::Span< T, DYNAMIC_EXTENT >::pointer
T * pointer
Definition: span.hpp:84
microstrain::Span< T, DYNAMIC_EXTENT >::value_type
typename std::remove_cv< T >::type value_type
Definition: span.hpp:82
mip::metadata::ParameterInfo
Definition: mip_structures.hpp:127
microstrain::Span::subspan
constexpr Span< T, Count==DYNAMIC_EXTENT ? DYNAMIC_EXTENT :Extent-Count > subspan() const
Definition: span.hpp:71
microstrain::Span< T, DYNAMIC_EXTENT >::Span
constexpr Span(const T(&arr)[N])
Definition: span.hpp:92
microstrain::Span< T, DYNAMIC_EXTENT >::const_reference
const T & const_reference
Definition: span.hpp:87
microstrain::Span< T, DYNAMIC_EXTENT >::data
constexpr pointer data() const noexcept
Definition: span.hpp:103
microstrain::Span::begin
constexpr pointer begin() const noexcept
Definition: span.hpp:54
microstrain::Span< T, DYNAMIC_EXTENT >::subspan
constexpr Span< T, DYNAMIC_EXTENT > subspan(size_t index) const
Definition: span.hpp:109
microstrain::Span< const mip::metadata::ParameterInfo >::const_reference
const const mip::metadata::ParameterInfo & const_reference
Definition: span.hpp:50
microstrain::Span< T, DYNAMIC_EXTENT >::begin
constexpr pointer begin() const noexcept
Definition: span.hpp:94
microstrain::Span< const mip::metadata::ParameterInfo >::const_pointer
const const mip::metadata::ParameterInfo * const_pointer
Definition: span.hpp:49
microstrain::Span< T, DYNAMIC_EXTENT >::size
constexpr size_t size() const noexcept
Definition: span.hpp:105
microstrain::Span< const mip::metadata::ParameterInfo >::difference_type
ptrdiff_t difference_type
Definition: span.hpp:46
microstrain::Span::operator[]
constexpr reference operator[](size_t idx) noexcept
Definition: span.hpp:60
microstrain::Span::empty
constexpr bool empty() const noexcept
Definition: span.hpp:66
microstrain::Span::end
constexpr pointer end() const noexcept
Definition: span.hpp:55
microstrain::Span< T, DYNAMIC_EXTENT >::back
constexpr element_type back() const noexcept
Definition: span.hpp:98
microstrain::Span< T, DYNAMIC_EXTENT >::reference
T & reference
Definition: span.hpp:85
microstrain::Span< T, DYNAMIC_EXTENT >::subspan
constexpr Span< T, Count > subspan() const
Definition: span.hpp:111
microstrain::Span::back
constexpr element_type back() const noexcept
Definition: span.hpp:58
microstrain::Span< const mip::metadata::ParameterInfo >::value_type
typename std::remove_cv< const mip::metadata::ParameterInfo >::type value_type
Definition: span.hpp:45
microstrain::Span::extent
static constexpr size_t extent
Definition: span.hpp:42
microstrain::Span< T, DYNAMIC_EXTENT >::Span
constexpr Span(pointer ptr, size_t cnt)
Definition: span.hpp:90
microstrain::Span< T, DYNAMIC_EXTENT >::operator[]
constexpr reference operator[](size_t idx) noexcept
Definition: span.hpp:100
microstrain::Span< T, DYNAMIC_EXTENT >::operator[]
constexpr const_reference operator[](size_t idx) const noexcept
Definition: span.hpp:101
microstrain
Definition: embedded_time.h:8
mip::metadata::Span
microstrain::Span< T > Span
Definition: mip_structures.hpp:13