COMMS
Template library intended to help with implementation of communication protocols.
Loading...
Searching...
No Matches
ArrayView.h
Go to the documentation of this file.
1//
2// Copyright 2017 - 2025 (C). Alex Robenko. All rights reserved.
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
10
11#pragma once
12
13#include "comms/Assert.h"
14
15#include <algorithm>
16#include <iterator>
17
18namespace comms
19{
20
21namespace util
22{
23
28template <typename T>
30{
31public:
33 using value_type = T;
34
37
39 using pointer = T*;
40
43
45 using const_pointer = const T*;
46
49
51 using reference = T&;
52
55
57 using const_reference = const T&;
58
61
63 using size_type = std::size_t;
64
67
71
74
77
80
82 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
83
86
89
92
94 ArrayView() noexcept = default;
95
97 ArrayView(const ArrayView&) noexcept = default;
98
101 : m_data(data),
102 m_len(len)
103 {
104 }
105
107 template <typename TIter>
108 ArrayView(TIter iter, size_type len) noexcept
109 : m_data(reinterpret_cast<const_pointer>(&(*iter))),
110 m_len(len)
111 {
112 }
113
115 template <std::size_t TN>
116 ArrayView(const T (&data)[TN]) noexcept
117 : m_data(data),
118 m_len(TN)
119 {
120 }
121
123 template <std::size_t TN>
124 ArrayView(T (&data)[TN]) noexcept
125 : m_data(data),
126 m_len(TN)
127 {
128 }
129
131 template <typename TIter>
132 ArrayView(TIter first, TIter last) noexcept
133 : m_data(reinterpret_cast<const_pointer>(&(*first))),
134 m_len(static_cast<std::size_t>(std::distance(first, last)))
135 {
136 COMMS_ASSERT(0 <= std::distance(first, last));
137 }
138
140 ~ArrayView() noexcept = default;
141
143 ArrayView& operator=(const ArrayView&) = default;
144
146 template <std::size_t TN>
147 ArrayView& operator=(const T (&data)[TN])
148 {
149 m_data = data;
150 m_len = TN;
151 return *this;
152 }
153
155 template <std::size_t TN>
157 {
158 m_data = data;
159 m_len = TN;
160 return *this;
161 }
162
164 constexpr const_iterator begin() const noexcept
165 {
166 return m_data;
167 }
168
170 constexpr const_iterator cbegin() const noexcept
171 {
172 return begin();
173 }
174
176 constexpr const_iterator end() const noexcept
177 {
178 return begin() + m_len;
179 }
180
182 constexpr const_iterator cend() const noexcept
183 {
184 return end();
185 }
186
189 {
190 return std::reverse_iterator<const_iterator>(end());
191 }
192
195 {
196 return rbegin();
197 }
198
200 reverse_iterator rend() const noexcept
201 {
202 return std::reverse_iterator<const_iterator>(begin());
203 }
204
207 {
208 return rend();
209 }
210
213 {
214 return m_data[pos];
215 }
216
220 {
221 COMMS_ASSERT(pos < m_len);
222 return m_data[pos];
223 }
224
227 constexpr const_reference front() const
228 {
229 return m_data[0];
230 }
231
234 constexpr const_reference back() const
235 {
236 return m_data[m_len - 1U];
237 }
238
240 constexpr size_type size() const noexcept
241 {
242 return m_len;
243 }
244
246 constexpr size_type length() const noexcept
247 {
248 return size();
249 }
250
253 constexpr bool empty() const noexcept
254 {
255 return size() == 0U;
256 }
257
261 {
262 std::advance(m_data, n);
263 m_len -= n;
264 }
265
269 {
270 m_len -= n;
271 }
272
274 void swap(ArrayView& other) noexcept
275 {
276 std::swap(m_data, other.m_data);
277 std::swap(m_len, other.m_len);
278 }
279
282 {
283 return m_data;
284 }
285
286private:
287 const_pointer m_data = nullptr;
288 size_type m_len = 0;
289};
290
293template<typename T>
294bool operator<(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
295{
296 return std::lexicographical_compare(view1.begin(), view1.end(), view2.begin(), view2.end());
297}
298
301template<typename T>
302bool operator<=(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
303{
304 return !(view2 < view1);
305}
306
309template<typename T>
310bool operator>(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
311{
312 return (view2 < view1);
313}
314
317template<typename T>
318bool operator>=(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
319{
320 return !(view1 < view2);
321}
322
325template<typename T>
326bool operator==(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
327{
328 return
329 (view1.size() == view2.size()) &&
330 std::equal(view1.begin(), view1.end(), view2.begin());
331}
332
335template<typename T>
336bool operator!=(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
337{
338 return !(view1 == view2);
339}
340
341} // namespace util
342
343} // namespace comms
344
345namespace std
346{
347
350template <typename T>
352{
353 view1.swap(view2);
354}
355
356} // namespace std
This file contains classes required for generic custom assertion functionality.
#define COMMS_ASSERT(expr)
Generic assert macro.
Definition Assert.h:168
Describes an object that can refer to a constant contiguous sequence of other objects.
Definition ArrayView.h:30
const_iterator ConstIterator
Same as const_iterator.
Definition ArrayView.h:73
~ArrayView() noexcept=default
Destructor.
const_reference at(size_type pos) const
Element access with range check.
Definition ArrayView.h:219
bool operator!=(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Inequality compare between the views.
Definition ArrayView.h:336
constexpr const_iterator begin() const noexcept
Iterator to begining of the sequence.
Definition ArrayView.h:164
pointer Pointer
Same as pointer.
Definition ArrayView.h:42
reverse_iterator ReverseIterator
Same as reverse_iterator.
Definition ArrayView.h:91
void remove_prefix(size_type n)
Narrow the view by skipping number of elements at the beginning.
Definition ArrayView.h:260
bool operator<(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Lexicographical compare between the views.
Definition ArrayView.h:294
bool operator<=(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Lexicographical compare between the views.
Definition ArrayView.h:302
ArrayView & operator=(T(&data)[TN])
Assign array of elements with known size.
Definition ArrayView.h:156
const_reverse_iterator crend() const noexcept
Reverse iterator to the beginning of the sequence.
Definition ArrayView.h:206
const_iterator iterator
Same as const_iterator.
Definition ArrayView.h:76
constexpr const_iterator end() const noexcept
Iterator to the end of the sequence.
Definition ArrayView.h:176
ArrayView() noexcept=default
Default constructor.
value_type ValueType
Same as value_type.
Definition ArrayView.h:36
bool operator>=(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Lexicographical compare between the views.
Definition ArrayView.h:318
constexpr size_type size() const noexcept
Get number of element in the view.
Definition ArrayView.h:240
std::reverse_iterator< const_iterator > const_reverse_iterator
Same as std::reverse_iterator<const_iterator>
Definition ArrayView.h:82
iterator Iterator
Same as iterator.
Definition ArrayView.h:79
std::size_t size_type
Equal to std::size_t.
Definition ArrayView.h:63
T * pointer
Pointer to the single element (T*)
Definition ArrayView.h:39
reverse_iterator rend() const noexcept
Reverse iterator to the beginning of the sequence.
Definition ArrayView.h:200
void remove_suffix(size_type n)
Narrow the view by dropping number of elements at the end.
Definition ArrayView.h:268
constexpr const_reference operator[](size_type pos) const
Element access operator.
Definition ArrayView.h:212
ArrayView(TIter iter, size_type len) noexcept
Constructor.
Definition ArrayView.h:108
const_reverse_iterator ConstReverseIterator
Same as const_reverse_iterator.
Definition ArrayView.h:85
reference Reference
Same as reference.
Definition ArrayView.h:54
bool operator>(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Lexicographical compare between the views.
Definition ArrayView.h:310
constexpr size_type length() const noexcept
Same as ref size()
Definition ArrayView.h:246
const_reference ConstReference
Same as const_reference.
Definition ArrayView.h:60
constexpr bool empty() const noexcept
Check the view is empty.
Definition ArrayView.h:253
ArrayView(const T(&data)[TN]) noexcept
Construct out of array of elements with known size.
Definition ArrayView.h:116
constexpr const_iterator cend() const noexcept
Iterator to the end of the sequence.
Definition ArrayView.h:182
bool operator==(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Equality compare between the views.
Definition ArrayView.h:326
void swap(ArrayView &other) noexcept
Swap contents of two views.
Definition ArrayView.h:274
const_reverse_iterator reverse_iterator
Same as const_reverse_iterator.
Definition ArrayView.h:88
size_type SizeType
Same as size_type;.
Definition ArrayView.h:66
const_pointer data() const
Returns a pointer to the beginning of the underlying sequence.
Definition ArrayView.h:281
const_pointer const_iterator
Implementation defined constant RandomAccessIterator and ContiguousIterator whose value_type is T.
Definition ArrayView.h:70
const_pointer ConstPointer
Same as const_pointer.
Definition ArrayView.h:48
ArrayView(TIter first, TIter last) noexcept
Construct out of iterators rande.
Definition ArrayView.h:132
T & reference
Reference to an element (T&)
Definition ArrayView.h:51
ArrayView(T(&data)[TN]) noexcept
Construct out of array of elements with known size.
Definition ArrayView.h:124
T value_type
Type of the single element.
Definition ArrayView.h:33
constexpr const_iterator cbegin() const noexcept
Iterator to begining of the sequence.
Definition ArrayView.h:170
const_reverse_iterator crbegin() const noexcept
Reverse iterator to the end of the sequence.
Definition ArrayView.h:194
const T & const_reference
Reference to a const element (const T&)
Definition ArrayView.h:57
const_reverse_iterator rbegin() const noexcept
Reverse iterator to the end of the sequence.
Definition ArrayView.h:188
const T * const_pointer
Pointer to the constant element (const T*)
Definition ArrayView.h:45
constexpr const_reference front() const
Access the first element.
Definition ArrayView.h:227
constexpr const_reference back() const
Access the last element.
Definition ArrayView.h:234
Main namespace for all classes / functions of COMMS library.
STL namespace.