EnTT 4.0.0
Loading...
Searching...
No Matches
entity.hpp
1#ifndef ENTT_ENTITY_ENTITY_HPP
2#define ENTT_ENTITY_ENTITY_HPP
3
4#include "../config/config.h"
5#include "../core/bit.hpp"
6#include "../stl/bit.hpp"
7#include "../stl/concepts.hpp"
8#include "../stl/cstddef.hpp"
9#include "../stl/cstdint.hpp"
10#include "../stl/type_traits.hpp"
11#include "fwd.hpp"
12
13namespace entt {
14
16namespace internal {
17
18template<typename>
19struct entt_traits;
20
21template<typename Type>
22requires requires {
23 requires stl::is_enum_v<Type>;
24 typename internal::entt_traits<stl::underlying_type_t<Type>>::value_type;
25}
26struct entt_traits<Type>: entt_traits<stl::underlying_type_t<Type>> {
27 using value_type = Type;
28};
29
30template<typename Type>
31requires requires { typename Type::entity_type; }
32struct entt_traits<Type>
33 : entt_traits<typename Type::entity_type> {
34 using value_type = Type;
35};
36
37template<>
38struct entt_traits<stl::uint32_t> {
39 using value_type = stl::uint32_t;
40
41 using entity_type = stl::uint32_t;
42 using version_type = stl::uint16_t;
43
44 static constexpr entity_type entity_mask = 0xFFFFF;
45 static constexpr entity_type version_mask = 0xFFF;
46};
47
48template<>
49struct entt_traits<stl::uint64_t> {
50 using value_type = stl::uint64_t;
51
52 using entity_type = stl::uint64_t;
53 using version_type = stl::uint32_t;
54
55 static constexpr entity_type entity_mask = 0xFFFFFFFF;
56 static constexpr entity_type version_mask = 0xFFFFFFFF;
57};
58
59} // namespace internal
61
66template<typename Type>
67concept entity_like = requires {
68 typename internal::entt_traits<Type>::value_type;
69};
70
75template<typename Traits>
77 static constexpr auto length = stl::popcount(Traits::entity_mask);
78
79public:
81 using value_type = Traits::value_type;
83 using entity_type = Traits::entity_type;
85 using version_type = Traits::version_type;
86
88 static constexpr entity_type entity_mask = Traits::entity_mask;
90 static constexpr entity_type version_mask = Traits::version_mask;
91
97 [[nodiscard]] static constexpr entity_type to_integral(const value_type value) noexcept {
98 return static_cast<entity_type>(value);
99 }
100
106 [[nodiscard]] static constexpr entity_type to_entity(const value_type value) noexcept {
107 static_assert(Traits::entity_mask && ((Traits::entity_mask & (Traits::entity_mask + 1)) == 0), "Invalid entity mask");
108 return (to_integral(value) & entity_mask);
109 }
110
116 [[nodiscard]] static constexpr version_type to_version(const value_type value) noexcept {
117 if constexpr(Traits::version_mask == 0u) {
118 return version_type{};
119 } else {
120 static_assert((Traits::version_mask & (Traits::version_mask + 1)) == 0, "Invalid version mask");
121 return (static_cast<version_type>(to_integral(value) >> length) & version_mask);
122 }
123 }
124
130 [[nodiscard]] static constexpr value_type next(const value_type value) noexcept {
131 const auto vers = to_version(value) + 1;
132 return construct(to_integral(value), static_cast<version_type>(vers + (vers == version_mask)));
133 }
134
145 [[nodiscard]] static constexpr value_type construct(const entity_type entity, const version_type version) noexcept {
146 if constexpr(Traits::version_mask == 0u) {
147 return value_type{entity & entity_mask};
148 } else {
149 return value_type{(entity & entity_mask) | (static_cast<entity_type>(version & version_mask) << length)};
150 }
151 }
152
163 [[nodiscard]] static constexpr value_type combine(const entity_type lhs, const entity_type rhs) noexcept {
164 if constexpr(Traits::version_mask == 0u) {
165 return value_type{lhs & entity_mask};
166 } else {
167 return value_type{(lhs & entity_mask) | (rhs & (version_mask << length))};
168 }
169 }
170};
171
176template<entity_like Type>
177struct entt_traits: basic_entt_traits<internal::entt_traits<Type>> {
181 static constexpr stl::size_t page_size = ENTT_SPARSE_PAGE;
182};
183
190template<typename Entity>
191[[nodiscard]] constexpr entt_traits<Entity>::entity_type to_integral(const Entity value) noexcept {
193}
194
201template<typename Entity>
202[[nodiscard]] constexpr entt_traits<Entity>::entity_type to_entity(const Entity value) noexcept {
203 return entt_traits<Entity>::to_entity(value);
204}
205
212template<typename Entity>
213[[nodiscard]] constexpr entt_traits<Entity>::version_type to_version(const Entity value) noexcept {
215}
216
218struct null_t {
224 template<entity_like Entity>
225 [[nodiscard]] constexpr operator Entity() const noexcept {
226 using traits_type = entt_traits<Entity>;
227 return traits_type::construct(traits_type::entity_mask, traits_type::version_mask);
228 }
229
235 [[nodiscard]] constexpr bool operator==([[maybe_unused]] const null_t other) const noexcept {
236 return true;
237 }
238
245 template<entity_like Entity>
246 [[nodiscard]] constexpr bool operator==(const Entity entity) const noexcept {
247 using traits_type = entt_traits<Entity>;
248 return traits_type::to_entity(entity) == traits_type::to_entity(*this);
249 }
250};
251
259 template<entity_like Entity>
260 [[nodiscard]] constexpr operator Entity() const noexcept {
261 using traits_type = entt_traits<Entity>;
262 return traits_type::construct(traits_type::entity_mask, traits_type::version_mask);
263 }
264
270 [[nodiscard]] constexpr bool operator==([[maybe_unused]] const tombstone_t other) const noexcept {
271 return true;
272 }
273
280 template<entity_like Entity>
281 [[nodiscard]] constexpr bool operator==(const Entity entity) const noexcept {
282 using traits_type = entt_traits<Entity>;
283
284 if constexpr(traits_type::version_mask == 0u) {
285 return false;
286 } else {
287 return (traits_type::to_version(entity) == traits_type::to_version(*this));
288 }
289 }
290};
291
299inline constexpr null_t null{};
300
308inline constexpr tombstone_t tombstone{};
309
310} // namespace entt
311
312#endif
Common basic entity traits implementation.
Definition entity.hpp:76
static constexpr value_type construct(const entity_type entity, const version_type version) noexcept
Constructs an identifier from its parts.
Definition entity.hpp:145
static constexpr value_type next(const value_type value) noexcept
Returns the successor of a given identifier.
Definition entity.hpp:130
static constexpr entity_type to_integral(const value_type value) noexcept
Converts an entity to its underlying type.
Definition entity.hpp:97
Traits::entity_type entity_type
Underlying entity type.
Definition entity.hpp:83
static constexpr entity_type to_entity(const value_type value) noexcept
Returns the entity part once converted to the underlying type.
Definition entity.hpp:106
Traits::value_type value_type
Value type.
Definition entity.hpp:81
static constexpr value_type combine(const entity_type lhs, const entity_type rhs) noexcept
Combines two identifiers in a single one.
Definition entity.hpp:163
Traits::version_type version_type
Underlying version type.
Definition entity.hpp:85
static constexpr version_type to_version(const value_type value) noexcept
Returns the version part once converted to the underlying type.
Definition entity.hpp:116
Specifies that a type is an entity-like type.
Definition entity.hpp:67
EnTT default namespace.
Definition dense_map.hpp:25
entity
Default entity identifier.
Definition fwd.hpp:15
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:202
constexpr null_t null
Compile-time constant for null entities.
Definition entity.hpp:299
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:213
constexpr tombstone_t tombstone
Compile-time constant for tombstone entities.
Definition entity.hpp:308
constexpr entt_traits< Entity >::entity_type to_integral(const Entity value) noexcept
Converts an entity to its underlying type.
Definition entity.hpp:191
Entity traits.
Definition entity.hpp:177
basic_entt_traits< internal::entt_traits< Type > > base_type
Base type.
Definition entity.hpp:179
Null object for all identifiers.
Definition entity.hpp:218
constexpr bool operator==(const Entity entity) const noexcept
Compares a null object and an identifier of any type.
Definition entity.hpp:246
constexpr bool operator==(const null_t other) const noexcept
Compares two null objects.
Definition entity.hpp:235
Tombstone object for all identifiers.
Definition entity.hpp:253
constexpr bool operator==(const Entity entity) const noexcept
Compares a tombstone object and an identifier of any type.
Definition entity.hpp:281
constexpr bool operator==(const tombstone_t other) const noexcept
Compares two tombstone objects.
Definition entity.hpp:270