EnTT  3.6.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::uint32_t> {
45  using entity_type = std::uint32_t;
47  using version_type = std::uint16_t;
49  using difference_type = std::int64_t;
50 
52  static constexpr entity_type entity_mask = 0xFFFFF;
54  static constexpr entity_type version_mask = 0xFFF;
56  static constexpr std::size_t entity_shift = 20u;
57 };
58 
59 
68 template<>
69 struct entt_traits<std::uint64_t> {
71  using entity_type = std::uint64_t;
73  using version_type = std::uint32_t;
75  using difference_type = std::int64_t;
76 
78  static constexpr entity_type entity_mask = 0xFFFFFFFF;
80  static constexpr entity_type version_mask = 0xFFFFFFFF;
82  static constexpr std::size_t entity_shift = 32u;
83 };
84 
85 
92 template<typename Entity>
93 [[nodiscard]] constexpr auto to_integral(const Entity entity) ENTT_NOEXCEPT {
94  return static_cast<typename entt_traits<Entity>::entity_type>(entity);
95 }
96 
97 
99 struct null_t {
105  template<typename Entity>
106  [[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
107  return Entity{entt_traits<Entity>::entity_mask};
108  }
109 
114  [[nodiscard]] constexpr bool operator==(const null_t &) const ENTT_NOEXCEPT {
115  return true;
116  }
117 
122  [[nodiscard]] constexpr bool operator!=(const null_t &) const ENTT_NOEXCEPT {
123  return false;
124  }
125 
132  template<typename Entity>
133  [[nodiscard]] constexpr bool operator==(const Entity &entity) const ENTT_NOEXCEPT {
134  return (to_integral(entity) & entt_traits<Entity>::entity_mask) == to_integral(static_cast<Entity>(*this));
135  }
136 
143  template<typename Entity>
144  [[nodiscard]] constexpr bool operator!=(const Entity &entity) const ENTT_NOEXCEPT {
145  return !(entity == *this);
146  }
147 };
148 
149 
157 template<typename Entity>
158 [[nodiscard]] constexpr bool operator==(const Entity &entity, const null_t &other) ENTT_NOEXCEPT {
159  return other.operator==(entity);
160 }
161 
162 
170 template<typename Entity>
171 [[nodiscard]] constexpr bool operator!=(const Entity &entity, const null_t &other) ENTT_NOEXCEPT {
172  return !(other == entity);
173 }
174 
175 
189 inline constexpr null_t null{};
190 
191 
192 }
193 
194 
195 #endif
entt::operator==
constexpr bool operator==(const Entity &entity, const null_t &other) noexcept
Compares a null object and an entity identifier of any type.
Definition: entity.hpp:158
entt::entt_traits< std::uint64_t >::version_type
std::uint32_t version_type
Underlying version type.
Definition: entity.hpp:73
entt::entt_traits< std::uint64_t >::entity_type
std::uint64_t entity_type
Underlying entity type.
Definition: entity.hpp:71
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:133
entt::to_integral
constexpr auto to_integral(const Entity entity) noexcept
Converts an entity type to its underlying type.
Definition: entity.hpp:93
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:144
entt::entt_traits< std::uint32_t >::entity_type
std::uint32_t entity_type
Underlying entity type.
Definition: entity.hpp:45
entt::null_t
Null object for all entity identifiers.
Definition: entity.hpp:99
entt::entt_traits< std::uint64_t >::difference_type
std::int64_t difference_type
Difference type.
Definition: entity.hpp:75
entt::entt_traits< std::uint32_t >::difference_type
std::int64_t difference_type
Difference type.
Definition: entity.hpp:49
entt::null_t::operator!=
constexpr bool operator!=(const null_t &) const noexcept
Compares two null objects.
Definition: entity.hpp:122
entt
EnTT default namespace.
Definition: algorithm.hpp:13
entt::entt_traits< std::uint32_t >::version_type
std::uint16_t version_type
Underlying version type.
Definition: entity.hpp:47
entt::entt_traits
Entity traits.
Definition: entity.hpp:21
entt::null_t::operator==
constexpr bool operator==(const null_t &) const noexcept
Compares two null objects.
Definition: entity.hpp:114
entt::entity
entity
Default entity identifier.
Definition: fwd.hpp:60
entt::operator!=
bool operator!=(const any &lhs, const any &rhs) noexcept
Checks if two wrappers differ in their content.
Definition: any.hpp:313