File: array.hpp

package info (click to toggle)
higan 106-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 9,640 kB
  • sloc: cpp: 108,736; ansic: 809; makefile: 22; sh: 7
file content (53 lines) | stat: -rw-r--r-- 978 bytes parent folder | download
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
#pragma once

#include <nall/range.hpp>

namespace nall {

template<typename T, uint Capacity>
struct array {
  auto capacity() const -> uint { return Capacity; }
  auto size() const -> uint { return _size; }

  auto reset() -> void {
    for(uint n : range(_size)) _pool.t[n].~T();
    _size = 0;
  }

  auto operator[](uint index) -> T& {
    return _pool.t[index];
  }

  auto operator[](uint index) const -> const T& {
    return _pool.t[index];
  }

  auto append() -> T& {
    new(_pool.t + _size) T;
    return _pool.t[_size++];
  }

  auto append(const T& value) -> void {
    new(_pool.t + _size++) T(value);
  }

  auto append(T&& value) -> void {
    new(_pool.t + _size++) T(move(value));
  }

  auto begin() { return &_pool.t[0]; }
  auto end() { return &_pool.t[_size]; }

  auto begin() const { return &_pool.t[0]; }
  auto end() const { return &_pool.t[_size]; }

private:
  union U {
    U() {}
    ~U() {}
    T t[Capacity];
  } _pool;
  uint _size = 0;
};

}