diff options
author | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 |
---|---|---|
committer | Devtools Arcadia <[email protected]> | 2022-02-07 18:08:42 +0300 |
commit | 1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch) | |
tree | e26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/stream/zerocopy_output.h |
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/stream/zerocopy_output.h')
-rw-r--r-- | util/stream/zerocopy_output.h | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/util/stream/zerocopy_output.h b/util/stream/zerocopy_output.h new file mode 100644 index 00000000000..a388be04b04 --- /dev/null +++ b/util/stream/zerocopy_output.h @@ -0,0 +1,57 @@ +#pragma once + +#include <util/system/yassert.h> + +#include "output.h" + +/** + * @addtogroup Streams + * @{ + */ + +/** + * Output stream with direct access to the output buffer. + * + * Derived classes must implement `DoNext` and `DoUndo` methods. + */ +class IZeroCopyOutput: public IOutputStream { +public: + IZeroCopyOutput() noexcept = default; + ~IZeroCopyOutput() override = default; + + IZeroCopyOutput(IZeroCopyOutput&&) noexcept = default; + IZeroCopyOutput& operator=(IZeroCopyOutput&&) noexcept = default; + + /** + * Returns the next buffer to write to from this output stream. + * + * @param ptr[out] Pointer to the start of the buffer. + * @returns Size of the returned buffer, in bytes. + * Return value is always nonzero. + */ + template <class T> + inline size_t Next(T** ptr) { + Y_ASSERT(ptr); + + return DoNext((void**)ptr); + } + + /** + * Tells the stream that `len` bytes at the end of the buffer returned previously + * by Next were actually not written so the current position in stream must be moved backwards. + * `len` must not be greater than the size of the buffer previously returned by `Next`. + * + * @param len[in] Number of bytes at the end to move the position by. + * + */ + inline void Undo(size_t len) { + return DoUndo(len); + } + +protected: + void DoWrite(const void* buf, size_t len) override; + virtual size_t DoNext(void** ptr) = 0; + virtual void DoUndo(size_t len) = 0; +}; + +/** @} */ |