entity: adds to_entity and to_version to the entt namespace (see #788)

This commit is contained in:
Michele Caini
2021-09-28 14:52:52 +02:00
parent 72776d6fb6
commit 1bfb877a3b
2 changed files with 36 additions and 20 deletions

View File

@@ -130,14 +130,30 @@ public:
};
/**
* @brief Converts an entity to its underlying type.
* @copydoc entt_traits<Entity>::to_integral
* @tparam Entity The value type.
* @param entity The value to convert.
* @return The integral representation of the given value.
*/
template<typename Entity>
[[nodiscard]] constexpr typename entt_traits<Entity>::entity_type to_integral(const Entity entity) ENTT_NOEXCEPT {
return entt_traits<Entity>::to_integral(entity);
[[nodiscard]] constexpr typename entt_traits<Entity>::entity_type to_integral(const Entity value) ENTT_NOEXCEPT {
return entt_traits<Entity>::to_integral(value);
}
/**
* @copydoc entt_traits<Entity>::to_entity
* @tparam Entity The value type.
*/
template<typename Entity>
[[nodiscard]] constexpr typename entt_traits<Entity>::entity_type to_entity(const Entity value) ENTT_NOEXCEPT {
return entt_traits<Entity>::to_entity(value);
}
/**
* @copydoc entt_traits<Entity>::to_version
* @tparam Entity The value type.
*/
template<typename Entity>
[[nodiscard]] constexpr typename entt_traits<Entity>::version_type to_version(const Entity value) ENTT_NOEXCEPT {
return entt_traits<Entity>::to_version(value);
}
/*! @brief Null object for all identifiers. */

View File

@@ -14,20 +14,20 @@ TEST(Entity, Traits) {
const auto entity = registry.create();
const auto other = registry.create();
ASSERT_EQ(entt::to_integral(entity), traits_type::to_integral(entity));
ASSERT_EQ(entt::to_integral(entity), entt::to_integral(entity));
ASSERT_NE(entt::to_integral(entity), entt::to_integral<entt::entity>(entt::null));
ASSERT_NE(entt::to_integral(entity), entt::to_integral(entt::entity{}));
ASSERT_EQ(traits_type::to_entity(entity), 0u);
ASSERT_EQ(traits_type::to_version(entity), 1u);
ASSERT_EQ(traits_type::to_entity(other), 1u);
ASSERT_EQ(traits_type::to_version(other), 0u);
ASSERT_EQ(entt::to_entity(entity), 0u);
ASSERT_EQ(entt::to_version(entity), 1u);
ASSERT_EQ(entt::to_entity(other), 1u);
ASSERT_EQ(entt::to_version(other), 0u);
ASSERT_EQ(traits_type::construct(traits_type::to_entity(entity), traits_type::to_version(entity)), entity);
ASSERT_EQ(traits_type::construct(traits_type::to_entity(other), traits_type::to_version(other)), other);
ASSERT_NE(traits_type::construct(traits_type::to_entity(entity), {}), entity);
ASSERT_EQ(traits_type::construct(entt::to_entity(entity), entt::to_version(entity)), entity);
ASSERT_EQ(traits_type::construct(entt::to_entity(other), entt::to_version(other)), other);
ASSERT_NE(traits_type::construct(entt::to_entity(entity), {}), entity);
ASSERT_EQ(traits_type::construct(traits_type::to_entity(other), traits_type::to_version(entity)), traits_type::combine(traits_type::to_integral(other), traits_type::to_integral(entity)));
ASSERT_EQ(traits_type::construct(entt::to_entity(other), entt::to_version(entity)), traits_type::combine(entt::to_integral(other), entt::to_integral(entity)));
ASSERT_EQ(traits_type::combine(entt::tombstone, entt::null), tombstone);
ASSERT_EQ(traits_type::combine(entt::null, entt::tombstone), null);
@@ -44,8 +44,8 @@ TEST(Entity, Null) {
entt::registry registry{};
const auto entity = registry.create();
ASSERT_EQ(traits_type::combine(entt::null, traits_type::to_integral(entity)), (traits_type::construct(traits_type::to_entity(null), traits_type::to_version(entity))));
ASSERT_EQ(traits_type::combine(entt::null, traits_type::to_integral(null)), null);
ASSERT_EQ(traits_type::combine(entt::null, entt::to_integral(entity)), (traits_type::construct(entt::to_entity(null), entt::to_version(entity))));
ASSERT_EQ(traits_type::combine(entt::null, entt::to_integral(null)), null);
ASSERT_EQ(traits_type::combine(entt::null, entt::tombstone), null);
registry.emplace<int>(entity, 42);
@@ -73,8 +73,8 @@ TEST(Entity, Tombstone) {
entt::registry registry{};
const auto entity = registry.create();
ASSERT_EQ(traits_type::combine(traits_type::to_integral(entity), entt::tombstone), (traits_type::construct(traits_type::to_entity(entity), traits_type::to_version(tombstone))));
ASSERT_EQ(traits_type::combine(entt::tombstone, traits_type::to_integral(tombstone)), tombstone);
ASSERT_EQ(traits_type::combine(entt::to_integral(entity), entt::tombstone), (traits_type::construct(entt::to_entity(entity), entt::to_version(tombstone))));
ASSERT_EQ(traits_type::combine(entt::tombstone, entt::to_integral(tombstone)), tombstone);
ASSERT_EQ(traits_type::combine(entt::tombstone, entt::null), tombstone);
registry.emplace<int>(entity, 42);
@@ -85,8 +85,8 @@ TEST(Entity, Tombstone) {
ASSERT_TRUE(entity != entt::tombstone);
ASSERT_TRUE(entt::tombstone != entity);
const auto vers = traits_type::to_version(entt::tombstone);
const auto other = traits_type::construct(traits_type::to_entity(entity), vers);
constexpr auto vers = entt::to_version(tombstone);
const auto other = traits_type::construct(entt::to_entity(entity), vers);
ASSERT_FALSE(registry.valid(entt::tombstone));
ASSERT_NE(registry.destroy(entity, vers), vers);