37#ifndef ARRAY_WRAPPER_H
38#define ARRAY_WRAPPER_H
48#include "data/constant.h"
49#include "data/dataHandler.h"
50#include "data/demangle.h"
70 static_assert(std::is_fundamental<T>::value ||
71 std::is_same<T, Data::Constant>(),
72 "Template class PrimitiveTypeArray<T> can only be used "
73 "for primitive types.");
88 mutable std::map<std::type_index, size_t> cachedAddressSpace;
119 const size_t& address)
const;
158 virtual bool canHandle(
const std::type_info& type)
const override;
162 const std::type_info& type)
const override;
194 const size_t address)
const override;
198 const std::type_info& type,
const size_t address)
const override;
200#ifdef CODE_GENERATION
212 if (
typeid(T) == type) {
217 return (this->getAddressSpace(type) > 0);
222 const size_t& address)
const
224 size_t addressSpace = this->getAddressSpace(type);
226 if (addressSpace == 0) {
227 std::stringstream message;
228 message <<
"Data type " << DEMANGLE_TYPEID_NAME(type.name())
229 <<
" cannot be accessed in a "
230 << DEMANGLE_TYPEID_NAME(
typeid(*this).name()) <<
".";
231 throw std::invalid_argument(message.str());
235 if (address >= addressSpace) {
236 std::stringstream message;
237 message <<
"Data type " << DEMANGLE_TYPEID_NAME(type.name())
238 <<
" cannot be accessed at address " << address
239 <<
", address space size is " << addressSpace <<
".";
240 throw std::out_of_range(message.str());
259 auto iter = this->cachedAddressSpace.find(type);
260 if (iter != this->cachedAddressSpace.end()) {
264 if (type ==
typeid(T)) {
265 this->cachedAddressSpace.emplace(type, this->nbElements);
266 return this->nbElements;
271 std::string typeName = DEMANGLE_TYPEID_NAME(type.name());
272 std::string regex{DEMANGLE_TYPEID_NAME(
typeid(T).name())};
273 regex.append(
"\\s*(const\\s*)?\\[([0-9]+)\\]");
274 std::regex arrayType(regex);
276 if (std::regex_match(typeName.c_str(), cm, arrayType)) {
277 int size = std::atoi(cm[2].str().c_str());
278 if (size <= this->nbElements) {
279 size_t result = this->nbElements - size + 1;
280 this->cachedAddressSpace.emplace(type, result);
290 const std::type_info& type,
const size_t address)
const
293 std::vector<size_t> result;
296 const size_t space = this->getAddressSpace(type);
297 if (space > address) {
299 if (type ==
typeid(T)) {
300 result.push_back(address);
304 for (
int i = 0; i < (this->nbElements - space + 1); i++) {
305 result.push_back(address + i);
314 const std::type_info& type,
const size_t address)
const
316 if (this->containerPtr ==
nullptr) {
317 throw std::runtime_error(
"Null pointer access.");
321 checkAddressAndType(type, address);
324 if (type ==
typeid(T)) {
326 &(this->containerPtr->at(address)),
327 UntypedSharedPtr::emptyDestructor<const T>());
334 size_t arraySize = this->nbElements - this->getAddressSpace(type) + 1;
335 T* array =
new T[arraySize];
338 for (
size_t idx = 0; idx < arraySize; idx++) {
339 array[idx] = this->containerPtr->at(address + idx);
344 std::make_shared<UntypedSharedPtr::Model<const T[]>>(array)};
351 return this->nbElements;
356 this->invalidCachedHash =
true;
368 if (ptr ==
nullptr) {
369 this->containerPtr = ptr;
370 this->invalidCachedHash =
true;
376 if (ptr->size() != nbElements) {
377 std::stringstream message;
378 message <<
"Size of pointed data (" << ptr->size()
379 <<
") does not correspond to the size of the ArrayWrapper ("
380 << this->nbElements <<
").";
381 throw std::domain_error(message.str());
385 this->containerPtr = ptr;
386 this->invalidCachedHash =
true;
392 if (this->containerPtr ==
nullptr) {
393 return this->cachedHash = 0;
397 this->cachedHash = Data::Hash<size_t>()(this->id);
400 Data::Hash<T> hasher;
402 for (T dataElement : *(this->containerPtr)) {
405 (this->cachedHash >> 1) | (this->cachedHash << 63);
406 this->cachedHash ^= hasher((T)dataElement);
410 this->invalidCachedHash =
false;
412 return this->cachedHash;
415#ifdef CODE_GENERATION
419 const std::type_info& a =
typeid(T);
426 std::vector<size_t> sizes = {nbElements};
435#include "data/primitiveTypeArray.h"
Definition: arrayWrapper.h:69
ArrayWrapper(const ArrayWrapper< T > &other)=default
Default copy constructor.
std::vector< T > * containerPtr
Pointer to the array containing the data accessed through the ArrayWrapper.
Definition: arrayWrapper.h:105
virtual ~ArrayWrapper()=default
Default destructor.
void resetData() override
Inherited from DataHandler. Does nothing.
Definition: arrayWrapper.h:359
ArrayWrapper(size_t size=8, std::vector< T > *ptr=nullptr)
Constructor for the ArrayWrapper class.
Definition: arrayWrapper.h:137
void invalidateCachedHash()
Invalidate the hash of the container.
Definition: arrayWrapper.h:354
const size_t nbElements
Number of elements contained pointer vector.
Definition: arrayWrapper.h:99
virtual size_t getLargestAddressSpace(void) const override
Inherited from DataHandler.
Definition: arrayWrapper.h:348
virtual DataHandler * clone() const override
Return a PrimitiveTypeArray<T> where all data of the ArrayWrapper has been copied.
Definition: arrayWrapper.h:247
virtual std::vector< size_t > getDimensionsSize() const override
Inherited from DataHandler.
Definition: arrayWrapper.h:424
virtual size_t getAddressSpace(const std::type_info &type) const override
Inherited from DataHandler.
Definition: arrayWrapper.h:256
virtual size_t updateHash() const override
Implementation of the updateHash method.
Definition: arrayWrapper.h:389
virtual UntypedSharedPtr getDataAt(const std::type_info &type, const size_t address) const override
Inherited from DataHandler.
Definition: arrayWrapper.h:313
virtual std::vector< size_t > getAddressesAccessed(const std::type_info &type, const size_t address) const override
Inherited from DataHandler.
Definition: arrayWrapper.h:289
void setPointer(std::vector< T > *ptr)
Set the pointer of the ArrayWrapper.
Definition: arrayWrapper.h:365
virtual const std::type_info & getNativeType() const override
Inherited from DataHandler.
Definition: arrayWrapper.h:417
void checkAddressAndType(const std::type_info &type, const size_t &address) const
Definition: arrayWrapper.h:221
virtual bool canHandle(const std::type_info &type) const override
Inherited from DataHandler.
Definition: arrayWrapper.h:210
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
Class behaving as a std::shared_ptr whose type is not templated.
Definition: untypedSharedPtr.h:72
Definition: array2DWrapper.h:44