EnTT 3.13.2
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
21 [[nodiscard]] bool valid() const {
22 return (!tombstone_check || *it != tombstone)
23 && std::all_of(++pools->begin(), pools->end(), [entt = *it](const auto *curr) { return curr->contains(entt); })
24 && std::none_of(filter->cbegin(), filter->cend(), [entt = *it](const auto *curr) { return curr && curr->contains(entt); });
25 }
26
27public:
28 using difference_type = typename iterator_type::difference_type;
29 using value_type = typename iterator_type::value_type;
30 using pointer = typename iterator_type::pointer;
31 using reference = typename iterator_type::reference;
32 using iterator_category = std::bidirectional_iterator_tag;
33
34 constexpr runtime_view_iterator() noexcept
35 : pools{},
36 filter{},
37 it{},
38 tombstone_check{} {}
39
40 runtime_view_iterator(const std::vector<Set *> &cpools, const std::vector<Set *> &ignore, iterator_type curr) noexcept
41 : pools{&cpools},
42 filter{&ignore},
43 it{curr},
44 tombstone_check{pools->size() == 1u && (*pools)[0u]->policy() == deletion_policy::in_place} {
45 if(it != (*pools)[0]->end() && !valid()) {
46 ++(*this);
47 }
48 }
49
50 runtime_view_iterator &operator++() {
51 while(++it != (*pools)[0]->end() && !valid()) {}
52 return *this;
53 }
54
55 runtime_view_iterator operator++(int) {
56 runtime_view_iterator orig = *this;
57 return ++(*this), orig;
58 }
59
60 runtime_view_iterator &operator--() {
61 while(--it != (*pools)[0]->begin() && !valid()) {}
62 return *this;
63 }
64
65 runtime_view_iterator operator--(int) {
66 runtime_view_iterator orig = *this;
67 return operator--(), orig;
68 }
69
70 [[nodiscard]] pointer operator->() const noexcept {
71 return it.operator->();
72 }
73
74 [[nodiscard]] reference operator*() const noexcept {
75 return *operator->();
76 }
77
78 [[nodiscard]] constexpr bool operator==(const runtime_view_iterator &other) const noexcept {
79 return it == other.it;
80 }
81
82 [[nodiscard]] constexpr bool operator!=(const runtime_view_iterator &other) const noexcept {
83 return !(*this == other);
84 }
85
86private:
87 const std::vector<Set *> *pools;
88 const std::vector<Set *> *filter;
89 iterator_type it;
90 bool tombstone_check;
91};
92
93} // namespace internal
119template<typename Type, typename Allocator>
121 using alloc_traits = std::allocator_traits<Allocator>;
122 static_assert(std::is_same_v<typename alloc_traits::value_type, Type *>, "Invalid value type");
123 using container_type = std::vector<Type *, Allocator>;
124
125public:
127 using allocator_type = Allocator;
129 using entity_type = typename Type::entity_type;
131 using size_type = std::size_t;
133 using common_type = Type;
135 using iterator = internal::runtime_view_iterator<common_type>;
136
140
146 : pools{allocator},
147 filter{allocator} {}
148
151
158 : pools{other.pools, allocator},
159 filter{other.filter, allocator} {}
160
163
170 : pools{std::move(other.pools), allocator},
171 filter{std::move(other.filter), allocator} {}
172
178
184
190 using std::swap;
191 swap(pools, other.pools);
192 swap(filter, other.filter);
193 }
194
200 return pools.get_allocator();
201 }
202
204 void clear() {
205 pools.clear();
206 filter.clear();
207 }
208
215 if(pools.empty() || !(base.size() < pools[0u]->size())) {
216 pools.push_back(&base);
217 } else {
218 pools.push_back(std::exchange(pools[0u], &base));
219 }
220
221 return *this;
222 }
223
230 filter.push_back(&base);
231 return *this;
232 }
233
239 return pools.empty() ? size_type{} : pools.front()->size();
240 }
241
251 return pools.empty() ? iterator{} : iterator{pools, filter, pools[0]->begin()};
252 }
253
260 [[nodiscard]] iterator end() const {
261 return pools.empty() ? iterator{} : iterator{pools, filter, pools[0]->end()};
262 }
263
269 [[nodiscard]] bool contains(const entity_type entt) const {
270 return !pools.empty()
271 && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *curr) { return curr->contains(entt); })
272 && std::none_of(filter.cbegin(), filter.cend(), [entt](const auto *curr) { return curr && curr->contains(entt); });
273 }
274
289 template<typename Func>
290 void each(Func func) const {
291 for(const auto entity: *this) {
292 func(entity);
293 }
294 }
295
296private:
297 container_type pools;
298 container_type filter;
299};
300
301} // namespace entt
302
303#endif
Generic runtime view.
basic_runtime_view(const basic_runtime_view &)=default
Default copy constructor.
iterator begin() const
Returns an iterator to the first entity that has the given components.
basic_runtime_view() noexcept
Default constructor to use to create empty, invalid views.
basic_runtime_view(const allocator_type &allocator)
Constructs an empty, invalid view with a given allocator.
internal::runtime_view_iterator< common_type > iterator
Bidirectional iterator type.
basic_runtime_view & operator=(const basic_runtime_view &)=default
Default copy assignment operator.
void swap(basic_runtime_view &other)
Exchanges the contents with those of a given view.
constexpr allocator_type get_allocator() const noexcept
Returns the associated allocator.
bool contains(const entity_type entt) const
Checks if a view contains an entity.
void each(Func func) const
Iterates entities and applies the given function object to them.
basic_runtime_view & exclude(common_type &base)
Adds an opaque storage object as a filter of a runtime view.
Allocator allocator_type
Allocator type.
void clear()
Clears the view.
typename Type::entity_type entity_type
Underlying entity identifier.
iterator end() const
Returns an iterator that is past the last entity that has the given components.
size_type size_hint() const
Estimates the number of entities iterated by the view.
Type common_type
Common type among all storage types.
basic_runtime_view(const basic_runtime_view &other, const allocator_type &allocator)
Allocator-extended copy constructor.
basic_runtime_view & iterate(common_type &base)
Appends an opaque storage object to a runtime view.
basic_runtime_view & operator=(basic_runtime_view &&) noexcept(std::is_nothrow_move_assignable_v< container_type >)=default
Default move assignment operator.
std::size_t size_type
Unsigned integer type.
basic_runtime_view(basic_runtime_view &&) noexcept(std::is_nothrow_move_constructible_v< container_type >)=default
Default move constructor.
EnTT default namespace.
Definition dense_map.hpp:21
entity
Default entity identifier.
Definition fwd.hpp:13
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:258
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.