CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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 public :
10  DynArray(unsigned char * storage, unsigned int isize) : a((T*)(storage)), s(isize){
11  for (auto i=0U; i<s; ++i) new((begin()+i)) T();
12  }
13  DynArray(unsigned char * storage, unsigned int isize, T const& it) : a((T*)(storage)), s(isize){
14  for (auto i=0U; i<s; ++i) new((begin()+i)) T(it);
15  }
16 
17  ~DynArray() { for (auto i=0U; i<s; ++i) a[i].~T(); }
18 
19  T & operator[](unsigned int i) { return a[i];}
20  T * begin() { return a;}
21  T * end() { return a+s;}
22  T const & operator[](unsigned int i) const { return a[i];}
23  T const * begin() const { return a;}
24  T const * end() const { return a+s;}
25  unsigned int size() const { return s;}
26 };
27 
28 #define declareDynArray(T,n,x) alignas(alignof(T)) unsigned char x ## _storage[sizeof(T)*n]; DynArray<T> x(x ## _storage,n)
29 #define initDynArray(T,n,x,i) alignas(alignof(T)) unsigned char x ## _storage[sizeof(T)*n]; DynArray<T> x(x ## _storage,n,i)
30 
31 
32 #endif // CommonTools_Utils_DynArray_H
int i
Definition: DBlmapReader.cc:9
T const * begin() const
Definition: DynArray.h:23
T const * end() const
Definition: DynArray.h:24
T * a
Definition: DynArray.h:7
T * end()
Definition: DynArray.h:21
T const & operator[](unsigned int i) const
Definition: DynArray.h:22
DynArray(unsigned char *storage, unsigned int isize, T const &it)
Definition: DynArray.h:13
T * begin()
Definition: DynArray.h:20
unsigned int s
Definition: DynArray.h:8
DynArray(unsigned char *storage, unsigned int isize)
Definition: DynArray.h:10
unsigned int size() const
Definition: DynArray.h:25
long double T
~DynArray()
Definition: DynArray.h:17
T & operator[](unsigned int i)
Definition: DynArray.h:19