zerocopy_output.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #pragma once
  2. #include <util/system/yassert.h>
  3. #include "output.h"
  4. /**
  5. * @addtogroup Streams
  6. * @{
  7. */
  8. /**
  9. * Output stream with direct access to the output buffer.
  10. *
  11. * Derived classes must implement `DoNext` and `DoUndo` methods.
  12. */
  13. class IZeroCopyOutput: public IOutputStream {
  14. public:
  15. IZeroCopyOutput() noexcept = default;
  16. ~IZeroCopyOutput() override = default;
  17. IZeroCopyOutput(IZeroCopyOutput&&) noexcept = default;
  18. IZeroCopyOutput& operator=(IZeroCopyOutput&&) noexcept = default;
  19. /**
  20. * Returns the next buffer to write to from this output stream.
  21. *
  22. * @param ptr[out] Pointer to the start of the buffer.
  23. * @returns Size of the returned buffer, in bytes.
  24. * Return value is always nonzero.
  25. */
  26. template <class T>
  27. inline size_t Next(T** ptr) {
  28. Y_ASSERT(ptr);
  29. return DoNext((void**)ptr);
  30. }
  31. /**
  32. * Tells the stream that `len` bytes at the end of the buffer returned previously
  33. * by Next were actually not written so the current position in stream must be moved backwards.
  34. * `len` must not be greater than the size of the buffer previously returned by `Next`.
  35. *
  36. * @param len[in] Number of bytes at the end to move the position by.
  37. *
  38. */
  39. inline void Undo(size_t len) {
  40. return DoUndo(len);
  41. }
  42. protected:
  43. void DoWrite(const void* buf, size_t len) override;
  44. virtual size_t DoNext(void** ptr) = 0;
  45. virtual void DoUndo(size_t len) = 0;
  46. };
  47. /** @} */