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

registry: temporary changes to remove args from assure

Michele Caini 6 лет назад
Родитель
Сommit
a984ce5bba
2 измененных файлов с 24 добавлено и 18 удалено
  1. 1 2
      TODO
  2. 23 16
      src/entt/entity/registry.hpp

+ 1 - 2
TODO

@@ -22,8 +22,7 @@
 * meta:
   - type -> alias, resolve search by type id and alias
   - no longer detached unnamed types, all them are "searchable"
-* remove args... from registry::assure and the others (index would be duplicated)
-  - use prepare properly and do not forward to assure
+* review prepare after clone and the others have been removed
 
 * WIP:
  - deprecate clone, stamp, snapshot, loader, ...

+ 23 - 16
src/entt/entity/registry.hpp

@@ -191,8 +191,8 @@ class basic_registry {
         }
     };
 
-    template<typename Component, typename... Args>
-    const pool_handler<Component> & assure(Args &&... args) const {
+    template<typename Component>
+    const pool_handler<Component> & assure() const {
         static std::size_t index{pools.size()};
 
         if(const auto length = pools.size(); !(index < length) || pools[index].type_id != type_info<Component>::id()) {
@@ -203,7 +203,7 @@ class basic_registry {
 
                 pdata.type_id = type_info<Component>::id();
                 // use reset and new to avoid instantiating std::unique_ptr with pool_handler<Component>
-                pdata.pool.reset(new pool_handler<Component>(std::forward<Args>(args)...));
+                pdata.pool.reset(new pool_handler<Component>());
 
                 pdata.remove = [](sparse_set<entity_type> &cpool, basic_registry &owner, const entity_type entt) {
                     static_cast<pool_handler<Component> &>(cpool).remove(owner, entt);
@@ -211,7 +211,14 @@ class basic_registry {
 
                 if constexpr(std::is_copy_constructible_v<std::decay_t<Component>>) {
                     pdata.assure = [](basic_registry &other, const sparse_set<entity_type> &cpool) {
-                        other.assure<Component>(static_cast<const pool_handler<Component> &>(cpool));
+                        if constexpr(ENTT_ENABLE_ETO(Component)) {
+                            other.assure<Component>().assign(other, cpool.begin(), cpool.end());
+                        } else {
+                            other.assure<Component>().assign(other, cpool.begin(), cpool.end(), [&cpool](auto *instance) {
+                                const auto &source = static_cast<const pool_handler<Component> &>(cpool);
+                                std::copy(source.cbegin(), source.cend(), instance);
+                            });
+                        }
                     };
 
                     pdata.stamp = [](basic_registry &other, const entity_type dst, const sparse_set<entity_type> &cpool, const entity_type src) {
@@ -249,13 +256,11 @@ public:
     /**
      * @brief Prepares a pool for the given type if required.
      * @tparam Component Type of component for which to prepare a pool.
-     * @tparam Args Types of arguments to use to prepare the pool.
-     * @param args Parameters to use to initialize the pool.
      */
-    template<typename Component, typename... Args>
-    void prepare(Args &&... args) {
+    template<typename Component>
+    void prepare() {
         ENTT_ASSERT(std::none_of(pools.cbegin(), pools.cend(), [](auto &&pdata) { return pdata.type_id == type_info<Component>::id(); }));
-        assure<Component>(std::forward<Args>(args)...);
+        assure<Component>();
     }
 
     /**
@@ -1508,16 +1513,18 @@ public:
         other.destroyed = destroyed;
         other.entities = entities;
 
-        if constexpr(sizeof...(Component) == 0) {
-            std::for_each(pools.cbegin(), pools.cend(), [&other](auto &&pdata) {
+        std::for_each(pools.cbegin(), pools.cend(), [&other](auto &&pdata) {
+            if constexpr(sizeof...(Component) == 0) {
                 if(pdata.assure && ((pdata.type_id != type_info<Exclude>::id()) && ...)) {
                     pdata.assure(other, *pdata.pool);
                 }
-            });
-        } else {
-            static_assert(sizeof...(Exclude) == 0 && std::conjunction_v<std::is_copy_constructible<Component>...>);
-            (other.assure<Component>(assure<Component>()), ...);
-        }
+            } else {
+                static_assert(sizeof...(Exclude) == 0 && std::conjunction_v<std::is_copy_constructible<Component>...>);
+                if(pdata.assure && ((pdata.type_id == type_info<Component>::id()) || ...)) {
+                    pdata.assure(other, *pdata.pool);
+                }
+            }
+        });
 
         return other;
     }