EnTT 3.13.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 "fwd.hpp"
9
10namespace entt {
11
13namespace internal {
14
15// waiting for C++20 and std::popcount
16template<typename Type>
17constexpr int popcount(Type value) noexcept {
18 return value ? (int(value & 1) + popcount(value >> 1)) : 0;
19}
20
21template<typename, typename = void>
22struct entt_traits;
23
24template<typename Type>
25struct entt_traits<Type, std::enable_if_t<std::is_enum_v<Type>>>
26 : entt_traits<std::underlying_type_t<Type>> {
27 using value_type = Type;
28};
29
30template<typename Type>
31struct entt_traits<Type, std::enable_if_t<std::is_class_v<Type>>>
32 : entt_traits<typename Type::entity_type> {
33 using value_type = Type;
34};
35
36template<>
37struct entt_traits<std::uint32_t> {
38 using value_type = std::uint32_t;
39
40 using entity_type = std::uint32_t;
41 using version_type = std::uint16_t;
42
43 static constexpr entity_type entity_mask = 0xFFFFF;
44 static constexpr entity_type version_mask = 0xFFF;
45};
46
47template<>
48struct entt_traits<std::uint64_t> {
49 using value_type = std::uint64_t;
50
51 using entity_type = std::uint64_t;
52 using version_type = std::uint32_t;
53
54 static constexpr entity_type entity_mask = 0xFFFFFFFF;
55 static constexpr entity_type version_mask = 0xFFFFFFFF;
56};
57
58} // namespace internal
65template<typename Traits>
67 static constexpr auto length = internal::popcount(Traits::entity_mask);
68
69 static_assert(Traits::entity_mask && ((typename Traits::entity_type{1} << length) == (Traits::entity_mask + 1)), "Invalid entity mask");
70 static_assert((typename Traits::entity_type{1} << internal::popcount(Traits::version_mask)) == (Traits::version_mask + 1), "Invalid version mask");
71
72public:
74 using value_type = typename Traits::value_type;
76 using entity_type = typename Traits::entity_type;
78 using version_type = typename Traits::version_type;
79
81 static constexpr entity_type entity_mask = Traits::entity_mask;
83 static constexpr entity_type version_mask = Traits::version_mask;
84
90 [[nodiscard]] static constexpr entity_type to_integral(const value_type value) noexcept {
91 return static_cast<entity_type>(value);
92 }
93
99 [[nodiscard]] static constexpr entity_type to_entity(const value_type value) noexcept {
100 return (to_integral(value) & entity_mask);
101 }
102
108 [[nodiscard]] static constexpr version_type to_version(const value_type value) noexcept {
109 return (static_cast<version_type>(to_integral(value) >> length) & version_mask);
110 }
111
117 [[nodiscard]] static constexpr value_type next(const value_type value) noexcept {
118 const auto vers = to_version(value) + 1;
119 return construct(to_integral(value), static_cast<version_type>(vers + (vers == version_mask)));
120 }
121
132 [[nodiscard]] static constexpr value_type construct(const entity_type entity, const version_type version) noexcept {
133 return value_type{(entity & entity_mask) | (static_cast<entity_type>(version & version_mask) << length)};
134 }
135
146 [[nodiscard]] static constexpr value_type combine(const entity_type lhs, const entity_type rhs) noexcept {
147 return value_type{(lhs & entity_mask) | (rhs & (version_mask << length))};
148 }
149};
150
155template<typename Type>
156struct entt_traits: basic_entt_traits<internal::entt_traits<Type>> {
160 static constexpr std::size_t page_size = ENTT_SPARSE_PAGE;
161};
162
169template<typename Entity>
170[[nodiscard]] constexpr typename entt_traits<Entity>::entity_type to_integral(const Entity value) noexcept {
172}
173
180template<typename Entity>
181[[nodiscard]] constexpr typename entt_traits<Entity>::entity_type to_entity(const Entity value) noexcept {
182 return entt_traits<Entity>::to_entity(value);
183}
184
191template<typename Entity>
192[[nodiscard]] constexpr typename entt_traits<Entity>::version_type to_version(const Entity value) noexcept {
194}
195
197struct null_t {
203 template<typename Entity>
204 [[nodiscard]] constexpr operator Entity() const noexcept {
205 using traits_type = entt_traits<Entity>;
206 constexpr auto value = traits_type::construct(traits_type::entity_mask, traits_type::version_mask);
207 return value;
208 }
209
215 [[nodiscard]] constexpr bool operator==([[maybe_unused]] const null_t other) const noexcept {
216 return true;
217 }
218
224 [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const null_t other) const noexcept {
225 return false;
226 }
227
234 template<typename Entity>
235 [[nodiscard]] constexpr bool operator==(const Entity entity) const noexcept {
236 using traits_type = entt_traits<Entity>;
237 return traits_type::to_entity(entity) == traits_type::to_entity(*this);
238 }
239
246 template<typename Entity>
247 [[nodiscard]] constexpr bool operator!=(const Entity entity) const noexcept {
248 return !(entity == *this);
249 }
250};
251
259template<typename Entity>
260[[nodiscard]] constexpr bool operator==(const Entity entity, const null_t other) noexcept {
261 return other.operator==(entity);
262}
263
271template<typename Entity>
272[[nodiscard]] constexpr bool operator!=(const Entity entity, const null_t other) noexcept {
273 return !(other == entity);
274}
275
283 template<typename Entity>
284 [[nodiscard]] constexpr operator Entity() const noexcept {
285 using traits_type = entt_traits<Entity>;
286 constexpr auto value = traits_type::construct(traits_type::entity_mask, traits_type::version_mask);
287 return value;
288 }
289
295 [[nodiscard]] constexpr bool operator==([[maybe_unused]] const tombstone_t other) const noexcept {
296 return true;
297 }
298
304 [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const tombstone_t other) const noexcept {
305 return false;
306 }
307
314 template<typename Entity>
315 [[nodiscard]] constexpr bool operator==(const Entity entity) const noexcept {
316 using traits_type = entt_traits<Entity>;
317 return traits_type::to_version(entity) == traits_type::to_version(*this);
318 }
319
326 template<typename Entity>
327 [[nodiscard]] constexpr bool operator!=(const Entity entity) const noexcept {
328 return !(entity == *this);
329 }
330};
331
339template<typename Entity>
340[[nodiscard]] constexpr bool operator==(const Entity entity, const tombstone_t other) noexcept {
341 return other.operator==(entity);
342}
343
351template<typename Entity>
352[[nodiscard]] constexpr bool operator!=(const Entity entity, const tombstone_t other) noexcept {
353 return !(other == entity);
354}
355
363inline constexpr null_t null{};
364
372inline constexpr tombstone_t tombstone{};
373
374} // namespace entt
375
376#endif
Common basic entity traits implementation.
Definition entity.hpp:66
typename Traits::entity_type entity_type
Underlying entity type.
Definition entity.hpp:76
static constexpr entity_type version_mask
Version mask size.
Definition entity.hpp:83
static constexpr value_type construct(const entity_type entity, const version_type version) noexcept
Constructs an identifier from its parts.
Definition entity.hpp:132
static constexpr value_type next(const value_type value) noexcept
Returns the successor of a given identifier.
Definition entity.hpp:117
static constexpr entity_type to_integral(const value_type value) noexcept
Converts an entity to its underlying type.
Definition entity.hpp:90
static constexpr entity_type entity_mask
Entity mask size.
Definition entity.hpp:81
static constexpr entity_type to_entity(const value_type value) noexcept
Returns the entity part once converted to the underlying type.
Definition entity.hpp:99
typename Traits::value_type value_type
Value type.
Definition entity.hpp:74
typename Traits::version_type version_type
Underlying version type.
Definition entity.hpp:78
static constexpr value_type combine(const entity_type lhs, const entity_type rhs) noexcept
Combines two identifiers in a single one.
Definition entity.hpp:146
static constexpr version_type to_version(const value_type value) noexcept
Returns the version part once converted to the underlying type.
Definition entity.hpp:108
EnTT default namespace.
Definition dense_map.hpp:21
entity
Default entity identifier.
Definition fwd.hpp:13
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:181
constexpr null_t null
Compile-time constant for null entities.
Definition entity.hpp:363
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:192
constexpr tombstone_t tombstone
Compile-time constant for tombstone entities.
Definition entity.hpp:372
constexpr entt_traits< Entity >::entity_type to_integral(const Entity value) noexcept
Converts an entity to its underlying type.
Definition entity.hpp:170
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Entity traits.
Definition entity.hpp:156
static constexpr std::size_t page_size
Page size, default is ENTT_SPARSE_PAGE.
Definition entity.hpp:160
Null object for all identifiers.
Definition entity.hpp:197
constexpr bool operator==(const Entity entity) const noexcept
Compares a null object and an identifier of any type.
Definition entity.hpp:235
constexpr bool operator!=(const Entity entity) const noexcept
Compares a null object and an identifier of any type.
Definition entity.hpp:247
constexpr bool operator!=(const null_t other) const noexcept
Compares two null objects.
Definition entity.hpp:224
constexpr bool operator==(const null_t other) const noexcept
Compares two null objects.
Definition entity.hpp:215
Tombstone object for all identifiers.
Definition entity.hpp:277
constexpr bool operator==(const Entity entity) const noexcept
Compares a tombstone object and an identifier of any type.
Definition entity.hpp:315
constexpr bool operator!=(const tombstone_t other) const noexcept
Compares two tombstone objects.
Definition entity.hpp:304
constexpr bool operator!=(const Entity entity) const noexcept
Compares a tombstone object and an identifier of any type.
Definition entity.hpp:327
constexpr bool operator==(const tombstone_t other) const noexcept
Compares two tombstone objects.
Definition entity.hpp:295