libirimager
IR Imager Direct-SDK
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 
18 template <class T>
19 class IRArray
20 {
21 private:
22  //Array pointer
23  T* _data;
24 
25  //Size of array
26  std::size_t _size;
27 public:
28 
29 
34  IRArray(std::size_t size);
35 
41  IRArray(std::size_t size, const T* const data);
42 
47  IRArray(const IRArray<T> &obj);
48 
52  ~IRArray();
53 
58  IRArray<T>& operator=(const IRArray<T>& obj);
59 
61 
66  T& operator[](const std::size_t index);
67 
72  const T& operator[](const std::size_t index) const;
73 
78  std::size_t size() const;
79 
84  T* const data() const;
85 };
86 
87 template <class T>
89 {
90  _size = size;
91  if(_size > 0)
92  {
93  _data = new T[_size];
94  }
95  else
96  {
97  _data = nullptr;
98  }
99 }
100 
101 template <class T>
102 IRArray<T>::IRArray(std::size_t size, const T* const data)
103 {
104  _size = size;
105  if(_size > 0)
106  {
107  _data = (T*)(new char[size * sizeof(T)]);
108  std::copy(data, data + _size, _data);
109  }
110  else
111  {
112  _data = nullptr;
113  }
114 }
115 
116 template <class T>
117 IRArray<T>::IRArray(const IRArray<T> &obj) : IRArray(obj._size, obj._data)
118 {
119 }
120 
121 template <class T>
123 {
124  if(_data != nullptr)
125  {
126  delete[] _data;
127  _data = nullptr;
128  }
129 }
130 
131 template <class T>
133 {
134  IRArray<T> tmp(obj);
135  _size = tmp._size;
136  _data = tmp._data;
137 
138  tmp._size = 0;
139  tmp._data = nullptr;
140  return *this;
141 }
142 
143 template <class T>
145 {
146  _size = obj._size;
147  _data = obj._data;
148 
149  obj._size = 0;
150  obj._data = nullptr;
151  return *this;
152 }
153 
154 template <class T>
155 T& IRArray<T>::operator[](const std::size_t index)
156 {
157  if (index < 0 || index >= _size)
158  {
159  std::stringstream ss;
160  ss << "Index " << index << " out of range. [0.." << _size << "[";
161  throw std::out_of_range(ss.str());
162  }
163  return _data[index];
164 }
165 
166 template <class T>
167 const T& IRArray<T>::operator[](const std::size_t index) const
168 {
169  if (index < 0 || index >= _size)
170  {
171  std::stringstream ss;
172  ss << "Index " << index << " out of range. [0.." << _size << "[";
173  throw std::out_of_range(ss.str());
174  }
175  return _data[index];
176 }
177 
178 template <class T>
179 std::size_t IRArray<T>::size() const
180 {
181  return _size;
182 }
183 
184 template <class T>
185 T* const IRArray<T>::data() const
186 {
187  return _data;
188 }
C-Array wrapper for handle size, copy and memory.
Definition: IRArray.h:19
T & operator[](const std::size_t index)
Index operator.
Definition: IRArray.h:155
IRArray(std::size_t size)
Initialize array with given size.
Definition: IRArray.h:88
~IRArray()
Deconstructor.
Definition: IRArray.h:122
std::size_t size() const
Returns size of array.
Definition: IRArray.h:179
T *const data() const
Returns pointer to array.
Definition: IRArray.h:185
IRArray< T > & operator=(const IRArray< T > &obj)
Assignment operator for copy IRArray.
Definition: IRArray.h:132