/* * Pool.hpp * * Created on: 12 δεκ. 2019 γ. * Author: LeonidTitov */ #ifndef LIB_COMMON_POOL_HPP_ #define LIB_COMMON_POOL_HPP_ #include #include namespace common { template struct PoolInterface { virtual Item * get() = 0; virtual void free( Item * & ) = 0; virtual ~PoolInterface() = default; }; template struct Pool : public PoolInterface { static const std::size_t size = quantity; typedef Item ItemType; Item * get(); void free( Item * & ); virtual ~Pool() noexcept {} private: Item pool[size]; typedef std::bitset Infos; Infos info; }; } template inline Item * common::Pool::get() { for( std::size_t index = 0; index < size; ++index ) if( not info.test(index) ) { info.set(index); return &pool[index]; } return nullptr; } template inline void common::Pool::free( Item * & item ) { std::size_t index = item - pool; if( info.test(index) ) { item = nullptr; info.reset(index); } } #endif /* LIB_COMMON_POOL_HPP_ */