EnTT: src/entt/entity/entity.hpp Source File

EnTT 3.16.0

Loading...
Searching...
No Matches
entity.hpp
1#ifndef ENTT_ENTITY_ENTITY_HPP
2#define ENTT_ENTITY_ENTITY_HPP
3
4#include <cstddef>
5#include <cstdint>
6#include <type_traits>
7#include "../config/config.h"
8#include "../core/bit.hpp"
9#include "fwd.hpp"
10
11namespace entt {
12
14namespace internal {
15
16template<typename, typename = void>
17struct entt_traits;
18
19template<typename Type>
20struct entt_traits<Type, std::enable_if_t<std::is_enum_v<Type>>>
21 : entt_traits<std::underlying_type_t<Type>> {
22 using value_type = Type;
23};
24
25template<typename Type>
26struct entt_traits<Type, std::enable_if_t<std::is_class_v<Type>>>
27 : entt_traits<typename Type::entity_type> {
28 using value_type = Type;
29};
30
31template<>
32struct entt_traits<std::uint32_t> {
33 using value_type = std::uint32_t;
34
35 using entity_type = std::uint32_t;
36 using version_type = std::uint16_t;
37
38 static constexpr entity_type entity_mask = 0xFFFFF;
39 static constexpr entity_type version_mask = 0xFFF;
40};
41
42template<>
43struct entt_traits<std::uint64_t> {
44 using value_type = std::uint64_t;
45
46 using entity_type = std::uint64_t;
47 using version_type = std::uint32_t;
48
49 static constexpr entity_type entity_mask = 0xFFFFFFFF;
50 static constexpr entity_type version_mask = 0xFFFFFFFF;
51};
52
53} // namespace internal
55
60template<typename Traits>
61 class basic_entt_traits {
62 static constexpr auto length = popcount(Traits::entity_mask);
63
64 static_assert(Traits::entity_mask && ((Traits::entity_mask & (Traits::entity_mask + 1)) == 0), "Invalid entity mask");
65 static_assert((Traits::version_mask & (Traits::version_mask + 1)) == 0, "Invalid version mask");
66
67public:
69 using value_type = typename Traits::value_type;
71 using entity_type = typename Traits::entity_type;
73 using version_type = typename Traits::version_type;
74
76 static constexpr entity_type entity_mask = Traits::entity_mask;
78 static constexpr entity_type version_mask = Traits::version_mask;
79
85 [[nodiscard]] static constexpr entity_type to_integral(const value_type value) noexcept {
86 return static_cast<entity_type>(value);
87 }
88
94 [[nodiscard]] static constexpr entity_type to_entity(const value_type value) noexcept {
95 return (to_integral(value) & entity_mask);
96 }
97
103 [[nodiscard]] static constexpr version_type to_version(const value_type value) noexcept {
104 if constexpr(Traits::version_mask == 0u) {
105 return version_type{};
106 } else {
107 return (static_cast<version_type>(to_integral(value) >> length) & version_mask);
108 }
109 }
110
116 [[nodiscard]] static constexpr value_type next(const value_type value) noexcept {
117 const auto vers = to_version(value) + 1;
118 return construct(to_integral(value), static_cast<version_type>(vers + (vers == version_mask)));
119 }
120
131 [[nodiscard]] static constexpr value_type construct(const entity_type entity, const version_type version) noexcept {
132 if constexpr(Traits::version_mask == 0u) {
133 return value_type{entity & entity_mask};
134 } else {
135 return value_type{(entity & entity_mask) | (static_cast<entity_type>(version & version_mask) << length)};
136 }
137 }
138
149 [[nodiscard]] static constexpr value_type combine(const entity_type lhs, const entity_type rhs) noexcept {
150 if constexpr(Traits::version_mask == 0u) {
151 return value_type{lhs & entity_mask};
152 } else {
153 return value_type{(lhs & entity_mask) | (rhs & (version_mask << length))};
154 }
155 }
156};
157
162template<typename Type>
163 struct entt_traits: basic_entt_traits<internal::entt_traits<Type>> {
165 using base_type = basic_entt_traits<internal::entt_traits<Type>>;
167 static constexpr std::size_t page_size = ENTT_SPARSE_PAGE;
168};
169
176template<typename Entity>
177[[nodiscard]] constexpr typename entt_traits<Entity>::entity_type to_integral(const Entity value) noexcept {
178 return entt_traits<Entity>::to_integral(value);
179}
180
187template<typename Entity>
188[[nodiscard]] constexpr typename entt_traits<Entity>::entity_type to_entity(const Entity value) noexcept {
189 return entt_traits<Entity>::to_entity(value);
190}
191
198template<typename Entity>
199[[nodiscard]] constexpr typename entt_traits<Entity>::version_type to_version(const Entity value) noexcept {
200 return entt_traits<Entity>::to_version(value);
201}
202
204 struct null_t {
210 template<typename Entity>
211 [[nodiscard]] constexpr operator Entity() const noexcept {
212 using traits_type = entt_traits<Entity>;
213 constexpr auto value = traits_type::construct(traits_type::entity_mask, traits_type::version_mask);
214 return value;
215 }
216
222 [[nodiscard]] constexpr bool operator==([[maybe_unused]] const null_t other) const noexcept {
223 return true;
224 }
225
231 [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const null_t other) const noexcept {
232 return false;
233 }
234
241 template<typename Entity>
242 [[nodiscard]] constexpr bool operator==(const Entity entity) const noexcept {
243 using traits_type = entt_traits<Entity>;
244 return traits_type::to_entity(entity) == traits_type::to_entity(*this);
245 }
246
253 template<typename Entity>
254 [[nodiscard]] constexpr bool operator!=(const Entity entity) const noexcept {
255 return !(entity == *this);
256 }
257};
258
266template<typename Entity>
267[[nodiscard]] constexpr bool operator==(const Entity lhs, const null_t rhs) noexcept {
268 return rhs.operator==(lhs);
269}
270
278template<typename Entity>
279[[nodiscard]] constexpr bool operator!=(const Entity lhs, const null_t rhs) noexcept {
280 return !(rhs == lhs);
281}
282
284 struct tombstone_t {
290 template<typename Entity>
291 [[nodiscard]] constexpr operator Entity() const noexcept {
292 using traits_type = entt_traits<Entity>;
293 constexpr auto value = traits_type::construct(traits_type::entity_mask, traits_type::version_mask);
294 return value;
295 }
296
302 [[nodiscard]] constexpr bool operator==([[maybe_unused]] const tombstone_t other) const noexcept {
303 return true;
304 }
305
311 [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const tombstone_t other) const noexcept {
312 return false;
313 }
314
321 template<typename Entity>
322 [[nodiscard]] constexpr bool operator==(const Entity entity) const noexcept {
323 using traits_type = entt_traits<Entity>;
324
325 if constexpr(traits_type::version_mask == 0u) {
326 return false;
327 } else {
328 return (traits_type::to_version(entity) == traits_type::to_version(*this));
329 }
330 }
331
338 template<typename Entity>
339 [[nodiscard]] constexpr bool operator!=(const Entity entity) const noexcept {
340 return !(entity == *this);
341 }
342};
343
351template<typename Entity>
352[[nodiscard]] constexpr bool operator==(const Entity lhs, const tombstone_t rhs) noexcept {
353 return rhs.operator==(lhs);
354}
355
363template<typename Entity>
364[[nodiscard]] constexpr bool operator!=(const Entity lhs, const tombstone_t rhs) noexcept {
365 return !(rhs == lhs);
366}
367
375 inline constexpr null_t null{};
376
384 inline constexpr tombstone_t tombstone{};
385
386} // namespace entt
387
388#endif
entt::basic_entt_traits
Common basic entity traits implementation.
Definition entity.hpp:61
entt::basic_entt_traits::entity_type
typename Traits::entity_type entity_type
Underlying entity type.
Definition entity.hpp:71
entt::basic_entt_traits< internal::entt_traits< Type > >::version_mask
static constexpr entity_type version_mask
Definition entity.hpp:78
entt::basic_entt_traits::construct
static constexpr value_type construct(const entity_type entity, const version_type version) noexcept
Constructs an identifier from its parts.
Definition entity.hpp:131
entt::basic_entt_traits::next
static constexpr value_type next(const value_type value) noexcept
Returns the successor of a given identifier.
Definition entity.hpp:116
entt::basic_entt_traits::to_integral
static constexpr entity_type to_integral(const value_type value) noexcept
Converts an entity to its underlying type.
Definition entity.hpp:85
entt::basic_entt_traits< internal::entt_traits< Type > >::entity_mask
static constexpr entity_type entity_mask
Definition entity.hpp:76
entt::basic_entt_traits::to_entity
static constexpr entity_type to_entity(const value_type value) noexcept
Returns the entity part once converted to the underlying type.
Definition entity.hpp:94
entt::basic_entt_traits::value_type
typename Traits::value_type value_type
Value type.
Definition entity.hpp:69
entt::basic_entt_traits::version_type
typename Traits::version_type version_type
Underlying version type.
Definition entity.hpp:73
entt::basic_entt_traits::combine
static constexpr value_type combine(const entity_type lhs, const entity_type rhs) noexcept
Combines two identifiers in a single one.
Definition entity.hpp:149
entt::basic_entt_traits::to_version
static constexpr version_type to_version(const value_type value) noexcept
Returns the version part once converted to the underlying type.
Definition entity.hpp:103
entt
EnTT default namespace.
Definition dense_map.hpp:22
entt::entity
entity
Default entity identifier.
Definition fwd.hpp:14
entt::to_entity
constexpr entt_traits< Entity >::entity_type to_entity(const Entity value) noexcept
Returns the entity part once converted to the underlying type.
Definition entity.hpp:188
entt::null
constexpr null_t null
Compile-time constant for null entities.
Definition entity.hpp:375
entt::to_version
constexpr entt_traits< Entity >::version_type to_version(const Entity value) noexcept
Returns the version part once converted to the underlying type.
Definition entity.hpp:199
entt::tombstone
constexpr tombstone_t tombstone
Compile-time constant for tombstone entities.
Definition entity.hpp:384
entt::to_integral
constexpr entt_traits< Entity >::entity_type to_integral(const Entity value) noexcept
Converts an entity to its underlying type.
Definition entity.hpp:177
entt::operator!=
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:238
entt::operator==
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:226
entt::popcount
constexpr std::enable_if_t< std::is_unsigned_v< Type >, int > popcount(const Type value) noexcept
Returns the number of set bits in a value (waiting for C++20 and std::popcount).
Definition bit.hpp:19
entt::entt_traits
Entity traits.
Definition entity.hpp:163
entt::entt_traits< typename Registry::entity_type >::page_size
static constexpr std::size_t page_size
Definition entity.hpp:167
entt::entt_traits::base_type
basic_entt_traits< internal::entt_traits< Type > > base_type
Base type.
Definition entity.hpp:165
entt::null_t
Null object for all identifiers.
Definition entity.hpp:204
entt::null_t::operator==
constexpr bool operator==(const Entity entity) const noexcept
Compares a null object and an identifier of any type.
Definition entity.hpp:242
entt::null_t::operator!=
constexpr bool operator!=(const Entity entity) const noexcept
Compares a null object and an identifier of any type.
Definition entity.hpp:254
entt::null_t::operator!=
constexpr bool operator!=(const null_t other) const noexcept
Compares two null objects.
Definition entity.hpp:231
entt::null_t::operator==
constexpr bool operator==(const null_t other) const noexcept
Compares two null objects.
Definition entity.hpp:222
entt::tombstone_t
Tombstone object for all identifiers.
Definition entity.hpp:284
entt::tombstone_t::operator==
constexpr bool operator==(const Entity entity) const noexcept
Compares a tombstone object and an identifier of any type.
Definition entity.hpp:322
entt::tombstone_t::operator!=
constexpr bool operator!=(const tombstone_t other) const noexcept
Compares two tombstone objects.
Definition entity.hpp:311
entt::tombstone_t::operator!=
constexpr bool operator!=(const Entity entity) const noexcept
Compares a tombstone object and an identifier of any type.
Definition entity.hpp:339
entt::tombstone_t::operator==
constexpr bool operator==(const tombstone_t other) const noexcept
Compares two tombstone objects.
Definition entity.hpp:302