blob: 38d3991da6019c720577a3a3be27a2df012fedac (
plain) (
blame)
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include <vespa/vespalib/util/alloc.h>
#include <vespa/vespalib/stllike/string.h>
namespace vespalib {
/**
Class that wraps a vector<char> and resizes it as necessary to hold data
put into it. Also has several utility functions commonly used in
data serialization.
All of the numeric helper methods (putInt/Long/Double etc.) in this class
store the numbers in network byte order.
*/
class GrowableByteBuffer
{
public:
/**
Creates a new GrowableByteBuffer with the given initial length and grow factor.
*/
GrowableByteBuffer(uint32_t initialLen = 256);
/**
If necessary, grows the buffer so it can hold the specified number of bytes,
then moves the position to after that length, and returns a pointer to the previous
position.
@param len The length to allocate
@return Returns a pointer to a buffer that the user can write data to.
*/
char* allocate(uint32_t len);
/**
Returns a pointer to the start of the allocated buffer.
*/
const char* getBuffer() const { return static_cast<const char *>(_buffer.get()); }
/**
Returns the current position.
*/
uint32_t position() const { return _position; }
/**
Adds the given buffer to this buffer.
*/
void putBytes(const void * buffer, uint32_t length);
/**
Adds a short to the buffer.
*/
void putShort(uint16_t v);
/**
Adds an int to the buffer.
*/
void putInt(uint32_t v);
/**
Adds a long to the buffer.
*/
void putLong(uint64_t v);
/**
Adds a double to the buffer.
*/
void putDouble(double v);
/**
Adds a string to the buffer (without nul-termination).
*/
void putString(vespalib::stringref v);
/**
* Adds a string to the buffer (including nul-termination).
* This matches com.yahoo.vespa.objects.Deserializer.getString.
*/
void put_c_string(vespalib::stringref v);
/**
Adds a single byte to the buffer.
*/
void putByte(uint8_t v);
/**
Adds a boolean to the buffer.
*/
void putBoolean(bool v);
private:
void putReverse(const char* buffer, uint32_t length);
using Alloc = vespalib::alloc::Alloc;
Alloc _buffer;
uint32_t _position;
};
}
|