MCPcopy Create free account
hub / github.com/9chu/LuaSTGPlus / FixedObjectPool

Class FixedObjectPool

LuaSTGPlus/ObjectPool.hpp:8–72  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

6 /// @brief ���������
7 template <typename T, size_t AllocCount>
8 class FixedObjectPool
9 {
10 private:
11 static_assert(std::is_pod<typename T>::value, "T must be a pod type.");
12
13 std::vector<size_t> m_FreeIndex; // ���пռ�������
14 std::array<bool, AllocCount> m_DataUsed; // ���ÿռ���
15 std::array<T, AllocCount> m_DataBuffer; // ���пռ�
16 public:
17 /// @brief ����һ������
18 /// @param[out] id ����id
19 /// @return �Ƿ�ɹ���ʧ�ܷ���false�������������
20 bool Alloc(size_t& id)
21 {
22 if (m_FreeIndex.size() > 0)
23 {
24 id = m_FreeIndex.back();
25 m_FreeIndex.pop_back();
26 m_DataUsed[id] = true;
27 return true;
28 }
29 id = static_cast<size_t>(-1);
30 return false;
31 }
32 /// @brief ����һ������
33 void Free(size_t id)
34 {
35 if (id < AllocCount && m_DataUsed[id])
36 {
37 m_DataUsed[id] = false;
38 m_FreeIndex.push_back(id);
39 }
40 }
41 /// @brief ��ȡ���������
42 /// @return ��id������nullptr
43 T* Data(size_t id)
44 {
45 if (id < AllocCount && m_DataUsed[id])
46 return &m_DataBuffer[id];
47 return nullptr;
48 }
49 /// @brief �����ѷ��������
50 size_t Size()
51 {
52 return m_DataBuffer.size() - m_FreeIndex.size();
53 }
54 /// @brief ��ն���ز��������ж���
55 void Clear()
56 {
57 m_FreeIndex.resize(m_DataBuffer.size());
58 for (size_t i = 0; i < m_DataBuffer.size(); ++i)
59 {
60 m_FreeIndex[i] = (m_DataBuffer.size() - 1) - i;
61 m_DataUsed[i] = false;
62 }
63 }
64 private:
65 FixedObjectPool& operator=(const FixedObjectPool&);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected