aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/serialization/slime_output_to_vector.h
blob: 4663e68ea5cad859ac9c40679713ed5c083a00fa (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#pragma once

#include <vespa/vespalib/data/output.h>
#include <vespa/vespalib/data/writable_memory.h>
#include <vector>

namespace document {

class SlimeOutputToVector : public vespalib::Output {
    std::vector<char> _buf;
    size_t _size;

public:
    SlimeOutputToVector();
    ~SlimeOutputToVector();

    vespalib::WritableMemory reserve(size_t reserve) override {
        if (_size + reserve > _buf.size()) {
            _buf.resize(_size + reserve);
        }
        return vespalib::WritableMemory(&_buf[_size], _buf.size() - _size);
    }

    Output &commit(size_t commit) override {
        _size += commit;
        return *this;
    }

    const char *data() const { return &_buf[0]; }
    size_t size() const { return _size; }
};

}  // namespace document