Flecs: include/flecs/addons/cpp/utils/array.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| 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
8
9namespace flecs {
10
12template <typename T>
13 struct array_iterator
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
34 array_iterator& operator++()
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>
47 struct array final { };
48
50template <typename T, size_t Size>
51 struct 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
74 array_iterator<T> begin() {
75 return array_iterator<T>(array_, 0);
76 }
77
79 array_iterator<T> end() {
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>
107 array<T, Size> to_array(const T (&elems)[Size]) {
108 return array<T, Size>(elems);
109}
110
112template <typename T, size_t Size>
113 struct 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}
flecs::to_array
array< T, Size > to_array(const T(&elems)[Size])
Create a flecs::array from a C array.
Definition array.hpp:107
flecs::component::array
untyped_component & array(int32_t elem_count)
Register array metadata for a component.
Definition untyped_component.inl:228
flecs::array< T, Size, enable_if_t< Size !=0 > >::end
array_iterator< T > end()
Return an iterator to the end.
Definition array.hpp:79
flecs::array< T, Size, enable_if_t< Size !=0 > >::begin
array_iterator< T > begin()
Return an iterator to the beginning.
Definition array.hpp:74
flecs::array< T, Size, enable_if_t< Size !=0 > >::size
size_t size()
Return the number of elements.
Definition array.hpp:84
flecs::array< T, Size, enable_if_t< Size !=0 > >::each
void each(const Func &func)
Invoke a function for each element.
Definition array.hpp:95
flecs::array< T, Size, enable_if_t< Size !=0 > >::operator[]
T & operator[](int index)
Element access by int index.
Definition array.hpp:64
flecs::array< T, Size, enable_if_t< Size !=0 > >::ptr
T * ptr()
Return a pointer to the underlying data.
Definition array.hpp:89
flecs::array< T, Size, enable_if_t< Size !=0 > >::array
array(const T(&elems)[Size])
Construct from a C array.
Definition array.hpp:56
flecs::array< T, Size, enable_if_t< Size !=0 > >::array
array()
Default constructor.
Definition array.hpp:53
flecs::array< T, Size, enable_if_t< Size !=0 > >::operator[]
T & operator[](size_t index)
Element access by size_t index.
Definition array.hpp:69
flecs::array< T, Size, enable_if_t< Size==0 > >::array
array()
Default constructor.
Definition array.hpp:115
flecs::array< T, Size, enable_if_t< Size==0 > >::size
size_t size()
Return the number of elements (always 0).
Definition array.hpp:126
flecs::array< T, Size, enable_if_t< Size==0 > >::operator[]
T operator[](size_t index)
Element access (aborts, array is empty).
Definition array.hpp:119
flecs::array< T, Size, enable_if_t< Size==0 > >::end
array_iterator< T > end()
Return an iterator to the end (empty range).
Definition array.hpp:123
flecs::array< T, Size, enable_if_t< Size==0 > >::ptr
T * ptr()
Return a null pointer (no data).
Definition array.hpp:131
flecs::array< T, Size, enable_if_t< Size==0 > >::begin
array_iterator< T > begin()
Return an iterator to the beginning (empty range).
Definition array.hpp:121
flecs::array< T, Size, enable_if_t< Size==0 > >::array
array(const T *(&elems))
Construct from a pointer (no-op).
Definition array.hpp:117
flecs::array_iterator
Iterator for flecs::array.
Definition array.hpp:14
flecs::array_iterator::array_iterator
array_iterator(T *value, int index)
Construct an iterator from a pointer and index.
Definition array.hpp:16
flecs::array_iterator::operator*
T & operator*() const
Dereference operator.
Definition array.hpp:28
flecs::array_iterator::operator!=
bool operator!=(array_iterator const &other) const
Inequality comparison operator.
Definition array.hpp:22
flecs::array_iterator::operator++
array_iterator & operator++()
Pre-increment operator.
Definition array.hpp:34
flecs::array
Array class (primary template, disabled).
Definition array.hpp:47