EnTT 4.0.0
Loading...
Searching...
No Matches
handle.hpp
1#ifndef ENTT_ENTITY_HANDLE_HPP
2#define ENTT_ENTITY_HANDLE_HPP
3
4#include "../config/config.h"
5#include "../core/iterator.hpp"
6#include "../core/type_traits.hpp"
7#include "../stl/iterator.hpp"
8#include "../stl/tuple.hpp"
9#include "../stl/type_traits.hpp"
10#include "../stl/utility.hpp"
11#include "entity.hpp"
12#include "fwd.hpp"
13
14namespace entt {
15
17namespace internal {
18
19template<typename It>
20class handle_storage_iterator final {
21 template<typename>
22 friend class handle_storage_iterator;
23
24 using underlying_type = stl::remove_reference_t<typename It::value_type::second_type>;
25 using entity_type = underlying_type::entity_type;
26
27public:
28 using value_type = stl::iterator_traits<It>::value_type;
29 using pointer = input_iterator_pointer<value_type>;
30 using reference = value_type;
31 using difference_type = stl::ptrdiff_t;
32 using iterator_category = stl::input_iterator_tag;
33 using iterator_concept = stl::forward_iterator_tag;
34
35 constexpr handle_storage_iterator() noexcept
36 : entt{null},
37 it{},
38 last{} {}
39
40 constexpr handle_storage_iterator(entity_type value, It from, It to) noexcept
41 : entt{value},
42 it{from},
43 last{to} {
44 while(it != last && !it->second.contains(entt)) {
45 ++it;
46 }
47 }
48
49 constexpr handle_storage_iterator &operator++() noexcept {
50 for(++it; it != last && !it->second.contains(entt); ++it) {}
51 return *this;
52 }
53
54 constexpr handle_storage_iterator operator++(int) noexcept {
55 const handle_storage_iterator orig = *this;
56 return ++(*this), orig;
57 }
58
59 [[nodiscard]] constexpr reference operator*() const noexcept {
60 return *it;
61 }
62
63 [[nodiscard]] constexpr pointer operator->() const noexcept {
64 return operator*();
65 }
66
67 template<typename Other>
68 [[nodiscard]] constexpr bool operator==(const handle_storage_iterator<Other> &other) const noexcept {
69 return it == other.it;
70 }
71
72private:
73 entity_type entt;
74 It it;
75 It last;
76};
77
78} // namespace internal
80
89template<typename Registry, typename... Scope>
92
93 [[nodiscard]] auto &owner_or_assert() const noexcept {
94 ENTT_ASSERT(owner != nullptr, "Invalid pointer to registry");
95 return static_cast<Registry &>(*owner);
96 }
97
98public:
100 using registry_type = Registry;
106 using size_type = stl::size_t;
109
111 basic_handle() noexcept
112 : owner{},
113 entt{null} {}
114
121 : owner{&ref},
122 entt{value} {}
123
134 [[nodiscard]] iterable storage() const noexcept {
135 auto underlying = owner_or_assert().storage();
136 return iterable{{entt, underlying.begin(), underlying.end()}, {entt, underlying.end(), underlying.end()}};
137 }
138
140 [[nodiscard]] explicit operator bool() const noexcept {
141 return owner && owner->valid(entt);
142 }
143
149 [[nodiscard]] bool valid() const {
150 return static_cast<bool>(*this);
151 }
152
157 [[nodiscard]] registry_type *registry() const noexcept {
158 return owner;
159 }
160
165 [[nodiscard]] entity_type entity() const noexcept {
166 return entt;
167 }
168
170 [[nodiscard]] operator entity_type() const noexcept {
171 return entity();
172 }
173
175 void destroy() {
176 owner_or_assert().destroy(stl::exchange(entt, null));
177 }
178
183 void destroy(const version_type version) {
184 owner_or_assert().destroy(stl::exchange(entt, null), version);
185 }
186
194 template<typename Type, typename... Args>
195 // NOLINTNEXTLINE(modernize-use-nodiscard)
196 decltype(auto) emplace(Args &&...args) const {
197 static_assert(((sizeof...(Scope) == 0) || ... || stl::is_same_v<Type, Scope>), "Invalid type");
198 return owner_or_assert().template emplace<Type>(entt, stl::forward<Args>(args)...);
199 }
200
208 template<typename Type, typename... Args>
209 decltype(auto) emplace_or_replace(Args &&...args) const {
210 static_assert(((sizeof...(Scope) == 0) || ... || stl::is_same_v<Type, Scope>), "Invalid type");
211 return owner_or_assert().template emplace_or_replace<Type>(entt, stl::forward<Args>(args)...);
212 }
213
221 template<typename Type, typename... Func>
222 decltype(auto) patch(Func &&...func) const {
223 static_assert(((sizeof...(Scope) == 0) || ... || stl::is_same_v<Type, Scope>), "Invalid type");
224 return owner_or_assert().template patch<Type>(entt, stl::forward<Func>(func)...);
225 }
226
234 template<typename Type, typename... Args>
235 decltype(auto) replace(Args &&...args) const {
236 static_assert(((sizeof...(Scope) == 0) || ... || stl::is_same_v<Type, Scope>), "Invalid type");
237 return owner_or_assert().template replace<Type>(entt, stl::forward<Args>(args)...);
238 }
239
245 template<typename... Type>
246 // NOLINTNEXTLINE(modernize-use-nodiscard)
248 static_assert(sizeof...(Scope) == 0 || (type_list_contains_v<type_list<Scope...>, Type> && ...), "Invalid type");
249 return owner_or_assert().template remove<Type...>(entt);
250 }
251
256 template<typename... Type>
257 void erase() const {
258 static_assert(sizeof...(Scope) == 0 || (type_list_contains_v<type_list<Scope...>, Type> && ...), "Invalid type");
259 owner_or_assert().template erase<Type...>(entt);
260 }
261
267 template<typename... Type>
268 [[nodiscard]] decltype(auto) all_of() const {
269 return owner_or_assert().template all_of<Type...>(entt);
270 }
271
278 template<typename... Type>
279 [[nodiscard]] decltype(auto) any_of() const {
280 return owner_or_assert().template any_of<Type...>(entt);
281 }
282
288 template<typename... Type>
289 [[nodiscard]] decltype(auto) get() const {
290 static_assert(sizeof...(Scope) == 0 || (type_list_contains_v<type_list<Scope...>, Type> && ...), "Invalid type");
291 return owner_or_assert().template get<Type...>(entt);
292 }
293
301 template<typename Type, typename... Args>
302 [[nodiscard]] decltype(auto) get_or_emplace(Args &&...args) const {
303 static_assert(((sizeof...(Scope) == 0) || ... || stl::is_same_v<Type, Scope>), "Invalid type");
304 return owner_or_assert().template get_or_emplace<Type>(entt, stl::forward<Args>(args)...);
305 }
306
312 template<typename... Type>
313 [[nodiscard]] auto try_get() const {
314 static_assert(sizeof...(Scope) == 0 || (type_list_contains_v<type_list<Scope...>, Type> && ...), "Invalid type");
315 return owner_or_assert().template try_get<Type...>(entt);
316 }
317
322 [[nodiscard]] bool orphan() const {
323 return owner_or_assert().orphan(entt);
324 }
325
333 template<typename... Other>
334 [[nodiscard]] bool operator==(const basic_handle<Other...> &other) const noexcept {
335 return owner == other.registry() && entt == other.entity();
336 }
337
343 [[nodiscard]] constexpr bool operator==(const null_t other) const noexcept {
344 return (entt == other);
345 }
346
354 template<typename Other, typename... Args>
355 operator basic_handle<Other, Args...>() const noexcept {
356 static_assert(stl::is_same_v<Other, Registry> || stl::is_same_v<stl::remove_const_t<Other>, Registry>, "Invalid conversion between different handles");
357 static_assert((sizeof...(Scope) == 0 || ((sizeof...(Args) != 0 && sizeof...(Args) <= sizeof...(Scope)) && ... && (type_list_contains_v<type_list<Scope...>, Args>))), "Invalid conversion between different handles");
358 return owner ? basic_handle<Other, Args...>{*owner, entt} : basic_handle<Other, Args...>{};
359 }
360
361private:
362 registry_type *owner;
364};
365
366} // namespace entt
367
368#endif
internal::entt_traits< Type >::value_type value_type
Definition entity.hpp:81
internal::entt_traits< Type >::version_type version_type
Definition entity.hpp:85
void destroy()
Destroys the entity associated with a handle.
Definition handle.hpp:175
void erase() const
Erases the given elements from a handle.
Definition handle.hpp:257
decltype(auto) replace(Args &&...args) const
Replaces the given element for a handle.
Definition handle.hpp:235
decltype(auto) get() const
Returns references to the given elements for a handle.
Definition handle.hpp:289
decltype(auto) any_of() const
Checks if a handle has at least one of the given elements.
Definition handle.hpp:279
decltype(auto) all_of() const
Checks if a handle has all the given elements.
Definition handle.hpp:268
iterable storage() const noexcept
Returns an iterable object to use to visit a handle.
Definition handle.hpp:134
decltype(auto) patch(Func &&...func) const
Patches the given element for a handle.
Definition handle.hpp:222
constexpr bool operator==(const null_t other) const noexcept
Compares a handle with the null object.
Definition handle.hpp:343
iterable_adaptor< internal::handle_storage_iterator< typename decltype(stl::declval< registry_type >().storage())::iterator > > iterable
Definition handle.hpp:108
decltype(auto) emplace(Args &&...args) const
Assigns the given element to a handle.
Definition handle.hpp:196
basic_handle() noexcept
Constructs an invalid handle.
Definition handle.hpp:111
bool operator==(const basic_handle< Other... > &other) const noexcept
Compares two handles.
Definition handle.hpp:334
basic_handle(registry_type &ref, entity_type value) noexcept
Constructs a handle from a given registry and entity.
Definition handle.hpp:120
void destroy(const version_type version)
Destroys the entity associated with a handle.
Definition handle.hpp:183
decltype(auto) emplace_or_replace(Args &&...args) const
Assigns or replaces the given element for a handle.
Definition handle.hpp:209
bool orphan() const
Checks if a handle has elements assigned.
Definition handle.hpp:322
auto try_get() const
Returns pointers to the given elements for a handle.
Definition handle.hpp:313
size_type remove() const
Removes the given elements from a handle.
Definition handle.hpp:247
traits_type::value_type entity_type
Definition handle.hpp:102
decltype(auto) get_or_emplace(Args &&...args) const
Returns a reference to the given element for a handle.
Definition handle.hpp:302
traits_type::version_type version_type
Definition handle.hpp:104
bool valid() const
Checks if a handle refers to a valid registry and entity.
Definition handle.hpp:149
entity_type entity() const noexcept
Returns the entity associated with a handle.
Definition handle.hpp:165
registry_type * registry() const noexcept
Returns a pointer to the underlying registry, if any.
Definition handle.hpp:157
EnTT default namespace.
Definition dense_map.hpp:25
constexpr null_t null
Compile-time constant for null entities.
Definition entity.hpp:299
constexpr bool type_list_contains_v
Helper variable template.
@ ref
Aliasing mode, non-const reference.
Definition fwd.hpp:19
Entity traits.
Definition entity.hpp:177
Utility class to create an iterable object from a pair of iterators.
Definition iterator.hpp:125
Null object for all identifiers.
Definition entity.hpp:218
A class to use to push around lists of types, nothing more.