9 namespace DataStructures
12 Pool<T>::Pool(
int totalCapacity, PoolItemFactory<T> *pInstanceFactory)
13 :m_pInstanceFactory(pInstanceFactory),
18 PoolEntry<T>* pCurrent = NULL;
20 m_entries.reserve(totalCapacity);
22 for (
int i = 0; i < totalCapacity; ++i)
25 m_entries.push_back(entry);
26 pCurrent = &(m_entries[i]);
28 pCurrent->allocated =
false;
29 pCurrent->counter = 1;
30 pCurrent->instance = m_pInstanceFactory->CreateItem();
32 m_freeIndices.push(i);
40 for (
size_t i = 0; i < m_entries.size(); ++i)
42 PoolEntry<T>* pCurrent = &(m_entries[i]);
43 Eegeo_DELETE pCurrent->instance;
51 PoolHandle Pool<T>::Allocate()
53 PoolEntry<T>* pCurrent = NULL;
57 if (m_freeIndices.empty())
60 m_entries.push_back(entry);
62 pCurrent = &m_entries.back();
63 pCurrent->counter = 0;
64 pCurrent->instance = m_pInstanceFactory->CreateItem();
66 index = m_entries.size() - 1;
70 index = m_freeIndices.top();
73 pCurrent = &m_entries[index];
79 pCurrent->allocated =
true;
81 return PoolHandle(pCurrent->counter, index);
86 void Pool<T>::Release(PoolHandle handle)
88 u32 index = handle.GetIndex();
90 PoolEntry<T>* pCurrent = &(m_entries[index]);
91 pCurrent->allocated =
false;
94 m_freeIndices.push(index);
100 T Pool<T>::Resolve(PoolHandle handle)
102 u32 index = handle.GetIndex();
104 Eegeo_ASSERT(m_entries[index].counter == handle.GetCounter(),
"Failed to resolve handle");
106 T pInstance = m_entries[index].instance;