forked from eBay/NuRaft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.hxx
261 lines (226 loc) · 6.62 KB
/
buffer.hxx
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/************************************************************************
Modifications Copyright 2017-2019 eBay Inc.
Author/Developer(s): Jung-Sang Ahn
Original Copyright:
See URL: https://github.com/datatechnology/cornerstone
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
**************************************************************************/
#ifndef _BUFFER_HXX_
#define _BUFFER_HXX_
#include "basic_types.hxx"
#include "pp_util.hxx"
#include "ptr.hxx"
#include <string>
namespace nuraft {
class buffer {
buffer() = delete;
__nocopy__(buffer);
public:
/**
* Allocate a new memory buffer with given size.
*
* @param size Size of memory to allocate.
* @return buffer instance.
*/
static ptr<buffer> alloc(const size_t size);
/**
* Copy the data in the given buffer starting from the current position.
* It will allocate a new memory buffer.
*
* WARNING: It will move the position of the given `buf`.
*
* @param buf Buffer to copy.
* @return buffer instance.
*/
static ptr<buffer> copy(const buffer& buf);
/**
* Clone the given buffer, starting from the beginning.
* It will allocate a new memory buffer.
*
* WARNING: It WILL NOT move the position of given `buf`.
*
* @param buf Buffer to copy.
* @return buffer instance.
*/
static ptr<buffer> clone(const buffer& buf);
/**
* Get total size of entire buffer container, including meta section.
*
* @return Size of container.
*/
size_t container_size() const;
/**
* Get the size that this buffer can contain (i.e., user data size).
*
* @return Size of buffer capacity.
*/
size_t size() const;
/**
* Get the current position of cursor inside buffer.
*
* @return Position of cursor.
*/
size_t pos() const;
/**
* Set the position of cursor.
*
* @param p New position.
*/
void pos(size_t p);
/**
* Get the memory pointer to the current position.
*
* @return Pointer to the current position.
*/
byte* data() const;
/**
* Get the memory pointer to the beginning of the data,
* regardless of the current position.
*
* @return Pointer to the begining of the data.
*/
byte* data_begin() const;
/*********************************************************
*
* NOTE:
* We don't recommend using below get/put APIs, as they
* modify the position field embedded in `buffer` instance itself,
* which prevents concurrent reads to the same `buffer`.
* It is also mistake-prone if someone moves the position
* and forgets to reset it, since moved position remains with the
* buffer.
*
* Instead, you can use `buffer_serializer`.
*
*********************************************************/
/**
* Get 4-byte signed integer.
*
* @return 4-byte singed integer.
*/
int32 get_int();
/**
* Get 8-byte unsigned integer.
*
* @return 8-byte unsigned integer.
*/
ulong get_ulong();
/**
* Get 1-byte unsigned integer.
*
* @return 1-byte unsigned integer.
*/
byte get_byte();
/**
* Read 4-byte length followed by byte array, and then return them.
* It will NOT allocate a new memory, but return the
* reference to the memory inside buffer only.
*
* @param[out] len Size of returned byte array.
* @return Pointer to the starting point of byte array.
*/
const byte* get_bytes(size_t& len);
/**
* Read byte array of given buffer's size,
* and copy it to the given buffer.
*
* @param dst Buffer where the data will be stored.
*/
void get(ptr<buffer>& dst);
/**
* Read C-style string (null terminated).
* It will NOT allocate a new memory, but return the
* reference to the memory inside buffer only.
*
* @return C-style string.
*/
const char* get_str();
/**
* Read byte array of given size.
* It will NOT allocate a new memory, but return the
* reference to the memory inside buffer only.
*
* @param len Size to read.
* @return Pointer to the starting point of byte array.
*/
byte* get_raw(size_t len);
/**
* Put an 1-byte unsigned integer.
*
* @param b 1-byte unsigned integer.
*/
void put(byte b);
/**
* Put a byte array.
* This function will put 4-byte length first, and then
* the actual byte array next.
*
* @param ba Pointer to the byte array.
* @param len Length of the byte array.
*/
void put(const char* ba, size_t len);
void put(const byte* ba, size_t len);
/**
* Put a 4-byte signed integer.
*
* @param val 4-byte signed integer.
*/
void put(int32 val);
/**
* Put an 8-byte unsigned integer.
*
* @param val 8-byte unsigned integer.
*/
void put(ulong val);
/**
* Put a C-style string.
*
* WARNING:
* Given string SHOULD NOT contain any NULL character
* in the middle.
*
* If it contains, please use `put(const byte*, size_t)`.
*
* @param str String.
*/
void put(const std::string& str);
/**
* Put given buffer.
*
* WARNING:
* It does not put length info of given buffer,
* so caller should know the length of buffer in advance
* before calling `get(ptr<buffer>)`.
*
* If not, please use `put(const byte*, size_t)`.
*
* @param buf Buffer.
*/
void put(const buffer& buf);
/**
* Put a byte array.
*
* WARNING:
* It does not put the given length info,
* so caller should know the length of byte array in advance
* before calling `get_raw(size_t)`.
*
* If not, please use `put(const byte*, size_t)`.
*
* @param ba Pointer to the byte array.
* @param len Length of the byte array.
*/
void put_raw(const byte* ba, size_t len);
};
std::ostream& operator << (std::ostream& out, buffer& buf);
std::istream& operator >> (std::istream& in, buffer& buf);
}
#endif //_BUFFER_HXX_