GEGELATI
Loading...
Searching...
No Matches
pointerWrapper.h
1
36#ifndef POINTER_WRAPPER_H
37#define POINTER_WRAPPER_H
38
39#include "data/constant.h"
40#include "data/dataHandler.h"
41#include "data/hash.h"
42#include "demangle.h"
43
44namespace Data {
45
53 template <class T> class PointerWrapper : public DataHandler
54 {
55 static_assert(std::is_fundamental<T>::value ||
56 std::is_same<T, Data::Constant>(),
57 "Template class PrimitiveTypeArray<T> can only be used "
58 "for primitive types.");
59
60 protected:
65
72 virtual size_t updateHash() const override;
73
74 public:
80 PointerWrapper(T* ptr = nullptr)
81 {
82 this->setPointer(ptr);
83 };
84
86 virtual ~PointerWrapper() = default;
87
90
91 // Friend relation needed for copy-construction.
92 template <class U> friend class PrimitiveTypeArray;
93
100 virtual DataHandler* clone() const override;
101
103 virtual bool canHandle(const std::type_info& type) const override;
104
106 virtual size_t getAddressSpace(
107 const std::type_info& type) const override;
108
110 virtual size_t getLargestAddressSpace(void) const override;
111
113 virtual size_t getHash() const override;
114
116 void resetData() override;
117
126 void setPointer(T* ptr);
127
129 virtual UntypedSharedPtr getDataAt(const std::type_info& type,
130 const size_t address) const override;
131
133 virtual std::vector<size_t> getAddressesAccessed(
134 const std::type_info& type, const size_t address) const override;
135
136#ifdef CODE_GENERATION
138 virtual const std::type_info& getNativeType() const override;
139
141 virtual std::vector<size_t> getDimensionsSize() const override;
142#endif
143 };
144
145 template <class T> inline size_t PointerWrapper<T>::getHash() const
146 {
147 return updateHash();
148 }
149
150 template <class T> inline size_t PointerWrapper<T>::updateHash() const
151 {
152 if (this->containerPtr != nullptr) {
153
154 // reset
155 this->cachedHash = Data::Hash<size_t>()(this->id);
156
157 // Rotate by 1 because otherwise, xor is comutative.
158 this->cachedHash =
159 (this->cachedHash >> 1) | (this->cachedHash << 63);
160
161 this->cachedHash ^= Data::Hash<T>()(*this->containerPtr);
162 return this->cachedHash;
163 }
164 else {
165 return this->cachedHash = 0;
166 }
167 }
168
169 // Declare class for clone method
170 template <class T> class PrimitiveTypeArray;
171
172 template <class T> inline DataHandler* PointerWrapper<T>::clone() const
173 {
174 // Create a constantCopy of the PointerWrapper content.
175 DataHandler* result = new PrimitiveTypeArray<T>(*this);
176
177 return result;
178 }
179
180 template <class T>
181 inline bool PointerWrapper<T>::canHandle(const std::type_info& type) const
182 {
183 if (typeid(T) == type) {
184 return true;
185 }
186 return false;
187 }
188
189 template <class T>
191 const std::type_info& type) const
192 {
193 if (typeid(T) == type) {
194 return 1;
195 }
196 else {
197 return 0;
198 }
199 }
200
201 template <class T>
203 {
204 return 1;
205 }
206
207 template <class T> inline void PointerWrapper<T>::resetData()
208 {
209 // Does nothing
210 }
211
212 template <class T> inline void PointerWrapper<T>::setPointer(T* ptr)
213 {
214 this->containerPtr = ptr;
215 return;
216 }
217
218 template <class T>
220 const std::type_info& type, const size_t address) const
221 {
222 if (this->containerPtr == nullptr) {
223 throw std::runtime_error("Null pointer access.");
224 }
225
226#ifndef NDEBUG
227 // Throw exception in case of invalid arguments.
228 if (!this->canHandle(type)) {
229 std::stringstream message;
230 message << "Data type " << DEMANGLE_TYPEID_NAME(type.name())
231 << " cannot be accessed in a "
232 << DEMANGLE_TYPEID_NAME(typeid(*this).name()) << ".";
233 throw std::invalid_argument(message.str());
234 }
235
236 if (address > 0) {
237 std::stringstream message;
238 message << "Data type " << DEMANGLE_TYPEID_NAME(type.name())
239 << " cannot be accessed at address " << address
240 << ", address space size is 1.";
241 throw std::out_of_range(message.str());
242 }
243#endif
244
245 UntypedSharedPtr result(this->containerPtr,
246 UntypedSharedPtr::emptyDestructor<const T>());
247 return result;
248 }
249
250 template <class T>
251 inline std::vector<size_t> PointerWrapper<T>::getAddressesAccessed(
252 const std::type_info& type, const size_t address) const
253 {
254 // Initialize the result
255 std::vector<size_t> result;
256
257 // If the accessed address is valid fill the result.
258 const size_t space = this->getAddressSpace(type);
259 if (space > address) {
260 // For the native type.
261 if (type == typeid(T)) {
262 result.push_back(address);
263 }
264 }
265 return result;
266 }
267
268#ifdef CODE_GENERATION
269 template <class T>
270 inline const std::type_info& PointerWrapper<T>::getNativeType() const
271 {
272 const std::type_info& a = typeid(T);
273 return a;
274 }
275
276 template <class T>
277 inline std::vector<size_t> PointerWrapper<T>::getDimensionsSize() const
278 {
279 std::vector<size_t> sizes = {1};
280 return sizes;
281 }
282#endif
283
284} // namespace Data
285
286#endif
size_t getAddressSpace(const std::type_info &type, size_t *dim1, size_t *dim2) const
Utility function for the class.
Definition array2DWrapper.h:217
std::vector< T > * containerPtr
Pointer to the array containing the data accessed through the ArrayWrapper.
Definition arrayWrapper.h:105
virtual size_t updateHash() const override
Implementation of the updateHash method.
Definition arrayWrapper.h:407
virtual bool canHandle(const std::type_info &type) const override
Inherited from DataHandler.
Definition arrayWrapper.h:221
Base class for all sources of data to be accessed by a TPG Instruction executed within a Program.
Definition dataHandler.h:55
size_t cachedHash
Cached value returned by the getHash() function.
Definition dataHandler.h:78
const size_t id
Identifier of each DataHandler.
Definition dataHandler.h:70
Definition pointerWrapper.h:54
void setPointer(T *ptr)
Set the pointer of the PointerWrapper.
Definition pointerWrapper.h:212
void resetData() override
Inherited from DataHandler. Does nothing.
Definition pointerWrapper.h:207
PointerWrapper(T *ptr=nullptr)
Constructor for the PointerWrapper class.
Definition pointerWrapper.h:80
virtual DataHandler * clone() const override
Return a PrimitiveTypeArray<T> where the data of the PointerWrapper has been copied.
Definition pointerWrapper.h:172
virtual size_t getAddressSpace(const std::type_info &type) const override
Inherited from DataHandler.
Definition pointerWrapper.h:190
virtual std::vector< size_t > getDimensionsSize() const override
Inherited from DataHandler.
Definition pointerWrapper.h:277
virtual const std::type_info & getNativeType() const override
Inherited from DataHandler.
Definition pointerWrapper.h:270
virtual bool canHandle(const std::type_info &type) const override
Inherited from DataHandler.
Definition pointerWrapper.h:181
PointerWrapper(const PointerWrapper< T > &other)=default
Default copy constructor.
virtual size_t getHash() const override
Inherited from DataHandler.
Definition pointerWrapper.h:145
virtual std::vector< size_t > getAddressesAccessed(const std::type_info &type, const size_t address) const override
Inherited from DataHandler.
Definition pointerWrapper.h:251
virtual size_t getLargestAddressSpace(void) const override
Inherited from DataHandler.
Definition pointerWrapper.h:202
virtual size_t updateHash() const override
Implementation of the updateHash method.
Definition pointerWrapper.h:150
virtual ~PointerWrapper()=default
Default destructor.
virtual UntypedSharedPtr getDataAt(const std::type_info &type, const size_t address) const override
Inherited from DataHandler.
Definition pointerWrapper.h:219
T * containerPtr
Pointer to the data accessed through the PointerWrapper.
Definition pointerWrapper.h:64
DataHandler for 2D arrays of primitive types.
Definition primitiveTypeArray2D.h:69
PrimitiveTypeArray2D(const size_t w=2, const size_t h=4)
Constructor for the 2D array.
Definition primitiveTypeArray2D.h:142
Definition primitiveTypeArray.h:55
Class behaving as a std::shared_ptr whose type is not templated.
Definition untypedSharedPtr.h:72
Definition array2DWrapper.h:44