-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbuffer.h
167 lines (125 loc) · 3.3 KB
/
buffer.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#pragma once
#include <cstdint>
#include <cassert>
#include <sys/socket.h> // for iovec
class Buffer {
private:
uint8_t* _base = nullptr;
size_t _size = 0;
size_t _position = 0;
protected:
uint8_t* allocate(size_t n);
uint8_t& element(size_t x) const;
Buffer(uint8_t* base, size_t size);
public:
size_t size() const;
size_t position() const;
size_t available() const;
void reset();
};
class ReadBuffer : public Buffer {
public:
ReadBuffer(const uint8_t* base, size_t size);
const void* base() const;
template<typename T> const T& read();
template<typename T> const T* read(size_t n);
const uint8_t& operator[](size_t x) const;
operator iovec() const;
};
class WriteBuffer : public Buffer {
public:
WriteBuffer(uint8_t* base, size_t size);
void* base() const;
template<typename T> T& reserve();
template<typename T> T* reserve(size_t n);
template<typename T> T& write(const T& v);
uint8_t& operator[](size_t x) const;
operator iovec() const;
};
//---------------------------------------------------------------------
inline Buffer::Buffer(uint8_t* base, size_t size) : _base(base), _size(size)
{
assert(base);
}
inline ReadBuffer::ReadBuffer(const uint8_t* base, size_t size)
: Buffer(const_cast<uint8_t*>(base), size)
{
}
inline WriteBuffer::WriteBuffer(uint8_t* base, size_t size)
: Buffer(base, size)
{
}
//---------------------------------------------------------------------
inline void Buffer::reset() {
_position = 0;
}
inline size_t Buffer::size() const {
return _size;
}
inline size_t Buffer::position() const {
return _position;
}
inline size_t Buffer::available() const {
assert(_size >= _position);
return _size - _position;
}
inline uint8_t* Buffer::allocate(size_t n) {
assert(_base);
assert(available() >= n);
auto* p = _base + _position;
_position += n;
return p;
}
//---------------------------------------------------------------------
inline const void* ReadBuffer::base() const {
return &element(0);
}
inline void* WriteBuffer::base() const {
return &element(0);
}
//---------------------------------------------------------------------
inline uint8_t& Buffer::element(size_t x) const {
assert(_base);
assert(x < _size);
return _base[x];
}
inline const uint8_t& ReadBuffer::operator[](size_t x) const {
return element(x);
}
inline uint8_t& WriteBuffer::operator[](size_t x) const {
return element(x);
}
//---------------------------------------------------------------------
template<typename T>
const T* ReadBuffer::read(size_t n) {
return reinterpret_cast<const T*>(allocate(n * sizeof(T)));
}
template<typename T>
const T& ReadBuffer::read() {
return *read<T>(1);
}
//---------------------------------------------------------------------
template<typename T>
T* WriteBuffer::reserve(size_t n) {
return reinterpret_cast<T*>(allocate(n * sizeof(T)));
}
template<typename T>
T& WriteBuffer::reserve() {
return *reserve<T>(1);
}
template<typename T>
T& WriteBuffer::write(const T& v) {
auto* p = reserve<T>(1);
*p = v;
return *p;
}
//---------------------------------------------------------------------
inline ReadBuffer::operator iovec() const
{
return iovec { const_cast<void *>(base()), size() };
}
inline WriteBuffer::operator iovec() const
{
return iovec { base(), position() };
}
//---------------------------------------------------------------------