🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

340 lines
12 KiB

  1. #ifndef ENTT_ENTITY_HANDLE_HPP
  2. #define ENTT_ENTITY_HANDLE_HPP
  3. #include <tuple>
  4. #include <type_traits>
  5. #include <utility>
  6. #include "../config/config.h"
  7. #include "../core/type_traits.hpp"
  8. #include "fwd.hpp"
  9. #include "registry.hpp"
  10. namespace entt {
  11. /**
  12. * @brief Non-owning handle to an entity.
  13. *
  14. * Tiny wrapper around a registry and an entity.
  15. *
  16. * @tparam Entity A valid entity type (see entt_traits for more details).
  17. * @tparam Type Types to which to restrict the scope of a handle.
  18. */
  19. template<typename Entity, typename... Type>
  20. struct basic_handle {
  21. /*! @brief Type of registry accepted by the handle. */
  22. using registry_type = constness_as_t<basic_registry<std::remove_const_t<Entity>>, Entity>;
  23. /*! @brief Underlying entity identifier. */
  24. using entity_type = typename registry_type::entity_type;
  25. /*! @brief Underlying version type. */
  26. using version_type = typename registry_type::version_type;
  27. /*! @brief Unsigned integer type. */
  28. using size_type = typename registry_type::size_type;
  29. /*! @brief Constructs an invalid handle. */
  30. basic_handle() ENTT_NOEXCEPT
  31. : reg{},
  32. entt{null} {}
  33. /**
  34. * @brief Constructs a handle from a given registry and entity.
  35. * @param ref An instance of the registry class.
  36. * @param value A valid identifier.
  37. */
  38. basic_handle(registry_type &ref, entity_type value) ENTT_NOEXCEPT
  39. : reg{&ref},
  40. entt{value} {}
  41. /**
  42. * @brief Constructs a const handle from a non-const one.
  43. * @tparam Other A valid entity type (see entt_traits for more details).
  44. * @tparam Args Scope of the handle to construct.
  45. * @return A const handle referring to the same registry and the same
  46. * entity.
  47. */
  48. template<typename Other, typename... Args>
  49. operator basic_handle<Other, Args...>() const ENTT_NOEXCEPT {
  50. static_assert(std::is_same_v<Other, Entity> || std::is_same_v<std::remove_const_t<Other>, Entity>, "Invalid conversion between different handles");
  51. static_assert((sizeof...(Type) == 0 || ((sizeof...(Args) != 0 && sizeof...(Args) <= sizeof...(Type)) && ... && (type_list_contains_v<type_list<Type...>, Args>))), "Invalid conversion between different handles");
  52. return reg ? basic_handle<Other, Args...>{*reg, entt} : basic_handle<Other, Args...>{};
  53. }
  54. /**
  55. * @brief Converts a handle to its underlying entity.
  56. * @return The contained identifier.
  57. */
  58. [[nodiscard]] operator entity_type() const ENTT_NOEXCEPT {
  59. return entity();
  60. }
  61. /**
  62. * @brief Checks if a handle refers to non-null registry pointer and entity.
  63. * @return True if the handle refers to non-null registry and entity, false otherwise.
  64. */
  65. [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
  66. return reg && reg->valid(entt);
  67. }
  68. /**
  69. * @brief Checks if a handle refers to a valid entity or not.
  70. * @return True if the handle refers to a valid entity, false otherwise.
  71. */
  72. [[nodiscard]] bool valid() const {
  73. return reg->valid(entt);
  74. }
  75. /**
  76. * @brief Returns a pointer to the underlying registry, if any.
  77. * @return A pointer to the underlying registry, if any.
  78. */
  79. [[nodiscard]] registry_type *registry() const ENTT_NOEXCEPT {
  80. return reg;
  81. }
  82. /**
  83. * @brief Returns the entity associated with a handle.
  84. * @return The entity associated with the handle.
  85. */
  86. [[nodiscard]] entity_type entity() const ENTT_NOEXCEPT {
  87. return entt;
  88. }
  89. /**
  90. * @brief Destroys the entity associated with a handle.
  91. * @sa basic_registry::destroy
  92. */
  93. void destroy() {
  94. reg->destroy(entt);
  95. }
  96. /**
  97. * @brief Destroys the entity associated with a handle.
  98. * @sa basic_registry::destroy
  99. * @param version A desired version upon destruction.
  100. */
  101. void destroy(const version_type version) {
  102. reg->destroy(entt, version);
  103. }
  104. /**
  105. * @brief Assigns the given component to a handle.
  106. * @sa basic_registry::emplace
  107. * @tparam Component Type of component to create.
  108. * @tparam Args Types of arguments to use to construct the component.
  109. * @param args Parameters to use to initialize the component.
  110. * @return A reference to the newly created component.
  111. */
  112. template<typename Component, typename... Args>
  113. decltype(auto) emplace(Args &&...args) const {
  114. static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
  115. return reg->template emplace<Component>(entt, std::forward<Args>(args)...);
  116. }
  117. /**
  118. * @brief Assigns or replaces the given component for a handle.
  119. * @sa basic_registry::emplace_or_replace
  120. * @tparam Component Type of component to assign or replace.
  121. * @tparam Args Types of arguments to use to construct the component.
  122. * @param args Parameters to use to initialize the component.
  123. * @return A reference to the newly created component.
  124. */
  125. template<typename Component, typename... Args>
  126. decltype(auto) emplace_or_replace(Args &&...args) const {
  127. static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
  128. return reg->template emplace_or_replace<Component>(entt, std::forward<Args>(args)...);
  129. }
  130. /**
  131. * @brief Patches the given component for a handle.
  132. * @sa basic_registry::patch
  133. * @tparam Component Type of component to patch.
  134. * @tparam Func Types of the function objects to invoke.
  135. * @param func Valid function objects.
  136. * @return A reference to the patched component.
  137. */
  138. template<typename Component, typename... Func>
  139. decltype(auto) patch(Func &&...func) const {
  140. static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
  141. return reg->template patch<Component>(entt, std::forward<Func>(func)...);
  142. }
  143. /**
  144. * @brief Replaces the given component for a handle.
  145. * @sa basic_registry::replace
  146. * @tparam Component Type of component to replace.
  147. * @tparam Args Types of arguments to use to construct the component.
  148. * @param args Parameters to use to initialize the component.
  149. * @return A reference to the component being replaced.
  150. */
  151. template<typename Component, typename... Args>
  152. decltype(auto) replace(Args &&...args) const {
  153. static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
  154. return reg->template replace<Component>(entt, std::forward<Args>(args)...);
  155. }
  156. /**
  157. * @brief Removes the given components from a handle.
  158. * @sa basic_registry::remove
  159. * @tparam Component Types of components to remove.
  160. * @return The number of components actually removed.
  161. */
  162. template<typename... Component>
  163. size_type remove() const {
  164. static_assert(sizeof...(Type) == 0 || (type_list_contains_v<type_list<Type...>, Component> && ...), "Invalid type");
  165. return reg->template remove<Component...>(entt);
  166. }
  167. /**
  168. * @brief Erases the given components from a handle.
  169. * @sa basic_registry::erase
  170. * @tparam Component Types of components to erase.
  171. */
  172. template<typename... Component>
  173. void erase() const {
  174. static_assert(sizeof...(Type) == 0 || (type_list_contains_v<type_list<Type...>, Component> && ...), "Invalid type");
  175. reg->template erase<Component...>(entt);
  176. }
  177. /**
  178. * @brief Checks if a handle has all the given components.
  179. * @sa basic_registry::all_of
  180. * @tparam Component Components for which to perform the check.
  181. * @return True if the handle has all the components, false otherwise.
  182. */
  183. template<typename... Component>
  184. [[nodiscard]] decltype(auto) all_of() const {
  185. return reg->template all_of<Component...>(entt);
  186. }
  187. /**
  188. * @brief Checks if a handle has at least one of the given components.
  189. * @sa basic_registry::any_of
  190. * @tparam Component Components for which to perform the check.
  191. * @return True if the handle has at least one of the given components,
  192. * false otherwise.
  193. */
  194. template<typename... Component>
  195. [[nodiscard]] decltype(auto) any_of() const {
  196. return reg->template any_of<Component...>(entt);
  197. }
  198. /**
  199. * @brief Returns references to the given components for a handle.
  200. * @sa basic_registry::get
  201. * @tparam Component Types of components to get.
  202. * @return References to the components owned by the handle.
  203. */
  204. template<typename... Component>
  205. [[nodiscard]] decltype(auto) get() const {
  206. static_assert(sizeof...(Type) == 0 || (type_list_contains_v<type_list<Type...>, Component> && ...), "Invalid type");
  207. return reg->template get<Component...>(entt);
  208. }
  209. /**
  210. * @brief Returns a reference to the given component for a handle.
  211. * @sa basic_registry::get_or_emplace
  212. * @tparam Component Type of component to get.
  213. * @tparam Args Types of arguments to use to construct the component.
  214. * @param args Parameters to use to initialize the component.
  215. * @return Reference to the component owned by the handle.
  216. */
  217. template<typename Component, typename... Args>
  218. [[nodiscard]] decltype(auto) get_or_emplace(Args &&...args) const {
  219. static_assert(((sizeof...(Type) == 0) || ... || std::is_same_v<Component, Type>), "Invalid type");
  220. return reg->template get_or_emplace<Component>(entt, std::forward<Args>(args)...);
  221. }
  222. /**
  223. * @brief Returns pointers to the given components for a handle.
  224. * @sa basic_registry::try_get
  225. * @tparam Component Types of components to get.
  226. * @return Pointers to the components owned by the handle.
  227. */
  228. template<typename... Component>
  229. [[nodiscard]] auto try_get() const {
  230. static_assert(sizeof...(Type) == 0 || (type_list_contains_v<type_list<Type...>, Component> && ...), "Invalid type");
  231. return reg->template try_get<Component...>(entt);
  232. }
  233. /**
  234. * @brief Checks if a handle has components assigned.
  235. * @return True if the handle has no components assigned, false otherwise.
  236. */
  237. [[nodiscard]] bool orphan() const {
  238. return reg->orphan(entt);
  239. }
  240. /**
  241. * @brief Visits a handle and returns the pools for its components.
  242. *
  243. * The signature of the function should be equivalent to the following:
  244. *
  245. * @code{.cpp}
  246. * void(id_type, const basic_sparse_set<entity_type> &);
  247. * @endcode
  248. *
  249. * Returned pools are those that contain the entity associated with the
  250. * handle.
  251. *
  252. * @tparam Func Type of the function object to invoke.
  253. * @param func A valid function object.
  254. */
  255. template<typename Func>
  256. void visit(Func &&func) const {
  257. for(auto [id, storage]: reg->storage()) {
  258. if(storage.contains(entt)) {
  259. func(id, storage);
  260. }
  261. }
  262. }
  263. private:
  264. registry_type *reg;
  265. entity_type entt;
  266. };
  267. /**
  268. * @brief Compares two handles.
  269. * @tparam Args Scope of the first handle.
  270. * @tparam Other Scope of the second handle.
  271. * @param lhs A valid handle.
  272. * @param rhs A valid handle.
  273. * @return True if both handles refer to the same registry and the same
  274. * entity, false otherwise.
  275. */
  276. template<typename... Args, typename... Other>
  277. [[nodiscard]] bool operator==(const basic_handle<Args...> &lhs, const basic_handle<Other...> &rhs) ENTT_NOEXCEPT {
  278. return lhs.registry() == rhs.registry() && lhs.entity() == rhs.entity();
  279. }
  280. /**
  281. * @brief Compares two handles.
  282. * @tparam Args Scope of the first handle.
  283. * @tparam Other Scope of the second handle.
  284. * @param lhs A valid handle.
  285. * @param rhs A valid handle.
  286. * @return False if both handles refer to the same registry and the same
  287. * entity, true otherwise.
  288. */
  289. template<typename... Args, typename... Other>
  290. [[nodiscard]] bool operator!=(const basic_handle<Args...> &lhs, const basic_handle<Other...> &rhs) ENTT_NOEXCEPT {
  291. return !(lhs == rhs);
  292. }
  293. /**
  294. * @brief Deduction guide.
  295. * @tparam Entity A valid entity type (see entt_traits for more details).
  296. */
  297. template<typename Entity>
  298. basic_handle(basic_registry<Entity> &, Entity) -> basic_handle<Entity>;
  299. /**
  300. * @brief Deduction guide.
  301. * @tparam Entity A valid entity type (see entt_traits for more details).
  302. */
  303. template<typename Entity>
  304. basic_handle(const basic_registry<Entity> &, Entity) -> basic_handle<const Entity>;
  305. } // namespace entt
  306. #endif