Michele Caini 6 лет назад
Родитель
Сommit
b3be6c75c3
2 измененных файлов с 15 добавлено и 30 удалено
  1. 3 3
      src/entt/entity/registry.hpp
  2. 12 27
      src/entt/entity/storage.hpp

+ 3 - 3
src/entt/entity/registry.hpp

@@ -81,9 +81,9 @@ class basic_registry {
             }
         }
 
-        template<typename It, typename... Comp>
-        auto batch(basic_registry &registry, It first, It last, const Comp &... value) {
-            auto it = storage<Entity, Component>::batch(first, last, value...);
+        template<typename It, typename... Args>
+        auto batch(basic_registry &registry, It first, It last, Args &&... args) {
+            auto it = storage<Entity, Component>::batch(first, last, std::forward<Args>(args)...);
 
             if(!construction.empty()) {
                 std::for_each(first, last, [this, &registry, it](const auto entt) mutable {

+ 12 - 27
src/entt/entity/storage.hpp

@@ -333,9 +333,10 @@ public:
 
     /**
      * @brief Assigns one or more entities to a storage and default constructs
-     * their objects.
+     * or copy constructs their objects.
      *
-     * The object type must be at least move and default insertable.
+     * The object type must be at least move and default insertable if no
+     * arguments are provided, move and copy insertable otherwise.
      *
      * @warning
      * Attempting to assign an entity that already belongs to the storage
@@ -344,37 +345,21 @@ public:
      * storage already contains the given entity.
      *
      * @tparam It Type of forward iterator.
+     * @tparam Args Types of arguments to use to construct the object.
      * @param first An iterator to the first element of the range of entities.
      * @param last An iterator past the last element of the range of entities.
+     * @param args Parameters to use to construct an object for the entities.
      * @return An iterator to the list of instances just created and sorted the
      * same of the entities.
      */
-    template<typename It>
-    iterator_type batch(It first, It last) {
-        instances.resize(instances.size() + std::distance(first, last));
-        // entity goes after component in case constructor throws
-        underlying_type::batch(first, last);
-        return begin();
-    }
+    template<typename It, typename... Args>
+    iterator_type batch(It first, It last, Args &&... args) {
+        if constexpr(sizeof...(Args) == 0) {
+            instances.resize(instances.size() + std::distance(first, last));
+        } else {
+            instances.resize(instances.size() + std::distance(first, last), Type{std::forward<Args>(args)...});
+        }
 
-    /**
-     * @brief Assigns one or more entities to a storage and copy constructs
-     * their objects.
-     *
-     * The object type must be at least move and copy insertable.
-     *
-     * @sa batch
-     *
-     * @tparam It Type of forward iterator.
-     * @param first An iterator to the first element of the range of entities.
-     * @param last An iterator past the last element of the range of entities.
-     * @param value The value to initialize the new objects with.
-     * @return An iterator to the list of instances just created and sorted the
-     * same of the entities.
-     */
-    template<typename It>
-    iterator_type batch(It first, It last, const object_type &value) {
-        instances.resize(instances.size() + std::distance(first, last), value);
         // entity goes after component in case constructor throws
         underlying_type::batch(first, last);
         return begin();