GEGELATI

Loading...
Searching...
No Matches
primitiveTypeArray.h
1
39#ifndef PRIMITIVE_TYPE_ARRAY_H
40#define PRIMITIVE_TYPE_ARRAY_H
41
42#include "data/arrayWrapper.h"
43#include "data/dataHandler.h"
44#include "data/pointerWrapper.h"
45
46 namespace Data {
54 template <class T> class PrimitiveTypeArray : public ArrayWrapper<T>
55 {
56 protected:
60 std::vector<T> data;
61
62 public:
69 PrimitiveTypeArray(size_t size = 8);
70
72 PrimitiveTypeArray(const PrimitiveTypeArray<T>& other);
73
75 PrimitiveTypeArray(const ArrayWrapper<T>& other);
76
78 PrimitiveTypeArray(const PointerWrapper<T>& other);
79
81 virtual ~PrimitiveTypeArray() = default;
82
84 virtual DataHandler* clone() const override;
85
90 void resetData() override;
91
110 void setDataAt(const std::type_info& type, const size_t address,
111 const T& value);
126 PrimitiveTypeArray<T>& operator=(const PrimitiveTypeArray<T>& other);
127 };
128
129 template <class T>
130 PrimitiveTypeArray<T>::PrimitiveTypeArray(size_t size)
131 : ArrayWrapper<T>(size, nullptr), data(size)
132 {
133 this->setPointer(&(this->data));
134 }
135
136 template <class T>
137 PrimitiveTypeArray<T>::PrimitiveTypeArray(
138 const PrimitiveTypeArray<T>& other)
139 : ArrayWrapper<T>(other), data(other.data)
140 {
141 // Set the pointer to the right data
142 this->setPointer(&(this->data));
143 }
144
145 template <class T>
146 PrimitiveTypeArray<T>::PrimitiveTypeArray(const ArrayWrapper<T>& other)
147 : ArrayWrapper<T>(other), data(this->nbElements)
148 {
149 if (this->containerPtr != NULL) {
150 // Copy the data from the given ArrayWrapper
151 for (size_t i = 0; i < this->nbElements; i++) {
152 // exploit the fact that the container pointer still points to
153 // data from other.
154 this->data[i] = this->containerPtr->at(i);
155 }
156 }
157 else {
158 this->resetData();
159 }
160
161 // Set the pointer to the right data
162 this->setPointer(&(this->data));
163 }
164
165 template <class T>
166 PrimitiveTypeArray<T>::PrimitiveTypeArray(const PointerWrapper<T>& other)
167 : ArrayWrapper<T>(other, 1), data(1)
168 {
169 if (other.containerPtr != NULL) {
170 // Copy the data from the given PointerWrapper
171 this->data[0] = *other.containerPtr;
172 }
173 else {
174 this->resetData();
175 }
176
177 // Set the pointer to the right data
178 this->setPointer(&(this->data));
179 }
180
181 template <class T> inline DataHandler* PrimitiveTypeArray<T>::clone() const
182 {
183 // Default copy construtor does the deep copy.
184 PrimitiveTypeArray<T>* result = new PrimitiveTypeArray<T>(*this);
185
186 return result;
187 }
188
189 template <class T> void PrimitiveTypeArray<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 PrimitiveTypeArray<T>::setDataAt(const std::type_info& type,
201 const size_t address, const T& value)
202 {
203#ifndef NDEBUG
204 // Throw exception in case of invalid arguments.
205 this->checkAddressAndType(type, address);
206#endif
207
208 this->data.at(address) = value;
209
210 // Invalidate the cached hash.
211 this->invalidCachedHash = true;
212 }
213 template <class T>
214 PrimitiveTypeArray<T>& PrimitiveTypeArray<T>::operator=(
215 const PrimitiveTypeArray<T>& other)
216 {
217 // Guard self assignment
218 if (this != &other) {
219 if (this->nbElements != other.nbElements) {
220 std::stringstream message;
221 message << "Assigned PrimitiveTypeArray do not have the same "
222 "size : "
223 << this->nbElements << " / " << other.nbElements << ".";
224 throw std::domain_error(message.str());
225 }
226
227 // Copy Data from right arg to this
228 for (auto i = 0; i < this->nbElements; i++) {
229 this->data.at(i) = other.data.at(i);
230 }
231 }
232 return *this;
233 }
234} // namespace Data
235
236#endif
Data::ArrayWrapper
Definition arrayWrapper.h:69
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::PrimitiveTypeArray2D
PrimitiveTypeArray2D(const size_t w=2, const size_t h=4)
Constructor for the 2D array.
Definition primitiveTypeArray2D.h:142
Data::PrimitiveTypeArray
Definition primitiveTypeArray.h:55
Data::PrimitiveTypeArray::PrimitiveTypeArray
PrimitiveTypeArray(size_t size=8)
Constructor for the PrimitiveTypeArray class.
Definition primitiveTypeArray.h:130
Data::PrimitiveTypeArray::~PrimitiveTypeArray
virtual ~PrimitiveTypeArray()=default
Default destructor.
Data::PrimitiveTypeArray::data
std::vector< T > data
Array storing the data of the PrimitiveTypeArray.
Definition primitiveTypeArray.h:60
Data::PrimitiveTypeArray::PrimitiveTypeArray
PrimitiveTypeArray(const PointerWrapper< T > &other)
Copy content from a PointerWrapper.
Definition primitiveTypeArray.h:166
Data::PrimitiveTypeArray::clone
virtual DataHandler * clone() const override
Inherited from DataHandler.
Definition primitiveTypeArray.h:181
Data::PrimitiveTypeArray::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 primitiveTypeArray.h:200
Data::PrimitiveTypeArray::PrimitiveTypeArray
PrimitiveTypeArray(const PrimitiveTypeArray< T > &other)
Copy constructor (deep copy).
Definition primitiveTypeArray.h:137
Data::PrimitiveTypeArray::operator=
PrimitiveTypeArray< T > & operator=(const PrimitiveTypeArray< T > &other)
Assignement Operator for PrimitiveTypeArray<T>
Definition primitiveTypeArray.h:214
Data::PrimitiveTypeArray::resetData
void resetData() override
Sets all elements of the Array to 0 (or its equivalent for the given template param....
Definition primitiveTypeArray.h:189
Data::PrimitiveTypeArray::PrimitiveTypeArray
PrimitiveTypeArray(const ArrayWrapper< T > &other)
Copy content from an ArrayWrapper.
Definition primitiveTypeArray.h:146
Data
Definition array2DWrapper.h:44