Просмотр исходного кода

multi-component registry::reserve

Michele Caini 6 лет назад
Родитель
Сommit
d4b18b1e27
2 измененных файлов с 15 добавлено и 19 удалено
  1. 14 17
      src/entt/entity/registry.hpp
  2. 1 2
      test/entt/entity/registry.cpp

+ 14 - 17
src/entt/entity/registry.hpp

@@ -382,29 +382,26 @@ public:
     }
     }
 
 
     /**
     /**
-     * @brief Increases the capacity of the pool for the given component.
+     * @brief Increases the capacity of the registry or of the pools for the
+     * given components.
      *
      *
-     * If the new capacity is greater than the current capacity, new storage is
-     * allocated, otherwise the method does nothing.
+     * If no components are specified, the capacity of the registry is
+     * increased, that is the number of entities it contains. Otherwise the
+     * capacity of the pools for the given components is increased.
      *
      *
-     * @tparam Component Type of component for which to reserve storage.
-     * @param cap Desired capacity.
-     */
-    template<typename Component>
-    void reserve(const size_type cap) {
-        assure<Component>()->reserve(cap);
-    }
-
-    /**
-     * @brief Increases the capacity of a registry in terms of entities.
-     *
-     * If the new capacity is greater than the current capacity, new storage is
-     * allocated, otherwise the method does nothing.
+     * In both cases, if the new capacity is greater than the current capacity,
+     * new storage is allocated, otherwise the method does nothing.
      *
      *
+     * @tparam Component Types of components for which to reserve storage.
      * @param cap Desired capacity.
      * @param cap Desired capacity.
      */
      */
+    template<typename... Component>
     void reserve(const size_type cap) {
     void reserve(const size_type cap) {
-        entities.reserve(cap);
+        if constexpr(sizeof...(Component) == 0) {
+            entities.reserve(cap);
+        } else {
+            (assure<Component>()->reserve(cap), ...);
+        }
     }
     }
 
 
     /**
     /**

+ 1 - 2
test/entt/entity/registry.cpp

@@ -90,9 +90,8 @@ TEST(Registry, Functionalities) {
 
 
     ASSERT_EQ(registry.size(), entt::registry::size_type{0});
     ASSERT_EQ(registry.size(), entt::registry::size_type{0});
     ASSERT_EQ(registry.alive(), entt::registry::size_type{0});
     ASSERT_EQ(registry.alive(), entt::registry::size_type{0});
+    ASSERT_NO_THROW((registry.reserve<int, char>(8)));
     ASSERT_NO_THROW(registry.reserve(42));
     ASSERT_NO_THROW(registry.reserve(42));
-    ASSERT_NO_THROW(registry.reserve<int>(8));
-    ASSERT_NO_THROW(registry.reserve<char>(8));
     ASSERT_TRUE(registry.empty());
     ASSERT_TRUE(registry.empty());
 
 
     ASSERT_EQ(registry.capacity(), entt::registry::size_type{42});
     ASSERT_EQ(registry.capacity(), entt::registry::size_type{42});