Flecs v4.0
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
array.hpp
Go to the documentation of this file.
1
9namespace flecs {
10
11template <typename T>
13{
14 explicit array_iterator(T* value, int index) {
15 value_ = value;
16 index_ = index;
17 }
18
19 bool operator!=(array_iterator const& other) const
20 {
21 return index_ != other.index_;
22 }
23
24 T & operator*() const
25 {
26 return value_[index_];
27 }
28
29 array_iterator& operator++()
30 {
31 ++index_;
32 return *this;
33 }
34
35private:
36 T* value_;
37 int index_;
38};
39
40template <typename T, size_t Size, class Enable = void>
41struct array final { };
42
43template <typename T, size_t Size>
44struct array<T, Size, enable_if_t<Size != 0> > final {
45 array() {};
46
47 array(const T (&elems)[Size]) {
48 int i = 0;
49 for (auto it = this->begin(); it != this->end(); ++ it) {
50 *it = elems[i ++];
51 }
52 }
53
54 T& operator[](int index) {
55 return array_[index];
56 }
57
58 T& operator[](size_t index) {
59 return array_[index];
60 }
61
62 array_iterator<T> begin() {
63 return array_iterator<T>(array_, 0);
64 }
65
66 array_iterator<T> end() {
67 return array_iterator<T>(array_, Size);
68 }
69
70 size_t size() {
71 return Size;
72 }
73
74 T* ptr() {
75 return array_;
76 }
77
78 template <typename Func>
79 void each(const Func& func) {
80 for (auto& elem : *this) {
81 func(elem);
82 }
83 }
84
85private:
86 T array_[Size];
87};
88
89template<typename T, size_t Size>
90array<T, Size> to_array(const T (&elems)[Size]) {
91 return array<T, Size>(elems);
92}
93
94// Specialized class for zero-sized array
95template <typename T, size_t Size>
96struct array<T, Size, enable_if_t<Size == 0>> final {
97 array() {};
98 array(const T* (&elems)) { (void)elems; }
99 T operator[](size_t index) { ecs_os_abort(); (void)index; return T(); }
100 array_iterator<T> begin() { return array_iterator<T>(nullptr, 0); }
101 array_iterator<T> end() { return array_iterator<T>(nullptr, 0); }
102
103 size_t size() {
104 return 0;
105 }
106
107 T* ptr() {
108 return NULL;
109 }
110};
111
112}