aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yson_pull/detail/input/stream.h
blob: 791cd5a3f52e80329b76d5fd619e6739f186fc45 (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
#pragma once

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

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

#include <util/stream/buffered.h>
#include <util/stream/file.h>
#include <util/stream/zerocopy.h>
#include <util/system/file.h>

namespace NYsonPull {
    namespace NDetail {
        namespace NInput {
            class TStreamBase: public NYsonPull::NInput::IStream {
            protected:
                result DoFillBufferFrom(IZeroCopyInput& input) {
                    void* ptr = nullptr;
                    size_t size = input.Next(&ptr);
                    if (Y_UNLIKELY(size == 0)) {
                        return result::at_end;
                    }
                    buffer().reset(static_cast<ui8*>(ptr), static_cast<ui8*>(ptr) + size);
                    return result::have_more_data;
                }
            };

            class TZeroCopy: public TStreamBase {
                IZeroCopyInput* Input;

            public:
                explicit TZeroCopy(IZeroCopyInput* input)
                    : Input(input)
                {
                }

            protected:
                result do_fill_buffer() override {
                    return DoFillBufferFrom(*Input);
                }
            };

            template <typename TBuffered>
            class TOwned: public TStreamBase {
                TBuffered Input;

            public:
                template <typename... Args>
                explicit TOwned(Args&&... args)
                    : Input(std::forward<Args>(args)...)
                {
                }

            protected:
                result do_fill_buffer() override {
                    return DoFillBufferFrom(Input);
                }
            };

            class TFHandle: public TOwned<TFileInput> {
            public:
                TFHandle(int fd, size_t buffer_size)
                    : TOwned<TFileInput>(Duplicate(fd), buffer_size)
                {
                }
            };
        }
    }     // namespace NDetail
}