aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/zerocopy.h
blob: 7676b1baffbfdb5eff2fe53844f2e7eea43de080 (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
#pragma once

#include <util/system/yassert.h>
#include <util/system/defaults.h>
#include <util/generic/ylimits.h>

#include "input.h"

class IOutputStream;

/**
 * @addtogroup Streams
 * @{
 */

/**
 * Input stream with direct access to the input buffer.
 *
 * Derived classes must implement `DoNext` method.
 */
class IZeroCopyInput: public IInputStream {
public:
    IZeroCopyInput() noexcept = default;
    ~IZeroCopyInput() override;

    IZeroCopyInput(IZeroCopyInput&&) noexcept = default;
    IZeroCopyInput& operator=(IZeroCopyInput&&) noexcept = default;

    /**
     * Returns the next data chunk from this input stream.
     *
     * Note that this function is not guaranteed to return the requested number
     * of bytes, even if they are in fact available in the stream.
     *
     * @param ptr[out]                  Pointer to the start of the data chunk.
     * @param len[in]                   Maximal size of the data chunk to be returned, in bytes.
     * @returns                         Size of the returned data chunk, in bytes.
     *                                  Return value of zero signals end of stream.
     */
    template <class T>
    inline size_t Next(T** ptr, size_t len) {
        Y_ASSERT(ptr);

        return DoNext((const void**)ptr, len);
    }

    template <class T>
    inline size_t Next(T** ptr) {
        return Next(ptr, Max<size_t>());
    }

protected:
    size_t DoRead(void* buf, size_t len) override;
    size_t DoSkip(size_t len) override;
    ui64 DoReadAll(IOutputStream& out) override;
    virtual size_t DoNext(const void** ptr, size_t len) = 0;
};

/**
 * Input stream with direct access to the input buffer and ability to undo read
 *
 * Derived classes must implement `DoUndo` method.
 */
class IZeroCopyInputFastReadTo: public IZeroCopyInput {
public:
    IZeroCopyInputFastReadTo() noexcept = default;
    ~IZeroCopyInputFastReadTo() override;

    IZeroCopyInputFastReadTo(IZeroCopyInputFastReadTo&&) noexcept = default;
    IZeroCopyInputFastReadTo& operator=(IZeroCopyInputFastReadTo&&) noexcept = default;

protected:
    size_t DoReadTo(TString& st, char ch) override;

private:
    /**
     * Undo read.
     *
     * Note that this function not check if you try undo more that read. In fact Undo used for undo read in last chunk.
     *
     * @param len[in]                   Bytes to undo.
     */
    inline void Undo(size_t len) {
        if (len) {
            DoUndo(len);
        }
    }
    virtual void DoUndo(size_t len) = 0;
};

/** @} */