All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
PoolImpl.h
1 // Copyright eeGeo Ltd (2012-2014), All Rights Reserved
2 
3 #pragma once
4 
5 #include "Pool.h"
6 
7 namespace Eegeo
8 {
9  namespace DataStructures
10  {
11  template <typename T>
12  Pool<T>::Pool(int totalCapacity, PoolItemFactory<T> *pInstanceFactory)
13  :m_pInstanceFactory(pInstanceFactory),
14  m_count(0)
15  {
16  if(totalCapacity > 0)
17  {
18  PoolEntry<T>* pCurrent = NULL;
19 
20  m_entries.reserve(totalCapacity);
21 
22  for (int i = 0; i < totalCapacity; ++i)
23  {
24  PoolEntry<T> entry;
25  m_entries.push_back(entry);
26  pCurrent = &(m_entries[i]);
27 
28  pCurrent->allocated = false;
29  pCurrent->counter = 1;
30  pCurrent->instance = m_pInstanceFactory->CreateItem();
31 
32  m_freeIndices.push(i);
33  }
34  }
35  }
36 
37  template <typename T>
38  Pool<T>::~Pool()
39  {
40  for (size_t i = 0; i < m_entries.size(); ++i)
41  {
42  PoolEntry<T>* pCurrent = &(m_entries[i]);
43  Eegeo_DELETE pCurrent->instance;
44  }
45 
46  m_entries.clear();
47  }
48 
49 
50  template <typename T>
51  PoolHandle Pool<T>::Allocate()
52  {
53  PoolEntry<T>* pCurrent = NULL;
54 
55  u16 index = 0;
56 
57  if (m_freeIndices.empty())
58  {
59  PoolEntry<T> entry;
60  m_entries.push_back(entry);
61 
62  pCurrent = &m_entries.back();
63  pCurrent->counter = 0;
64  pCurrent->instance = m_pInstanceFactory->CreateItem();
65 
66  index = m_entries.size() - 1;
67  }
68  else
69  {
70  index = m_freeIndices.top();
71  m_freeIndices.pop();
72 
73  pCurrent = &m_entries[index];
74  }
75 
76  ++pCurrent->counter;
77  ++m_count;
78 
79  pCurrent->allocated = true;
80 
81  return PoolHandle(pCurrent->counter, index);
82  }
83 
84 
85  template <typename T>
86  void Pool<T>::Release(PoolHandle handle)
87  {
88  u32 index = handle.GetIndex();
89 
90  PoolEntry<T>* pCurrent = &(m_entries[index]);
91  pCurrent->allocated = false;
92  ++pCurrent->counter;
93 
94  m_freeIndices.push(index);
95 
96  --m_count;
97  }
98 
99  template <typename T>
100  T Pool<T>::Resolve(PoolHandle handle)
101  {
102  u32 index = handle.GetIndex();
103 
104  Eegeo_ASSERT(m_entries[index].counter == handle.GetCounter(), "Failed to resolve handle");
105 
106  T pInstance = m_entries[index].instance;
107  return pInstance;
108  }
109  }
110 }