Browse Source

safer registry::prepare

Michele Caini 6 years ago
parent
commit
b31afe5c8c
2 changed files with 8 additions and 2 deletions
  1. 0 1
      TODO
  2. 8 1
      src/entt/entity/registry.hpp

+ 0 - 1
TODO

@@ -35,4 +35,3 @@
 * named types: almost-stable index optimization for direct access to pools, no more linear searches
 * named types: almost-stable index optimization for direct access to pools, no more linear searches
   - can implicitly generate types for meta benefit from a similar approach?
   - can implicitly generate types for meta benefit from a similar approach?
 * multi component registry::remove and some others?
 * multi component registry::remove and some others?
-* registry::prepare is unsafe

+ 8 - 1
src/entt/entity/registry.hpp

@@ -253,6 +253,11 @@ class basic_registry {
             pdata = &pools[ctype];
             pdata = &pools[ctype];
         }
         }
 
 
+        if constexpr(sizeof...(Args) != 0) {
+            ENTT_ASSERT(!pdata->pool || !pdata->pool->size());
+            pdata->pool.reset();
+        }
+
         if(!pdata->pool) {
         if(!pdata->pool) {
             pdata->runtime_type = ctype;
             pdata->runtime_type = ctype;
             pdata->pool = std::make_unique<pool_type<Component>>(std::forward<Args>(args)...);
             pdata->pool = std::make_unique<pool_type<Component>>(std::forward<Args>(args)...);
@@ -327,7 +332,9 @@ public:
      */
      */
     template<typename Component, typename... Args>
     template<typename Component, typename... Args>
     void prepare(Args &&... args) {
     void prepare(Args &&... args) {
-        assure<Component>(std::forward<Args>(args)...);
+        ENTT_ASSERT(std::none_of(pools.cbegin(), pools.cend(), [ctype = to_integer(type<Component>())](auto &&pdata) {return pdata.runtime_type == ctype; }));
+        [[maybe_unused]] auto *cpool = assure<Component>(std::forward<Args>(args)...);
+        ENTT_ASSERT(cpool->size() == 0);
     }
     }
 
 
     /**
     /**