1#ifndef ENTT_ENTITY_HANDLE_HPP
2#define ENTT_ENTITY_HANDLE_HPP
8#include "../core/iterator.hpp"
9#include "../core/type_traits.hpp"
23class handle_storage_iterator final {
24 template<
typename Other>
25 friend class handle_storage_iterator;
27 using underlying_type = std::remove_reference_t<typename It::value_type::second_type>;
28 using entity_type =
typename underlying_type::entity_type;
31 using value_type =
typename std::iterator_traits<It>::value_type;
32 using pointer = input_iterator_pointer<value_type>;
33 using reference = value_type;
34 using difference_type = std::ptrdiff_t;
35 using iterator_category = std::input_iterator_tag;
37 constexpr handle_storage_iterator() noexcept
42 constexpr handle_storage_iterator(entity_type value, It from, It to) noexcept
46 while(it != last && !it->second.contains(
entt)) {
51 constexpr handle_storage_iterator &operator++() noexcept {
52 while(++it != last && !it->second.contains(
entt)) {}
56 constexpr handle_storage_iterator operator++(
int)
noexcept {
57 handle_storage_iterator orig = *
this;
58 return ++(*this), orig;
61 [[nodiscard]]
constexpr reference operator*() const noexcept {
65 [[nodiscard]]
constexpr pointer operator->() const noexcept {
69 template<
typename ILhs,
typename IRhs>
70 friend constexpr bool operator==(
const handle_storage_iterator<ILhs> &,
const handle_storage_iterator<IRhs> &)
noexcept;
78template<
typename ILhs,
typename IRhs>
79[[nodiscard]]
constexpr bool operator==(
const handle_storage_iterator<ILhs> &lhs,
const handle_storage_iterator<IRhs> &rhs)
noexcept {
80 return lhs.it == rhs.it;
83template<
typename ILhs,
typename IRhs>
84[[nodiscard]]
constexpr bool operator!=(
const handle_storage_iterator<ILhs> &lhs,
const handle_storage_iterator<IRhs> &rhs)
noexcept {
103template<
typename Registry,
typename... Scope>
139 auto iterable = reg->storage();
140 using iterator_type = internal::handle_storage_iterator<
typename decltype(iterable)::iterator>;
141 return iterable_adaptor{iterator_type{
entt, iterable.begin(), iterable.end()}, iterator_type{
entt, iterable.end(), iterable.end()}};
151 template<
typename Other,
typename... Args>
153 static_assert(std::is_same_v<Other, Registry> || std::is_same_v<std::remove_const_t<Other>, Registry>,
"Invalid conversion between different handles");
154 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");
171 [[nodiscard]]
explicit operator bool() const noexcept {
172 return reg && reg->valid(
entt);
180 return reg->valid(
entt);
201 reg->destroy(std::exchange(
entt,
null));
209 reg->destroy(std::exchange(
entt,
null), version);
219 template<
typename Component,
typename... Args>
220 decltype(
auto)
emplace(Args &&...args)
const {
221 static_assert(((
sizeof...(Scope) == 0) || ... || std::is_same_v<Component, Scope>),
"Invalid type");
222 return reg->template emplace<Component>(
entt, std::forward<Args>(args)...);
232 template<
typename Component,
typename... Args>
234 static_assert(((
sizeof...(Scope) == 0) || ... || std::is_same_v<Component, Scope>),
"Invalid type");
235 return reg->template emplace_or_replace<Component>(
entt, std::forward<Args>(args)...);
245 template<
typename Component,
typename... Func>
246 decltype(
auto)
patch(Func &&...func)
const {
247 static_assert(((
sizeof...(Scope) == 0) || ... || std::is_same_v<Component, Scope>),
"Invalid type");
248 return reg->template patch<Component>(
entt, std::forward<Func>(func)...);
258 template<
typename Component,
typename... Args>
259 decltype(
auto)
replace(Args &&...args)
const {
260 static_assert(((
sizeof...(Scope) == 0) || ... || std::is_same_v<Component, Scope>),
"Invalid type");
261 return reg->template replace<Component>(
entt, std::forward<Args>(args)...);
269 template<
typename... Component>
272 return reg->template
remove<Component...>(
entt);
279 template<
typename... Component>
290 template<
typename... Component>
291 [[nodiscard]]
decltype(
auto)
all_of()
const {
292 return reg->template
all_of<Component...>(
entt);
301 template<
typename... Component>
302 [[nodiscard]]
decltype(
auto)
any_of()
const {
303 return reg->template
any_of<Component...>(
entt);
311 template<
typename... Component>
312 [[nodiscard]]
decltype(
auto)
get()
const {
314 return reg->template
get<Component...>(
entt);
324 template<
typename Component,
typename... Args>
326 static_assert(((
sizeof...(Scope) == 0) || ... || std::is_same_v<Component, Scope>),
"Invalid type");
327 return reg->template get_or_emplace<Component>(
entt, std::forward<Args>(args)...);
335 template<
typename... Component>
346 return reg->orphan(
entt);
363template<
typename... Args,
typename... Other>
365 return lhs.registry() == rhs.registry() && lhs.entity() == rhs.entity();
377template<
typename... Args,
typename... Other>
379 return !(lhs == rhs);
constexpr null_t null
Compile-time constant for null entities.
constexpr bool type_list_contains_v
Helper variable template.
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.
Non-owning handle to an entity.
void destroy()
Destroys the entity associated with a handle.
void erase() const
Erases the given components from a handle.
typename registry_type::entity_type entity_type
Underlying entity identifier.
decltype(auto) replace(Args &&...args) const
Replaces the given component for a handle.
decltype(auto) get() const
Returns references to the given components for a handle.
decltype(auto) any_of() const
Checks if a handle has at least one of the given components.
decltype(auto) all_of() const
Checks if a handle has all the given components.
decltype(auto) patch(Func &&...func) const
Patches the given component for a handle.
decltype(auto) emplace(Args &&...args) const
Assigns the given component to a handle.
basic_handle() noexcept
Constructs an invalid handle.
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 component for a handle.
bool orphan() const
Checks if a handle has components assigned.
auto try_get() const
Returns pointers to the given components for a handle.
size_type remove() const
Removes the given components from a handle.
typename registry_type::version_type version_type
Underlying version type.
decltype(auto) get_or_emplace(Args &&...args) const
Returns a reference to the given component for a handle.
typename registry_type::size_type size_type
Unsigned integer type.
bool valid() const
Checks if a handle refers to a valid entity or not.
auto storage() const noexcept
Returns an iterable object to use to visit a handle.
entity_type entity() const noexcept
Returns the entity associated with a handle.
Registry registry_type
Type of registry accepted by the handle.
registry_type * registry() const noexcept
Returns a pointer to the underlying registry, if any.
Utility class to create an iterable object from a pair of iterators.
A class to use to push around lists of types, nothing more.