handle.hpp 14 KB

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