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

#pragma once

#include "input.h"
#include "output.h"
#include <vespa/vespalib/stllike/allocator.h>
#include <iosfwd>
#include <vector>

namespace vespalib {

/**
 * Simple buffer class that implements the Input/Output
 * interfaces. Requesting the memory region of this buffer or
 * comparing buffers will only look at the data conceptually contained
 * in the buffer, ignoring evicted data and reserved data not yet
 * committed.
 **/
class SimpleBuffer : public Input,
                     public Output
{
private:
    std::vector<char, allocator_large<char>> _data;
    size_t            _used;

public:
    SimpleBuffer();
    ~SimpleBuffer();
    Memory obtain() override;
    Input &evict(size_t bytes) override;
    WritableMemory reserve(size_t bytes) override;
    Output &commit(size_t bytes) override;
    SimpleBuffer &add(char c) {
        _data.push_back(c);
        ++_used;
        return *this;
    }
    Memory get() const { return Memory(_data.data(), _used); }
    bool operator==(const SimpleBuffer &rhs) const { return (get() == rhs.get()); }
};

std::ostream &operator<<(std::ostream &os, const SimpleBuffer &buf);

} // namespace vespalib