GEGELATI

Loading...
Searching...
No Matches
primitiveTypeArray2D.h
1
38#ifndef PRIMITIVE_TYPE_ARRAY_2D_H
39#define PRIMITIVE_TYPE_ARRAY_2D_H
40
41#include <tuple>
42
43#include "data/array2DWrapper.h"
44#include "data/dataHandler.h"
45#include "data/primitiveTypeArray.h"
46
47 namespace Data {
48
68 template <typename T> class PrimitiveTypeArray2D : public Array2DWrapper<T>
69 {
70 protected:
74 std::vector<T> data;
75
76 public:
86 PrimitiveTypeArray2D(const size_t w = 2, const size_t h = 4);
87
89 PrimitiveTypeArray2D(const PrimitiveTypeArray2D<T>& other);
90
92 PrimitiveTypeArray2D(const Array2DWrapper<T>& other);
93
95 virtual DataHandler* clone() const override;
96
101 void resetData() override;
102
121 void setDataAt(const std::type_info& type, const size_t address,
122 const T& value);
137 PrimitiveTypeArray2D<T>& operator=(
138 const PrimitiveTypeArray2D<T>& other);
139 };
140
141 template <typename T>
142 inline PrimitiveTypeArray2D<T>::PrimitiveTypeArray2D(const size_t w,
143 const size_t h)
144 : Array2DWrapper<T>(w, h, nullptr), data(h * w)
145 {
146 // Set the pointer to the right data
147 this->setPointer(&(this->data));
148 }
149
150 template <typename T>
151 inline PrimitiveTypeArray2D<T>::PrimitiveTypeArray2D(
152 const PrimitiveTypeArray2D<T>& other)
153 : Array2DWrapper<T>(other), data(other.data)
154 {
155 // Set the pointer to the right data
156 this->setPointer(&(this->data));
157 }
158
159 template <class T>
160 PrimitiveTypeArray2D<T>::PrimitiveTypeArray2D(
161 const Array2DWrapper<T>& other)
162 : Array2DWrapper<T>(other), data(this->nbElements)
163 {
164 if (this->containerPtr != NULL) {
165 // Copy the data from the given ArrayWrapper
166 for (size_t i = 0; i < this->nbElements; i++) {
167 // exploit the fact that the container pointer still points to
168 // data from other.
169 this->data[i] = this->containerPtr->at(i);
170 }
171 }
172 else {
173 this->resetData();
174 }
175
176 // Set the pointer to the right data
177 this->setPointer(&(this->data));
178 }
179
180 template <typename T>
181 inline DataHandler* PrimitiveTypeArray2D<T>::clone() const
182 {
183 // Copy construtor should do the deep copy.
184 DataHandler* result = new PrimitiveTypeArray2D<T>(*this);
185
186 return result;
187 }
188
189 template <class T> void PrimitiveTypeArray2D<T>::resetData()
190 {
191 for (T& elt : this->data) {
192 elt = T{0};
193 }
194
195 // Invalidate the cached hash
196 this->invalidCachedHash = true;
197 }
198
199 template <class T>
200 void PrimitiveTypeArray2D<T>::setDataAt(const std::type_info& type,
201 const size_t address,
202 const T& value)
203 {
204#ifndef NDEBUG
205 // Throw exception in case of invalid arguments.
206 this->checkAddressAndType(type, address);
207#endif
208
209 this->data.at(address) = value;
210
211 // Invalidate the cached hash.
212 this->invalidCachedHash = true;
213 }
214
215 template <class T>
216 PrimitiveTypeArray2D<T>& PrimitiveTypeArray2D<T>::operator=(
217 const PrimitiveTypeArray2D<T>& other)
218 {
219 // Guard self assignment
220 if (this != &other) {
221 if (this->nbElements != other.nbElements) {
222 std::stringstream message;
223 message << "Assigned PrimitiveTypeArray2D do not have the same "
224 "size : "
225 << this->nbElements << " / " << other.nbElements << ".";
226 throw std::domain_error(message.str());
227 }
228
229 // Copy Data from right arg to this
230 for (auto i = 0; i < this->nbElements; i++) {
231 this->data.at(i) = other.data.at(i);
232 }
233 }
234 return *this;
235 }
236}; // namespace Data
237#endif
Data::Array2DWrapper
DataHandler for 2D arrays of primitive types.
Definition array2DWrapper.h:68
Data::ArrayWrapper::containerPtr
std::vector< T > * containerPtr
Pointer to the array containing the data accessed through the ArrayWrapper.
Definition arrayWrapper.h:105
Data::ArrayWrapper::nbElements
const size_t nbElements
Number of elements contained pointer vector.
Definition arrayWrapper.h:99
Data::ArrayWrapper::setPointer
void setPointer(std::vector< T > *ptr)
Set the pointer of the ArrayWrapper.
Definition arrayWrapper.h:383
Data::ArrayWrapper::checkAddressAndType
void checkAddressAndType(const std::type_info &type, const size_t &address) const
Definition arrayWrapper.h:232
Data::DataHandler
Base class for all sources of data to be accessed by a TPG Instruction executed within a Program.
Definition dataHandler.h:55
Data::DataHandler::invalidCachedHash
bool invalidCachedHash
Boolean value indicating whether the current cachedValue is valid, or not.
Definition dataHandler.h:90
Data::PrimitiveTypeArray2D
DataHandler for 2D arrays of primitive types.
Definition primitiveTypeArray2D.h:69
Data::PrimitiveTypeArray2D::data
std::vector< T > data
Array storing the data of the PrimitiveTypeArray2D.
Definition primitiveTypeArray2D.h:74
Data::PrimitiveTypeArray2D::operator=
PrimitiveTypeArray2D< T > & operator=(const PrimitiveTypeArray2D< T > &other)
Assignement Operator for PrimitiveTypeArray2D<T>
Definition primitiveTypeArray2D.h:216
Data::PrimitiveTypeArray2D::PrimitiveTypeArray2D
PrimitiveTypeArray2D(const size_t w=2, const size_t h=4)
Constructor for the 2D array.
Definition primitiveTypeArray2D.h:142
Data::PrimitiveTypeArray2D::resetData
void resetData() override
Sets all elements of the Array to 0 (or its equivalent for the given template param....
Definition primitiveTypeArray2D.h:189
Data::PrimitiveTypeArray2D::clone
virtual DataHandler * clone() const override
Inherited from DataHandler.
Definition primitiveTypeArray2D.h:181
Data::PrimitiveTypeArray2D::setDataAt
void setDataAt(const std::type_info &type, const size_t address, const T &value)
Set the data at the given address to the given value.
Definition primitiveTypeArray2D.h:200
Data
Definition array2DWrapper.h:44