EnTT  3.10.0
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 
10 namespace entt {
11 
17 namespace internal {
18 
19 template<typename, typename = void>
20 struct entt_traits;
21 
22 template<typename Type>
23 struct entt_traits<Type, std::enable_if_t<std::is_enum_v<Type>>>
24  : entt_traits<std::underlying_type_t<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 
30 template<>
31 struct entt_traits<std::uint32_t> {
32  using entity_type = std::uint32_t;
33  using version_type = std::uint16_t;
34 
35  static constexpr entity_type entity_mask = 0xFFFFF;
36  static constexpr entity_type version_mask = 0xFFF;
37  static constexpr std::size_t entity_shift = 20u;
38 };
39 
40 template<>
41 struct entt_traits<std::uint64_t> {
42  using entity_type = std::uint64_t;
43  using version_type = std::uint32_t;
44 
45  static constexpr entity_type entity_mask = 0xFFFFFFFF;
46  static constexpr entity_type version_mask = 0xFFFFFFFF;
47  static constexpr std::size_t entity_shift = 32u;
48 };
49 
50 } // namespace internal
51 
61 template<typename Type>
62 class entt_traits: internal::entt_traits<Type> {
63  using base_type = internal::entt_traits<Type>;
64 
65 public:
67  using value_type = Type;
69  using entity_type = typename base_type::entity_type;
71  using version_type = typename base_type::version_type;
73  static constexpr entity_type reserved = base_type::entity_mask | (base_type::version_mask << base_type::entity_shift);
75  static constexpr auto page_size = ENTT_SPARSE_PAGE;
76 
82  [[nodiscard]] static constexpr entity_type to_integral(const value_type value) ENTT_NOEXCEPT {
83  return static_cast<entity_type>(value);
84  }
85 
91  [[nodiscard]] static constexpr entity_type to_entity(const value_type value) ENTT_NOEXCEPT {
92  return (to_integral(value) & base_type::entity_mask);
93  }
94 
100  [[nodiscard]] static constexpr version_type to_version(const value_type value) ENTT_NOEXCEPT {
101  return (to_integral(value) >> base_type::entity_shift);
102  }
103 
114  [[nodiscard]] static constexpr value_type construct(const entity_type entity, const version_type version) ENTT_NOEXCEPT {
115  return value_type{(entity & base_type::entity_mask) | (static_cast<entity_type>(version) << base_type::entity_shift)};
116  }
117 
128  [[nodiscard]] static constexpr value_type combine(const entity_type lhs, const entity_type rhs) ENTT_NOEXCEPT {
129  constexpr auto mask = (base_type::version_mask << base_type::entity_shift);
130  return value_type{(lhs & base_type::entity_mask) | (rhs & mask)};
131  }
132 };
133 
138 template<typename Entity>
139 [[nodiscard]] constexpr typename entt_traits<Entity>::entity_type to_integral(const Entity value) ENTT_NOEXCEPT {
140  return entt_traits<Entity>::to_integral(value);
141 }
142 
147 template<typename Entity>
148 [[nodiscard]] constexpr typename entt_traits<Entity>::entity_type to_entity(const Entity value) ENTT_NOEXCEPT {
149  return entt_traits<Entity>::to_entity(value);
150 }
151 
156 template<typename Entity>
157 [[nodiscard]] constexpr typename entt_traits<Entity>::version_type to_version(const Entity value) ENTT_NOEXCEPT {
158  return entt_traits<Entity>::to_version(value);
159 }
160 
162 struct null_t {
168  template<typename Entity>
169  [[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
170  using entity_traits = entt_traits<Entity>;
171  return entity_traits::combine(entity_traits::reserved, entity_traits::reserved);
172  }
173 
179  [[nodiscard]] constexpr bool operator==([[maybe_unused]] const null_t other) const ENTT_NOEXCEPT {
180  return true;
181  }
182 
188  [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const null_t other) const ENTT_NOEXCEPT {
189  return false;
190  }
191 
198  template<typename Entity>
199  [[nodiscard]] constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT {
200  using entity_traits = entt_traits<Entity>;
201  return entity_traits::to_entity(entity) == entity_traits::to_entity(*this);
202  }
203 
210  template<typename Entity>
211  [[nodiscard]] constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT {
212  return !(entity == *this);
213  }
214 };
215 
223 template<typename Entity>
224 [[nodiscard]] constexpr bool operator==(const Entity entity, const null_t other) ENTT_NOEXCEPT {
225  return other.operator==(entity);
226 }
227 
235 template<typename Entity>
236 [[nodiscard]] constexpr bool operator!=(const Entity entity, const null_t other) ENTT_NOEXCEPT {
237  return !(other == entity);
238 }
239 
241 struct tombstone_t {
247  template<typename Entity>
248  [[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
249  using entity_traits = entt_traits<Entity>;
250  return entity_traits::combine(entity_traits::reserved, entity_traits::reserved);
251  }
252 
258  [[nodiscard]] constexpr bool operator==([[maybe_unused]] const tombstone_t other) const ENTT_NOEXCEPT {
259  return true;
260  }
261 
267  [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const tombstone_t other) const ENTT_NOEXCEPT {
268  return false;
269  }
270 
277  template<typename Entity>
278  [[nodiscard]] constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT {
279  using entity_traits = entt_traits<Entity>;
280  return entity_traits::to_version(entity) == entity_traits::to_version(*this);
281  }
282 
289  template<typename Entity>
290  [[nodiscard]] constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT {
291  return !(entity == *this);
292  }
293 };
294 
302 template<typename Entity>
303 [[nodiscard]] constexpr bool operator==(const Entity entity, const tombstone_t other) ENTT_NOEXCEPT {
304  return other.operator==(entity);
305 }
306 
314 template<typename Entity>
315 [[nodiscard]] constexpr bool operator!=(const Entity entity, const tombstone_t other) ENTT_NOEXCEPT {
316  return !(other == entity);
317 }
318 
326 inline constexpr null_t null{};
327 
335 inline constexpr tombstone_t tombstone{};
336 
337 } // namespace entt
338 
339 #endif
Entity traits.
Definition: entity.hpp:62
static constexpr auto page_size
Page size, default is ENTT_SPARSE_PAGE.
Definition: entity.hpp:75
typename base_type::entity_type entity_type
Underlying entity type.
Definition: entity.hpp:69
static constexpr entity_type reserved
Reserved identifier.
Definition: entity.hpp:73
static constexpr entity_type to_integral(const value_type value)
Converts an entity to its underlying type.
Definition: entity.hpp:82
static constexpr entity_type to_entity(const value_type value)
Returns the entity part once converted to the underlying type.
Definition: entity.hpp:91
static constexpr value_type construct(const entity_type entity, const version_type version)
Constructs an identifier from its parts.
Definition: entity.hpp:114
Type value_type
Value type.
Definition: entity.hpp:67
static constexpr value_type combine(const entity_type lhs, const entity_type rhs)
Combines two identifiers in a single one.
Definition: entity.hpp:128
typename base_type::version_type version_type
Underlying version type.
Definition: entity.hpp:71
static constexpr version_type to_version(const value_type value)
Returns the version part once converted to the underlying type.
Definition: entity.hpp:100
EnTT default namespace.
Definition: dense_map.hpp:22
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
entity
Default entity identifier.
Definition: fwd.hpp:47
constexpr tombstone_t tombstone
Compile-time constant for tombstone entities.
Definition: entity.hpp:335
constexpr entt_traits< Entity >::entity_type to_integral(const Entity value)
Converts an entity to its underlying type.
Definition: entity.hpp:139
constexpr entt_traits< Entity >::entity_type to_entity(const Entity value)
Returns the entity part once converted to the underlying type.
Definition: entity.hpp:148
bool operator!=(const basic_any< Len, Align > &lhs, const basic_any< Len, Align > &rhs)
Checks if two wrappers differ in their content.
Definition: any.hpp:402
constexpr entt_traits< Entity >::version_type to_version(const Entity value)
Returns the version part once converted to the underlying type.
Definition: entity.hpp:157
Null object for all identifiers.
Definition: entity.hpp:162
constexpr bool operator!=([[maybe_unused]] const null_t other) const
Compares two null objects.
Definition: entity.hpp:188
constexpr bool operator!=(const Entity entity) const
Compares a null object and an identifier of any type.
Definition: entity.hpp:211
constexpr bool operator==(const Entity entity) const
Compares a null object and an identifier of any type.
Definition: entity.hpp:199
constexpr bool operator==([[maybe_unused]] const null_t other) const
Compares two null objects.
Definition: entity.hpp:179
Tombstone object for all identifiers.
Definition: entity.hpp:241
constexpr bool operator==([[maybe_unused]] const tombstone_t other) const
Compares two tombstone objects.
Definition: entity.hpp:258
constexpr bool operator==(const Entity entity) const
Compares a tombstone object and an identifier of any type.
Definition: entity.hpp:278
constexpr bool operator!=(const Entity entity) const
Compares a tombstone object and an identifier of any type.
Definition: entity.hpp:290
constexpr bool operator!=([[maybe_unused]] const tombstone_t other) const
Compares two tombstone objects.
Definition: entity.hpp:267