PST SDK  6.0.0.0-272350a
PstArray.h
Go to the documentation of this file.
1 // Copyright PS-Tech B.V. All Rights Reserved.
2 
3 #pragma once
4 
5 #include "TrackerExceptions.h"
6 
7 #include <cstddef>
8 #include <cassert>
9 #include <string>
10 
11 namespace PSTech
12 {
13 namespace Utils
14 {
19  template<typename T, size_t Size>
20  class PstArray
21  {
22  public:
23  typedef T value_type;
24  typedef size_t size_type;
25  typedef T* pointer;
26  typedef const T* const_pointer;
27  typedef T& reference;
28  typedef const T& const_reference;
29  typedef T* iterator;
30  typedef const T* const_iterator;
31 
35  constexpr size_type size() const
36  {
37  return Size;
38  }
39 
41  constexpr bool empty() const
42  {
43  return Size == 0;
44  }
45 
47  const_pointer data() const
48  {
49  return _array;
50  }
51 
53  pointer data()
54  {
55  return _array;
56  }
57 
63  const_reference at(size_type index) const
64  {
65  check_bounds(index);
66  return _array[index];
67  }
68 
74  reference at(size_type index)
75  {
76  check_bounds(index);
77  return _array[index];
78  }
79 
81  const_reference front() const
82  {
83  assert(Size > 0);
84  return _array[0];
85  }
86 
88  reference front()
89  {
90  assert(Size > 0);
91  return _array[0];
92  }
93 
95  const_reference back() const
96  {
97  assert(Size > 0);
98  return _array[Size - 1];
99  }
100 
102  reference back()
103  {
104  assert(Size > 0);
105  return _array[Size - 1];
106  }
107 
109  const_iterator cbegin() const
110  {
111  return _array;
112  }
113 
115  const_iterator begin() const
116  {
117  return _array;
118  }
119 
121  iterator begin()
122  {
123  return _array;
124  }
125 
127  const_iterator cend() const
128  {
129  return _array + Size;
130  }
131 
133  const_iterator end() const
134  {
135  return _array + Size;
136  }
137 
139  iterator end()
140  {
141  return _array + Size;
142  }
143 
145  const_reference operator [] (size_type index) const
146  {
147  assert(index < Size);
148  return _array[index];
149  }
150 
152  reference operator [] (size_type index)
153  {
154  assert(index < Size);
155  return _array[index];
156  }
157 
159  bool operator==(const PstArray<T, Size>& array) const
160  {
161  for (size_t index = 0; index < Size; ++index)
162  {
163  if (_array[index] != array[index])
164  {
165  return false;
166  }
167  }
168  return true;
169  }
170 
172  bool operator!=(const PstArray<T, Size>& array) const
173  {
174  return !(*this == array);
175  }
176 
178  value_type _array[Size];
179 
180  private:
181  void check_bounds(size_type index)
182  {
183  if (index >= Size)
184  {
185  throw OutOfRangeException(("Requested index out of range: " + std::to_string(index) + " >= " + std::to_string(Size)).c_str());
186  }
187  }
188  };
189 }
190 }
bool operator!=(const PstArray< T, Size > &array) const
Definition: PstArray.h:172
Definition: ExportedTypeConversions.h:8
Definition: TrackerExceptions.h:54
T * iterator
Definition: PstArray.h:29
pointer data()
Definition: PstArray.h:53
value_type _array[Size]
Definition: PstArray.h:178
const_reference operator[](size_type index) const
Definition: PstArray.h:145
constexpr bool empty() const
Definition: PstArray.h:41
void check_bounds(size_type index)
Definition: PstArray.h:181
size_t size_type
Definition: PstArray.h:24
const_reference front() const
Definition: PstArray.h:81
const_iterator cbegin() const
Definition: PstArray.h:109
iterator begin()
Definition: PstArray.h:121
reference at(size_type index)
Definition: PstArray.h:74
const_reference back() const
Definition: PstArray.h:95
iterator end()
Definition: PstArray.h:139
const T * const_pointer
Definition: PstArray.h:26
reference front()
Definition: PstArray.h:88
T * pointer
Definition: PstArray.h:25
constexpr size_type size() const
Definition: PstArray.h:35
const_pointer data() const
Definition: PstArray.h:47
T & reference
Definition: PstArray.h:27
const T * const_iterator
Definition: PstArray.h:30
reference back()
Definition: PstArray.h:102
const_reference at(size_type index) const
Definition: PstArray.h:63
bool operator==(const PstArray< T, Size > &array) const
Definition: PstArray.h:159
T value_type
Definition: PstArray.h:23
const_iterator cend() const
Definition: PstArray.h:127
const T & const_reference
Definition: PstArray.h:28
const_iterator end() const
Definition: PstArray.h:133
const_iterator begin() const
Definition: PstArray.h:115
Definition: PstArray.h:20