Loading...
Searching...
No Matches
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}
TrackerExceptions.h
PSTech::OutOfRangeException
Definition TrackerExceptions.h:59
PSTech::Utils::PstArray
Definition PstArray.h:21
PSTech::Utils::PstArray::check_bounds
void check_bounds(size_type index)
Definition PstArray.h:181
PSTech::Utils::PstArray::front
const_reference front() const
Definition PstArray.h:81
PSTech::Utils::PstArray::value_type
T value_type
Definition PstArray.h:23
PSTech::Utils::PstArray::begin
const_iterator begin() const
Definition PstArray.h:115
PSTech::Utils::PstArray::const_iterator
const T * const_iterator
Definition PstArray.h:30
PSTech::Utils::PstArray::size
constexpr size_type size() const
Definition PstArray.h:35
PSTech::Utils::PstArray::data
const_pointer data() const
Definition PstArray.h:47
PSTech::Utils::PstArray::iterator
T * iterator
Definition PstArray.h:29
PSTech::Utils::PstArray::data
pointer data()
Definition PstArray.h:53
PSTech::Utils::PstArray::cbegin
const_iterator cbegin() const
Definition PstArray.h:109
PSTech::Utils::PstArray::size_type
size_t size_type
Definition PstArray.h:24
PSTech::Utils::PstArray::const_pointer
const T * const_pointer
Definition PstArray.h:26
PSTech::Utils::PstArray::back
const_reference back() const
Definition PstArray.h:95
PSTech::Utils::PstArray::pointer
T * pointer
Definition PstArray.h:25
PSTech::Utils::PstArray::front
reference front()
Definition PstArray.h:88
PSTech::Utils::PstArray::at
const_reference at(size_type index) const
Definition PstArray.h:63
PSTech::Utils::PstArray::cend
const_iterator cend() const
Definition PstArray.h:127
PSTech::Utils::PstArray::reference
T & reference
Definition PstArray.h:27
PSTech::Utils::PstArray::_array
value_type _array[Size]
Definition PstArray.h:178
PSTech::Utils::PstArray::at
reference at(size_type index)
Definition PstArray.h:74
PSTech::Utils::PstArray::end
iterator end()
Definition PstArray.h:139
PSTech::Utils::PstArray::end
const_iterator end() const
Definition PstArray.h:133
PSTech::Utils::PstArray::begin
iterator begin()
Definition PstArray.h:121
PSTech::Utils::PstArray::back
reference back()
Definition PstArray.h:102
PSTech::Utils::PstArray::const_reference
const T & const_reference
Definition PstArray.h:28
PSTech::Utils::PstArray::empty
constexpr bool empty() const
Definition PstArray.h:41
PSTech::Utils
Definition PstArray.h:14
PSTech
Definition ExportedTypeConversions.h:9