aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson_pull/detail/output/buffered.h
blob: 475cf3478528dfe20b0e2a6f0dcbecbf9895968e (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
#pragma once

#include <library/cpp/yson_pull/detail/macros.h>

#include <library/cpp/yson_pull/output.h>

#include <util/generic/strbuf.h>

namespace NYsonPull {
    namespace NDetail {
        namespace NOutput {
            template <typename T>
            class TBuffered: public NYsonPull::NOutput::IStream {
                TArrayHolder<ui8> buffer_;
                size_t size_;

            public:
                TBuffered(size_t buffer_size)
                    : buffer_{new ui8[buffer_size]}
                    , size_{buffer_size} {
                    reset_buffer();
                }

            protected:
                void do_flush_buffer(TStringBuf extra) override {
                    auto& buf = buffer();
                    if (!buf.is_empty()) {
                        do_write({reinterpret_cast<const char*>(buf.begin()), buf.used()});
                        reset_buffer();
                    }
                    if (extra.size() >= buf.available()) {
                        do_write(extra);
                    } else if (extra.size() > 0) {
                        ::memcpy(buf.pos(), extra.data(), extra.size());
                        buf.advance(extra.size());
                    }
                }

            private:
                void do_write(TStringBuf data) {
                    // CRTP dispatch
                    static_cast<T*>(this)->write(data);
                }

                void reset_buffer() {
                    buffer().reset(buffer_.Get(), buffer_.Get() + size_);
                }
            };
        }
    }     // namespace NDetail
}