* extended entt_traits<T>::to_type support
* added null_t::operator|
* added tombstone_t::operator|
This commit is contained in:
Michele Caini
2021-06-04 11:01:26 +02:00
parent 29265e4181
commit 90be1db402
2 changed files with 52 additions and 4 deletions

View File

@@ -123,6 +123,17 @@ public:
return Type{(entity & traits_type::entity_mask) | (version << traits_type::entity_shift)};
}
/**
* @brief Constructs an identifier from its parts.
* @param entity The entity part of the identifier.
* @param version The version part of the identifier.
* @return A properly constructed identifier.
*/
[[nodiscard]] static constexpr auto to_type(const Type entity, const Type version) ENTT_NOEXCEPT {
constexpr auto mask = (traits_type::version_mask << traits_type::entity_shift);
return Type{(to_integral(entity) & traits_type::entity_mask) | (to_integral(version) & mask)};
}
/**
* @brief Returns the reserved identifer.
* @return The reserved identifier.
@@ -150,7 +161,7 @@ struct null_t {
/**
* @brief Converts the null object to identifiers of any type.
* @tparam Entity Type of entity identifier.
* @return The null representation for the given identifier.
* @return The null representation for the given type.
*/
template<typename Entity>
[[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
@@ -194,6 +205,17 @@ struct null_t {
[[nodiscard]] constexpr bool operator!=(const Entity &entity) const ENTT_NOEXCEPT {
return !(entity == *this);
}
/**
* @brief Creates a null object from an entity identifier of any type.
* @tparam Entity Type of entity identifier.
* @param entity Entity identifier to turn into a null object.
* @return The null representation for the given identifier.
*/
template<typename Entity>
[[nodiscard]] constexpr Entity operator|(const Entity &entity) const ENTT_NOEXCEPT {
return entt_traits<Entity>::to_type(*this, entity);
}
};
@@ -228,7 +250,7 @@ struct tombstone_t {
/**
* @brief Converts the tombstone object to identifiers of any type.
* @tparam Entity Type of entity identifier.
* @return The tombstone representation for the given identifier.
* @return The tombstone representation for the given type.
*/
template<typename Entity>
[[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
@@ -252,7 +274,7 @@ struct tombstone_t {
}
/**
* @brief Compares a null tombstone and an entity identifier of any type.
* @brief Compares a tombstone object and an entity identifier of any type.
* @tparam Entity Type of entity identifier.
* @param entity Entity identifier with which to compare.
* @return False if the two elements differ, true otherwise.
@@ -269,9 +291,20 @@ struct tombstone_t {
* @return True if the two elements differ, false otherwise.
*/
template<typename Entity>
[[nodiscard]] constexpr bool operator!=(const Entity &entity) const ENTT_NOEXCEPT {
[[nodiscard]] constexpr bool operator!=(const Entity &entity) const ENTT_NOEXCEPT {
return !(entity == *this);
}
/**
* @brief Creates a tombstone object from an entity identifier of any type.
* @tparam Entity Type of entity identifier.
* @param entity Entity identifier to turn into a tombstone object.
* @return The tombstone representation for the given identifier.
*/
template<typename Entity>
[[nodiscard]] constexpr Entity operator|(const Entity &entity) const ENTT_NOEXCEPT {
return entt_traits<Entity>::to_type(entity, *this);
}
};

View File

@@ -24,6 +24,13 @@ TEST(Entity, Traits) {
ASSERT_EQ(traits_type::to_type(traits_type::to_entity(entity), traits_type::to_version(entity)), entity);
ASSERT_EQ(traits_type::to_type(traits_type::to_entity(other), traits_type::to_version(other)), other);
ASSERT_NE(traits_type::to_type(traits_type::to_integral(entity), {}), entity);
ASSERT_EQ(traits_type::to_type(entity, entity), entity);
ASSERT_EQ(traits_type::to_type(other, other), other);
ASSERT_NE(traits_type::to_type(entity, {}), entity);
ASSERT_EQ(traits_type::reserved(), entt::tombstone | static_cast<entt::entity>(entt::null));
ASSERT_EQ(traits_type::reserved(), entt::null | static_cast<entt::entity>(entt::tombstone));
}
TEST(Entity, Null) {
@@ -38,6 +45,10 @@ TEST(Entity, Null) {
entt::registry registry{};
const auto entity = registry.create();
ASSERT_EQ((entt::null | entity), (traits_type::to_type(entt::null, entity)));
ASSERT_EQ((entt::null | static_cast<entt::entity>(entt::null)), static_cast<entt::entity>(entt::null));
ASSERT_EQ((entt::null | static_cast<entt::entity>(entt::tombstone)), static_cast<entt::entity>(entt::null));
registry.emplace<int>(entity, 42);
ASSERT_FALSE(entity == entt::null);
@@ -64,6 +75,10 @@ TEST(Entity, Tombstone) {
entt::registry registry{};
const auto entity = registry.create();
ASSERT_EQ((entt::tombstone | entity), (traits_type::to_type(entity, entt::tombstone)));
ASSERT_EQ((entt::tombstone | static_cast<entt::entity>(entt::tombstone)), static_cast<entt::entity>(entt::tombstone));
ASSERT_EQ((entt::tombstone | static_cast<entt::entity>(entt::null)), static_cast<entt::entity>(entt::tombstone));
registry.emplace<int>(entity, 42);
ASSERT_FALSE(entity == entt::tombstone);