EnTT 3.16.0
Loading...
Searching...
No Matches
storage.hpp
1#ifndef ENTT_ENTITY_STORAGE_HPP
2#define ENTT_ENTITY_STORAGE_HPP
3
4#include <cstddef>
5#include <iterator>
6#include <memory>
7#include <tuple>
8#include <type_traits>
9#include <utility>
10#include <vector>
11#include "../config/config.h"
12#include "../core/bit.hpp"
13#include "../core/iterator.hpp"
14#include "../core/memory.hpp"
15#include "../core/type_info.hpp"
16#include "component.hpp"
17#include "entity.hpp"
18#include "fwd.hpp"
19#include "sparse_set.hpp"
20
21namespace entt {
22
24namespace internal {
25
26template<typename Container, auto Page>
27class storage_iterator final {
28 friend storage_iterator<const Container, Page>;
29
30 using container_type = std::remove_const_t<Container>;
31 using alloc_traits = std::allocator_traits<typename container_type::allocator_type>;
32
33 using iterator_traits = std::iterator_traits<std::conditional_t<
34 std::is_const_v<Container>,
35 typename alloc_traits::template rebind_traits<typename std::pointer_traits<typename container_type::value_type>::element_type>::const_pointer,
36 typename alloc_traits::template rebind_traits<typename std::pointer_traits<typename container_type::value_type>::element_type>::pointer>>;
37
38public:
39 using value_type = typename iterator_traits::value_type;
40 using pointer = typename iterator_traits::pointer;
41 using reference = typename iterator_traits::reference;
42 using difference_type = typename iterator_traits::difference_type;
43 using iterator_category = std::random_access_iterator_tag;
44
45 constexpr storage_iterator() noexcept = default;
46
47 constexpr storage_iterator(Container *ref, const difference_type idx) noexcept
48 : payload{ref},
49 offset{idx} {}
50
51 template<bool Const = std::is_const_v<Container>, typename = std::enable_if_t<Const>>
52 constexpr storage_iterator(const storage_iterator<std::remove_const_t<Container>, Page> &other) noexcept
53 : storage_iterator{other.payload, other.offset} {}
54
55 constexpr storage_iterator &operator++() noexcept {
56 return --offset, *this;
57 }
58
59 constexpr storage_iterator operator++(int) noexcept {
60 const storage_iterator orig = *this;
61 return ++(*this), orig;
62 }
63
64 constexpr storage_iterator &operator--() noexcept {
65 return ++offset, *this;
66 }
67
68 constexpr storage_iterator operator--(int) noexcept {
69 const storage_iterator orig = *this;
70 return operator--(), orig;
71 }
72
73 constexpr storage_iterator &operator+=(const difference_type value) noexcept {
74 offset -= value;
75 return *this;
76 }
77
78 constexpr storage_iterator operator+(const difference_type value) const noexcept {
79 storage_iterator copy = *this;
80 return (copy += value);
81 }
82
83 constexpr storage_iterator &operator-=(const difference_type value) noexcept {
84 return (*this += -value);
85 }
86
87 constexpr storage_iterator operator-(const difference_type value) const noexcept {
88 return (*this + -value);
89 }
90
91 [[nodiscard]] constexpr reference operator[](const difference_type value) const noexcept {
92 const auto pos = static_cast<typename Container::size_type>(index() - value);
93 return (*payload)[pos / Page][fast_mod(static_cast<std::size_t>(pos), Page)];
94 }
95
96 [[nodiscard]] constexpr pointer operator->() const noexcept {
97 return std::addressof(operator[](0));
98 }
99
100 [[nodiscard]] constexpr reference operator*() const noexcept {
101 return operator[](0);
102 }
103
104 [[nodiscard]] constexpr difference_type index() const noexcept {
105 return offset - 1;
106 }
107
108private:
109 Container *payload;
110 difference_type offset;
111};
112
113template<typename Lhs, typename Rhs, auto Page>
114[[nodiscard]] constexpr std::ptrdiff_t operator-(const storage_iterator<Lhs, Page> &lhs, const storage_iterator<Rhs, Page> &rhs) noexcept {
115 return rhs.index() - lhs.index();
116}
117
118template<typename Lhs, typename Rhs, auto Page>
119[[nodiscard]] constexpr bool operator==(const storage_iterator<Lhs, Page> &lhs, const storage_iterator<Rhs, Page> &rhs) noexcept {
120 return lhs.index() == rhs.index();
121}
122
123template<typename Lhs, typename Rhs, auto Page>
124[[nodiscard]] constexpr bool operator!=(const storage_iterator<Lhs, Page> &lhs, const storage_iterator<Rhs, Page> &rhs) noexcept {
125 return !(lhs == rhs);
126}
127
128template<typename Lhs, typename Rhs, auto Page>
129[[nodiscard]] constexpr bool operator<(const storage_iterator<Lhs, Page> &lhs, const storage_iterator<Rhs, Page> &rhs) noexcept {
130 return lhs.index() > rhs.index();
131}
132
133template<typename Lhs, typename Rhs, auto Page>
134[[nodiscard]] constexpr bool operator>(const storage_iterator<Lhs, Page> &lhs, const storage_iterator<Rhs, Page> &rhs) noexcept {
135 return rhs < lhs;
136}
137
138template<typename Lhs, typename Rhs, auto Page>
139[[nodiscard]] constexpr bool operator<=(const storage_iterator<Lhs, Page> &lhs, const storage_iterator<Rhs, Page> &rhs) noexcept {
140 return !(lhs > rhs);
141}
142
143template<typename Lhs, typename Rhs, auto Page>
144[[nodiscard]] constexpr bool operator>=(const storage_iterator<Lhs, Page> &lhs, const storage_iterator<Rhs, Page> &rhs) noexcept {
145 return !(lhs < rhs);
146}
147
148template<typename It, typename... Other>
149class extended_storage_iterator final {
150 template<typename Iter, typename... Args>
151 friend class extended_storage_iterator;
152
153public:
154 using iterator_type = It;
155 using value_type = decltype(std::tuple_cat(std::make_tuple(*std::declval<It>()), std::forward_as_tuple(*std::declval<Other>()...)));
156 using pointer = input_iterator_pointer<value_type>;
157 using reference = value_type;
158 using difference_type = std::ptrdiff_t;
159 using iterator_category = std::input_iterator_tag;
160 using iterator_concept = std::forward_iterator_tag;
161
162 constexpr extended_storage_iterator()
163 : it{} {}
164
165 constexpr extended_storage_iterator(iterator_type base, Other... other)
166 : it{base, other...} {}
167
168 template<typename... Args, typename = std::enable_if_t<(!std::is_same_v<Other, Args> && ...) && (std::is_constructible_v<Other, Args> && ...)>>
169 constexpr extended_storage_iterator(const extended_storage_iterator<It, Args...> &other)
170 : it{other.it} {}
171
172 constexpr extended_storage_iterator &operator++() noexcept {
173 return ++std::get<It>(it), (++std::get<Other>(it), ...), *this;
174 }
175
176 constexpr extended_storage_iterator operator++(int) noexcept {
177 const extended_storage_iterator orig = *this;
178 return ++(*this), orig;
179 }
180
181 [[nodiscard]] constexpr pointer operator->() const noexcept {
182 return operator*();
183 }
184
185 [[nodiscard]] constexpr reference operator*() const noexcept {
186 return {*std::get<It>(it), *std::get<Other>(it)...};
187 }
188
189 [[nodiscard]] constexpr iterator_type base() const noexcept {
190 return std::get<It>(it);
191 }
192
193 template<typename... Lhs, typename... Rhs>
194 friend constexpr bool operator==(const extended_storage_iterator<Lhs...> &, const extended_storage_iterator<Rhs...> &) noexcept;
195
196private:
197 std::tuple<It, Other...> it;
198};
199
200template<typename... Lhs, typename... Rhs>
201[[nodiscard]] constexpr bool operator==(const extended_storage_iterator<Lhs...> &lhs, const extended_storage_iterator<Rhs...> &rhs) noexcept {
202 return std::get<0>(lhs.it) == std::get<0>(rhs.it);
203}
204
205template<typename... Lhs, typename... Rhs>
206[[nodiscard]] constexpr bool operator!=(const extended_storage_iterator<Lhs...> &lhs, const extended_storage_iterator<Rhs...> &rhs) noexcept {
207 return !(lhs == rhs);
208}
209
210} // namespace internal
212
228template<typename Type, typename Entity, typename Allocator, typename>
229class basic_storage: public basic_sparse_set<Entity, typename std::allocator_traits<Allocator>::template rebind_alloc<Entity>> {
230 using alloc_traits = std::allocator_traits<Allocator>;
231 static_assert(std::is_same_v<typename alloc_traits::value_type, Type>, "Invalid value type");
232 using container_type = std::vector<typename alloc_traits::pointer, typename alloc_traits::template rebind_alloc<typename alloc_traits::pointer>>;
234 using underlying_iterator = typename underlying_type::basic_iterator;
235 using traits_type = component_traits<Type, Entity>;
236
237 [[nodiscard]] auto &element_at(const std::size_t pos) const {
238 return payload[pos / traits_type::page_size][fast_mod(pos, traits_type::page_size)];
239 }
240
241 auto assure_at_least(const std::size_t pos) {
242 const auto idx = pos / traits_type::page_size;
243
244 if(!(idx < payload.size())) {
245 auto curr = payload.size();
246 allocator_type allocator{get_allocator()};
247 payload.resize(idx + 1u, nullptr);
248
249 ENTT_TRY {
250 for(const auto last = payload.size(); curr < last; ++curr) {
251 payload[curr] = alloc_traits::allocate(allocator, traits_type::page_size);
252 }
253 }
254 ENTT_CATCH {
255 payload.resize(curr);
256 ENTT_THROW;
257 }
258 }
259
260 return payload[idx] + fast_mod(pos, traits_type::page_size);
261 }
262
263 template<typename... Args>
264 auto emplace_element(const Entity entt, const bool force_back, Args &&...args) {
265 const auto it = base_type::try_emplace(entt, force_back);
266
267 ENTT_TRY {
268 auto *elem = to_address(assure_at_least(static_cast<size_type>(it.index())));
269 entt::uninitialized_construct_using_allocator(elem, get_allocator(), std::forward<Args>(args)...);
270 }
271 ENTT_CATCH {
272 base_type::pop(it, it + 1u);
273 ENTT_THROW;
274 }
275
276 return it;
277 }
278
279 void shrink_to_size(const std::size_t sz) {
280 const auto from = (sz + traits_type::page_size - 1u) / traits_type::page_size;
281 allocator_type allocator{get_allocator()};
282
283 for(auto pos = sz, length = base_type::size(); pos < length; ++pos) {
284 if constexpr(traits_type::in_place_delete) {
285 if(base_type::data()[pos] != tombstone) {
286 alloc_traits::destroy(allocator, std::addressof(element_at(pos)));
287 }
288 } else {
289 alloc_traits::destroy(allocator, std::addressof(element_at(pos)));
290 }
291 }
292
293 for(auto pos = from, last = payload.size(); pos < last; ++pos) {
294 alloc_traits::deallocate(allocator, payload[pos], traits_type::page_size);
295 }
296
297 payload.resize(from);
298 payload.shrink_to_fit();
299 }
300
301 void swap_at(const std::size_t lhs, const std::size_t rhs) {
302 using std::swap;
303 swap(element_at(lhs), element_at(rhs));
304 }
305
306 void move_to(const std::size_t lhs, const std::size_t rhs) {
307 auto &elem = element_at(lhs);
308 allocator_type allocator{get_allocator()};
309 entt::uninitialized_construct_using_allocator(to_address(assure_at_least(rhs)), allocator, std::move(elem));
310 alloc_traits::destroy(allocator, std::addressof(elem));
311 }
312
313private:
314 [[nodiscard]] const void *get_at(const std::size_t pos) const final {
315 return std::addressof(element_at(pos));
316 }
317
318 void swap_or_move([[maybe_unused]] const std::size_t from, [[maybe_unused]] const std::size_t to) override {
319 static constexpr bool is_pinned_type = !(std::is_move_constructible_v<Type> && std::is_move_assignable_v<Type>);
320 // use a runtime value to avoid compile-time suppression that drives the code coverage tool crazy
321 ENTT_ASSERT((from + 1u) && !is_pinned_type, "Pinned type");
322
323 if constexpr(!is_pinned_type) {
324 if constexpr(traits_type::in_place_delete) {
325 (base_type::operator[](to) == tombstone) ? move_to(from, to) : swap_at(from, to);
326 } else {
327 swap_at(from, to);
328 }
329 }
330 }
331
332protected:
338 void pop(underlying_iterator first, underlying_iterator last) override {
339 for(allocator_type allocator{get_allocator()}; first != last; ++first) {
340 // cannot use first.index() because it would break with cross iterators
341 auto &elem = element_at(base_type::index(*first));
342
343 if constexpr(traits_type::in_place_delete) {
345 alloc_traits::destroy(allocator, std::addressof(elem));
346 } else {
347 auto &other = element_at(base_type::size() - 1u);
348 // destroying on exit allows reentrant destructors
349 [[maybe_unused]] auto unused = std::exchange(elem, std::move(other));
350 alloc_traits::destroy(allocator, std::addressof(other));
352 }
353 }
354 }
355
357 void pop_all() override {
358 allocator_type allocator{get_allocator()};
359
360 for(auto first = base_type::begin(); !(first.index() < 0); ++first) {
361 if constexpr(traits_type::in_place_delete) {
362 if(*first != tombstone) {
364 alloc_traits::destroy(allocator, std::addressof(element_at(static_cast<size_type>(first.index()))));
365 }
366 } else {
368 alloc_traits::destroy(allocator, std::addressof(element_at(static_cast<size_type>(first.index()))));
369 }
370 }
371 }
372
380 underlying_iterator try_emplace([[maybe_unused]] const Entity entt, [[maybe_unused]] const bool force_back, const void *value) override {
381 if(value != nullptr) {
382 if constexpr(std::is_copy_constructible_v<element_type>) {
383 return emplace_element(entt, force_back, *static_cast<const element_type *>(value));
384 } else {
385 return base_type::end();
386 }
387 } else {
388 if constexpr(std::is_default_constructible_v<element_type>) {
389 return emplace_element(entt, force_back);
390 } else {
391 return base_type::end();
392 }
393 }
394 }
395
396public:
398 using allocator_type = Allocator;
400 using base_type = underlying_type;
402 using element_type = Type;
406 using entity_type = Entity;
408 using size_type = std::size_t;
410 using difference_type = std::ptrdiff_t;
412 using pointer = typename container_type::pointer;
414 using const_pointer = typename alloc_traits::template rebind_traits<typename alloc_traits::const_pointer>::const_pointer;
416 using iterator = internal::storage_iterator<container_type, traits_type::page_size>;
418 using const_iterator = internal::storage_iterator<const container_type, traits_type::page_size>;
420 using reverse_iterator = std::reverse_iterator<iterator>;
422 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
433
437
442 explicit basic_storage(const allocator_type &allocator)
444 payload{allocator} {}
445
447 basic_storage(const basic_storage &) = delete;
448
453 basic_storage(basic_storage &&other) noexcept
454 : base_type{static_cast<base_type &&>(other)},
455 payload{std::move(other.payload)} {}
456
462 basic_storage(basic_storage &&other, const allocator_type &allocator)
463 : base_type{static_cast<base_type &&>(other), allocator},
464 payload{std::move(other.payload), allocator} {
465 ENTT_ASSERT(alloc_traits::is_always_equal::value || get_allocator() == other.get_allocator(), "Copying a storage is not allowed");
466 }
467
469 // NOLINTNEXTLINE(bugprone-exception-escape)
470 ~basic_storage() override {
471 shrink_to_size(0u);
472 }
473
479
486 ENTT_ASSERT(alloc_traits::is_always_equal::value || get_allocator() == other.get_allocator(), "Copying a storage is not allowed");
487 swap(other);
488 return *this;
489 }
490
495 void swap(basic_storage &other) noexcept {
496 using std::swap;
497 swap(payload, other.payload);
498 base_type::swap(other);
499 }
500
505 [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
506 return payload.get_allocator();
507 }
508
517 void reserve(const size_type cap) override {
518 if(cap != 0u) {
520 assure_at_least(cap - 1u);
521 }
522 }
523
529 [[nodiscard]] size_type capacity() const noexcept override {
530 return payload.size() * traits_type::page_size;
531 }
532
534 void shrink_to_fit() override {
536 shrink_to_size(base_type::size());
537 }
538
543 [[nodiscard]] const_pointer raw() const noexcept {
544 return payload.data();
545 }
546
548 [[nodiscard]] pointer raw() noexcept {
549 return payload.data();
550 }
551
559 [[nodiscard]] const_iterator cbegin() const noexcept {
560 const auto pos = static_cast<difference_type>(base_type::size());
561 return const_iterator{&payload, pos};
562 }
563
565 [[nodiscard]] const_iterator begin() const noexcept {
566 return cbegin();
567 }
568
570 [[nodiscard]] iterator begin() noexcept {
571 const auto pos = static_cast<difference_type>(base_type::size());
572 return iterator{&payload, pos};
573 }
574
580 [[nodiscard]] const_iterator cend() const noexcept {
581 return const_iterator{&payload, {}};
582 }
583
585 [[nodiscard]] const_iterator end() const noexcept {
586 return cend();
587 }
588
590 [[nodiscard]] iterator end() noexcept {
591 return iterator{&payload, {}};
592 }
593
601 [[nodiscard]] const_reverse_iterator crbegin() const noexcept {
602 return std::make_reverse_iterator(cend());
603 }
604
606 [[nodiscard]] const_reverse_iterator rbegin() const noexcept {
607 return crbegin();
608 }
609
611 [[nodiscard]] reverse_iterator rbegin() noexcept {
612 return std::make_reverse_iterator(end());
613 }
614
620 [[nodiscard]] const_reverse_iterator crend() const noexcept {
621 return std::make_reverse_iterator(cbegin());
622 }
623
625 [[nodiscard]] const_reverse_iterator rend() const noexcept {
626 return crend();
627 }
628
630 [[nodiscard]] reverse_iterator rend() noexcept {
631 return std::make_reverse_iterator(begin());
632 }
633
644 [[nodiscard]] const value_type &get(const entity_type entt) const noexcept {
645 return element_at(base_type::index(entt));
646 }
647
649 [[nodiscard]] value_type &get(const entity_type entt) noexcept {
650 return const_cast<value_type &>(std::as_const(*this).get(entt));
651 }
652
658 [[nodiscard]] std::tuple<const value_type &> get_as_tuple(const entity_type entt) const noexcept {
659 return std::forward_as_tuple(get(entt));
660 }
661
663 [[nodiscard]] std::tuple<value_type &> get_as_tuple(const entity_type entt) noexcept {
664 return std::forward_as_tuple(get(entt));
665 }
666
679 template<typename... Args>
680 value_type &emplace(const entity_type entt, Args &&...args) {
681 if constexpr(std::is_aggregate_v<value_type> && (sizeof...(Args) != 0u || !std::is_default_constructible_v<value_type>)) {
682 const auto it = emplace_element(entt, false, Type{std::forward<Args>(args)...});
683 return element_at(static_cast<size_type>(it.index()));
684 } else {
685 const auto it = emplace_element(entt, false, std::forward<Args>(args)...);
686 return element_at(static_cast<size_type>(it.index()));
687 }
688 }
689
697 template<typename... Func>
698 value_type &patch(const entity_type entt, Func &&...func) {
699 const auto idx = base_type::index(entt);
700 auto &elem = element_at(idx);
701 (std::forward<Func>(func)(elem), ...);
702 return elem;
703 }
704
719 template<typename It>
720 iterator insert(It first, It last, const value_type &value = {}) {
721 for(; first != last; ++first) {
722 emplace_element(*first, true, value);
723 }
724
725 return begin();
726 }
727
741 template<typename EIt, typename CIt, typename = std::enable_if_t<std::is_same_v<typename std::iterator_traits<CIt>::value_type, value_type>>>
742 iterator insert(EIt first, EIt last, CIt from) {
743 for(; first != last; ++first, ++from) {
744 emplace_element(*first, true, *from);
745 }
746
747 return begin();
748 }
749
758 [[nodiscard]] iterable each() noexcept {
759 return iterable{{base_type::begin(), begin()}, {base_type::end(), end()}};
760 }
761
763 [[nodiscard]] const_iterable each() const noexcept {
765 }
766
774 [[nodiscard]] reverse_iterable reach() noexcept {
776 }
777
779 [[nodiscard]] const_reverse_iterable reach() const noexcept {
781 }
782
783private:
784 container_type payload;
785};
786
788template<typename Type, typename Entity, typename Allocator>
789class basic_storage<Type, Entity, Allocator, std::enable_if_t<component_traits<Type, Entity>::page_size == 0u>>
790 : public basic_sparse_set<Entity, typename std::allocator_traits<Allocator>::template rebind_alloc<Entity>> {
791 using alloc_traits = std::allocator_traits<Allocator>;
792 static_assert(std::is_same_v<typename alloc_traits::value_type, Type>, "Invalid value type");
793 using traits_type = component_traits<Type, Entity>;
794
795public:
797 using allocator_type = Allocator;
801 using element_type = Type;
803 using value_type = void;
805 using entity_type = Entity;
807 using size_type = std::size_t;
809 using difference_type = std::ptrdiff_t;
820
824
829 explicit basic_storage(const allocator_type &allocator)
830 : base_type{type_id<element_type>(), storage_policy, allocator} {}
831
833 basic_storage(const basic_storage &) = delete;
834
839 basic_storage(basic_storage &&other) noexcept = default;
840
846 basic_storage(basic_storage &&other, const allocator_type &allocator)
847 : base_type{std::move(other), allocator} {}
848
850 ~basic_storage() override = default;
851
857
863 basic_storage &operator=(basic_storage &&other) noexcept = default;
864
869 [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
870 // std::allocator<void> has no cross constructors (waiting for C++20)
871 if constexpr(std::is_void_v<element_type> && !std::is_constructible_v<allocator_type, typename base_type::allocator_type>) {
872 return allocator_type{};
873 } else {
875 }
876 }
877
887 void get([[maybe_unused]] const entity_type entt) const noexcept {
888 ENTT_ASSERT(base_type::contains(entt), "Invalid entity");
889 }
890
896 [[nodiscard]] std::tuple<> get_as_tuple([[maybe_unused]] const entity_type entt) const noexcept {
897 ENTT_ASSERT(base_type::contains(entt), "Invalid entity");
898 return std::tuple{};
899 }
900
912 }
913
920 template<typename... Func>
921 void patch([[maybe_unused]] const entity_type entt, Func &&...func) {
922 ENTT_ASSERT(base_type::contains(entt), "Invalid entity");
923 (std::forward<Func>(func)(), ...);
924 }
925
932 template<typename It>
933 void insert(It first, It last) {
934 for(; first != last; ++first) {
935 base_type::try_emplace(*first, true);
936 }
937 }
938
946 [[nodiscard]] iterable each() noexcept {
948 }
949
951 [[nodiscard]] const_iterable each() const noexcept {
953 }
954
962 [[nodiscard]] reverse_iterable reach() noexcept {
964 }
965
967 [[nodiscard]] const_reverse_iterable reach() const noexcept {
969 }
970};
971
977template<typename Entity, typename Allocator>
978class basic_storage<Entity, Entity, Allocator>
979 : public basic_sparse_set<Entity, Allocator> {
980 using alloc_traits = std::allocator_traits<Allocator>;
981 static_assert(std::is_same_v<typename alloc_traits::value_type, Entity>, "Invalid value type");
982 using underlying_iterator = typename basic_sparse_set<Entity, Allocator>::basic_iterator;
983 using traits_type = entt_traits<Entity>;
984
985 auto from_placeholder() noexcept {
986 const auto entt = traits_type::combine(static_cast<typename traits_type::entity_type>(placeholder), {});
987 ENTT_ASSERT(entt != null, "No more entities available");
988 placeholder += static_cast<size_type>(entt != null);
989 return entt;
990 }
991
992 auto next() noexcept {
993 entity_type entt = from_placeholder();
994
996 entt = from_placeholder();
997 }
998
999 return entt;
1000 }
1001
1002protected:
1004 void pop_all() override {
1006 placeholder = {};
1007 }
1008
1014 underlying_iterator try_emplace(const Entity hint, const bool, const void *) override {
1015 return base_type::find(generate(hint));
1016 }
1017
1018public:
1020 using allocator_type = Allocator;
1024 using element_type = Entity;
1026 using value_type = void;
1028 using entity_type = Entity;
1030 using size_type = std::size_t;
1032 using difference_type = std::ptrdiff_t;
1043
1048
1053 explicit basic_storage(const allocator_type &allocator)
1054 : base_type{type_id<void>(), storage_policy, allocator} {}
1055
1057 basic_storage(const basic_storage &) = delete;
1058
1063 // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
1065 : base_type{static_cast<base_type &&>(other)},
1066 placeholder{other.placeholder} {}
1067
1073 // NOLINTNEXTLINE(cppcoreguidelines-rvalue-reference-param-not-moved)
1074 basic_storage(basic_storage &&other, const allocator_type &allocator)
1075 : base_type{static_cast<base_type &&>(other), allocator},
1076 placeholder{other.placeholder} {}
1077
1079 ~basic_storage() override = default;
1080
1086
1093 placeholder = other.placeholder;
1094 base_type::operator=(std::move(other));
1095 return *this;
1096 }
1097
1102 void swap(basic_storage &other) noexcept {
1103 using std::swap;
1104 swap(placeholder, other.placeholder);
1105 base_type::swap(other);
1106 }
1107
1117 void get([[maybe_unused]] const entity_type entt) const noexcept {
1118 ENTT_ASSERT(base_type::index(entt) < base_type::free_list(), "The requested entity is not a live one");
1119 }
1120
1126 [[nodiscard]] std::tuple<> get_as_tuple([[maybe_unused]] const entity_type entt) const noexcept {
1127 ENTT_ASSERT(base_type::index(entt) < base_type::free_list(), "The requested entity is not a live one");
1128 return std::tuple{};
1129 }
1130
1136 const auto len = base_type::free_list();
1137 const auto entt = (len == base_type::size()) ? next() : base_type::data()[len];
1138 return *base_type::try_emplace(entt, true);
1139 }
1140
1151 if(hint != null && hint != tombstone) {
1152 if(const auto curr = traits_type::construct(traits_type::to_entity(hint), base_type::current(hint)); curr == tombstone || !(base_type::index(curr) < base_type::free_list())) {
1153 return *base_type::try_emplace(hint, true);
1154 }
1155 }
1156
1157 return generate();
1158 }
1159
1166 template<typename It>
1167 void generate(It first, It last) {
1168 for(const auto sz = base_type::size(); first != last && base_type::free_list() != sz; ++first) {
1170 }
1171
1172 for(; first != last; ++first) {
1173 *first = *base_type::try_emplace(next(), true);
1174 }
1175 }
1176
1183 template<typename... Func>
1184 void patch([[maybe_unused]] const entity_type entt, Func &&...func) {
1185 ENTT_ASSERT(base_type::index(entt) < base_type::free_list(), "The requested entity is not a live one");
1186 (std::forward<Func>(func)(), ...);
1187 }
1188
1196 [[nodiscard]] iterable each() noexcept {
1197 return std::as_const(*this).each();
1198 }
1199
1201 [[nodiscard]] const_iterable each() const noexcept {
1202 const auto it = base_type::cend();
1203 const auto offset = static_cast<difference_type>(base_type::free_list());
1204 return const_iterable{it - offset, it};
1205 }
1206
1214 [[nodiscard]] reverse_iterable reach() noexcept {
1215 return std::as_const(*this).reach();
1216 }
1217
1219 [[nodiscard]] const_reverse_iterable reach() const noexcept {
1220 const auto it = base_type::crbegin();
1221 const auto offset = static_cast<difference_type>(base_type::free_list());
1222 return const_reverse_iterable{it, it + offset};
1223 }
1224
1232 void start_from(const entity_type hint) {
1233 placeholder = static_cast<size_type>(traits_type::to_entity(hint));
1234 }
1235
1236private:
1237 size_type placeholder{};
1238};
1239
1240} // namespace entt
1241
1242#endif
typename Traits::entity_type entity_type
Underlying entity type.
Definition entity.hpp:71
static constexpr value_type construct(const entity_type entity, const version_type version) noexcept
Constructs an identifier from its parts.
Definition entity.hpp:131
static constexpr entity_type to_entity(const value_type value) noexcept
Returns the entity part once converted to the underlying type.
Definition entity.hpp:94
static constexpr value_type combine(const entity_type lhs, const entity_type rhs) noexcept
Combines two identifiers in a single one.
Definition entity.hpp:149
static constexpr version_type to_version(const value_type value) noexcept
Returns the version part once converted to the underlying type.
Definition entity.hpp:103
virtual basic_iterator try_emplace(const Entity entt, const bool force_back, const void *=nullptr)
version_type current(const entity_type entt) const noexcept
Returns the contained version for an identifier.
size_type free_list() const noexcept
Returns data on the free list whose meaning depends on the mode.
const_reverse_iterable reach() const noexcept
Returns a reverse iterable object to use to visit a storage.
Definition storage.hpp:1219
std::size_t size_type
Unsigned integer type.
Definition storage.hpp:1030
iterable_adaptor< internal::extended_storage_iterator< typename base_type::reverse_iterator > > reverse_iterable
Extended reverse iterable storage proxy.
Definition storage.hpp:1038
static constexpr deletion_policy storage_policy
Storage deletion policy.
Definition storage.hpp:1042
void get(const entity_type entt) const noexcept
Returns the object assigned to an entity, that is void.
Definition storage.hpp:1117
void value_type
Type of the objects assigned to entities.
Definition storage.hpp:1026
entity_type generate(const entity_type hint)
Creates a new identifier or recycles a destroyed one.
Definition storage.hpp:1150
iterable_adaptor< internal::extended_storage_iterator< typename base_type::const_iterator > > const_iterable
Constant extended iterable storage proxy.
Definition storage.hpp:1036
Entity entity_type
Underlying entity identifier.
Definition storage.hpp:1028
void pop_all() override
Erases all entities of a storage.
Definition storage.hpp:1004
~basic_storage() override=default
Default destructor.
underlying_iterator try_emplace(const Entity hint, const bool, const void *) override
Assigns an entity to a storage.
Definition storage.hpp:1014
basic_storage & operator=(basic_storage &&other) noexcept
Move assignment operator.
Definition storage.hpp:1092
entity_type generate()
Creates a new identifier or recycles a destroyed one.
Definition storage.hpp:1135
reverse_iterable reach() noexcept
Returns a reverse iterable object to use to visit a storage.
Definition storage.hpp:1214
basic_storage(basic_storage &&other, const allocator_type &allocator)
Allocator-extended move constructor.
Definition storage.hpp:1074
std::ptrdiff_t difference_type
Signed integer type.
Definition storage.hpp:1032
void swap(basic_storage &other) noexcept
Exchanges the contents with those of a given storage.
Definition storage.hpp:1102
const_iterable each() const noexcept
Returns an iterable object to use to visit a storage.
Definition storage.hpp:1201
basic_storage(const allocator_type &allocator)
Constructs an empty container with a given allocator.
Definition storage.hpp:1053
void generate(It first, It last)
Assigns each element in a range an identifier.
Definition storage.hpp:1167
iterable_adaptor< internal::extended_storage_iterator< typename base_type::const_reverse_iterator > > const_reverse_iterable
Constant extended reverse iterable storage proxy.
Definition storage.hpp:1040
iterable_adaptor< internal::extended_storage_iterator< typename base_type::iterator > > iterable
Extended iterable storage proxy.
Definition storage.hpp:1034
basic_sparse_set< Entity, Allocator > base_type
Base type.
Definition storage.hpp:1022
basic_storage(basic_storage &&other) noexcept
Move constructor.
Definition storage.hpp:1064
iterable each() noexcept
Returns an iterable object to use to visit a storage.
Definition storage.hpp:1196
basic_storage(const basic_storage &)=delete
Default copy constructor, deleted on purpose.
void patch(const entity_type entt, Func &&...func)
Updates a given identifier.
Definition storage.hpp:1184
void start_from(const entity_type hint)
Sets the starting identifier for generation.
Definition storage.hpp:1232
basic_storage & operator=(const basic_storage &)=delete
Default copy assignment operator, deleted on purpose.
std::tuple get_as_tuple(const entity_type entt) const noexcept
Returns an empty tuple.
Definition storage.hpp:1126
basic_storage & operator=(const basic_storage &)=delete
Default copy assignment operator, deleted on purpose.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition storage.hpp:869
basic_sparse_set< Entity, typename alloc_traits::template rebind_alloc< Entity > > base_type
Base type.
Definition storage.hpp:799
void emplace(const entity_type entt)
Assigns an entity to a storage and constructs its object.
Definition storage.hpp:910
std::tuple get_as_tuple(const entity_type entt) const noexcept
Returns an empty tuple.
Definition storage.hpp:896
void patch(const entity_type entt, Func &&...func)
Updates the instance assigned to a given entity in-place.
Definition storage.hpp:921
reverse_iterable reach() noexcept
Returns a reverse iterable object to use to visit a storage.
Definition storage.hpp:962
basic_storage(const allocator_type &allocator)
Constructs an empty container with a given allocator.
Definition storage.hpp:829
basic_storage(basic_storage &&other, const allocator_type &allocator)
Allocator-extended move constructor.
Definition storage.hpp:846
iterable_adaptor< internal::extended_storage_iterator< typename base_type::iterator > > iterable
Extended iterable storage proxy.
Definition storage.hpp:811
iterable_adaptor< internal::extended_storage_iterator< typename base_type::reverse_iterator > > reverse_iterable
Extended reverse iterable storage proxy.
Definition storage.hpp:815
iterable_adaptor< internal::extended_storage_iterator< typename base_type::const_iterator > > const_iterable
Constant extended iterable storage proxy.
Definition storage.hpp:813
basic_storage(const basic_storage &)=delete
Default copy constructor, deleted on purpose.
void get(const entity_type entt) const noexcept
Returns the object assigned to an entity, that is void.
Definition storage.hpp:887
const_iterable each() const noexcept
Returns an iterable object to use to visit a storage.
Definition storage.hpp:951
const_reverse_iterable reach() const noexcept
Returns a reverse iterable object to use to visit a storage.
Definition storage.hpp:967
basic_storage & operator=(basic_storage &&other) noexcept=default
Move assignment operator.
iterable_adaptor< internal::extended_storage_iterator< typename base_type::const_reverse_iterator > > const_reverse_iterable
Constant extended reverse iterable storage proxy.
Definition storage.hpp:817
iterator end() noexcept
Returns an iterator to the end.
Definition storage.hpp:590
~basic_storage() override
Default destructor.
Definition storage.hpp:470
basic_storage()
Default constructor.
Definition storage.hpp:435
iterator insert(EIt first, EIt last, CIt from)
Assigns one or more entities to a storage and constructs their objects from a given range.
Definition storage.hpp:742
const_reverse_iterator rbegin() const noexcept
Returns a reverse iterator to the beginning.
Definition storage.hpp:606
const_iterator end() const noexcept
Returns an iterator to the end.
Definition storage.hpp:585
const_pointer raw() const noexcept
Direct access to the array of objects.
Definition storage.hpp:543
void pop_all() override
Erases all entities of a storage.
Definition storage.hpp:357
basic_storage(basic_storage &&other) noexcept
Move constructor.
Definition storage.hpp:453
const value_type & get(const entity_type entt) const noexcept
Returns the object assigned to an entity.
Definition storage.hpp:644
const_iterator cend() const noexcept
Returns an iterator to the end.
Definition storage.hpp:580
size_type capacity() const noexcept override
Returns the number of elements that a storage has currently allocated space for.
Definition storage.hpp:529
iterable_adaptor< internal::extended_storage_iterator< typename base_type::const_reverse_iterator, const_reverse_iterator > > const_reverse_iterable
Definition storage.hpp:430
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition storage.hpp:505
typename alloc_traits::template rebind_traits< typename alloc_traits::const_pointer >::const_pointer const_pointer
Definition storage.hpp:414
const_iterable each() const noexcept
Returns an iterable object to use to visit a storage.
Definition storage.hpp:763
void swap(basic_storage &other) noexcept
Exchanges the contents with those of a given storage.
Definition storage.hpp:495
reverse_iterator rbegin() noexcept
Returns a reverse iterator to the beginning.
Definition storage.hpp:611
internal::storage_iterator< container_type, traits_type::page_size > iterator
Definition storage.hpp:416
const_iterator begin() const noexcept
Returns an iterator to the beginning.
Definition storage.hpp:565
underlying_type base_type
Definition storage.hpp:400
reverse_iterator rend() noexcept
Returns a reverse iterator to the end.
Definition storage.hpp:630
basic_storage & operator=(basic_storage &&other) noexcept
Move assignment operator.
Definition storage.hpp:485
const_reverse_iterator rend() const noexcept
Returns a reverse iterator to the end.
Definition storage.hpp:625
underlying_iterator try_emplace(const Entity entt, const bool force_back, const void *value) override
Assigns an entity to a storage.
Definition storage.hpp:380
const_iterator cbegin() const noexcept
Returns an iterator to the beginning.
Definition storage.hpp:559
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition storage.hpp:422
value_type & patch(const entity_type entt, Func &&...func)
Updates the instance assigned to a given entity in-place.
Definition storage.hpp:698
std::reverse_iterator< iterator > reverse_iterator
Definition storage.hpp:420
std::size_t size_type
Unsigned integer type.
Definition storage.hpp:408
iterator insert(It first, It last, const value_type &value={})
Assigns one or more entities to a storage and constructs their objects from a given instance.
Definition storage.hpp:720
void pop(underlying_iterator first, underlying_iterator last) override
Erases entities from a storage.
Definition storage.hpp:338
typename container_type::pointer pointer
Definition storage.hpp:412
reverse_iterable reach() noexcept
Returns a reverse iterable object to use to visit a storage.
Definition storage.hpp:774
iterable_adaptor< internal::extended_storage_iterator< typename base_type::reverse_iterator, reverse_iterator > > reverse_iterable
Definition storage.hpp:428
iterable_adaptor< internal::extended_storage_iterator< typename base_type::iterator, iterator > > iterable
Definition storage.hpp:424
pointer raw() noexcept
Direct access to the array of objects.
Definition storage.hpp:548
std::tuple< value_type & > get_as_tuple(const entity_type entt) noexcept
Returns the object assigned to an entity as a tuple.
Definition storage.hpp:663
std::ptrdiff_t difference_type
Definition storage.hpp:410
value_type & get(const entity_type entt) noexcept
Returns the object assigned to an entity.
Definition storage.hpp:649
const_reverse_iterator crend() const noexcept
Returns a reverse iterator to the end.
Definition storage.hpp:620
const_reverse_iterable reach() const noexcept
Returns a reverse iterable object to use to visit a storage.
Definition storage.hpp:779
iterable each() noexcept
Returns an iterable object to use to visit a storage.
Definition storage.hpp:758
basic_storage & operator=(const basic_storage &)=delete
Default copy assignment operator, deleted on purpose.
void reserve(const size_type cap) override
Increases the capacity of a storage.
Definition storage.hpp:517
basic_storage(const allocator_type &allocator)
Constructs an empty storage with a given allocator.
Definition storage.hpp:442
value_type & emplace(const entity_type entt, Args &&...args)
Assigns an entity to a storage and constructs its object.
Definition storage.hpp:680
std::tuple< const value_type & > get_as_tuple(const entity_type entt) const noexcept
Returns the object assigned to an entity as a tuple.
Definition storage.hpp:658
void shrink_to_fit() override
Requests the removal of unused capacity.
Definition storage.hpp:534
const_reverse_iterator crbegin() const noexcept
Returns a reverse iterator to the beginning.
Definition storage.hpp:601
internal::storage_iterator< const container_type, traits_type::page_size > const_iterator
Definition storage.hpp:418
basic_storage(basic_storage &&other, const allocator_type &allocator)
Allocator-extended move constructor.
Definition storage.hpp:462
iterable_adaptor< internal::extended_storage_iterator< typename base_type::const_iterator, const_iterator > > const_iterable
Definition storage.hpp:426
basic_storage(const basic_storage &)=delete
Default copy constructor, deleted on purpose.
iterator begin() noexcept
Returns an iterator to the beginning.
Definition storage.hpp:570
static constexpr deletion_policy storage_policy
Definition storage.hpp:432
Allocator allocator_type
Allocator type.
Definition storage.hpp:398
EnTT default namespace.
Definition dense_map.hpp:22
constexpr std::enable_if_t< std::is_unsigned_v< Type >, Type > fast_mod(const Type value, const std::size_t mod) noexcept
Fast module utility function (powers of two only).
Definition bit.hpp:63
constexpr null_t null
Compile-time constant for null entities.
Definition entity.hpp:375
constexpr tombstone_t tombstone
Compile-time constant for tombstone entities.
Definition entity.hpp:384
constexpr Type * uninitialized_construct_using_allocator(Type *value, const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:238
constexpr get_t< Type... > get
Variable template for lists of observed elements.
Definition fwd.hpp:167
constexpr bool operator<=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator<(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr type_list< Type..., Other... > operator+(type_list< Type... >, type_list< Other... >)
Concatenates multiple type lists.
deletion_policy
Storage deletion policy.
Definition fwd.hpp:17
@ swap_only
Swap-only deletion policy.
Definition fwd.hpp:23
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator>=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
const type_info & type_id() noexcept
Returns the type info object associated to a given type.
@ ref
Aliasing mode, non-const reference.
Definition fwd.hpp:19
constexpr bool operator>(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr auto to_address(Type &&ptr) noexcept
Unwraps fancy pointers, does nothing otherwise (waiting for C++20).
Definition memory.hpp:20
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Common way to access various properties of components.
Definition component.hpp:43
static constexpr bool in_place_delete
Definition component.hpp:52
static constexpr std::size_t page_size
Definition component.hpp:54
Entity traits.
Definition entity.hpp:163
Utility class to create an iterable object from a pair of iterators.
Definition iterator.hpp:141