Gaia-ECS v0.9.3
A simple and powerful entity component system
Loading...
Searching...
No Matches
sarray_impl.h
1#pragma once
2#include "gaia/config/config.h"
3
4#include <cstddef>
5#include <new>
6#include <tuple>
7#include <type_traits>
8#include <utility>
9
10#include "gaia/core/iterator.h"
11#include "gaia/core/utility.h"
12#include "gaia/mem/data_layout_policy.h"
13#include "gaia/mem/mem_utils.h"
14#include "gaia/mem/raw_data_holder.h"
15
16namespace gaia {
17 namespace cnt {
18 namespace sarr_detail {
19 using difference_type = uint32_t;
20 using size_type = uint32_t;
21 } // namespace sarr_detail
22
25 template <typename T, sarr_detail::size_type N>
26 class sarr {
27 public:
28 static_assert(N > 0);
29
30 using value_type = T;
31 using reference = T&;
32 using const_reference = const T&;
33 using pointer = T*;
34 using const_pointer = const T*;
36 using difference_type = sarr_detail::difference_type;
37 using size_type = sarr_detail::size_type;
38
39 using iterator = pointer;
40 using const_iterator = const_pointer;
42
43 static constexpr size_t value_size = sizeof(T);
44 static constexpr size_type extent = N;
45 static constexpr uint32_t allocated_bytes = view_policy::get_min_byte_size(0, N);
46
48
49 constexpr sarr() noexcept {
50 core::call_ctor_raw_n(data(), extent);
51 }
52
55 constexpr sarr(core::zero_t) noexcept {
56 core::call_ctor_raw_n(data(), extent);
57
58 // explicit zeroing
59 for (auto i = (size_type)0; i < extent; ++i)
60 operator[](i) = {};
61 }
62
63 ~sarr() {
64 core::call_dtor_n(data(), extent);
65 }
66
67 template <typename InputIt>
68 constexpr sarr(InputIt first, InputIt last) noexcept {
69 core::call_ctor_raw_n(data(), extent);
70
71 const auto count = (size_type)core::distance(first, last);
72
73 if constexpr (std::is_pointer_v<InputIt>) {
74 for (size_type i = 0; i < count; ++i)
75 operator[](i) = first[i];
76 } else if constexpr (std::is_same_v<typename InputIt::iterator_category, core::random_access_iterator_tag>) {
77 for (size_type i = 0; i < count; ++i)
78 operator[](i) = *(first[i]);
79 } else {
80 size_type i = 0;
81 for (auto it = first; it != last; ++it)
82 operator[](++i) = *it;
83 }
84 }
85
86 constexpr sarr(std::initializer_list<T> il): sarr(il.begin(), il.end()) {}
87
88 constexpr sarr(const sarr& other): sarr(other.begin(), other.end()) {}
89
90 constexpr sarr(sarr&& other) noexcept {
91 GAIA_ASSERT(core::addressof(other) != this);
92
93 core::call_ctor_raw_n(data(), extent);
94 mem::move_elements<T, false>((uint8_t*)m_data, (uint8_t*)other.m_data, other.size(), 0, extent, other.extent);
95 }
96
97 sarr& operator=(std::initializer_list<T> il) {
98 *this = sarr(il.begin(), il.end());
99 return *this;
100 }
101
102 constexpr sarr& operator=(const sarr& other) {
103 GAIA_ASSERT(core::addressof(other) != this);
104
105 core::call_ctor_raw_n(data(), extent);
106 mem::copy_elements<T, false>(
107 GAIA_ACC((uint8_t*)&m_data[0]), GAIA_ACC((const uint8_t*)&other.m_data[0]), other.size(), 0, extent,
108 other.extent);
109
110 return *this;
111 }
112
113 constexpr sarr& operator=(sarr&& other) noexcept {
114 GAIA_ASSERT(core::addressof(other) != this);
115
116 core::call_ctor_raw_n(data(), extent);
117 mem::move_elements<T, false>(
118 GAIA_ACC((uint8_t*)&m_data[0]), GAIA_ACC((uint8_t*)&other.m_data[0]), other.size(), 0, extent,
119 other.extent);
120
121 return *this;
122 }
123
124 GAIA_CLANG_WARNING_PUSH()
125 // Memory is aligned so we can silence this warning
126 GAIA_CLANG_WARNING_DISABLE("-Wcast-align")
127
128 GAIA_NODISCARD constexpr pointer data() noexcept {
129 return GAIA_ACC((pointer)&m_data[0]);
130 }
131
132 GAIA_NODISCARD constexpr const_pointer data() const noexcept {
133 return GAIA_ACC((const_pointer)&m_data[0]);
134 }
135
136 GAIA_NODISCARD constexpr decltype(auto) operator[](size_type pos) noexcept {
137 GAIA_ASSERT(pos < size());
138 return view_policy::set({GAIA_ACC((typename view_policy::TargetCastType) & m_data[0]), extent}, pos);
139 }
140
141 GAIA_NODISCARD constexpr decltype(auto) operator[](size_type pos) const noexcept {
142 GAIA_ASSERT(pos < size());
143 return view_policy::get({GAIA_ACC((typename view_policy::TargetCastType) & m_data[0]), extent}, pos);
144 }
145
146 GAIA_CLANG_WARNING_POP()
147
148 GAIA_NODISCARD constexpr size_type size() const noexcept {
149 return N;
150 }
151
152 GAIA_NODISCARD constexpr bool empty() const noexcept {
153 return begin() == end();
154 }
155
156 GAIA_NODISCARD constexpr size_type capacity() const noexcept {
157 return N;
158 }
159
160 GAIA_NODISCARD constexpr size_type max_size() const noexcept {
161 return N;
162 }
163
164 GAIA_NODISCARD constexpr decltype(auto) front() noexcept {
165 return (reference)*begin();
166 }
167
168 GAIA_NODISCARD constexpr decltype(auto) front() const noexcept {
169 return (const_reference)*begin();
170 }
171
172 GAIA_NODISCARD constexpr decltype(auto) back() noexcept {
173 return (reference) operator[](N - 1);
174 }
175
176 GAIA_NODISCARD constexpr decltype(auto) back() const noexcept {
177 return (const_reference) operator[](N - 1);
178 }
179
180 GAIA_NODISCARD constexpr auto begin() noexcept {
181 return iterator(GAIA_ACC(&m_data[0]));
182 }
183
184 GAIA_NODISCARD constexpr auto begin() const noexcept {
185 return const_iterator(GAIA_ACC(&m_data[0]));
186 }
187
188 GAIA_NODISCARD constexpr auto cbegin() const noexcept {
189 return const_iterator(GAIA_ACC(&m_data[0]));
190 }
191
192 GAIA_NODISCARD constexpr auto rbegin() noexcept {
193 return iterator((pointer)&back());
194 }
195
196 GAIA_NODISCARD constexpr auto rbegin() const noexcept {
197 return const_iterator((const_pointer)&back());
198 }
199
200 GAIA_NODISCARD constexpr auto crbegin() const noexcept {
201 return const_iterator((const_pointer)&back());
202 }
203
204 GAIA_NODISCARD constexpr auto end() noexcept {
205 return iterator(GAIA_ACC((pointer)&m_data[0]) + size());
206 }
207
208 GAIA_NODISCARD constexpr auto end() const noexcept {
209 return const_iterator(GAIA_ACC((const_pointer)&m_data[0]) + size());
210 }
211
212 GAIA_NODISCARD constexpr auto cend() const noexcept {
213 return const_iterator(GAIA_ACC((const_pointer)&m_data[0]) + size());
214 }
215
216 GAIA_NODISCARD constexpr auto rend() noexcept {
217 return iterator(GAIA_ACC((pointer)&m_data[0]) - 1);
218 }
219
220 GAIA_NODISCARD constexpr auto rend() const noexcept {
221 return const_iterator(GAIA_ACC((const_pointer)&m_data[0]) - 1);
222 }
223
224 GAIA_NODISCARD constexpr auto crend() const noexcept {
225 return const_iterator(GAIA_ACC((const_pointer)&m_data[0]) - 1);
226 }
227
228 GAIA_NODISCARD constexpr bool operator==(const sarr& other) const {
229 for (size_type i = 0; i < N; ++i)
230 if (!(operator[](i) == other[i]))
231 return false;
232 return true;
233 }
234
235 GAIA_NODISCARD constexpr bool operator!=(const sarr& other) const {
236 return !operator==(other);
237 }
238 };
239
240 namespace detail {
241 template <typename T, uint32_t N, uint32_t... I>
242 constexpr sarr<std::remove_cv_t<T>, N> to_array_impl(T (&a)[N], std::index_sequence<I...> /*no_name*/) {
243 return {{a[I]...}};
244 }
245 } // namespace detail
246
247 template <typename T, uint32_t N>
248 constexpr sarr<std::remove_cv_t<T>, N> to_array(T (&a)[N]) {
249 return detail::to_array_impl(a, std::make_index_sequence<N>{});
250 }
251
252 template <typename T, typename... U>
253 sarr(T, U...) -> sarr<T, 1 + (uint32_t)sizeof...(U)>;
254
255 } // namespace cnt
256} // namespace gaia
257
258namespace std {
259 template <typename T, uint32_t N>
260 struct tuple_size<gaia::cnt::sarr<T, N>>: std::integral_constant<uint32_t, N> {};
261
262 template <size_t I, typename T, uint32_t N>
263 struct tuple_element<I, gaia::cnt::sarr<T, N>> {
264 using type = T;
265 };
266} // namespace std
Array of elements of type.
Definition sarray_impl.h:26
constexpr sarr(core::zero_t) noexcept
Zero-initialization constructor. Because sarr is not aggretate type, doing: sarr<int,...
Definition sarray_impl.h:55
Checks if endianess was detected correctly at compile-time.
Definition bitset.h:9
Definition utility.h:87
View policy for accessing and storing data in the AoS way. Good for random access and when accessing ...
Definition data_layout_policy.h:112
Definition raw_data_holder.h:12