aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson_pull/buffer.h
blob: 04c9220ef338b3e937bf33e43f8f56c427dc1ee2 (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#pragma once

#include <util/system/types.h>
#include <util/system/yassert.h>

#include <cstddef>

namespace NYsonPull {
    //! \brief A non-owning buffer model.
    //!
    //! Represents a \p pos pointer moving between \p begin and \p end.
    template <typename T>
    class buffer {
        T* begin_ = nullptr;
        T* pos_ = nullptr;
        T* end_ = nullptr;

    public:
        T* begin() const noexcept {
            return begin_;
        }
        T* pos() const noexcept {
            return pos_;
        }
        T* end() const noexcept {
            return end_;
        }

        //! \brief Amount of data after current position.
        size_t available() const noexcept {
            return end_ - pos_;
        }

        //! \brief Amount of data before current position.
        size_t used() const noexcept {
            return pos_ - begin_;
        }

        //! \brief Move current position \p nbytes forward.
        void advance(size_t nbytes) noexcept {
            Y_ASSERT(pos_ + nbytes <= end_);
            pos_ += nbytes;
        }

        //! \brief Reset buffer pointers.
        void reset(T* new_begin, T* new_end, T* new_pos) {
            begin_ = new_begin;
            pos_ = new_pos;
            end_ = new_end;
        }

        //! \brief Reset buffer to beginning
        void reset(T* new_begin, T* new_end) {
            reset(new_begin, new_end, new_begin);
        }
    };

    class output_buffer: public buffer<ui8> {
    public:
        //! \brief An output buffer is empty when there is no data written to it.
        bool is_empty() const noexcept {
            return pos() == begin();
        }

        //! \brief An output buffer is full when there is no space to write more data to it.
        bool is_full() const noexcept {
            return pos() == end();
        }
    };

    class input_buffer: public buffer<const ui8> {
    public:
        //! An input stream is empty when there is no data to read in it.
        bool is_empty() const noexcept {
            return pos() == end();
        }
    };

}