ICG 2
Electric Boogaloo
SpecifiedSequenceDataType.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #include <deque>
5 
6 template <typename T>
7 void* sequence_construct (int num) {
8  T* temp = (T*)calloc( num, sizeof(T));
9  for (int ii=0 ; ii<num ; ii++) { new( &temp[ii]) T(); }
10  return ((void *)temp);
11 }
12 
13 // A deAllocator
14 // Object constructed with placement new must call the destructor explicitly (https://isocpp.org/wiki/faq/dtors#placement-new)
15 template <typename T>
16 void sequence_destruct (void* address) {
17  T* temp = static_cast<T*> (address);
18  temp->~T();
19 }
20 
21 template <typename T>
23  public:
24 
25  SpecifiedSequenceDataType( std::string typeSpecifierName) : SequenceDataType(typeSpecifierName, sizeof(T), sequence_construct<T>, sequence_destruct<T>) {}
26 
27  // Rule of 3
31 
32  std::vector<void *> getElementAddresses (void * address) const override {
33  std::vector<void *> addresses;
34 
35  T * container = static_cast<T *> (address);
36 
37  typename T::iterator curr = container->begin();
38  typename T::iterator end = container->end();
39 
40  while (curr != end) {
41  addresses.push_back(&*curr);
42  curr++;
43  }
44 
45  return addresses;
46  }
47 
48  // TODO: template specialization to disallow resizing for std::array
49  bool resize (void * address, int n_elems) const {
50  T * container = static_cast<T *> (address);
51  container->resize(n_elems);
52  return true;
53  }
54 
55  int getNumElements (void * address) const override {
56  T * container = static_cast<T *> (address);
57  return container->size();
58  }
59 
60  // TODO: Specialize for std::array
61  bool clear (void * address) const override {
62  T * container = static_cast<T *> (address);
63  container->clear();
64  return true;
65  }
66 
67 };
void * sequence_construct(int num)
Definition: SpecifiedSequenceDataType.hpp:7
void sequence_destruct(void *address)
Definition: SpecifiedSequenceDataType.hpp:16
Represents an STL sequence type - vector, list, deque, array.
Definition: SequenceDataType.hpp:15
Definition: SpecifiedSequenceDataType.hpp:22
SpecifiedSequenceDataType(const SpecifiedSequenceDataType< T > &other)=delete
SpecifiedSequenceDataType(std::string typeSpecifierName)
Definition: SpecifiedSequenceDataType.hpp:25
~SpecifiedSequenceDataType()=default
bool resize(void *address, int n_elems) const
resize the underlying container
Definition: SpecifiedSequenceDataType.hpp:49
SpecifiedSequenceDataType & operator=(SpecifiedSequenceDataType< T > rhs)=delete
std::vector< void * > getElementAddresses(void *address) const override
Given the address of a sequence of this type, get a list of all the addresses of the elements within.
Definition: SpecifiedSequenceDataType.hpp:32
bool clear(void *address) const override
clear the underlying container
Definition: SpecifiedSequenceDataType.hpp:61
int getNumElements(void *address) const override
Given the address of a sequence of this type, get the number of elements in this sequence.
Definition: SpecifiedSequenceDataType.hpp:55