Skip to content
Flecs v4.1
array.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/utils/array.hpp
3 * @brief Array class.
4 *
5 * Array class. Simple std::array-like utility that is mostly there to aid
6 * template code where template expansion would lead to an array with size 0.
7 */
8
9namespace flecs {
10
11/** Iterator for flecs::array. */
12template <typename T>
14{
15 /** Construct an iterator from a pointer and index. */
16 explicit array_iterator(T* value, int index) {
17 value_ = value;
18 index_ = index;
19 }
20
21 /** Inequality comparison operator. */
22 bool operator!=(array_iterator const& other) const
23 {
24 return index_ != other.index_;
25 }
26
27 /** Dereference operator. */
28 T & operator*() const
29 {
30 return value_[index_];
31 }
32
33 /** Pre-increment operator. */
35 {
36 ++index_;
37 return *this;
38 }
39
40private:
41 T* value_;
42 int index_;
43};
44
45/** Array class (primary template, disabled). */
46template <typename T, size_t Size, class Enable = void>
47struct array final { };
48
49/** Array class specialization for non-zero sizes. */
50template <typename T, size_t Size>
51struct array<T, Size, enable_if_t<Size != 0> > final {
52 /** Default constructor. */
53 array() {};
54
55 /** Construct from a C array. */
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
63 /** Element access by int index. */
64 T& operator[](int index) {
65 return array_[index];
66 }
67
68 /** Element access by size_t index. */
69 T& operator[](size_t index) {
70 return array_[index];
71 }
72
73 /** Return an iterator to the beginning. */
75 return array_iterator<T>(array_, 0);
76 }
77
78 /** Return an iterator to the end. */
80 return array_iterator<T>(array_, Size);
81 }
82
83 /** Return the number of elements. */
84 size_t size() {
85 return Size;
86 }
87
88 /** Return a pointer to the underlying data. */
89 T* ptr() {
90 return array_;
91 }
92
93 /** Invoke a function for each element. */
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
105/** Create a flecs::array from a C array. */
106template<typename T, size_t Size>
107array<T, Size> to_array(const T (&elems)[Size]) {
108 return array<T, Size>(elems);
109}
110
111/** Array class specialization for zero-sized arrays. */
112template <typename T, size_t Size>
113struct array<T, Size, enable_if_t<Size == 0>> final {
114 /** Default constructor. */
115 array() {};
116 /** Construct from a pointer (no-op). */
117 array(const T* (&elems)) { (void)elems; }
118 /** Element access (aborts, array is empty). */
119 T operator[](size_t index) { ecs_os_abort(); (void)index; return T(); }
120 /** Return an iterator to the beginning (empty range). */
121 array_iterator<T> begin() { return array_iterator<T>(nullptr, 0); }
122 /** Return an iterator to the end (empty range). */
123 array_iterator<T> end() { return array_iterator<T>(nullptr, 0); }
124
125 /** Return the number of elements (always 0). */
126 size_t size() {
127 return 0;
128 }
129
130 /** Return a null pointer (no data). */
131 T* ptr() {
132 return nullptr;
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