#pragma once #include #include #include #include template> class ObjectPool { public: using wrapper_type = lambda_unique_ptr; ObjectPool(std::function createObject) : createObject(createObject) {} wrapper_type acquire() { std::lock_guard lock(poolMutex); if (pool.empty()) { pool.push(createObject()); } auto pointer = pool.top(); pool.pop(); return wrapper_type(pointer.get(), [this, pointer](value_type*) { std::lock_guard lock(poolMutex); this->pool.push(pointer); }); } bool empty() const { std::lock_guard lock(poolMutex); return pool.empty(); } size_t size() const { std::lock_guard lock(poolMutex); return pool.size(); } private: std::function createObject; std::stack> pool; mutable std::mutex poolMutex; };