Browse Source

entity: added entt_traits<T>::combine

Michele Caini 4 years ago
parent
commit
8bbdd92739
2 changed files with 20 additions and 10 deletions
  1. 17 2
      src/entt/entity/entity.hpp
  2. 3 8
      test/entt/entity/entity.cpp

+ 17 - 2
src/entt/entity/entity.hpp

@@ -111,8 +111,8 @@ public:
      * @return The integral representation of the version part.
      */
     [[nodiscard]] static constexpr version_type to_version(const value_type value) ENTT_NOEXCEPT {
-        constexpr auto mask = (entity_traits::version_mask << entity_traits::entity_shift);
-        return ((to_integral(value) & mask) >> entity_traits::entity_shift);
+        constexpr auto version_mask = (entity_traits::version_mask << entity_traits::entity_shift);
+        return ((to_integral(value) & version_mask) >> entity_traits::entity_shift);
     }
 
     /**
@@ -128,6 +128,21 @@ public:
     [[nodiscard]] static constexpr value_type construct(const entity_type entity = entity_traits::entity_mask, const version_type version = entity_traits::version_mask) ENTT_NOEXCEPT {
         return value_type{(entity & entity_traits::entity_mask) | (static_cast<entity_type>(version) << entity_traits::entity_shift)};
     }
+
+    /**
+     * @brief Combines two identifiers in a single one.
+     *
+     * The returned identifier is a copy of the first element except for its
+     * version, which is taken from the second element.
+     *
+     * @param lhs The identifier from which to take the entity part.
+     * @param rhs The identifier from which to take the version part.
+     * @return A properly constructed identifier.
+     */
+    [[nodiscard]] static constexpr value_type combine(const value_type lhs, const value_type rhs) ENTT_NOEXCEPT {
+        constexpr auto version_mask = (entity_traits::version_mask << entity_traits::entity_shift);
+        return value_type{to_entity(lhs) | (to_integral(rhs) & version_mask)};
+    }
 };
 
 

+ 3 - 8
test/entt/entity/entity.cpp

@@ -23,13 +23,12 @@ TEST(Entity, Traits) {
 
     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_EQ(traits_type::construct(traits_type::to_entity(other), traits_type::to_version(entity)), traits_type::combine(other, entity));
     ASSERT_NE(traits_type::construct(traits_type::to_entity(entity), {}), entity);
 
-    ASSERT_EQ(traits_type::construct(), entt::tombstone | static_cast<entt::entity>(entt::null));
-    ASSERT_EQ(traits_type::construct(), entt::null | static_cast<entt::entity>(entt::tombstone));
+    ASSERT_EQ(traits_type::combine(entt::tombstone, entt::null), entt::tombstone | static_cast<entt::entity>(entt::null));
+    ASSERT_EQ(traits_type::combine(entt::null, entt::tombstone), entt::null | static_cast<entt::entity>(entt::tombstone));
 
-    ASSERT_EQ(traits_type::construct(), static_cast<entt::entity>(entt::null));
-    ASSERT_EQ(traits_type::construct(), static_cast<entt::entity>(entt::tombstone));
     ASSERT_EQ(traits_type::construct(), entt::entity{~entt::id_type{}});
 }
 
@@ -39,8 +38,6 @@ TEST(Entity, Null) {
     constexpr entt::entity null = entt::null;
 
     ASSERT_FALSE(entt::entity{} == entt::null);
-    ASSERT_TRUE(entt::entity{traits_type::construct()} == entt::null);
-
     ASSERT_TRUE(entt::null == entt::null);
     ASSERT_FALSE(entt::null != entt::null);
 
@@ -71,8 +68,6 @@ TEST(Entity, Tombstone) {
     constexpr entt::entity null = entt::null;
 
     ASSERT_FALSE(entt::entity{} == entt::tombstone);
-    ASSERT_TRUE(entt::entity{traits_type::construct()} == entt::tombstone);
-
     ASSERT_TRUE(entt::tombstone == entt::tombstone);
     ASSERT_FALSE(entt::tombstone != entt::tombstone);