diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:15 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:15 +0300 |
commit | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (patch) | |
tree | da2c34829458c7d4e74bdfbdf85dff449e9e7fb8 /util/stream/buffered.h | |
parent | 778e51ba091dc39e7b7fcab2b9cf4dbedfb6f2b5 (diff) | |
download | ydb-72cb13b4aff9bc9cf22e49251bc8fd143f82538f.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 1 of 2.
Diffstat (limited to 'util/stream/buffered.h')
-rw-r--r-- | util/stream/buffered.h | 160 |
1 files changed, 80 insertions, 80 deletions
diff --git a/util/stream/buffered.h b/util/stream/buffered.h index 0847186141..e43d7ca406 100644 --- a/util/stream/buffered.h +++ b/util/stream/buffered.h @@ -1,13 +1,13 @@ #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> - +#include <util/generic/typetraits.h> +#include <util/generic/store_policy.h> + /** * @addtogroup Streams_Buffered * @{ @@ -21,33 +21,33 @@ * to the user to free it. */ class TBufferedInput: public IZeroCopyInput { -public: +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: + +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_; -}; - + +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. @@ -60,7 +60,7 @@ private: * so it's up to the user to free it. */ class TBufferedOutputBase: public IZeroCopyOutput { -public: +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 @@ -83,17 +83,17 @@ public: 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); - } - + SetFlushPropagateMode(propagate); + SetFinishPropagateMode(propagate); + } + /** * @param propagate Whether `Flush` calls should be propagated * to the underlying stream. By default they @@ -107,65 +107,65 @@ public: * are not. */ void SetFinishPropagateMode(bool propagate) noexcept; - - class TImpl; - -protected: + + 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_; -}; - + +private: + THolder<TImpl> Impl_; +}; + /** * Buffered output stream with a fixed-size buffer. * * @see TBufferedOutputBase */ -class TBufferedOutput: public TBufferedOutputBase { -public: +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: +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 { +}; + +namespace NPrivate { + struct TMyBufferedOutput: public TBufferedOutput { inline TMyBufferedOutput(IOutputStream* slave, size_t buflen) - : TBufferedOutput(slave, buflen) - { - SetFinishPropagateMode(true); - } - }; - - template <class T> - struct TBufferedStreamFor { + : TBufferedOutput(slave, buflen) + { + SetFinishPropagateMode(true); + } + }; + + template <class T> + struct TBufferedStreamFor { using TResult = std::conditional_t<std::is_base_of<IInputStream, T>::value, TBufferedInput, TMyBufferedOutput>; - }; -} - + }; +} + /** * A mixin class that turns unbuffered stream into a buffered one. * @@ -179,30 +179,30 @@ namespace NPrivate { * 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 { + */ +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) + +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(); - } + , 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 @@ -214,22 +214,22 @@ public: * TAdaptivelyBuffered<TUnbufferedFileOutput> file_output("/path/to/file"); * @endcode */ -template <class TSlave> -class TAdaptivelyBuffered: private TEmbedPolicy<TSlave>, public TAdaptiveBufferedOutput { +template <class TSlave> +class TAdaptivelyBuffered: private TEmbedPolicy<TSlave>, public TAdaptiveBufferedOutput { using TSlaveBase = TEmbedPolicy<TSlave>; - -public: - template <typename... Args> - inline TAdaptivelyBuffered(Args&&... args) + +public: + template <typename... Args> + inline TAdaptivelyBuffered(Args&&... args) : TSlaveBase(std::forward<Args>(args)...) - , TAdaptiveBufferedOutput(TSlaveBase::Ptr()) - { - } + , TAdaptiveBufferedOutput(TSlaveBase::Ptr()) + { + } TAdaptivelyBuffered(const TAdaptivelyBuffered&) = delete; TAdaptivelyBuffered& operator=(const TAdaptivelyBuffered&) = delete; TAdaptivelyBuffered(TAdaptivelyBuffered&& other) = delete; TAdaptivelyBuffered& operator=(TAdaptivelyBuffered&& other) = delete; -}; - +}; + /** @} */ |