EnTT  3.6.0
handle.hpp
1 #ifndef ENTT_ENTITY_HANDLE_HPP
2 #define ENTT_ENTITY_HANDLE_HPP
3 
4 
5 #include <tuple>
6 #include <type_traits>
7 #include <utility>
8 #include "../config/config.h"
9 #include "../core/type_traits.hpp"
10 #include "fwd.hpp"
11 #include "registry.hpp"
12 
13 
14 namespace entt {
15 
16 
25 template<typename Entity, typename... Type>
26 struct basic_handle {
28  using entity_type = std::remove_const_t<Entity>;
31 
33  basic_handle() ENTT_NOEXCEPT
34  : reg{}, entt{null}
35  {}
36 
42  basic_handle(registry_type &ref, entity_type value) ENTT_NOEXCEPT
43  : reg{&ref}, entt{value}
44  {}
45 
53  template<typename... Args>
54  [[nodiscard]] bool operator==(const basic_handle<Args...> &other) const ENTT_NOEXCEPT {
55  return reg == other.registry() && entt == other.entity();
56  }
57 
65  template<typename Other, typename... Args>
66  operator basic_handle<Other, Args...>() const ENTT_NOEXCEPT {
67  static_assert(
68  (std::is_same_v<Other, Entity> || std::is_same_v<std::remove_const_t<Other>, Entity>)
69  && (sizeof...(Type) == 0 || ((sizeof...(Args) != 0 && sizeof...(Args) <= sizeof...(Type)) && ... && (type_list_contains_v<type_list<Type...>, Args>))),
70  "Invalid conversion between different handles"
71  );
72 
73  return reg ? basic_handle<Other, Args...>{*reg, entt} : basic_handle<Other, Args...>{};
74  }
75 
80  [[nodiscard]] operator entity_type() const ENTT_NOEXCEPT {
81  return entity();
82  }
83 
88  [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
89  return reg && reg->valid(entt);
90  }
91 
96  [[nodiscard]] bool valid() const {
97  return reg->valid(entt);
98  }
99 
104  [[nodiscard]] registry_type * registry() const ENTT_NOEXCEPT {
105  return reg;
106  }
107 
112  [[nodiscard]] entity_type entity() const ENTT_NOEXCEPT {
113  return entt;
114  }
115 
120  void destroy() {
121  reg->destroy(entt);
122  }
123 
129  void destroy(const typename registry_type::version_type version) {
130  reg->destroy(entt, version);
131  }
132 
141  template<typename Component, typename... Args>
142  decltype(auto) emplace(Args &&... args) const {
143  static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
144  return reg->template emplace<Component>(entt, std::forward<Args>(args)...);
145  }
146 
155  template<typename Component, typename... Args>
156  decltype(auto) emplace_or_replace(Args &&... args) const {
157  static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
158  return reg->template emplace_or_replace<Component>(entt, std::forward<Args>(args)...);
159  }
160 
169  template<typename Component, typename... Func>
170  decltype(auto) patch(Func &&... func) const {
171  static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
172  return reg->template patch<Component>(entt, std::forward<Func>(func)...);
173  }
174 
183  template<typename Component, typename... Args>
184  decltype(auto) replace(Args &&... args) const {
185  static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
186  return reg->template replace<Component>(entt, std::forward<Args>(args)...);
187  }
188 
194  template<typename... Component>
195  void remove() const {
196  static_assert(sizeof...(Type) == 0 || (type_list_contains_v<type_list<Type...>, Component> && ...), "Invalid type");
197  reg->template remove<Component...>(entt);
198  }
199 
206  template<typename... Component>
207  decltype(auto) remove_if_exists() const {
208  static_assert(sizeof...(Type) == 0 || (type_list_contains_v<type_list<Type...>, Component> && ...), "Invalid type");
209  return reg->template remove_if_exists<Component...>(entt);
210  }
211 
216  void remove_all() const {
217  static_assert(sizeof...(Type) == 0, "Invalid operation");
218  reg->remove_all(entt);
219  }
220 
227  template<typename... Component>
228  [[nodiscard]] decltype(auto) has() const {
229  return reg->template has<Component...>(entt);
230  }
231 
239  template<typename... Component>
240  [[nodiscard]] decltype(auto) any() const {
241  return reg->template any<Component...>(entt);
242  }
243 
250  template<typename... Component>
251  [[nodiscard]] decltype(auto) get() const {
252  static_assert(sizeof...(Type) == 0 || (type_list_contains_v<type_list<Type...>, Component> && ...), "Invalid type");
253  return reg->template get<Component...>(entt);
254  }
255 
264  template<typename Component, typename... Args>
265  [[nodiscard]] decltype(auto) get_or_emplace(Args &&... args) const {
266  static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
267  return reg->template get_or_emplace<Component>(entt, std::forward<Args>(args)...);
268  }
269 
276  template<typename... Component>
277  [[nodiscard]] auto try_get() const {
278  static_assert(sizeof...(Type) == 0 || (type_list_contains_v<type_list<Type...>, Component> && ...), "Invalid type");
279  return reg->template try_get<Component...>(entt);
280  }
281 
286  [[nodiscard]] bool orphan() const {
287  return reg->orphan(entt);
288  }
289 
296  template<typename Func>
297  void visit(Func &&func) const {
298  reg->visit(entt, std::forward<Func>(func));
299  }
300 
301 private:
302  registry_type *reg;
304 };
305 
306 
316 template<typename Type, typename Other>
317 bool operator!=(const basic_handle<Type> &lhs, const basic_handle<Other> &rhs) ENTT_NOEXCEPT {
318  return !(lhs == rhs);
319 }
320 
321 
326 template<typename Entity>
328 
329 
334 template<typename Entity>
336 
337 
338 }
339 
340 
341 #endif
entt::basic_handle::remove
void remove() const
Removes the given components from a handle.
Definition: handle.hpp:195
entt::basic_handle::entity_type
std::remove_const_t< Entity > entity_type
Underlying entity identifier.
Definition: handle.hpp:28
entt::basic_handle::patch
decltype(auto) patch(Func &&... func) const
Patches the given component for a handle.
Definition: handle.hpp:170
entt::type_list< Type... >
entt::basic_handle::replace
decltype(auto) replace(Args &&... args) const
Replaces the given component for a handle.
Definition: handle.hpp:184
entt::basic_handle::destroy
void destroy()
Destroys the entity associated with a handle.
Definition: handle.hpp:120
entt::basic_handle::has
decltype(auto) has() const
Checks if a handle has all the given components.
Definition: handle.hpp:228
entt::basic_handle::orphan
bool orphan() const
Checks if a handle has components assigned.
Definition: handle.hpp:286
entt::basic_handle
Non-owning handle to an entity.
Definition: handle.hpp:26
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:156
entt::basic_handle::entity
entity_type entity() const noexcept
Returns the entity associated with a handle.
Definition: handle.hpp:112
entt::basic_handle::registry
registry_type * registry() const noexcept
Returns a pointer to the underlying registry, if any.
Definition: handle.hpp:104
entt::basic_handle::get
decltype(auto) get() const
Returns references to the given components for a handle.
Definition: handle.hpp:251
entt::basic_handle::basic_handle
basic_handle(registry_type &ref, entity_type value) noexcept
Constructs a handle from a given registry and entity.
Definition: handle.hpp:42
entt::basic_handle::visit
void visit(Func &&func) const
Visits a handle and returns the types for its components.
Definition: handle.hpp:297
entt::basic_handle::operator==
bool operator==(const basic_handle< Args... > &other) const noexcept
Compares two handles.
Definition: handle.hpp:54
entt
EnTT default namespace.
Definition: algorithm.hpp:13
entt::type_list_contains_v
constexpr auto type_list_contains_v
Helper variable template.
Definition: type_traits.hpp:290
entt::basic_handle::emplace
decltype(auto) emplace(Args &&... args) const
Assigns the given component to a handle.
Definition: handle.hpp:142
entt::basic_handle::valid
bool valid() const
Checks if a handle refers to a valid entity or not.
Definition: handle.hpp:96
entt::basic_handle::basic_handle
basic_handle() noexcept
Constructs an invalid handle.
Definition: handle.hpp:33
entt::any
A SBO friendly, type-safe container for single values of any type.
Definition: any.hpp:18
entt::basic_handle
basic_handle(basic_registry< Entity > &, Entity) -> basic_handle< Entity >
Deduction guide.
entt::basic_handle::remove_all
void remove_all() const
Removes all the components from a handle and makes it orphaned.
Definition: handle.hpp:216
entt::basic_registry
Fast and reliable entity-component system.
Definition: registry.hpp:44
entt::basic_handle::destroy
void destroy(const typename registry_type::version_type version)
Destroys the entity associated with a handle.
Definition: handle.hpp:129
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:265
entt::basic_handle::try_get
auto try_get() const
Returns pointers to the given components for a handle.
Definition: handle.hpp:277
entt::constness_as_t
typename constness_as< To, From >::type constness_as_t
Alias template to facilitate the transcription of the constness.
Definition: type_traits.hpp:549
entt::operator!=
bool operator!=(const any &lhs, const any &rhs) noexcept
Checks if two wrappers differ in their content.
Definition: any.hpp:313
entt::basic_handle::remove_if_exists
decltype(auto) remove_if_exists() const
Removes the given components from a handle.
Definition: handle.hpp:207
entt::basic_handle::registry_type
constness_as_t< basic_registry< entity_type >, Entity > registry_type
Type of registry accepted by the handle.
Definition: handle.hpp:30