EnTT  3.8.1
entity.hpp
1 #ifndef ENTT_ENTITY_ENTITY_HPP
2 #define ENTT_ENTITY_ENTITY_HPP
3 
4 
5 #include <cstddef>
6 #include <cstdint>
7 #include <type_traits>
8 #include "../config/config.h"
9 
10 
11 namespace entt {
12 
13 
20 namespace internal {
21 
22 
23 template<typename, typename = void>
24 struct entt_traits;
25 
26 
27 template<typename Type>
28 struct entt_traits<Type, std::enable_if_t<std::is_enum_v<Type>>>
29  : entt_traits<std::underlying_type_t<Type>>
30 {};
31 
32 
33 template<typename Type>
34 struct entt_traits<Type, std::enable_if_t<std::is_class_v<Type>>>
35  : entt_traits<typename Type::entity_type>
36 {};
37 
38 
39 template<>
40 struct entt_traits<std::uint32_t> {
41  using entity_type = std::uint32_t;
42  using version_type = std::uint16_t;
43  using difference_type = std::int64_t;
44 
45  static constexpr entity_type entity_mask = 0xFFFFF;
46  static constexpr entity_type version_mask = 0xFFF;
47  static constexpr std::size_t entity_shift = 20u;
48 };
49 
50 
51 template<>
52 struct entt_traits<std::uint64_t> {
53  using entity_type = std::uint64_t;
54  using version_type = std::uint32_t;
55  using difference_type = std::int64_t;
56 
57  static constexpr entity_type entity_mask = 0xFFFFFFFF;
58  static constexpr entity_type version_mask = 0xFFFFFFFF;
59  static constexpr std::size_t entity_shift = 32u;
60 };
61 
62 
63 }
64 
65 
76 template<typename Type>
77 class entt_traits: private internal::entt_traits<Type> {
78  using traits_type = internal::entt_traits<Type>;
79 
80 public:
82  using value_type = Type;
84  using entity_type = typename traits_type::entity_type;
86  using version_type = typename traits_type::version_type;
88  using difference_type = typename traits_type::difference_type;
89 
95  [[nodiscard]] static constexpr entity_type to_integral(const value_type value) ENTT_NOEXCEPT {
96  return static_cast<entity_type>(value);
97  }
98 
104  [[nodiscard]] static constexpr entity_type to_entity(const value_type value) ENTT_NOEXCEPT {
105  return (to_integral(value) & traits_type::entity_mask);
106  }
107 
113  [[nodiscard]] static constexpr version_type to_version(const value_type value) ENTT_NOEXCEPT {
114  constexpr auto mask = (traits_type::version_mask << traits_type::entity_shift);
115  return ((to_integral(value) & mask) >> traits_type::entity_shift);
116  }
117 
128  [[nodiscard]] static constexpr value_type construct(const entity_type entity = traits_type::entity_mask, const version_type version = traits_type::version_mask) ENTT_NOEXCEPT {
129  return value_type{(entity & traits_type::entity_mask) | (static_cast<entity_type>(version) << traits_type::entity_shift)};
130  }
131 };
132 
133 
140 template<typename Entity>
141 [[nodiscard]] constexpr auto to_integral(const Entity entity) ENTT_NOEXCEPT {
143 }
144 
145 
147 struct null_t {
153  template<typename Entity>
154  [[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
156  }
157 
163  [[nodiscard]] constexpr bool operator==([[maybe_unused]] const null_t other) const ENTT_NOEXCEPT {
164  return true;
165  }
166 
172  [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const null_t other) const ENTT_NOEXCEPT {
173  return false;
174  }
175 
182  template<typename Entity>
183  [[nodiscard]] constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT {
185  }
186 
193  template<typename Entity>
194  [[nodiscard]] constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT {
195  return !(entity == *this);
196  }
197 
204  template<typename Entity>
205  [[nodiscard]] constexpr Entity operator|(const Entity entity) const ENTT_NOEXCEPT {
207  }
208 };
209 
210 
218 template<typename Entity>
219 [[nodiscard]] constexpr bool operator==(const Entity entity, const null_t other) ENTT_NOEXCEPT {
220  return other.operator==(entity);
221 }
222 
223 
231 template<typename Entity>
232 [[nodiscard]] constexpr bool operator!=(const Entity entity, const null_t other) ENTT_NOEXCEPT {
233  return !(other == entity);
234 }
235 
236 
238 struct tombstone_t {
244  template<typename Entity>
245  [[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
247  }
248 
254  [[nodiscard]] constexpr bool operator==([[maybe_unused]] const tombstone_t other) const ENTT_NOEXCEPT {
255  return true;
256  }
257 
263  [[nodiscard]] constexpr bool operator!=([[maybe_unused]] const tombstone_t other) const ENTT_NOEXCEPT {
264  return false;
265  }
266 
273  template<typename Entity>
274  [[nodiscard]] constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT {
276  }
277 
284  template<typename Entity>
285  [[nodiscard]] constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT {
286  return !(entity == *this);
287  }
288 
295  template<typename Entity>
296  [[nodiscard]] constexpr Entity operator|(const Entity entity) const ENTT_NOEXCEPT {
298  }
299 };
300 
301 
309 template<typename Entity>
310 [[nodiscard]] constexpr bool operator==(const Entity entity, const tombstone_t other) ENTT_NOEXCEPT {
311  return other.operator==(entity);
312 }
313 
314 
322 template<typename Entity>
323 [[nodiscard]] constexpr bool operator!=(const Entity entity, const tombstone_t other) ENTT_NOEXCEPT {
324  return !(other == entity);
325 }
326 
327 
335 inline constexpr null_t null{};
336 
337 
345 inline constexpr tombstone_t tombstone{};
346 
347 
348 }
349 
350 
351 #endif
Entity traits.
Definition: entity.hpp:77
static constexpr entity_type to_integral(const value_type value)
Converts an entity to its underlying type.
Definition: entity.hpp:95
static constexpr entity_type to_entity(const value_type value)
Returns the entity part once converted to the underlying type.
Definition: entity.hpp:104
typename traits_type::version_type version_type
Underlying version type.
Definition: entity.hpp:86
typename traits_type::entity_type entity_type
Underlying entity type.
Definition: entity.hpp:84
static constexpr value_type construct(const entity_type entity=traits_type::entity_mask, const version_type version=traits_type::version_mask)
Constructs an identifier from its parts.
Definition: entity.hpp:128
Type value_type
Value type.
Definition: entity.hpp:82
static constexpr version_type to_version(const value_type value)
Returns the version part once converted to the underlying type.
Definition: entity.hpp:113
typename traits_type::difference_type difference_type
Difference type.
Definition: entity.hpp:88
EnTT default namespace.
Definition: algorithm.hpp:13
entity
Default entity identifier.
Definition: fwd.hpp:61
constexpr auto to_integral(const Entity entity)
Converts an entity to its underlying type.
Definition: entity.hpp:141
constexpr tombstone_t tombstone
Compile-time constant for tombstone entities.
Definition: entity.hpp:345
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:368
constexpr bool operator==(const Entity entity, const null_t other)
Compares a null object and an entity identifier of any type.
Definition: entity.hpp:219
Null object for all entity identifiers.
Definition: entity.hpp:147
constexpr bool operator!=([[maybe_unused]] const null_t other) const
Compares two null objects.
Definition: entity.hpp:172
constexpr bool operator!=(const Entity entity) const
Compares a null object and an entity identifier of any type.
Definition: entity.hpp:194
constexpr bool operator==(const Entity entity) const
Compares a null object and an entity identifier of any type.
Definition: entity.hpp:183
constexpr bool operator==([[maybe_unused]] const null_t other) const
Compares two null objects.
Definition: entity.hpp:163
constexpr Entity operator|(const Entity entity) const
Creates a null object from an entity identifier of any type.
Definition: entity.hpp:205
Tombstone object for all entity identifiers.
Definition: entity.hpp:238
constexpr Entity operator|(const Entity entity) const
Creates a tombstone object from an entity identifier of any type.
Definition: entity.hpp:296
constexpr bool operator==([[maybe_unused]] const tombstone_t other) const
Compares two tombstone objects.
Definition: entity.hpp:254
constexpr bool operator==(const Entity entity) const
Compares a tombstone object and an entity identifier of any type.
Definition: entity.hpp:274
constexpr bool operator!=(const Entity entity) const
Compares a tombstone object and an entity identifier of any type.
Definition: entity.hpp:285
constexpr bool operator!=([[maybe_unused]] const tombstone_t other) const
Compares two tombstone objects.
Definition: entity.hpp:263