EnTT  3.5.0
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 template<typename, typename = void>
21 struct entt_traits;
22 
23 
28 template<typename Type>
29 struct entt_traits<Type, std::enable_if_t<std::is_enum_v<Type>>>
30  : entt_traits<std::underlying_type_t<Type>>
31 {};
32 
33 
42 template<>
43 struct entt_traits<std::uint16_t> {
45  using entity_type = std::uint16_t;
47  using version_type = std::uint8_t;
49  using difference_type = std::int16_t;
50 
52  static constexpr entity_type entity_mask = 0xFFF;
54  static constexpr entity_type version_mask = 0xF;
56  static constexpr std::size_t entity_shift = 12u;
57 };
58 
59 
68 template<>
69 struct entt_traits<std::uint32_t> {
71  using entity_type = std::uint32_t;
73  using version_type = std::uint16_t;
75  using difference_type = std::int32_t;
76 
78  static constexpr entity_type entity_mask = 0xFFFFF;
80  static constexpr entity_type version_mask = 0xFFF;
82  static constexpr std::size_t entity_shift = 20u;
83 };
84 
85 
94 template<>
95 struct entt_traits<std::uint64_t> {
97  using entity_type = std::uint64_t;
99  using version_type = std::uint32_t;
101  using difference_type = std::int64_t;
102 
104  static constexpr entity_type entity_mask = 0xFFFFFFFF;
106  static constexpr entity_type version_mask = 0xFFFFFFFF;
108  static constexpr std::size_t entity_shift = 32u;
109 };
110 
111 
118 template<typename Entity>
119 [[nodiscard]] constexpr auto to_integral(const Entity entity) ENTT_NOEXCEPT {
120  return static_cast<typename entt_traits<Entity>::entity_type>(entity);
121 }
122 
123 
125 struct null_t {
131  template<typename Entity>
132  [[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
133  return Entity{entt_traits<Entity>::entity_mask};
134  }
135 
140  [[nodiscard]] constexpr bool operator==(null_t) const ENTT_NOEXCEPT {
141  return true;
142  }
143 
148  [[nodiscard]] constexpr bool operator!=(null_t) const ENTT_NOEXCEPT {
149  return false;
150  }
151 
158  template<typename Entity>
159  [[nodiscard]] constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT {
160  return (to_integral(entity) & entt_traits<Entity>::entity_mask) == to_integral(static_cast<Entity>(*this));
161  }
162 
169  template<typename Entity>
170  [[nodiscard]] constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT {
171  return !(entity == *this);
172  }
173 };
174 
175 
183 template<typename Entity>
184 [[nodiscard]] constexpr bool operator==(const Entity entity, null_t other) ENTT_NOEXCEPT {
185  return other.operator==(entity);
186 }
187 
188 
196 template<typename Entity>
197 [[nodiscard]] constexpr bool operator!=(const Entity entity, null_t other) ENTT_NOEXCEPT {
198  return !(other == entity);
199 }
200 
201 
215 inline constexpr null_t null{};
216 
217 
218 }
219 
220 
221 #endif
entt::null_t::operator==
constexpr bool operator==(null_t) const noexcept
Compares two null objects.
Definition: entity.hpp:140
entt::entt_traits< std::uint64_t >::version_type
std::uint32_t version_type
Underlying version type.
Definition: entity.hpp:99
entt::entt_traits< std::uint64_t >::entity_type
std::uint64_t entity_type
Underlying entity type.
Definition: entity.hpp:97
entt::to_integral
constexpr auto to_integral(const Entity entity) noexcept
Converts an entity type to its underlying type.
Definition: entity.hpp:119
entt::entt_traits< std::uint32_t >::entity_type
std::uint32_t entity_type
Underlying entity type.
Definition: entity.hpp:71
entt::null_t
Null object for all entity identifiers.
Definition: entity.hpp:125
entt::null_t::operator==
constexpr bool operator==(const Entity entity) const noexcept
Compares a null object and an entity identifier of any type.
Definition: entity.hpp:159
entt::entt_traits< std::uint64_t >::difference_type
std::int64_t difference_type
Difference type.
Definition: entity.hpp:101
entt::entt_traits< std::uint16_t >::version_type
std::uint8_t version_type
Underlying version type.
Definition: entity.hpp:47
entt::entt_traits< std::uint16_t >::entity_type
std::uint16_t entity_type
Underlying entity type.
Definition: entity.hpp:45
entt::entt_traits< std::uint32_t >::difference_type
std::int32_t difference_type
Difference type.
Definition: entity.hpp:75
entt
EnTT default namespace.
Definition: algorithm.hpp:13
entt::null_t::operator!=
constexpr bool operator!=(null_t) const noexcept
Compares two null objects.
Definition: entity.hpp:148
entt::entt_traits< std::uint32_t >::version_type
std::uint16_t version_type
Underlying version type.
Definition: entity.hpp:73
entt::entt_traits< std::uint16_t >::difference_type
std::int16_t difference_type
Difference type.
Definition: entity.hpp:49
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:227
entt::entt_traits
Entity traits.
Definition: entity.hpp:21
entt::operator==
constexpr bool operator==(const Entity entity, null_t other) noexcept
Compares a null object and an entity identifier of any type.
Definition: entity.hpp:184
entt::entity
entity
Default entity identifier.
Definition: fwd.hpp:52
entt::null_t::operator!=
constexpr bool operator!=(const Entity entity) const noexcept
Compares a null object and an entity identifier of any type.
Definition: entity.hpp:170