EnTT  3.5.0
handle.hpp
1 #ifndef ENTT_ENTITY_HANDLE_HPP
2 #define ENTT_ENTITY_HANDLE_HPP
3 
4 
5 #include "registry.hpp"
6 
7 
8 namespace entt {
9 
10 
18 template<typename Entity>
19 struct basic_handle {
21  using entity_type = std::remove_const_t<Entity>;
22 
24  using registry_type = std::conditional_t<
25  std::is_const_v<Entity>,
28  >;
29 
35  basic_handle(registry_type &ref, entity_type value = null) ENTT_NOEXCEPT
36  : reg{&ref}, entt{value}
37  {}
38 
44  basic_handle & operator=(const entity_type value) ENTT_NOEXCEPT {
45  entt = value;
46  return *this;
47  }
48 
53  basic_handle & operator=(null_t) ENTT_NOEXCEPT {
54  return (*this = static_cast<entity_type>(null));
55  }
56 
61  [[nodiscard]] operator basic_handle<const entity_type>() const ENTT_NOEXCEPT {
62  return {*reg, entt};
63  }
64 
69  [[nodiscard]] operator entity_type() const ENTT_NOEXCEPT {
70  return entity();
71  }
72 
77  [[nodiscard]] explicit operator bool() const {
78  return reg->valid(entt);
79  }
80 
85  [[nodiscard]] registry_type & registry() const ENTT_NOEXCEPT {
86  return *reg;
87  }
88 
93  [[nodiscard]] entity_type entity() const ENTT_NOEXCEPT {
94  return entt;
95  }
96 
105  template<typename Component, typename... Args>
106  decltype(auto) emplace(Args &&... args) const {
107  return reg->template emplace<Component>(entt, std::forward<Args>(args)...);
108  }
109 
118  template<typename Component, typename... Args>
119  decltype(auto) emplace_or_replace(Args &&... args) const {
120  return reg->template emplace_or_replace<Component>(entt, std::forward<Args>(args)...);
121  }
122 
131  template<typename Component, typename... Func>
132  decltype(auto) patch(Func &&... func) const {
133  return reg->template patch<Component>(entt, std::forward<Func>(func)...);
134  }
135 
144  template<typename Component, typename... Args>
145  decltype(auto) replace(Args &&... args) const {
146  return reg->template replace<Component>(entt, std::forward<Args>(args)...);
147  }
148 
154  template<typename... Components>
155  void remove() const {
156  reg->template remove<Components...>(entt);
157  }
158 
165  template<typename... Components>
166  decltype(auto) remove_if_exists() const {
167  return reg->template remove_if_exists<Components...>(entt);
168  }
169 
174  void remove_all() const {
175  reg->remove_all(entt);
176  }
177 
184  template<typename... Components>
185  [[nodiscard]] decltype(auto) has() const {
186  return reg->template has<Components...>(entt);
187  }
188 
196  template<typename... Components>
197  [[nodiscard]] decltype(auto) any() const {
198  return reg->template any<Components...>(entt);
199  }
200 
207  template<typename... Components>
208  [[nodiscard]] decltype(auto) get() const {
209  return reg->template get<Components...>(entt);
210  }
211 
220  template<typename Component, typename... Args>
221  [[nodiscard]] decltype(auto) get_or_emplace(Args &&... args) const {
222  return reg->template get_or_emplace<Component>(entt, std::forward<Args>(args)...);
223  }
224 
231  template<typename... Components>
232  [[nodiscard]] decltype(auto) try_get() const {
233  return reg->template try_get<Components...>(entt);
234  }
235 
240  [[nodiscard]] bool orphan() const {
241  return reg->orphan(entt);
242  }
243 
250  template<typename Func>
251  void visit(Func &&func) const {
252  reg->visit(entt, std::forward<Func>(func));
253  }
254 
255 private:
256  registry_type *reg;
258 };
259 
260 
265 template<typename Entity>
267 
268 
273 template<typename Entity>
275 
276 
277 }
278 
279 
280 #endif
entt::basic_handle::remove_all
void remove_all() const
Removes all the components from a handle and makes it orphaned.
Definition: handle.hpp:174
entt::basic_handle
Non-owning handle to an entity.
Definition: fwd.hpp:36
entt::null_t
Null object for all entity identifiers.
Definition: entity.hpp:125
entt::basic_handle::emplace_or_replace
decltype(auto) emplace_or_replace(Args &&... args) const
Assigns or replaces the given component for a handle.
Definition: handle.hpp:119
entt::basic_handle::remove
void remove() const
Removes the given components from a handle.
Definition: handle.hpp:155
entt::basic_handle::remove_if_exists
decltype(auto) remove_if_exists() const
Removes the given components from a handle.
Definition: handle.hpp:166
entt::basic_handle::patch
decltype(auto) patch(Func &&... func) const
Patches the given component for a handle.
Definition: handle.hpp:132
entt::basic_handle::basic_handle
basic_handle(registry_type &ref, entity_type value=null) noexcept
Constructs a handle from a given registry and entity.
Definition: handle.hpp:35
entt::basic_handle::any
decltype(auto) any() const
Checks if a handle has at least one of the given components.
Definition: handle.hpp:197
entt
EnTT default namespace.
Definition: algorithm.hpp:13
entt::basic_handle::try_get
decltype(auto) try_get() const
Returns pointers to the given components for a handle.
Definition: handle.hpp:232
entt::basic_handle::has
decltype(auto) has() const
Checks if a handle has all the given components.
Definition: handle.hpp:185
entt::basic_handle::orphan
bool orphan() const
Checks if a handle has components assigned.
Definition: handle.hpp:240
entt::basic_handle::emplace
decltype(auto) emplace(Args &&... args) const
Assigns the given component to a handle.
Definition: handle.hpp:106
entt::basic_handle::replace
decltype(auto) replace(Args &&... args) const
Replaces the given component for a handle.
Definition: handle.hpp:145
entt::basic_handle::operator=
basic_handle & operator=(null_t) noexcept
Assigns the null object to a handle.
Definition: handle.hpp:53
entt::basic_handle::entity
entity_type entity() const noexcept
Returns the entity associated with a handle.
Definition: handle.hpp:93
entt::basic_handle
basic_handle(basic_registry< Entity > &, Entity) -> basic_handle< Entity >
Deduction guide.
entt::basic_handle::visit
void visit(Func &&func) const
Visits a handle and returns the types for its components.
Definition: handle.hpp:251
entt::basic_handle::get
decltype(auto) get() const
Returns references to the given components for a handle.
Definition: handle.hpp:208
entt::basic_registry< entity_type >
entt::basic_handle::operator=
basic_handle & operator=(const entity_type value) noexcept
Assigns an entity to a handle.
Definition: handle.hpp:44
entt::basic_handle::registry
registry_type & registry() const noexcept
Returns a reference to the underlying registry.
Definition: handle.hpp:85
entt::basic_handle::registry_type
std::conditional_t< std::is_const_v< Entity >, const basic_registry< entity_type >, basic_registry< entity_type > > registry_type
Type of registry accepted by the handle.
Definition: handle.hpp:28
entt::basic_handle::get_or_emplace
decltype(auto) get_or_emplace(Args &&... args) const
Returns a reference to the given component for a handle.
Definition: handle.hpp:221
entt::basic_handle::entity_type
std::remove_const_t< Entity > entity_type
Underlying entity identifier.
Definition: handle.hpp:21