]> git.tdb.fi Git - libs/game.git/commitdiff
Chain certain pool operations to pools of derived objects
authorMikko Rasa <tdb@tdb.fi>
Sun, 7 May 2023 17:32:02 +0000 (20:32 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 7 May 2023 17:32:02 +0000 (20:32 +0300)
source/game/pool.cpp
source/game/pool.h

index 174a939b9a8c1da16a25a757f9f4ef2525d91b7c..a986302e3433c6157772b0ca5c5ae206db12e704 100644 (file)
@@ -138,6 +138,13 @@ void PoolBase::add_block()
 }
 
 void PoolBase::destroy(void *obj)
+{
+       if(_destroy(obj))
+               throw invalid_argument("PoolBase::destroy");
+}
+
+
+int PoolBase::_destroy(void *obj)
 {
        unsigned block_index = 0;
        unsigned object_index = BLOCK_SIZE;
@@ -156,17 +163,27 @@ void PoolBase::destroy(void *obj)
        }
 
        if(object_index>=BLOCK_SIZE)
-               throw invalid_argument("PoolBase::destroy");
+       {
+               for(PoolBase *p: derived_pools)
+               {
+                       int result = p->_destroy(obj);
+                       if(result!=1)
+                               return result;
+               }
+               return 1;
+       }
 
        FlagType *flags = reinterpret_cast<FlagType *>(blocks[block_index]+BLOCK_SIZE*object_size);
        FlagType bit = 1<<(object_index%FLAG_BITS);
        if(!(flags[object_index/FLAG_BITS]&bit))
-               throw invalid_argument("PoolBase::destroy");
+               return 2;
 
        deleter(obj);
        flags[object_index/FLAG_BITS] &= ~bit;
        --object_count;
        free_list[object_count] = block_index*BLOCK_SIZE+object_index;
+
+       return 0;
 }
 
 } // namespace Msp::Game
index dde179defd3ebbad07bd1a3f17a2debe6827ecc3..6357b86bcf5fad9895d2878703f29ab37263aa4c 100644 (file)
@@ -85,6 +85,9 @@ private:
 public:
        std::uint32_t get_capacity() const { return capacity; }
        void destroy(void *);
+
+private:
+       int _destroy(void *);
 };
 
 
@@ -153,6 +156,9 @@ inline void PoolBase::iterate_objects(const F &func)
                        ++flags;
                }
        }
+
+       for(PoolBase *p: derived_pools)
+               p->iterate_objects(func);
 }