Skip to content

Commit d57328e

Browse files
committed
Introduce supervised buffer template, must fix Builder.cpp to accomodate it
1 parent 8f0fc65 commit d57328e

File tree

3 files changed

+90
-71
lines changed

3 files changed

+90
-71
lines changed

include/velocypack/Buffer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class Buffer {
194194

195195
// Steal external memory; only allowed when the buffer is not local,
196196
// i.e. !usesLocalMemory()
197-
T* steal() noexcept {
197+
virtual T* steal() noexcept {
198198
VELOCYPACK_ASSERT(!usesLocalMemory());
199199

200200
auto buffer = _buffer;
@@ -284,7 +284,7 @@ class Buffer {
284284
inline void poison(T*, ValueLength) noexcept {}
285285
#endif
286286

287-
void grow(ValueLength len) {
287+
virtual void grow(ValueLength len) {
288288
VELOCYPACK_ASSERT(_size + len >= sizeof(_local));
289289

290290
// need reallocation

include/velocypack/Builder.h

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ namespace arangodb::velocypack {
5050
class ArrayIterator;
5151
class ObjectIterator;
5252

53+
template<typename BufferType = Buffer<uint8_t>>
5354
class Builder {
5455
friend class Parser; // The parser needs access to internals.
5556

@@ -74,12 +75,12 @@ class Builder {
7475
// buffer. Whenever the stack is empty, one can use the start,
7576
// size and slice methods to get out the ready built VPack
7677
// object(s).
78+
public:
79+
using buffer_type = BufferType;
7780

7881
private:
79-
// Here we collect the result
80-
std::shared_ptr<Buffer<uint8_t>> _buffer;
81-
// used for quicker access than shared_ptr
82-
Buffer<uint8_t>* _bufferPtr;
82+
std::shared_ptr<BufferType> _buffer;
83+
BufferType* _bufferPtr;
8384
// Always points to the start of _buffer
8485
uint8_t* _start;
8586
// the append position
@@ -114,19 +115,19 @@ class Builder {
114115
explicit Builder(Options const* options);
115116

116117
// create an empty Builder, using an existing buffer and default Options
117-
explicit Builder(std::shared_ptr<Buffer<uint8_t>> buffer);
118+
explicit Builder(std::shared_ptr<BufferType> buffer);
118119

119120
// create an empty Builder, using an existing buffer and Options
120-
explicit Builder(std::shared_ptr<Buffer<uint8_t>> buffer,
121+
explicit Builder(std::shared_ptr<BufferType> buffer,
121122
Options const* options);
122123

123124
// create a Builder that uses an existing Buffer and default Options.
124125
// the Builder will not claim ownership for its Buffer
125-
explicit Builder(Buffer<uint8_t>& buffer) noexcept;
126+
explicit Builder(BufferType& buffer) noexcept;
126127

127128
// create a Builder that uses an existing Buffer. the Builder will not
128129
// claim ownership for this Buffer
129-
explicit Builder(Buffer<uint8_t>& buffer, Options const* options);
130+
explicit Builder(BufferType& buffer, Options const* options);
130131

131132
// populate a Builder from a Slice
132133
explicit Builder(Slice slice, Options const* options = &Options::Defaults);
@@ -141,9 +142,9 @@ class Builder {
141142
// get a reference to the Builder's Buffer object
142143
// note: this object may be a nullptr if the buffer was already stolen
143144
// from the Builder, or if the Builder has no ownership for the Buffer
144-
std::shared_ptr<Buffer<uint8_t>> const& buffer() const { return _buffer; }
145+
std::shared_ptr<BufferType> const& buffer() const { return _buffer; }
145146

146-
Buffer<uint8_t>& bufferRef() const {
147+
BufferType& bufferRef() const {
147148
if (_bufferPtr == nullptr) {
148149
throw Exception(Exception::InternalError, "Builder has no Buffer");
149150
}
@@ -153,9 +154,9 @@ class Builder {
153154
// steal the Builder's Buffer object. afterwards the Builder
154155
// is unusable - note: this may return a nullptr if the Builder does not
155156
// own the Buffer!
156-
std::shared_ptr<Buffer<uint8_t>> steal() {
157+
std::shared_ptr<BufferType> steal() {
157158
// After a steal the Builder is broken!
158-
std::shared_ptr<Buffer<uint8_t>> res(std::move(_buffer));
159+
std::shared_ptr<BufferType> res(std::move(_buffer));
159160
_bufferPtr = nullptr;
160161
_start = nullptr;
161162
clear();
@@ -613,7 +614,7 @@ class Builder {
613614
close();
614615
return *this;
615616
}
616-
617+
617618
void resetTo(std::size_t value) {
618619
_pos = value;
619620
VELOCYPACK_ASSERT(_bufferPtr != nullptr);
@@ -1145,7 +1146,7 @@ struct ArrayBuilder final : public BuilderContainer,
11451146

11461147
} // namespace arangodb::velocypack
11471148

1148-
using VPackBuilder = arangodb::velocypack::Builder;
1149+
using VPackBuilder = arangodb::velocypack::Builder<arangodb::velocypack::Buffer<uint8_t>>;
11491150
using VPackBuilderNonDeleter = arangodb::velocypack::BuilderNonDeleter;
11501151
using VPackBuilderContainer = arangodb::velocypack::BuilderContainer;
11511152
using VPackObjectBuilder = arangodb::velocypack::ObjectBuilder;

0 commit comments

Comments
 (0)