DynArray

Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#ifndef CommonTools_Utils_DynArray_H
#define CommonTools_Utils_DynArray_H

template <typename T>
class DynArray {
public:
  T* a = nullptr;
  unsigned int s = 0;

public:
  using size_type = unsigned int;
  using value_type = T;
  using reference = T&;
  using const_reference = T const&;

  DynArray() {}

  explicit DynArray(unsigned char* storage) : a((T*)(storage)), s(0) {}

  DynArray(unsigned char* storage, unsigned int isize) : a((T*)(storage)), s(isize) {
    for (auto i = 0U; i < s; ++i)
      new ((begin() + i)) T();
  }
  DynArray(unsigned char* storage, unsigned int isize, T const& it) : a((T*)(storage)), s(isize) {
    for (auto i = 0U; i < s; ++i)
      new ((begin() + i)) T(it);
  }

  DynArray(DynArray const&) = delete;
  DynArray& operator=(DynArray const&) = delete;

  DynArray(DynArray&& other) {
    a = other.a;
    s = other.s;
    other.s = 0;
    other.a = nullptr;
  }
  DynArray& operator=(DynArray&& other) {
    a = other.a;
    s = other.s;
    other.s = 0;
    other.a = nullptr;
    return *this;
  }

  ~DynArray() {
    for (auto i = 0U; i < s; ++i)
      a[i].~T();
  }

  T& operator[](unsigned int i) { return a[i]; }
  T* begin() { return a; }
  T* end() { return a + s; }
  T& front() { return a[0]; }
  T& back() { return a[s - 1]; }

  T const& operator[](unsigned int i) const { return a[i]; }
  T const* begin() const { return a; }
  T const* end() const { return a + s; }
  unsigned int size() const { return s; }
  bool empty() const { return 0 == s; }

  T const* data() const { return a; }
  T const& front() const { return a[0]; }
  T const& back() const { return a[s - 1]; }

  void pop_back() {
    back().~T();
    --s;
  }
  void push_back(T const& t) {
    new ((begin() + s)) T(t);
    ++s;
  }
  void push_back(T&& t) {
    new ((begin() + s)) T(t);
    ++s;
  }
};

namespace dynarray {
  template <typename T>
  inline T num(T s) {
    return s > 0 ? s : T(1);
  }
}  // namespace dynarray

#define unInitDynArray(T, n, x)                                                \
  alignas(alignof(T)) unsigned char x##_storage[sizeof(T) * dynarray::num(n)]; \
  DynArray<T> x(x##_storage)
#define declareDynArray(T, n, x)                                               \
  alignas(alignof(T)) unsigned char x##_storage[sizeof(T) * dynarray::num(n)]; \
  DynArray<T> x(x##_storage, n)
#define initDynArray(T, n, x, i)                                               \
  alignas(alignof(T)) unsigned char x##_storage[sizeof(T) * dynarray::num(n)]; \
  DynArray<T> x(x##_storage, n, i)

#endif  // CommonTools_Utils_DynArray_H