EnTT: src/entt/entity/runtime_view.hpp Source File

EnTT 3.16.0

Loading...
Searching...
No Matches
runtime_view.hpp
1#ifndef ENTT_ENTITY_RUNTIME_VIEW_HPP
2#define ENTT_ENTITY_RUNTIME_VIEW_HPP
3
4#include <algorithm>
5#include <cstddef>
6#include <iterator>
7#include <utility>
8#include <vector>
9#include "entity.hpp"
10#include "fwd.hpp"
11
12namespace entt {
13
15namespace internal {
16
17template<typename Set>
18class runtime_view_iterator final {
19 using iterator_type = typename Set::iterator;
20 using iterator_traits = std::iterator_traits<iterator_type>;
21
22 [[nodiscard]] bool valid() const {
23 return (!tombstone_check || *it != tombstone)
24 && std::all_of(++pools->begin(), pools->end(), [entt = *it](const auto *curr) { return curr->contains(entt); })
25 && std::none_of(filter->cbegin(), filter->cend(), [entt = *it](const auto *curr) { return curr && curr->contains(entt); });
26 }
27
28public:
29 using value_type = typename iterator_traits::value_type;
30 using pointer = typename iterator_traits::pointer;
31 using reference = typename iterator_traits::reference;
32 using difference_type = typename iterator_traits::difference_type;
33 using iterator_category = std::bidirectional_iterator_tag;
34
35 constexpr runtime_view_iterator() noexcept
36 : pools{},
37 filter{},
38 it{},
39 tombstone_check{} {}
40
41 runtime_view_iterator(const std::vector<Set *> &cpools, iterator_type curr, const std::vector<Set *> &ignore) noexcept
42 : pools{&cpools},
43 filter{&ignore},
44 it{curr},
45 tombstone_check{pools->size() == 1u && (*pools)[0u]->policy() == deletion_policy::in_place} {
46 if(it != (*pools)[0]->end() && !valid()) {
47 ++(*this);
48 }
49 }
50
51 runtime_view_iterator &operator++() {
52 ++it;
53 for(const auto last = (*pools)[0]->end(); it != last && !valid(); ++it) {}
54 return *this;
55 }
56
57 runtime_view_iterator operator++(int) {
58 const runtime_view_iterator orig = *this;
59 return ++(*this), orig;
60 }
61
62 runtime_view_iterator &operator--() {
63 --it;
64 for(const auto first = (*pools)[0]->begin(); it != first && !valid(); --it) {}
65 return *this;
66 }
67
68 runtime_view_iterator operator--(int) {
69 const runtime_view_iterator orig = *this;
70 return operator--(), orig;
71 }
72
73 [[nodiscard]] pointer operator->() const noexcept {
74 return it.operator->();
75 }
76
77 [[nodiscard]] reference operator*() const noexcept {
78 return *operator->();
79 }
80
81 [[nodiscard]] constexpr bool operator==(const runtime_view_iterator &other) const noexcept {
82 return it == other.it;
83 }
84
85 [[nodiscard]] constexpr bool operator!=(const runtime_view_iterator &other) const noexcept {
86 return !(*this == other);
87 }
88
89private:
90 const std::vector<Set *> *pools;
91 const std::vector<Set *> *filter;
92 iterator_type it;
93 bool tombstone_check;
94};
95
96} // namespace internal
98
122template<typename Type, typename Allocator>
123 class basic_runtime_view {
124 using alloc_traits = std::allocator_traits<Allocator>;
125 static_assert(std::is_same_v<typename alloc_traits::value_type, Type *>, "Invalid value type");
126 using container_type = std::vector<Type *, Allocator>;
127
128 [[nodiscard]] auto offset() const noexcept {
129 ENTT_ASSERT(!pools.empty(), "Invalid view");
130 const auto &leading = *pools.front();
131 return (leading.policy() == deletion_policy::swap_only) ? leading.free_list() : leading.size();
132 }
133
134public:
136 using allocator_type = Allocator;
138 using entity_type = typename Type::entity_type;
140 using size_type = std::size_t;
142 using difference_type = std::ptrdiff_t;
144 using common_type = Type;
146 using iterator = internal::runtime_view_iterator<common_type>;
147
149 basic_runtime_view() noexcept
150 : basic_runtime_view{allocator_type{}} {}
151
156 explicit basic_runtime_view(const allocator_type &allocator)
157 : pools{allocator},
158 filter{allocator} {}
159
161 basic_runtime_view(const basic_runtime_view &) = default;
162
168 basic_runtime_view(const basic_runtime_view &other, const allocator_type &allocator)
169 : pools{other.pools, allocator},
170 filter{other.filter, allocator} {}
171
173 basic_runtime_view(basic_runtime_view &&) noexcept = default;
174
180 basic_runtime_view(basic_runtime_view &&other, const allocator_type &allocator)
181 : pools{std::move(other.pools), allocator},
182 filter{std::move(other.filter), allocator} {}
183
185 ~basic_runtime_view() = default;
186
191 basic_runtime_view &operator=(const basic_runtime_view &) = default;
192
197 basic_runtime_view &operator=(basic_runtime_view &&) noexcept = default;
198
203 void swap(basic_runtime_view &other) noexcept {
204 using std::swap;
205 swap(pools, other.pools);
206 swap(filter, other.filter);
207 }
208
213 [[nodiscard]] constexpr allocator_type get_allocator() const noexcept {
214 return pools.get_allocator();
215 }
216
218 void clear() {
219 pools.clear();
220 filter.clear();
221 }
222
228 basic_runtime_view &iterate(common_type &base) {
229 if(pools.empty() || !(base.size() < pools.front()->size())) {
230 pools.push_back(&base);
231 } else {
232 pools.push_back(std::exchange(pools.front(), &base));
233 }
234
235 return *this;
236 }
237
243 basic_runtime_view &exclude(common_type &base) {
244 filter.push_back(&base);
245 return *this;
246 }
247
252 [[nodiscard]] size_type size_hint() const {
253 return pools.empty() ? size_type{} : offset();
254 }
255
264 [[nodiscard]] iterator begin() const {
265 return pools.empty() ? iterator{} : iterator{pools, pools.front()->end() - static_cast<difference_type>(offset()), filter};
266 }
267
274 [[nodiscard]] iterator end() const {
275 return pools.empty() ? iterator{} : iterator{pools, pools.front()->end(), filter};
276 }
277
282 [[nodiscard]] explicit operator bool() const noexcept {
283 return !(pools.empty() && filter.empty());
284 }
285
291 [[nodiscard]] bool contains(const entity_type entt) const {
292 return !pools.empty()
293 && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *curr) { return curr->contains(entt); })
294 && std::none_of(filter.cbegin(), filter.cend(), [entt](const auto *curr) { return curr && curr->contains(entt); })
295 && pools.front()->index(entt) < offset();
296 }
297
312 template<typename Func>
313 void each(Func func) const {
314 for(const auto entity: *this) {
315 func(entity);
316 }
317 }
318
319private:
320 container_type pools;
321 container_type filter;
322};
323
324} // namespace entt
325
326#endif
entt::basic_runtime_view::basic_runtime_view
basic_runtime_view(const basic_runtime_view &)=default
Default copy constructor.
entt::basic_runtime_view::begin
iterator begin() const
Returns an iterator to the first entity that has the given elements.
Definition runtime_view.hpp:264
entt::basic_runtime_view::basic_runtime_view
basic_runtime_view() noexcept
Default constructor to use to create empty, invalid views.
Definition runtime_view.hpp:149
entt::basic_runtime_view::basic_runtime_view
basic_runtime_view(const allocator_type &allocator)
Constructs an empty, invalid view with a given allocator.
Definition runtime_view.hpp:156
entt::basic_runtime_view< sparse_set >::iterator
internal::runtime_view_iterator< common_type > iterator
Definition runtime_view.hpp:146
entt::basic_runtime_view::operator=
basic_runtime_view & operator=(const basic_runtime_view &)=default
Default copy assignment operator.
entt::basic_runtime_view::get_allocator
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
Definition runtime_view.hpp:213
entt::basic_runtime_view< sparse_set >::difference_type
std::ptrdiff_t difference_type
Definition runtime_view.hpp:142
entt::basic_runtime_view::~basic_runtime_view
~basic_runtime_view()=default
Default destructor.
entt::basic_runtime_view::contains
bool contains(const entity_type entt) const
Checks if a view contains an entity.
Definition runtime_view.hpp:291
entt::basic_runtime_view::each
void each(Func func) const
Iterates entities and applies the given function object to them.
Definition runtime_view.hpp:313
entt::basic_runtime_view::exclude
basic_runtime_view & exclude(common_type &base)
Adds an opaque storage object as a filter of a runtime view.
Definition runtime_view.hpp:243
entt::basic_runtime_view< sparse_set >::allocator_type
Allocator allocator_type
Definition runtime_view.hpp:136
entt::basic_runtime_view::clear
void clear()
Clears the view.
Definition runtime_view.hpp:218
entt::basic_runtime_view< sparse_set >::entity_type
typename sparse_set::entity_type entity_type
Definition runtime_view.hpp:138
entt::basic_runtime_view::end
iterator end() const
Returns an iterator that is past the last entity that has the given elements.
Definition runtime_view.hpp:274
entt::basic_runtime_view::operator=
basic_runtime_view & operator=(basic_runtime_view &&) noexcept=default
Default move assignment operator.
entt::basic_runtime_view::size_hint
size_type size_hint() const
Estimates the number of entities iterated by the view.
Definition runtime_view.hpp:252
entt::basic_runtime_view< sparse_set >::common_type
sparse_set common_type
Definition runtime_view.hpp:144
entt::basic_runtime_view::basic_runtime_view
basic_runtime_view(const basic_runtime_view &other, const allocator_type &allocator)
Allocator-extended copy constructor.
Definition runtime_view.hpp:168
entt::basic_runtime_view< sparse_set >::swap
void swap(basic_runtime_view &other) noexcept
Definition runtime_view.hpp:203
entt::basic_runtime_view::iterate
basic_runtime_view & iterate(common_type &base)
Appends an opaque storage object to a runtime view.
Definition runtime_view.hpp:228
entt::basic_runtime_view< sparse_set >::size_type
std::size_t size_type
Definition runtime_view.hpp:140
entt::basic_runtime_view::basic_runtime_view
basic_runtime_view(basic_runtime_view &&) noexcept=default
Default move constructor.
entt::basic_sparse_set::size
size_type size() const noexcept
Returns the number of elements in a sparse set.
Definition sparse_set.hpp:614
entt
EnTT default namespace.
Definition dense_map.hpp:22
entt::entity
entity
Default entity identifier.
Definition fwd.hpp:14
entt::deletion_policy::swap_only
@ swap_only
Swap-only deletion policy.
Definition fwd.hpp:23
entt::operator!=
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:238
entt::operator==
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:226