#pragma once #include #include #include "RingBuffer.h" /** * A very specialized container. Made for holding one free * work buffers, and making sure they are destroyed. * * At construction time, objects are created to fill the pool. * pop the objects to get one, push back to return when done. * * Destructor will delete all the objects, even if they are not in the pool * at the time. * * Note that unlike RingBuffer, ManagePool manages T*, not T. * * All public functions are no-blocking, so may be called from the audio thread * without danger. Of course the constructor and destructor are exceptions - they may block. */ template class ManagedPool { public: ManagedPool(); /** Accessors from RingBuffer */ void push(T*); T* pop(); bool full() const; bool empty() const; private: /** * this ring buffer is where the raw T* are kept. * client pops and pushes here */ RingBuffer ringBuffer; std::vector< std::unique_ptr> lifetimeManager; }; template inline ManagedPool::ManagedPool() { // Manufacture the items here for (int i = 0; i < SIZE; ++i) { T * item = new T(); ringBuffer.push(item); // put the raw pointer in ring buffer for client to use. // Also add managed pointers to delete the objects on destroy. lifetimeManager.push_back(std::unique_ptr(item)); } } template inline void ManagedPool::push(T* value) { ringBuffer.push(value); } template inline T* ManagedPool::pop() { return ringBuffer.pop(); } template inline bool ManagedPool::full() const { return ringBuffer.full(); } template inline bool ManagedPool::empty() const { return ringBuffer.empty(); }