entity.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #ifndef ENTT_ENTITY_ENTITY_HPP
  2. #define ENTT_ENTITY_ENTITY_HPP
  3. #include <cstddef>
  4. #include <cstdint>
  5. #include <type_traits>
  6. #include "../config/config.h"
  7. #include "fwd.hpp"
  8. namespace entt {
  9. /**
  10. * @cond TURN_OFF_DOXYGEN
  11. * Internal details not to be documented.
  12. */
  13. namespace internal {
  14. // waiting for C++20 and std::popcount
  15. template<typename Type>
  16. constexpr int popcount(Type value) noexcept {
  17. return value ? (int(value & 1) + popcount(value >> 1)) : 0;
  18. }
  19. template<typename, typename = void>
  20. struct entt_traits;
  21. template<typename Type>
  22. struct entt_traits<Type, std::enable_if_t<std::is_enum_v<Type>>>
  23. : entt_traits<std::underlying_type_t<Type>> {
  24. using value_type = Type;
  25. };
  26. template<typename Type>
  27. struct entt_traits<Type, std::enable_if_t<std::is_class_v<Type>>>
  28. : entt_traits<typename Type::entity_type> {
  29. using value_type = Type;
  30. };
  31. template<>
  32. struct entt_traits<std::uint32_t> {
  33. using value_type = std::uint32_t;
  34. using entity_type = std::uint32_t;
  35. using version_type = std::uint16_t;
  36. static constexpr entity_type entity_mask = 0xFFFFF;
  37. static constexpr entity_type version_mask = 0xFFF;
  38. };
  39. template<>
  40. struct entt_traits<std::uint64_t> {
  41. using value_type = std::uint64_t;
  42. using entity_type = std::uint64_t;
  43. using version_type = std::uint32_t;
  44. static constexpr entity_type entity_mask = 0xFFFFFFFF;
  45. static constexpr entity_type version_mask = 0xFFFFFFFF;
  46. };
  47. } // namespace internal
  48. /**
  49. * Internal details not to be documented.
  50. * @endcond
  51. */
  52. /**
  53. * @brief Common basic entity traits implementation.
  54. * @tparam Traits Actual entity traits to use.
  55. */
  56. template<typename Traits>
  57. class basic_entt_traits {
  58. static constexpr auto length = internal::popcount(Traits::entity_mask);
  59. static_assert(Traits::entity_mask && ((typename Traits::entity_type{1} << length) == (Traits::entity_mask + 1)), "Invalid entity mask");
  60. static_assert((typename Traits::entity_type{1} << internal::popcount(Traits::version_mask)) == (Traits::version_mask + 1), "Invalid version mask");
  61. public:
  62. /*! @brief Value type. */
  63. using value_type = typename Traits::value_type;
  64. /*! @brief Underlying entity type. */
  65. using entity_type = typename Traits::entity_type;
  66. /*! @brief Underlying version type. */
  67. using version_type = typename Traits::version_type;
  68. /*! @brief Entity mask size. */
  69. static constexpr entity_type entity_mask = Traits::entity_mask;
  70. /*! @brief Version mask size */
  71. static constexpr entity_type version_mask = Traits::version_mask;
  72. /**
  73. * @brief Converts an entity to its underlying type.
  74. * @param value The value to convert.
  75. * @return The integral representation of the given value.
  76. */
  77. [[nodiscard]] static constexpr entity_type to_integral(const value_type value) noexcept {
  78. return static_cast<entity_type>(value);
  79. }
  80. /**
  81. * @brief Returns the entity part once converted to the underlying type.
  82. * @param value The value to convert.
  83. * @return The integral representation of the entity part.
  84. */
  85. [[nodiscard]] static constexpr entity_type to_entity(const value_type value) noexcept {
  86. return (to_integral(value) & entity_mask);
  87. }
  88. /**
  89. * @brief Returns the version part once converted to the underlying type.
  90. * @param value The value to convert.
  91. * @return The integral representation of the version part.
  92. */
  93. [[nodiscard]] static constexpr version_type to_version(const value_type value) noexcept {
  94. return (static_cast<version_type>(to_integral(value) >> length) & version_mask);
  95. }
  96. /**
  97. * @brief Returns the successor of a given identifier.
  98. * @param value The identifier of which to return the successor.
  99. * @return The successor of the given identifier.
  100. */
  101. [[nodiscard]] static constexpr value_type next(const value_type value) noexcept {
  102. const auto vers = to_version(value) + 1;
  103. return construct(to_integral(value), static_cast<version_type>(vers + (vers == version_mask)));
  104. }
  105. /**
  106. * @brief Constructs an identifier from its parts.
  107. *
  108. * If the version part is not provided, a tombstone is returned.<br/>
  109. * If the entity part is not provided, a null identifier is returned.
  110. *
  111. * @param entity The entity part of the identifier.
  112. * @param version The version part of the identifier.
  113. * @return A properly constructed identifier.
  114. */
  115. [[nodiscard]] static constexpr value_type construct(const entity_type entity, const version_type version) noexcept {
  116. return value_type{(entity & entity_mask) | (static_cast<entity_type>(version & version_mask) << length)};
  117. }
  118. /**
  119. * @brief Combines two identifiers in a single one.
  120. *
  121. * The returned identifier is a copy of the first element except for its
  122. * version, which is taken from the second element.
  123. *
  124. * @param lhs The identifier from which to take the entity part.
  125. * @param rhs The identifier from which to take the version part.
  126. * @return A properly constructed identifier.
  127. */
  128. [[nodiscard]] static constexpr value_type combine(const entity_type lhs, const entity_type rhs) noexcept {
  129. return value_type{(lhs & entity_mask) | (rhs & (version_mask << length))};
  130. }
  131. };
  132. /**
  133. * @brief Entity traits.
  134. * @tparam Type Type of identifier.
  135. */
  136. template<typename Type>
  137. struct entt_traits: basic_entt_traits<internal::entt_traits<Type>> {
  138. /*! @brief Base type. */
  139. using base_type = basic_entt_traits<internal::entt_traits<Type>>;
  140. /*! @brief Page size, default is `ENTT_SPARSE_PAGE`. */
  141. static constexpr std::size_t page_size = ENTT_SPARSE_PAGE;
  142. };
  143. /**
  144. * @brief Converts an entity to its underlying type.
  145. * @tparam Entity The value type.
  146. * @param value The value to convert.
  147. * @return The integral representation of the given value.
  148. */
  149. template<typename Entity>
  150. [[nodiscard]] constexpr typename entt_traits<Entity>::entity_type to_integral(const Entity value) noexcept {
  151. return entt_traits<Entity>::to_integral(value);
  152. }
  153. /**
  154. * @brief Returns the entity part once converted to the underlying type.
  155. * @tparam Entity The value type.
  156. * @param value The value to convert.
  157. * @return The integral representation of the entity part.
  158. */
  159. template<typename Entity>
  160. [[nodiscard]] constexpr typename entt_traits<Entity>::entity_type to_entity(const Entity value) noexcept {
  161. return entt_traits<Entity>::to_entity(value);
  162. }
  163. /**
  164. * @brief Returns the version part once converted to the underlying type.
  165. * @tparam Entity The value type.
  166. * @param value The value to convert.
  167. * @return The integral representation of the version part.
  168. */
  169. template<typename Entity>
  170. [[nodiscard]] constexpr typename entt_traits<Entity>::version_type to_version(const Entity value) noexcept {
  171. return entt_traits<Entity>::to_version(value);
  172. }
  173. /*! @brief Null object for all identifiers. */
  174. struct null_t {
  175. /**
  176. * @brief Converts the null object to identifiers of any type.
  177. * @tparam Entity Type of identifier.
  178. * @return The null representation for the given type.
  179. */
  180. template<typename Entity>
  181. [[nodiscard]] constexpr operator Entity() const noexcept {
  182. using traits_type = entt_traits<Entity>;
  183. constexpr auto value = traits_type::construct(traits_type::entity_mask, traits_type::version_mask);
  184. return value;
  185. }
  186. /**
  187. * @brief Compares two null objects.
  188. * @param other A null object.
  189. * @return True in all cases.
  190. */
  191. [[nodiscard]] constexpr bool operator==([[maybe_unused]] const null_t other) const noexcept {
  192. return true;
  193. }
  194. /**
  195. * @brief Compares two null objects.
  196. * @param other A null object.
  197. * @return False in all cases.
  198. */
  199. [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const null_t other) const noexcept {
  200. return false;
  201. }
  202. /**
  203. * @brief Compares a null object and an identifier of any type.
  204. * @tparam Entity Type of identifier.
  205. * @param entity Identifier with which to compare.
  206. * @return False if the two elements differ, true otherwise.
  207. */
  208. template<typename Entity>
  209. [[nodiscard]] constexpr bool operator==(const Entity entity) const noexcept {
  210. using traits_type = entt_traits<Entity>;
  211. return traits_type::to_entity(entity) == traits_type::to_entity(*this);
  212. }
  213. /**
  214. * @brief Compares a null object and an identifier of any type.
  215. * @tparam Entity Type of identifier.
  216. * @param entity Identifier with which to compare.
  217. * @return True if the two elements differ, false otherwise.
  218. */
  219. template<typename Entity>
  220. [[nodiscard]] constexpr bool operator!=(const Entity entity) const noexcept {
  221. return !(entity == *this);
  222. }
  223. };
  224. /**
  225. * @brief Compares a null object and an identifier of any type.
  226. * @tparam Entity Type of identifier.
  227. * @param entity Identifier with which to compare.
  228. * @param other A null object yet to be converted.
  229. * @return False if the two elements differ, true otherwise.
  230. */
  231. template<typename Entity>
  232. [[nodiscard]] constexpr bool operator==(const Entity entity, const null_t other) noexcept {
  233. return other.operator==(entity);
  234. }
  235. /**
  236. * @brief Compares a null object and an identifier of any type.
  237. * @tparam Entity Type of identifier.
  238. * @param entity Identifier with which to compare.
  239. * @param other A null object yet to be converted.
  240. * @return True if the two elements differ, false otherwise.
  241. */
  242. template<typename Entity>
  243. [[nodiscard]] constexpr bool operator!=(const Entity entity, const null_t other) noexcept {
  244. return !(other == entity);
  245. }
  246. /*! @brief Tombstone object for all identifiers. */
  247. struct tombstone_t {
  248. /**
  249. * @brief Converts the tombstone object to identifiers of any type.
  250. * @tparam Entity Type of identifier.
  251. * @return The tombstone representation for the given type.
  252. */
  253. template<typename Entity>
  254. [[nodiscard]] constexpr operator Entity() const noexcept {
  255. using traits_type = entt_traits<Entity>;
  256. constexpr auto value = traits_type::construct(traits_type::entity_mask, traits_type::version_mask);
  257. return value;
  258. }
  259. /**
  260. * @brief Compares two tombstone objects.
  261. * @param other A tombstone object.
  262. * @return True in all cases.
  263. */
  264. [[nodiscard]] constexpr bool operator==([[maybe_unused]] const tombstone_t other) const noexcept {
  265. return true;
  266. }
  267. /**
  268. * @brief Compares two tombstone objects.
  269. * @param other A tombstone object.
  270. * @return False in all cases.
  271. */
  272. [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const tombstone_t other) const noexcept {
  273. return false;
  274. }
  275. /**
  276. * @brief Compares a tombstone object and an identifier of any type.
  277. * @tparam Entity Type of identifier.
  278. * @param entity Identifier with which to compare.
  279. * @return False if the two elements differ, true otherwise.
  280. */
  281. template<typename Entity>
  282. [[nodiscard]] constexpr bool operator==(const Entity entity) const noexcept {
  283. using traits_type = entt_traits<Entity>;
  284. return traits_type::to_version(entity) == traits_type::to_version(*this);
  285. }
  286. /**
  287. * @brief Compares a tombstone object and an identifier of any type.
  288. * @tparam Entity Type of identifier.
  289. * @param entity Identifier with which to compare.
  290. * @return True if the two elements differ, false otherwise.
  291. */
  292. template<typename Entity>
  293. [[nodiscard]] constexpr bool operator!=(const Entity entity) const noexcept {
  294. return !(entity == *this);
  295. }
  296. };
  297. /**
  298. * @brief Compares a tombstone object and an identifier of any type.
  299. * @tparam Entity Type of identifier.
  300. * @param entity Identifier with which to compare.
  301. * @param other A tombstone object yet to be converted.
  302. * @return False if the two elements differ, true otherwise.
  303. */
  304. template<typename Entity>
  305. [[nodiscard]] constexpr bool operator==(const Entity entity, const tombstone_t other) noexcept {
  306. return other.operator==(entity);
  307. }
  308. /**
  309. * @brief Compares a tombstone object and an identifier of any type.
  310. * @tparam Entity Type of identifier.
  311. * @param entity Identifier with which to compare.
  312. * @param other A tombstone object yet to be converted.
  313. * @return True if the two elements differ, false otherwise.
  314. */
  315. template<typename Entity>
  316. [[nodiscard]] constexpr bool operator!=(const Entity entity, const tombstone_t other) noexcept {
  317. return !(other == entity);
  318. }
  319. /**
  320. * @brief Compile-time constant for null entities.
  321. *
  322. * There exist implicit conversions from this variable to identifiers of any
  323. * allowed type. Similarly, there exist comparison operators between the null
  324. * entity and any other identifier.
  325. */
  326. inline constexpr null_t null{};
  327. /**
  328. * @brief Compile-time constant for tombstone entities.
  329. *
  330. * There exist implicit conversions from this variable to identifiers of any
  331. * allowed type. Similarly, there exist comparison operators between the
  332. * tombstone entity and any other identifier.
  333. */
  334. inline constexpr tombstone_t tombstone{};
  335. } // namespace entt
  336. #endif