-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynBuf.h
73 lines (65 loc) · 1.19 KB
/
DynBuf.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
#ifndef _DYNBUF_H
#define _DYNBUF_H
#include <vector>
/*
A basic dynamic buffer, exception free.
*/
class DynBuf
{
public:
DynBuf(size_t sz = 0)
{
Allocate(sz);
}
typedef std::vector<char> DynBufVec;
void* Allocate(size_t sz)
{
void* r = NULL;
try
{
if(Size() < sz)
mem.resize(sz);
if(Size())
r = GetPtr();
if(r && sz)
memset(r, 0, sz);
}
catch(...)
{
}
return r;
}
void* GetPtr()
{
if(Size())
return &mem.front(); //in c++11: .data()
return NULL;
}
void Free()
{
mem.clear();
}
DynBufVec & GetVector()
{
return mem;
}
const DynBufVec & GetVector() const
{
return mem;
}
size_t Size() const
{
return mem.size();
}
protected:
char & operator[](std::size_t idx)
{
return mem[idx];
};
const char & operator[](std::size_t idx) const
{
return mem[idx];
};
DynBufVec mem;
};
#endif //_DYNBUF_H