Version 8.9.0
 
Loading...
Searching...
No Matches
IRArray.h
1#pragma once
2/******************************************************************************
3 * Copyright (c) 2012-2019 All Rights Reserved, http://www.evocortex.com *
4 * Evocortex GmbH *
5 * Emilienstr. 10 *
6 * 90489 Nuremberg *
7 * Germany *
8 *****************************************************************************/
9#include <cstddef>
10#include <stdexcept>
11#include <sstream>
12
13namespace evo
14{
15
21template <class T>
23{
24private:
25 //Array pointer
26 T* _data;
27
28 //Size of array
29 std::size_t _size;
30public:
31
32
37 IRArray(std::size_t size);
38
44 IRArray(std::size_t size, const T* const data);
45
50 IRArray(const IRArray<T> &obj);
51
56
62
64
69 T& operator[](const std::size_t index);
70
75 const T& operator[](const std::size_t index) const;
76
81 std::size_t size() const;
82
87 T* const data() const;
88};
89
90} //namespace evo
91
92template <class T>
93evo::IRArray<T>::IRArray(std::size_t size)
94{
95 _size = size;
96 if(_size > 0)
97 {
98 _data = new T[_size];
99 }
100 else
101 {
102 _data = nullptr;
103 }
104}
105
106template <class T>
107evo::IRArray<T>::IRArray(std::size_t size, const T* const data)
108{
109 _size = size;
110 if(_size > 0)
111 {
112 _data = (T*)(new char[size * sizeof(T)]);
113 std::copy(data, data + _size, _data);
114 }
115 else
116 {
117 _data = nullptr;
118 }
119}
120
121template <class T>
122evo::IRArray<T>::IRArray(const evo::IRArray<T> &obj) : IRArray(obj._size, obj._data)
123{
124}
125
126template <class T>
128{
129 if(_data != nullptr)
130 {
131 delete[] _data;
132 _data = nullptr;
133 }
134}
135
136template <class T>
138{
139 IRArray<T> tmp(obj);
140 _size = tmp._size;
141 _data = tmp._data;
142
143 tmp._size = 0;
144 tmp._data = nullptr;
145 return *this;
146}
147
148template <class T>
150{
151 _size = obj._size;
152 _data = obj._data;
153
154 obj._size = 0;
155 obj._data = nullptr;
156 return *this;
157}
158
159template <class T>
160T& evo::IRArray<T>::operator[](const std::size_t index)
161{
162 if (index < 0 || index >= _size)
163 {
164 std::stringstream ss;
165 ss << "Index " << index << " out of range. [0.." << _size << "[";
166 throw std::out_of_range(ss.str());
167 }
168 return _data[index];
169}
170
171template <class T>
172const T& evo::IRArray<T>::operator[](const std::size_t index) const
173{
174 if (index < 0 || index >= _size)
175 {
176 std::stringstream ss;
177 ss << "Index " << index << " out of range. [0.." << _size << "[";
178 throw std::out_of_range(ss.str());
179 }
180 return _data[index];
181}
182
183template <class T>
184std::size_t evo::IRArray<T>::size() const
185{
186 return _size;
187}
188
189template <class T>
190T* const evo::IRArray<T>::data() const
191{
192 return _data;
193}
C-Array wrapper for handle size, copy and memory.
Definition: IRArray.h:23
~IRArray()
Deconstructor.
Definition: IRArray.h:127
IRArray< T > & operator=(const IRArray< T > &obj)
Assignment operator for copy IRArray.
Definition: IRArray.h:137
IRArray(std::size_t size, const T *const data)
Initialize array with given size and data.
Definition: IRArray.h:107
const T & operator[](const std::size_t index) const
Const index operator.
Definition: IRArray.h:172
IRArray(std::size_t size)
Initialize array with given size.
Definition: IRArray.h:93
IRArray(const IRArray< T > &obj)
Copy constructor to copy given IRArray.
Definition: IRArray.h:122
T *const data() const
Returns pointer to array.
Definition: IRArray.h:190
T & operator[](const std::size_t index)
Index operator.
Definition: IRArray.h:160
std::size_t size() const
Returns size of array.
Definition: IRArray.h:184