aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/data/simple_buffer.cpp
blob: 1b366505f0ed1e2be262ba796476e8d32844dbee (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "simple_buffer.h"
#include <cassert>

namespace vespalib {

SimpleBuffer::SimpleBuffer()
    : _data(),
      _used(0)
{
}

SimpleBuffer::~SimpleBuffer() = default;

Memory
SimpleBuffer::obtain()
{
    return Memory(_data.data(), _used);
}

Input &
SimpleBuffer::evict(size_t bytes)
{
    assert(bytes <= _used);
    _data.erase(_data.begin(), _data.begin() + bytes);
    _used -= bytes;
    return *this;
}

WritableMemory
SimpleBuffer::reserve(size_t bytes)
{
    assert((_used + bytes) >= _used);
    _data.resize(_used + bytes, char(0x55));
    return WritableMemory(&_data[_used], bytes);
}

Output &
SimpleBuffer::commit(size_t bytes)
{
    assert(bytes <= (_data.size() - _used));
    _used += bytes;
    return *this;
}

std::ostream &operator<<(std::ostream &os, const SimpleBuffer &buf) {
    return os << buf.get();
}

} // namespace vespalib