1#ifndef ENTT_ENTITY_HANDLE_HPP
2#define ENTT_ENTITY_HANDLE_HPP
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"
20class handle_storage_iterator final {
22 friend class handle_storage_iterator;
24 using underlying_type = stl::remove_reference_t<typename It::value_type::second_type>;
25 using entity_type = underlying_type::entity_type;
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;
35 constexpr handle_storage_iterator() noexcept
40 constexpr handle_storage_iterator(entity_type value, It from, It to) noexcept
44 while(it != last && !it->second.contains(entt)) {
49 constexpr handle_storage_iterator &operator++() noexcept {
50 for(++it; it != last && !it->second.contains(entt); ++it) {}
54 constexpr handle_storage_iterator operator++(
int)
noexcept {
55 const handle_storage_iterator orig = *
this;
56 return ++(*this), orig;
59 [[nodiscard]]
constexpr reference operator*() const noexcept {
63 [[nodiscard]]
constexpr pointer operator->() const noexcept {
67 template<
typename Other>
68 [[nodiscard]]
constexpr bool operator==(
const handle_storage_iterator<Other> &other)
const noexcept {
69 return it == other.it;
89template<
typename Registry,
typename... Scope>
93 [[nodiscard]]
auto &owner_or_assert()
const noexcept {
94 ENTT_ASSERT(owner !=
nullptr,
"Invalid pointer to registry");
95 return static_cast<Registry &
>(*owner);
135 auto underlying = owner_or_assert().storage();
136 return iterable{{
entt, underlying.begin(), underlying.end()}, {
entt, underlying.end(), underlying.end()}};
140 [[nodiscard]]
explicit operator bool() const noexcept {
141 return owner && owner->valid(
entt);
150 return static_cast<bool>(*this);
176 owner_or_assert().destroy(stl::exchange(
entt,
null));
184 owner_or_assert().destroy(stl::exchange(
entt,
null), version);
194 template<
typename Type,
typename... Args>
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)...);
208 template<
typename Type,
typename... Args>
210 static_assert(((
sizeof...(Scope) == 0) || ... || stl::is_same_v<Type, Scope>),
"Invalid type");
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)...);
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)...);
245 template<
typename... Type>
249 return owner_or_assert().template
remove<Type...>(
entt);
256 template<
typename... Type>
259 owner_or_assert().template
erase<Type...>(
entt);
267 template<
typename... Type>
268 [[nodiscard]]
decltype(
auto)
all_of()
const {
269 return owner_or_assert().template
all_of<Type...>(
entt);
278 template<
typename... Type>
279 [[nodiscard]]
decltype(
auto)
any_of()
const {
280 return owner_or_assert().template
any_of<Type...>(
entt);
288 template<
typename... Type>
289 [[nodiscard]]
decltype(
auto)
get()
const {
291 return owner_or_assert().template
get<Type...>(
entt);
301 template<
typename Type,
typename... Args>
303 static_assert(((
sizeof...(Scope) == 0) || ... || stl::is_same_v<Type, Scope>),
"Invalid type");
312 template<
typename... Type>
315 return owner_or_assert().template
try_get<Type...>(
entt);
323 return owner_or_assert().orphan(
entt);
333 template<
typename... Other>
335 return owner == other.registry() &&
entt == other.entity();
344 return (
entt == other);
354 template<
typename Other,
typename... Args>
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");
internal::entt_traits< Type >::value_type value_type
internal::entt_traits< Type >::version_type version_type
void destroy()
Destroys the entity associated with a handle.
void erase() const
Erases the given elements from a handle.
decltype(auto) replace(Args &&...args) const
Replaces the given element for a handle.
decltype(auto) get() const
Returns references to the given elements for a handle.
decltype(auto) any_of() const
Checks if a handle has at least one of the given elements.
decltype(auto) all_of() const
Checks if a handle has all the given elements.
iterable storage() const noexcept
Returns an iterable object to use to visit a handle.
decltype(auto) patch(Func &&...func) const
Patches the given element for a handle.
constexpr bool operator==(const null_t other) const noexcept
Compares a handle with the null object.
iterable_adaptor< internal::handle_storage_iterator< typename decltype(stl::declval< registry_type >().storage())::iterator > > iterable
decltype(auto) emplace(Args &&...args) const
Assigns the given element to a handle.
basic_handle() noexcept
Constructs an invalid handle.
bool operator==(const basic_handle< Other... > &other) const noexcept
Compares two handles.
basic_handle(registry_type &ref, entity_type value) noexcept
Constructs a handle from a given registry and entity.
void destroy(const version_type version)
Destroys the entity associated with a handle.
decltype(auto) emplace_or_replace(Args &&...args) const
Assigns or replaces the given element for a handle.
bool orphan() const
Checks if a handle has elements assigned.
auto try_get() const
Returns pointers to the given elements for a handle.
size_type remove() const
Removes the given elements from a handle.
traits_type::value_type entity_type
decltype(auto) get_or_emplace(Args &&...args) const
Returns a reference to the given element for a handle.
traits_type::version_type version_type
bool valid() const
Checks if a handle refers to a valid registry and entity.
entity_type entity() const noexcept
Returns the entity associated with a handle.
registry_type * registry() const noexcept
Returns a pointer to the underlying registry, if any.
constexpr null_t null
Compile-time constant for null entities.
constexpr bool type_list_contains_v
Helper variable template.
@ ref
Aliasing mode, non-const reference.
Utility class to create an iterable object from a pair of iterators.
Null object for all identifiers.
A class to use to push around lists of types, nothing more.