-
Notifications
You must be signed in to change notification settings - Fork 50
/
BufferManager.hpp
68 lines (61 loc) · 1.17 KB
/
BufferManager.hpp
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
#ifndef _MOURISL_BUFFER_MANAGER
#define _MOURISL_BUFFER_MANAGER
#include <stdio.h>
// Management of multiple buffers
// The main feature is to easily adjust size
template <class T>
class BufferManager
{
private:
T **_buffers ;
size_t *_bufferSize ;
int _bufferCnt ;
public:
BufferManager()
{
_buffers = NULL ;
_bufferSize = NULL ;
_bufferCnt = 0 ;
}
~BufferManager()
{
Free() ;
}
int GetBufferCount()
{
return _bufferCnt ;
}
void Free()
{
int i ;
for (i = 0 ; i < _bufferCnt ; ++i)
delete[] _buffers[i] ;
delete[] _buffers ;
delete[] _bufferSize ;
}
void Init(int cnt)
{
Free() ;
_bufferCnt = cnt ;
_buffers = new T*[cnt] ;
_bufferSize = new size_t[cnt] ;
for (int i = 0 ; i < cnt ; ++i)
{
const int defaultSize = 256 ;
_buffers[i] = new T[defaultSize] ;
_bufferSize[i] = defaultSize ;
}
}
// Get the ith buffer, planning use it to hold size elements
T *Get(int i, size_t size)
{
if (size > _bufferSize[i])
{
delete[] _buffers[i] ;
_buffers[i] = new T[size] ;
_bufferSize[i] = size ;
}
return _buffers[i] ;
}
} ;
#endif