-
Notifications
You must be signed in to change notification settings - Fork 0
/
CBuffer.h
82 lines (68 loc) · 1.25 KB
/
CBuffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#pragma once
#include <list>
#include "../../IStream.h"
#include "../../../Common/MyCom.h"
class IPipeBuffer :
public IInStream,
public IOutStream,
public IOutStreamFlush,
public IStreamGetSize,
public CMyUnknownImp
{
public:
MY_UNKNOWN_IMP3(IInStream,
IOutStream, IStreamGetSize);
virtual ~IPipeBuffer(){}
virtual void wait_for_read()=0;
virtual void wait_for_write()=0;
virtual void barrier()=0;
virtual IPipeBuffer* clone()=0;
};
IPipeBuffer* new_pipe_buffer();
template<class T>
class CObjectPool
{
protected:
typedef std::list<T*> List;
typedef typename List::iterator itertor;
List m_list;
int m_reserve;
int check_free()
{
int free=0;
while (m_list.size() > m_reserve)
{
free++;
delete m_list.front();
m_list.pop_front();
}
return free;
}
public:
CObjectPool(int max_reservation=100)
{
m_reserve = max_reservation;
}
~CObjectPool()
{
for (iterator it=m_list.begin();
it!=m_list.end(); ++it)
delete *it;
}
T* get()
{
T* o;
if (!m_list.empty())
{
=m_list.front();
m_list.pop_front();
}
else o=new T;
return o;
}
void recycle(T* o)
{
if (check_free()>0) delete o;
else m_list.push_back(o);
}
};