aboutsummaryrefslogtreecommitdiffstats
path: root/streamingvisitors/src/vespa/vsm/common/charbuffer.h
blob: 96535c89c2c1d868898725a703346938e3e79169 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <vector>
#include <memory>

namespace vsm {

/**
 * Simple growable char buffer.
 **/
class CharBuffer
{
private:
    std::vector<char> _buffer;
    size_t            _pos;

public:
    using SP = std::shared_ptr<CharBuffer>;

    /**
     * Creates a char buffer with len bytes.
     **/
    CharBuffer(size_t len = 0);

    /**
     * Copies n bytes from the src array into the underlying buffer at the
     * current position, and updates the position accordingly.
     * Resizing will occur if needed.
     **/
    void put(const char * src, size_t n);

    /**
     * Resizes the buffer so that the new length becomes len.
     * Resizing will not occur if len < current length.
     **/
    void resize(size_t len);

    /**
     * Resets the position to the beginning of the buffer.
     **/
    void reset() { _pos = 0; }

    const char * getBuffer() const { return &_buffer[0]; }
    size_t getLength() const { return _buffer.size(); }
    size_t getPos() const { return _pos; }
    size_t getRemaining() const { return getLength() - getPos(); }
    void put(char c) { put(&c, 1); }
};

}