|
1
|
1 #pragma once
|
|
|
2 #include "synchro.h"
|
|
|
3 #include <memory>
|
|
|
4 #include <list>
|
|
|
5
|
|
|
6 namespace pfc {
|
|
|
7 template<typename obj_t>
|
|
|
8 class objPool {
|
|
|
9 public:
|
|
|
10 objPool() : m_maxCount(pfc_infinite) {}
|
|
|
11 typedef std::shared_ptr<obj_t> objRef_t;
|
|
|
12
|
|
|
13 objRef_t get() {
|
|
|
14 insync(m_sync);
|
|
|
15 auto i = m_pool.begin();
|
|
|
16 if ( i == m_pool.end() ) return nullptr;
|
|
|
17 auto ret = *i;
|
|
|
18 m_pool.erase(i);
|
|
|
19 return ret;
|
|
|
20 }
|
|
|
21 objRef_t make() {
|
|
|
22 auto obj = get();
|
|
|
23 if ( ! obj ) obj = std::make_shared<obj_t>();
|
|
|
24 return obj;
|
|
|
25 }
|
|
|
26 void setMaxCount(size_t c) {
|
|
|
27 insync(m_sync);
|
|
|
28 m_maxCount = c;
|
|
|
29 }
|
|
|
30 void put(objRef_t obj) {
|
|
|
31 insync(m_sync);
|
|
|
32 if ( m_pool.size() < m_maxCount ) {
|
|
|
33 m_pool.push_back(obj);
|
|
|
34 }
|
|
|
35 }
|
|
|
36 private:
|
|
|
37 size_t m_maxCount;
|
|
|
38 std::list<objRef_t> m_pool;
|
|
|
39 critical_section m_sync;
|
|
|
40 };
|
|
|
41
|
|
|
42 } |