COMMS
Template library intended to help with implementation of communication protocols.
ArrayView.h
Go to the documentation of this file.
1 //
2 // Copyright 2017 - 2024 (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 
12 #pragma once
13 
14 #include <algorithm>
15 #include <iterator>
16 
17 #include "comms/Assert.h"
18 
19 namespace comms
20 {
21 
22 namespace util
23 {
24 
29 template <typename T>
30 class ArrayView
31 {
32 public:
34  using value_type = T;
35 
38 
40  using pointer = T*;
41 
43  using Pointer = pointer;
44 
46  using const_pointer = const T*;
47 
50 
52  using reference = T&;
53 
56 
58  using const_reference = const T&;
59 
62 
64  using size_type = std::size_t;
65 
68 
72 
75 
78 
80  using Iterator = iterator;
81 
83  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
84 
87 
90 
93 
95  ArrayView() noexcept = default;
96 
98  ArrayView(const ArrayView&) noexcept = default;
99 
101  ArrayView(const_pointer data, size_type len) noexcept
102  : data_(data),
103  len_(len)
104  {
105  }
106 
108  template <typename TIter>
109  ArrayView(TIter iter, size_type len) noexcept
110  : data_(reinterpret_cast<const_pointer>(&(*iter))),
111  len_(len)
112  {
113  }
114 
116  template <std::size_t TN>
117  ArrayView(const T (&data)[TN]) noexcept
118  : data_(data),
119  len_(TN)
120  {
121  }
122 
124  template <std::size_t TN>
125  ArrayView(T (&data)[TN]) noexcept
126  : data_(data),
127  len_(TN)
128  {
129  }
130 
132  ~ArrayView() noexcept = default;
133 
135  ArrayView& operator=(const ArrayView&) = default;
136 
138  template <std::size_t TN>
139  ArrayView& operator=(const T (&data)[TN])
140  {
141  data_ = data;
142  len_ = TN;
143  return *this;
144  }
145 
147  template <std::size_t TN>
148  ArrayView& operator=(T (&data)[TN])
149  {
150  data_ = data;
151  len_ = TN;
152  return *this;
153  }
154 
156  constexpr const_iterator begin() const noexcept
157  {
158  return data_;
159  }
160 
162  constexpr const_iterator cbegin() const noexcept
163  {
164  return begin();
165  }
166 
168  constexpr const_iterator end() const noexcept
169  {
170  return begin() + len_;
171  }
172 
174  constexpr const_iterator cend() const noexcept
175  {
176  return end();
177  }
178 
181  {
182  return std::reverse_iterator<const_iterator>(end());
183  }
184 
187  {
188  return rbegin();
189  }
190 
192  reverse_iterator rend() const noexcept
193  {
194  return std::reverse_iterator<const_iterator>(begin());
195  }
196 
198  const_reverse_iterator crend() const noexcept
199  {
200  return rend();
201  }
202 
204  constexpr const_reference operator[](size_type pos) const
205  {
206  return data_[pos];
207  }
208 
212  {
213  COMMS_ASSERT(pos < len_);
214  return data_[pos];
215  }
216 
219  constexpr const_reference front() const
220  {
221  return data_[0];
222  }
223 
226  constexpr const_reference back() const
227  {
228  return data_[len_ - 1U];
229  }
230 
232  constexpr size_type size() const noexcept
233  {
234  return len_;
235  }
236 
238  constexpr size_type length() const noexcept
239  {
240  return size();
241  }
242 
245  constexpr bool empty() const noexcept
246  {
247  return size() == 0U;
248  }
249 
253  {
254  std::advance(data_, n);
255  len_ -= n;
256  }
257 
261  {
262  len_ -= n;
263  }
264 
266  void swap(ArrayView& other) noexcept
267  {
268  std::swap(data_, other.data_);
269  std::swap(len_, other.len_);
270  }
271 
272 
273 private:
274  const_pointer data_ = nullptr;
275  size_type len_ = 0;
276 };
277 
280 template<typename T>
281 bool operator<(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
282 {
283  return std::lexicographical_compare(view1.begin(), view1.end(), view2.begin(), view2.end());
284 }
285 
288 template<typename T>
289 bool operator<=(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
290 {
291  return !(view2 < view1);
292 }
293 
296 template<typename T>
297 bool operator>(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
298 {
299  return (view2 < view1);
300 }
301 
304 template<typename T>
305 bool operator>=(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
306 {
307  return !(view1 < view2);
308 }
309 
312 template<typename T>
313 bool operator==(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
314 {
315  return
316  (view1.size() == view2.size()) &&
317  std::equal(view1.begin(), view1.end(), view2.begin());
318 }
319 
322 template<typename T>
323 bool operator!=(const ArrayView<T>& view1, const ArrayView<T>& view2) noexcept
324 {
325  return !(view1 == view2);
326 }
327 
328 
329 } // namespace util
330 
331 } // namespace comms
332 
333 namespace std
334 {
335 
338 template <typename T>
340 {
341  view1.swap(view2);
342 }
343 
344 } // 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:31
const_iterator ConstIterator
Same as const_iterator.
Definition: ArrayView.h:74
~ArrayView() noexcept=default
Destructor.
const_reference at(size_type pos) const
Element access with range check.
Definition: ArrayView.h:211
bool operator!=(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Inequality compare between the views.
Definition: ArrayView.h:323
constexpr const_iterator begin() const noexcept
Iterator to begining of the sequence.
Definition: ArrayView.h:156
pointer Pointer
Same as pointer.
Definition: ArrayView.h:43
reverse_iterator ReverseIterator
Same as reverse_iterator.
Definition: ArrayView.h:92
void remove_prefix(size_type n)
Narrow the view by skipping number of elements at the beginning.
Definition: ArrayView.h:252
bool operator<(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Lexicographical compare between the views.
Definition: ArrayView.h:281
bool operator<=(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Lexicographical compare between the views.
Definition: ArrayView.h:289
const_reverse_iterator crend() const noexcept
Reverse iterator to the beginning of the sequence.
Definition: ArrayView.h:198
const_iterator iterator
Same as const_iterator.
Definition: ArrayView.h:77
constexpr const_iterator end() const noexcept
Iterator to the end of the sequence.
Definition: ArrayView.h:168
ArrayView() noexcept=default
Default constructor.
value_type ValueType
Same as value_type.
Definition: ArrayView.h:37
bool operator>=(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Lexicographical compare between the views.
Definition: ArrayView.h:305
constexpr size_type size() const noexcept
Get number of element in the view.
Definition: ArrayView.h:232
std::reverse_iterator< const_iterator > const_reverse_iterator
Same as std::reverse_iterator<const_iterator>
Definition: ArrayView.h:83
iterator Iterator
Same as iterator.
Definition: ArrayView.h:80
std::size_t size_type
Equal to std::size_t.
Definition: ArrayView.h:64
T * pointer
Pointer to the single element (T*)
Definition: ArrayView.h:40
reverse_iterator rend() const noexcept
Reverse iterator to the beginning of the sequence.
Definition: ArrayView.h:192
void remove_suffix(size_type n)
Narrow the view by dropping number of elements at the end.
Definition: ArrayView.h:260
constexpr const_reference operator[](size_type pos) const
Element access operator.
Definition: ArrayView.h:204
ArrayView(TIter iter, size_type len) noexcept
Constructor.
Definition: ArrayView.h:109
const_reverse_iterator ConstReverseIterator
Same as const_reverse_iterator.
Definition: ArrayView.h:86
reference Reference
Same as reference.
Definition: ArrayView.h:55
bool operator>(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Lexicographical compare between the views.
Definition: ArrayView.h:297
constexpr size_type length() const noexcept
Same as ref size()
Definition: ArrayView.h:238
const_reference ConstReference
Same as const_reference.
Definition: ArrayView.h:61
constexpr bool empty() const noexcept
Check the view is empty.
Definition: ArrayView.h:245
ArrayView(const T(&data)[TN]) noexcept
Construct out of array of elements with known size.
Definition: ArrayView.h:117
constexpr const_iterator cend() const noexcept
Iterator to the end of the sequence.
Definition: ArrayView.h:174
bool operator==(const ArrayView< T > &view1, const ArrayView< T > &view2) noexcept
Equality compare between the views.
Definition: ArrayView.h:313
ArrayView & operator=(T(&data)[TN])
Assign array of elements with known size.
Definition: ArrayView.h:148
void swap(ArrayView &other) noexcept
Swap contents of two views.
Definition: ArrayView.h:266
const_reverse_iterator reverse_iterator
Same as const_reverse_iterator.
Definition: ArrayView.h:89
size_type SizeType
Same as size_type;.
Definition: ArrayView.h:67
const_pointer const_iterator
Implementation defined constant RandomAccessIterator and ContiguousIterator whose value_type is T.
Definition: ArrayView.h:71
const_pointer ConstPointer
Same as const_pointer.
Definition: ArrayView.h:49
T & reference
Reference to an element (T&)
Definition: ArrayView.h:52
ArrayView(T(&data)[TN]) noexcept
Construct out of array of elements with known size.
Definition: ArrayView.h:125
T value_type
Type of the single element.
Definition: ArrayView.h:34
constexpr const_iterator cbegin() const noexcept
Iterator to begining of the sequence.
Definition: ArrayView.h:162
const_reverse_iterator crbegin() const noexcept
Reverse iterator to the end of the sequence.
Definition: ArrayView.h:186
const T & const_reference
Reference to a const element (const T&)
Definition: ArrayView.h:58
const_reverse_iterator rbegin() const noexcept
Reverse iterator to the end of the sequence.
Definition: ArrayView.h:180
const T * const_pointer
Pointer to the constant element (const T*)
Definition: ArrayView.h:46
constexpr const_reference front() const
Access the first element.
Definition: ArrayView.h:219
void swap(comms::util::ArrayView< T > &view1, comms::util::ArrayView< T > &view2)
Specializes the std::swap algorithm.
Definition: ArrayView.h:339
constexpr const_reference back() const
Access the last element.
Definition: ArrayView.h:226
Main namespace for all classes / functions of COMMS library.