Commit 4b39741c authored by Hugo Beauzee-Luyssen's avatar Hugo Beauzee-Luyssen

Refactored Pool class

parent f8eb6631
......@@ -28,25 +28,32 @@
#include "Singleton.hpp"
template <typename T>
class Pool : public Singleton< Pool<T> >
template <typename T, size_t NB_ELEM = 5>
class Pool : public Singleton< Pool<T, NB_ELEM> >
{
public:
T* get()
{
QMutexLocker lock( m_mutex );
if ( m_pool.size() == 0 )
{
return new T;
return m_pool.dequeue();
}
uint8_t* ptr = m_pool.dequeue();
T* ret = new (ptr) T;
return ret;
}
void release( T* toRelease )
{
QMutexLocker lock( m_mutex );
toRelease->~T();
m_pool.enqueue( toRelease );
}
private:
Pool()
{
for ( size_t i = 0; i < NB_ELEM; ++i )
m_pool.enqueue( new uint8_t[ sizeof(T) ] );
m_mutex = new QMutex;
}
~Pool()
......@@ -58,8 +65,8 @@ private:
}
delete m_mutex;
}
QQueue<T*> m_pool;
QMutex* m_mutex;
QQueue<uint8_t*> m_pool;
QMutex* m_mutex;
friend class Singleton< Pool<T> >;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment