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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
#pragma once
#include "zerocopy.h"
#include "zerocopy_output.h"
#include <utility>
#include <util/generic/ptr.h>
#include <util/generic/typetraits.h>
#include <util/generic/store_policy.h>
/**
* @addtogroup Streams_Buffered
* @{
*/
/**
* Input stream that wraps the given stream and adds a buffer on top of it,
* thus making sure that data is read from the underlying stream in big chunks.
*
* Note that it does not claim ownership of the underlying stream, so it's up
* to the user to free it.
*/
class TBufferedInput: public IZeroCopyInput {
public:
TBufferedInput(IInputStream* slave, size_t buflen = 8192);
TBufferedInput(TBufferedInput&&) noexcept;
TBufferedInput& operator=(TBufferedInput&&) noexcept;
~TBufferedInput() override;
/**
* Switches the underlying stream to the one provided. Does not clear the
* data that was already buffered.
*
* @param slave New underlying stream.
*/
void Reset(IInputStream* slave);
protected:
size_t DoRead(void* buf, size_t len) override;
size_t DoReadTo(TString& st, char ch) override;
size_t DoSkip(size_t len) override;
size_t DoNext(const void** ptr, size_t len) override;
private:
class TImpl;
THolder<TImpl> Impl_;
};
/**
* Output stream that wraps the given stream and adds a buffer on top of it,
* thus making sure that data is written to the underlying stream in big chunks.
*
* Note that by default this stream does not propagate `Flush` and `Finish`
* calls to the underlying stream, instead simply flushing out the buffer.
* You can change this behavior by using propagation mode setters.
*
* Also note that this stream does not claim ownership of the underlying stream,
* so it's up to the user to free it.
*/
class TBufferedOutputBase: public IZeroCopyOutput {
public:
/**
* Constructs a buffered stream that dynamically adjusts the size of the
* buffer. This works best when the amount of data that will be passed
* through this stream is not known and can range in size from several
* kilobytes to several gigabytes.
*
* @param slave Underlying stream.
*/
TBufferedOutputBase(IOutputStream* slave);
/**
* Constructs a buffered stream with the given size of the buffer.
*
* @param slave Underlying stream.
* @param buflen Size of the buffer.
*/
TBufferedOutputBase(IOutputStream* slave, size_t buflen);
TBufferedOutputBase(TBufferedOutputBase&&) noexcept;
TBufferedOutputBase& operator=(TBufferedOutputBase&&) noexcept;
~TBufferedOutputBase() override;
/**
* @param propagate Whether `Flush` and `Finish` calls should
* be propagated to the underlying stream.
* By default they are not.
*/
inline void SetPropagateMode(bool propagate) noexcept {
SetFlushPropagateMode(propagate);
SetFinishPropagateMode(propagate);
}
/**
* @param propagate Whether `Flush` calls should be propagated
* to the underlying stream. By default they
* are not.
*/
void SetFlushPropagateMode(bool propagate) noexcept;
/**
* @param propagate Whether `Finish` calls should be propagated
* to the underlying stream. By default they
* are not.
*/
void SetFinishPropagateMode(bool propagate) noexcept;
class TImpl;
protected:
size_t DoNext(void** ptr) override;
void DoUndo(size_t len) override;
void DoWrite(const void* data, size_t len) override;
void DoWriteC(char c) override;
void DoFlush() override;
void DoFinish() override;
private:
THolder<TImpl> Impl_;
};
/**
* Buffered output stream with a fixed-size buffer.
*
* @see TBufferedOutputBase
*/
class TBufferedOutput: public TBufferedOutputBase {
public:
TBufferedOutput(IOutputStream* slave, size_t buflen = 8192);
~TBufferedOutput() override;
TBufferedOutput(TBufferedOutput&&) noexcept = default;
TBufferedOutput& operator=(TBufferedOutput&&) noexcept = default;
};
/**
* Buffered output stream that dynamically adjusts the size of the buffer based
* on the amount of data that's passed through it.
*
* @see TBufferedOutputBase
*/
class TAdaptiveBufferedOutput: public TBufferedOutputBase {
public:
TAdaptiveBufferedOutput(IOutputStream* slave);
~TAdaptiveBufferedOutput() override;
TAdaptiveBufferedOutput(TAdaptiveBufferedOutput&&) noexcept = default;
TAdaptiveBufferedOutput& operator=(TAdaptiveBufferedOutput&&) noexcept = default;
};
namespace NPrivate {
struct TMyBufferedOutput: public TBufferedOutput {
inline TMyBufferedOutput(IOutputStream* slave, size_t buflen)
: TBufferedOutput(slave, buflen)
{
SetFinishPropagateMode(true);
}
};
template <class T>
struct TBufferedStreamFor {
using TResult = std::conditional_t<std::is_base_of<IInputStream, T>::value, TBufferedInput, TMyBufferedOutput>;
};
} // namespace NPrivate
/**
* A mixin class that turns unbuffered stream into a buffered one.
*
* Note that using this mixin with a stream that is already buffered won't
* result in double buffering, e.g. `TBuffered<TBuffered<TUnbufferedFileInput>>` and
* `TBuffered<TUnbufferedFileInput>` are basically the same types.
*
* Example usage:
* @code
* TBuffered<TUnbufferedFileInput> file_input(1024, "/path/to/file");
* TBuffered<TUnbufferedFileOutput> file_output(1024, "/path/to/file");
* @endcode
* Here 1024 is the size of the buffer.
*/
template <class TSlave>
class TBuffered: private TEmbedPolicy<TSlave>, public ::NPrivate::TBufferedStreamFor<TSlave>::TResult {
using TSlaveBase = TEmbedPolicy<TSlave>;
using TBufferedBase = typename ::NPrivate::TBufferedStreamFor<TSlave>::TResult;
public:
template <typename... Args>
inline TBuffered(size_t b, Args&&... args)
: TSlaveBase(std::forward<Args>(args)...)
, TBufferedBase(TSlaveBase::Ptr(), b)
{
}
inline TSlave& Slave() noexcept {
return *this->Ptr();
}
TBuffered(const TBuffered&) = delete;
TBuffered& operator=(const TBuffered&) = delete;
TBuffered(TBuffered&&) = delete;
TBuffered& operator=(TBuffered&&) = delete;
};
/**
* A mixin class that turns unbuffered stream into an adaptively buffered one.
* Created stream differs from the one created via `TBuffered` template in that
* it dynamically adjusts the size of the buffer based on the amount of data
* that's passed through it.
*
* Example usage:
* @code
* TAdaptivelyBuffered<TUnbufferedFileOutput> file_output("/path/to/file");
* @endcode
*/
template <class TSlave>
class TAdaptivelyBuffered: private TEmbedPolicy<TSlave>, public TAdaptiveBufferedOutput {
using TSlaveBase = TEmbedPolicy<TSlave>;
public:
template <typename... Args>
inline TAdaptivelyBuffered(Args&&... args)
: TSlaveBase(std::forward<Args>(args)...)
, TAdaptiveBufferedOutput(TSlaveBase::Ptr())
{
}
TAdaptivelyBuffered(const TAdaptivelyBuffered&) = delete;
TAdaptivelyBuffered& operator=(const TAdaptivelyBuffered&) = delete;
TAdaptivelyBuffered(TAdaptivelyBuffered&& other) = delete;
TAdaptivelyBuffered& operator=(TAdaptivelyBuffered&& other) = delete;
};
/** @} */
|