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 - 2026 (C). Alex Robenko. All rights reserved.
3//
4// SPDX-License-Identifier: MPL-2.0
5//
6// This Source Code Form is subject to the terms of the Mozilla Public
7// License, v. 2.0. If a copy of the MPL was not distributed with this
8// file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
12
13#pragma once
14
15#include "comms/Assert.h"
16
17#include <cstddef>
18#include <iterator>
19
20namespace comms
21{
22
23namespace util
24{
25
30template <typename T>
32{
33public:
35 using value_type = T;
36
39
41 using pointer = T*;
42
45
47 using const_pointer = const T*;
48
51
53 using reference = T&;
54
57
59 using const_reference = const T&;
60
63
65 using size_type = std::size_t;
66
69
73
76
79
82
84 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
85
88
91
94
96 ArrayView() noexcept = default;
97
99 ArrayView(const ArrayView&) noexcept = default;
100
103 : m_data(data),
104 m_len(len)
105 {
106 }
107
109 template <typename TIter>
110 ArrayView(TIter iter, size_type len) noexcept
111 : m_data(reinterpret_cast<const_pointer>(&(*iter))),
112 m_len(len)
113 {
114 }
115
117 template <std::size_t TN>
118 ArrayView(const T (&data)[TN]) noexcept
119 : m_data(data),
120 m_len(TN)
121 {
122 }
123
125 template <std::size_t TN>
126 ArrayView(T (&data)[TN]) noexcept
127 : m_data(data),
128 m_len(TN)
129 {
130 }
131
133 template <typename TIter>
134 ArrayView(TIter first, TIter last) noexcept
135 : m_data(reinterpret_cast<const_pointer>(&(*first))),
136 m_len(static_cast<std::size_t>(std::distance(first, last)))
137 {
138 COMMS_ASSERT(0 <= std::distance(first, last));
139 }
140
142 ~ArrayView() noexcept = default;
143
145 ArrayView& operator=(const ArrayView&) = default;
146
148 template <std::size_t TN>
149 ArrayView& operator=(const T (&data)[TN])
150 {
151 m_data = data;
152 m_len = TN;
153 return *this;
154 }
155
157 template <std::size_t TN>
159 {
160 m_data = data;
161 m_len = TN;
162 return *this;
163 }
164
166 constexpr const_iterator begin() const noexcept
167 {
168 return m_data;
169 }
170
172 constexpr const_iterator cbegin() const noexcept
173 {
174 return begin();
175 }
176
178 constexpr const_iterator end() const noexcept
179 {
180 return begin() + m_len;
181 }
182
184 constexpr const_iterator cend() const noexcept
185 {
186 return end();
187 }
188
191 {
192 return std::reverse_iterator<const_iterator>(end());
193 }
194
197 {
198 return rbegin();
199 }
200
202 reverse_iterator rend() const noexcept
203 {
204 return std::reverse_iterator<const_iterator>(begin());
205 }
206
209 {
210 return rend();
211 }
212
215 {
216 return m_data[pos];
217 }
218
222 {
223 COMMS_ASSERT(pos < m_len);
224 return m_data[pos];
225 }
226
229 constexpr const_reference front() const
230 {
231 return m_data[0];
232 }
233
236 constexpr const_reference back() const
237 {
238 return m_data[m_len - 1U];
239 }
240
242 constexpr size_type size() const noexcept
243 {
244 return m_len;
245 }
246
248 constexpr size_type length() const noexcept
249 {
250 return size();
251 }
252
255 constexpr bool empty() const noexcept
256 {
257 return size() == 0U;
258 }
259
263 {
264 std::advance(m_data, n);
265 m_len -= n;
266 }
267
271 {
272 m_len -= n;
273 }
274
276 void swap(ArrayView& other) noexcept
277 {
278 std::swap(m_data, other.m_data);
279 std::swap(m_len, other.m_len);
280 }
281
284 {
285 return m_data;
286 }
287
288private:
289 const_pointer m_data = nullptr;
290 size_type m_len = 0;
291};
292
295template<typename T>
296bool operator<(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
297{
298 return std::lexicographical_compare(view1.begin(), view1.end(), view2.begin(), view2.end());
299}
300
303template<typename T>
304bool operator<=(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
305{
306 return !(view2 < view1);
307}
308
311template<typename T>
312bool operator>(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
313{
314 return (view2 < view1);
315}
316
319template<typename T>
320bool operator>=(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
321{
322 return !(view1 < view2);
323}
324
327template<typename T>
328bool operator==(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
329{
330 return
331 (view1.size() == view2.size()) &&
332 std::equal(view1.begin(), view1.end(), view2.begin());
333}
334
337template<typename T>
338bool operator!=(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
339{
340 return !(view1 == view2);
341}
342
343} // namespace util
344
345} // namespace comms
346
347namespace std
348{
349
352template <typename T>
354{
355 view1.swap(view2);
356}
357
358} // namespace std
This file contains classes required for generic custom assertion functionality.
#define COMMS_ASSERT(expr)
Generic assert macro.
Definition Assert.h:170
Describes an object that can refer to a constant contiguous sequence of other objects.
Definition ArrayView.h:32
const_iterator ConstIterator
Same as const_iterator.
Definition ArrayView.h:75
~ArrayView() noexcept=default
Destructor.
const_reference at(size_type pos) const
Element access with range check.
Definition ArrayView.h:221
bool operator!=(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Inequality compare between the views.
Definition ArrayView.h:338
constexpr const_iterator begin() const noexcept
Iterator to begining of the sequence.
Definition ArrayView.h:166
pointer Pointer
Same as pointer.
Definition ArrayView.h:44
reverse_iterator ReverseIterator
Same as reverse_iterator.
Definition ArrayView.h:93
void remove_prefix(size_type n)
Narrow the view by skipping number of elements at the beginning.
Definition ArrayView.h:262
bool operator<(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Lexicographical compare between the views.
Definition ArrayView.h:296
bool operator<=(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Lexicographical compare between the views.
Definition ArrayView.h:304
ArrayView & operator=(T(&data)[TN])
Assign array of elements with known size.
Definition ArrayView.h:158
const_reverse_iterator crend() const noexcept
Reverse iterator to the beginning of the sequence.
Definition ArrayView.h:208
const_iterator iterator
Same as const_iterator.
Definition ArrayView.h:78
constexpr const_iterator end() const noexcept
Iterator to the end of the sequence.
Definition ArrayView.h:178
ArrayView() noexcept=default
Default constructor.
value_type ValueType
Same as value_type.
Definition ArrayView.h:38
bool operator>=(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Lexicographical compare between the views.
Definition ArrayView.h:320
constexpr size_type size() const noexcept
Get number of element in the view.
Definition ArrayView.h:242
std::reverse_iterator< const_iterator > const_reverse_iterator
Same as std::reverse_iterator<const_iterator>
Definition ArrayView.h:84
iterator Iterator
Same as iterator.
Definition ArrayView.h:81
std::size_t size_type
Equal to std::size_t.
Definition ArrayView.h:65
T * pointer
Pointer to the single element (T*)
Definition ArrayView.h:41
reverse_iterator rend() const noexcept
Reverse iterator to the beginning of the sequence.
Definition ArrayView.h:202
void remove_suffix(size_type n)
Narrow the view by dropping number of elements at the end.
Definition ArrayView.h:270
constexpr const_reference operator[](size_type pos) const
Element access operator.
Definition ArrayView.h:214
ArrayView(TIter iter, size_type len) noexcept
Constructor.
Definition ArrayView.h:110
const_reverse_iterator ConstReverseIterator
Same as const_reverse_iterator.
Definition ArrayView.h:87
reference Reference
Same as reference.
Definition ArrayView.h:56
bool operator>(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Lexicographical compare between the views.
Definition ArrayView.h:312
constexpr size_type length() const noexcept
Same as ref size()
Definition ArrayView.h:248
const_reference ConstReference
Same as const_reference.
Definition ArrayView.h:62
constexpr bool empty() const noexcept
Check the view is empty.
Definition ArrayView.h:255
ArrayView(const T(&data)[TN]) noexcept
Construct out of array of elements with known size.
Definition ArrayView.h:118
constexpr const_iterator cend() const noexcept
Iterator to the end of the sequence.
Definition ArrayView.h:184
bool operator==(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Equality compare between the views.
Definition ArrayView.h:328
void swap(ArrayView &other) noexcept
Swap contents of two views.
Definition ArrayView.h:276
const_reverse_iterator reverse_iterator
Same as const_reverse_iterator.
Definition ArrayView.h:90
size_type SizeType
Same as size_type;.
Definition ArrayView.h:68
const_pointer data() const
Returns a pointer to the beginning of the underlying sequence.
Definition ArrayView.h:283
const_pointer const_iterator
Implementation defined constant RandomAccessIterator and ContiguousIterator whose value_type is T.
Definition ArrayView.h:72
const_pointer ConstPointer
Same as const_pointer.
Definition ArrayView.h:50
ArrayView(TIter first, TIter last) noexcept
Construct out of iterators rande.
Definition ArrayView.h:134
T & reference
Reference to an element (T&)
Definition ArrayView.h:53
ArrayView(T(&data)[TN]) noexcept
Construct out of array of elements with known size.
Definition ArrayView.h:126
T value_type
Type of the single element.
Definition ArrayView.h:35
constexpr const_iterator cbegin() const noexcept
Iterator to begining of the sequence.
Definition ArrayView.h:172
const_reverse_iterator crbegin() const noexcept
Reverse iterator to the end of the sequence.
Definition ArrayView.h:196
const T & const_reference
Reference to a const element (const T&)
Definition ArrayView.h:59
const_reverse_iterator rbegin() const noexcept
Reverse iterator to the end of the sequence.
Definition ArrayView.h:190
const T * const_pointer
Pointer to the constant element (const T*)
Definition ArrayView.h:47
constexpr const_reference front() const
Access the first element.
Definition ArrayView.h:229
constexpr const_reference back() const
Access the last element.
Definition ArrayView.h:236
Main namespace for all classes / functions of COMMS library.
STL namespace.