瀏覽代碼

entity: added basic_entt_traits::next (with tests)

Michele Caini 3 年之前
父節點
當前提交
87987bacde
共有 2 個文件被更改,包括 16 次插入0 次删除
  1. 10 0
      src/entt/entity/entity.hpp
  2. 6 0
      test/entt/entity/entity.cpp

+ 10 - 0
src/entt/entity/entity.hpp

@@ -117,6 +117,16 @@ public:
         return (to_integral(value) >> length);
     }
 
+    /**
+     * @brief Returns the successor of a given identifier.
+     * @param value The identifier of which to return the successor.
+     * @return The successor of the given identifier.
+     */
+    [[nodiscard]] static constexpr value_type next(const value_type value) noexcept {
+        const auto vers = to_version(value) + 1;
+        return construct(to_entity(value), static_cast<version_type>(vers + (vers == version_mask)));
+    }
+
     /**
      * @brief Constructs an identifier from its parts.
      *

+ 6 - 0
test/entt/entity/entity.cpp

@@ -29,6 +29,12 @@ TEST(Entity, Traits) {
 
     ASSERT_EQ(traits_type::combine(entt::tombstone, entt::null), tombstone);
     ASSERT_EQ(traits_type::combine(entt::null, entt::tombstone), null);
+
+    ASSERT_EQ(traits_type::next(entity), traits_type::construct(entt::to_integral(entity), entt::to_version(entity) + 1u));
+    ASSERT_EQ(traits_type::next(other), traits_type::construct(entt::to_integral(other), entt::to_version(other) + 1u));
+
+    ASSERT_EQ(traits_type::next(entt::tombstone), traits_type::construct(entt::null, {}));
+    ASSERT_EQ(traits_type::next(entt::null), traits_type::construct(entt::null, {}));
 }
 
 TEST(Entity, Null) {