CMS 3D CMS Logo

DynArray.h
Go to the documentation of this file.
1 #ifndef CommonTools_Utils_DynArray_H
2 #define CommonTools_Utils_DynArray_H
3 
4 template <typename T>
5 class DynArray {
6 public:
7  T* a = nullptr;
8  unsigned int s = 0;
9 
10 public:
11  using size_type = unsigned int;
12  using value_type = T;
13  using reference = T&;
14  using const_reference = T const&;
15 
16  DynArray() {}
17 
18  explicit DynArray(unsigned char* storage) : a((T*)(storage)), s(0) {}
19 
20  DynArray(unsigned char* storage, unsigned int isize) : a((T*)(storage)), s(isize) {
21  for (auto i = 0U; i < s; ++i)
22  new ((begin() + i)) T();
23  }
24  DynArray(unsigned char* storage, unsigned int isize, T const& it) : a((T*)(storage)), s(isize) {
25  for (auto i = 0U; i < s; ++i)
26  new ((begin() + i)) T(it);
27  }
28 
29  DynArray(DynArray const&) = delete;
30  DynArray& operator=(DynArray const&) = delete;
31 
33  a = other.a;
34  s = other.s;
35  other.s = 0;
36  other.a = nullptr;
37  }
39  a = other.a;
40  s = other.s;
41  other.s = 0;
42  other.a = nullptr;
43  return *this;
44  }
45 
47  for (auto i = 0U; i < s; ++i)
48  a[i].~T();
49  }
50 
51  T& operator[](unsigned int i) { return a[i]; }
52  T* begin() { return a; }
53  T* end() { return a + s; }
54  T& front() { return a[0]; }
55  T& back() { return a[s - 1]; }
56 
57  T const& operator[](unsigned int i) const { return a[i]; }
58  T const* begin() const { return a; }
59  T const* end() const { return a + s; }
60  unsigned int size() const { return s; }
61  bool empty() const { return 0 == s; }
62 
63  T const* data() const { return a; }
64  T const& front() const { return a[0]; }
65  T const& back() const { return a[s - 1]; }
66 
67  void pop_back() {
68  back().~T();
69  --s;
70  }
71  void push_back(T const& t) {
72  new ((begin() + s)) T(t);
73  ++s;
74  }
75  void push_back(T&& t) {
76  new ((begin() + s)) T(t);
77  ++s;
78  }
79 };
80 
81 namespace dynarray {
82  template <typename T>
83  inline T num(T s) {
84  return s > 0 ? s : T(1);
85  }
86 } // namespace dynarray
87 
88 #define unInitDynArray(T, n, x) \
89  alignas(alignof(T)) unsigned char x##_storage[sizeof(T) * dynarray::num(n)]; \
90  DynArray<T> x(x##_storage)
91 #define declareDynArray(T, n, x) \
92  alignas(alignof(T)) unsigned char x##_storage[sizeof(T) * dynarray::num(n)]; \
93  DynArray<T> x(x##_storage, n)
94 #define initDynArray(T, n, x, i) \
95  alignas(alignof(T)) unsigned char x##_storage[sizeof(T) * dynarray::num(n)]; \
96  DynArray<T> x(x##_storage, n, i)
97 
98 #endif // CommonTools_Utils_DynArray_H
DynArray & operator=(DynArray &&other)
Definition: DynArray.h:38
DynArray(DynArray &&other)
Definition: DynArray.h:32
T const & const_reference
Definition: DynArray.h:14
T const & front() const
Definition: DynArray.h:64
unsigned int size_type
Definition: DynArray.h:11
T const & back() const
Definition: DynArray.h:65
bool empty() const
Definition: DynArray.h:61
T * a
Definition: DynArray.h:7
T * end()
Definition: DynArray.h:53
T const & operator[](unsigned int i) const
Definition: DynArray.h:57
void pop_back()
Definition: DynArray.h:67
T & back()
Definition: DynArray.h:55
T & front()
Definition: DynArray.h:54
DynArray & operator=(DynArray const &)=delete
DynArray()
Definition: DynArray.h:16
DynArray(unsigned char *storage, unsigned int isize, T const &it)
Definition: DynArray.h:24
T const * begin() const
Definition: DynArray.h:58
T * begin()
Definition: DynArray.h:52
unsigned int s
Definition: DynArray.h:8
T const * data() const
Definition: DynArray.h:63
T const * end() const
Definition: DynArray.h:59
DynArray(unsigned char *storage, unsigned int isize)
Definition: DynArray.h:20
void push_back(T const &t)
Definition: DynArray.h:71
T num(T s)
Definition: DynArray.h:83
T & reference
Definition: DynArray.h:13
void push_back(T &&t)
Definition: DynArray.h:75
T value_type
Definition: DynArray.h:12
DynArray(unsigned char *storage)
Definition: DynArray.h:18
long double T
~DynArray()
Definition: DynArray.h:46
unsigned int size() const
Definition: DynArray.h:60
T & operator[](unsigned int i)
Definition: DynArray.h:51