aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/buffer.cpp
blob: d42b193a5f6149aafd0c0da00408e9e2ad12e792 (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "buffer.h"
#include <util/generic/buffer.h>
#include <util/generic/yexception.h>

class TBufferOutput::TImpl {
public:
    inline TImpl(TBuffer& buf)
        : Data_(buf)
    {
    }

    virtual ~TImpl() = default;

    inline size_t DoNext(void** ptr) {
        if (Data_.Avail() == 0) {
            Data_.Reserve(FastClp2(Data_.Capacity() + MinBufferGrowSize));
        }
        size_t previousSize = Data_.size();
        Data_.Resize(Data_.Capacity());
        *ptr = Data_.Begin() + previousSize;
        return Data_.Size() - previousSize;
    }

    inline void DoUndo(size_t len) {
        Y_VERIFY(len <= Data_.Size(), "trying to undo more bytes than actually written");
        Data_.Resize(Data_.size() - len);
    }

    inline void DoWrite(const void* buf, size_t len) {
        Data_.Append((const char*)buf, len);
    }

    inline void DoWriteC(char c) {
        Data_.Append(c);
    }

    inline TBuffer& Buffer() const noexcept {
        return Data_;
    }

private:
    TBuffer& Data_;
    static constexpr size_t MinBufferGrowSize = 16;
};

namespace {
    using TImpl = TBufferOutput::TImpl;

    class TOwnedImpl: private TBuffer, public TImpl {
    public:
        inline TOwnedImpl(size_t buflen)
            : TBuffer(buflen)
            , TImpl(static_cast<TBuffer&>(*this))
        {
        }
    };
}

TBufferOutput::TBufferOutput(size_t buflen)
    : Impl_(new TOwnedImpl(buflen))
{
}

TBufferOutput::TBufferOutput(TBuffer& buffer)
    : Impl_(new TImpl(buffer))
{
}

TBufferOutput::TBufferOutput(TBufferOutput&&) noexcept = default;
TBufferOutput& TBufferOutput::operator=(TBufferOutput&&) noexcept = default;

TBufferOutput::~TBufferOutput() = default;

TBuffer& TBufferOutput::Buffer() const noexcept {
    return Impl_->Buffer();
}

size_t TBufferOutput::DoNext(void** ptr) {
    return Impl_->DoNext(ptr);
}

void TBufferOutput::DoUndo(size_t len) {
    Impl_->DoUndo(len);
}

void TBufferOutput::DoWrite(const void* buf, size_t len) {
    Impl_->DoWrite(buf, len);
}

void TBufferOutput::DoWriteC(char c) {
    Impl_->DoWriteC(c);
}

TBufferInput::TBufferInput(const TBuffer& buffer)
    : Buf_(buffer)
    , Readed_(0)
{
}

TBufferInput::~TBufferInput() = default;

const TBuffer& TBufferInput::Buffer() const noexcept {
    return Buf_;
}

void TBufferInput::Rewind() noexcept {
    Readed_ = 0;
}

size_t TBufferInput::DoNext(const void** ptr, size_t len) {
    len = Min(Buf_.Size() - Readed_, len);
    *ptr = Buf_.data() + Readed_; 
    Readed_ += len;
    return len;
}

void TBufferInput::DoUndo(size_t len) {
    Y_VERIFY(len <= Readed_);
    Readed_ -= len;
}