Flecs v4.1
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
12template <typename T>
14{
16 explicit array_iterator(T* value, int index) {
17 value_ = value;
18 index_ = index;
19 }
20
22 bool operator!=(array_iterator const& other) const
23 {
24 return index_ != other.index_;
25 }
26
28 T & operator*() const
29 {
30 return value_[index_];
31 }
32
35 {
36 ++index_;
37 return *this;
38 }
39
40private:
41 T* value_;
42 int index_;
43};
44
46template <typename T, size_t Size, class Enable = void>
47struct array final { };
48
50template <typename T, size_t Size>
51struct array<T, Size, enable_if_t<Size != 0> > final {
53 array() {};
54
56 array(const T (&elems)[Size]) {
57 int i = 0;
58 for (auto it = this->begin(); it != this->end(); ++ it) {
59 *it = elems[i ++];
60 }
61 }
62
64 T& operator[](int index) {
65 return array_[index];
66 }
67
69 T& operator[](size_t index) {
70 return array_[index];
71 }
72
75 return array_iterator<T>(array_, 0);
76 }
77
80 return array_iterator<T>(array_, Size);
81 }
82
84 size_t size() {
85 return Size;
86 }
87
89 T* ptr() {
90 return array_;
91 }
92
94 template <typename Func>
95 void each(const Func& func) {
96 for (auto& elem : *this) {
97 func(elem);
98 }
99 }
100
101private:
102 T array_[Size];
103};
104
106template<typename T, size_t Size>
107array<T, Size> to_array(const T (&elems)[Size]) {
108 return array<T, Size>(elems);
109}
110
112template <typename T, size_t Size>
113struct array<T, Size, enable_if_t<Size == 0>> final {
115 array() {};
117 array(const T* (&elems)) { (void)elems; }
119 T operator[](size_t index) { ecs_os_abort(); (void)index; return T(); }
121 array_iterator<T> begin() { return array_iterator<T>(nullptr, 0); }
123 array_iterator<T> end() { return array_iterator<T>(nullptr, 0); }
124
126 size_t size() {
127 return 0;
128 }
129
131 T* ptr() {
132 return NULL;
133 }
134};
135
136}
array< T, Size > to_array(const T(&elems)[Size])
Create a flecs::array from a C array.
Definition array.hpp:107
array_iterator< T > end()
Return an iterator to the end.
Definition array.hpp:79
array_iterator< T > begin()
Return an iterator to the beginning.
Definition array.hpp:74
size_t size()
Return the number of elements.
Definition array.hpp:84
void each(const Func &func)
Invoke a function for each element.
Definition array.hpp:95
T & operator[](int index)
Element access by int index.
Definition array.hpp:64
T * ptr()
Return a pointer to the underlying data.
Definition array.hpp:89
array(const T(&elems)[Size])
Construct from a C array.
Definition array.hpp:56
T & operator[](size_t index)
Element access by size_t index.
Definition array.hpp:69
size_t size()
Return the number of elements (always 0).
Definition array.hpp:126
T operator[](size_t index)
Element access (aborts, array is empty).
Definition array.hpp:119
array_iterator< T > end()
Return an iterator to the end (empty range).
Definition array.hpp:123
T * ptr()
Return a null pointer (no data).
Definition array.hpp:131
array_iterator< T > begin()
Return an iterator to the beginning (empty range).
Definition array.hpp:121
array(const T *(&elems))
Construct from a pointer (no-op).
Definition array.hpp:117
Iterator for flecs::array.
Definition array.hpp:14
array_iterator(T *value, int index)
Construct an iterator from a pointer and index.
Definition array.hpp:16
T & operator*() const
Dereference operator.
Definition array.hpp:28
bool operator!=(array_iterator const &other) const
Inequality comparison operator.
Definition array.hpp:22
array_iterator & operator++()
Pre-increment operator.
Definition array.hpp:34
Array class (primary template, disabled).
Definition array.hpp:47