GEGELATI
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
45namespace Data {
53 template <class T> class PrimitiveTypeArray : public ArrayWrapper<T>
54 {
55 protected:
59 std::vector<T> data;
60
61 public:
68 PrimitiveTypeArray(size_t size = 8);
69
72
75
77 virtual ~PrimitiveTypeArray() = default;
78
80 virtual DataHandler* clone() const override;
81
86 void resetData() override;
87
106 void setDataAt(const std::type_info& type, const size_t address,
107 const T& value);
123 };
124
125 template <class T>
127 : ArrayWrapper<T>(size, nullptr), data(size)
128 {
129 this->setPointer(&(this->data));
130 }
131
132 template <class T>
134 const PrimitiveTypeArray<T>& other)
135 : ArrayWrapper<T>(other), data(other.data)
136 {
137 // Set the pointer to the right data
138 this->setPointer(&(this->data));
139 }
140
141 template <class T>
143 : ArrayWrapper<T>(other), data(this->nbElements)
144 {
145 if (this->containerPtr != NULL) {
146 // Copy the data from the given ArrayWrapper
147 for (size_t i = 0; i < this->nbElements; i++) {
148 // exploit the fact that the container pointer still points to
149 // data from other.
150 this->data[i] = this->containerPtr->at(i);
151 }
152 }
153 else {
154 this->resetData();
155 }
156
157 // Set the pointer to the right data
158 this->setPointer(&(this->data));
159 }
160
161 template <class T> inline DataHandler* PrimitiveTypeArray<T>::clone() const
162 {
163 // Default copy construtor does the deep copy.
164 PrimitiveTypeArray<T>* result = new PrimitiveTypeArray<T>(*this);
165
166 return result;
167 }
168
169 template <class T> void PrimitiveTypeArray<T>::resetData()
170 {
171 for (T& elt : this->data) {
172 elt = T{0};
173 }
174
175 // Invalidate the cached hash
176 this->invalidCachedHash = true;
177 }
178
179 template <class T>
180 void PrimitiveTypeArray<T>::setDataAt(const std::type_info& type,
181 const size_t address, const T& value)
182 {
183#ifndef NDEBUG
184 // Throw exception in case of invalid arguments.
185 this->checkAddressAndType(type, address);
186#endif
187
188 this->data.at(address) = value;
189
190 // Invalidate the cached hash.
191 this->invalidCachedHash = true;
192 }
193 template <class T>
195 const PrimitiveTypeArray<T>& other)
196 {
197 // Guard self assignment
198 if (this != &other) {
199 if (this->nbElements != other.nbElements) {
200 std::stringstream message;
201 message << "Assigned PrimitiveTypeArray do not have the same "
202 "size : "
203 << this->nbElements << " / " << other.nbElements << ".";
204 throw std::domain_error(message.str());
205 }
206
207 // Copy Data from right arg to this
208 for (auto i = 0; i < this->nbElements; i++) {
209 this->data.at(i) = other.data.at(i);
210 }
211 }
212 return *this;
213 }
214} // namespace Data
215
216#endif
Definition: arrayWrapper.h:69
std::vector< T > * containerPtr
Pointer to the array containing the data accessed through the ArrayWrapper.
Definition: arrayWrapper.h:105
const size_t nbElements
Number of elements contained pointer vector.
Definition: arrayWrapper.h:99
void setPointer(std::vector< T > *ptr)
Set the pointer of the ArrayWrapper.
Definition: arrayWrapper.h:365
Base class for all sources of data to be accessed by a TPG Instruction executed within a Program.
Definition: dataHandler.h:54
Definition: primitiveTypeArray.h:54
PrimitiveTypeArray(size_t size=8)
Constructor for the PrimitiveTypeArray class.
Definition: primitiveTypeArray.h:126
virtual ~PrimitiveTypeArray()=default
Default destructor.
std::vector< T > data
Array storing the data of the PrimitiveTypeArray.
Definition: primitiveTypeArray.h:59
virtual DataHandler * clone() const override
Inherited from DataHandler.
Definition: primitiveTypeArray.h:161
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:180
PrimitiveTypeArray(const PrimitiveTypeArray< T > &other)
Copy constructor (deep copy).
Definition: primitiveTypeArray.h:133
PrimitiveTypeArray< T > & operator=(const PrimitiveTypeArray< T > &other)
Assignement Operator for PrimitiveTypeArray<T>
Definition: primitiveTypeArray.h:194
void resetData() override
Sets all elements of the Array to 0 (or its equivalent for the given template param....
Definition: primitiveTypeArray.h:169
PrimitiveTypeArray(const ArrayWrapper< T > &other)
Copy content from an ArrayWrapper.
Definition: primitiveTypeArray.h:142
Definition: array2DWrapper.h:44