buffer.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #include "zerocopy.h"
  3. #include "zerocopy_output.h"
  4. #include <util/generic/ptr.h>
  5. class TBuffer;
  6. /**
  7. * @addtogroup Streams_Buffers
  8. * @{
  9. */
  10. /**
  11. * Output stream that writes into a `TBuffer`.
  12. */
  13. class TBufferOutput: public IZeroCopyOutput {
  14. public:
  15. class TImpl;
  16. /**
  17. * Constructs a stream that writes into an internal buffer.
  18. *
  19. * @param buflen Initial size of the internal buffer.
  20. */
  21. TBufferOutput(size_t buflen = 1024);
  22. /**
  23. * Constructs a stream that writes into the provided buffer. It's up to the
  24. * user to make sure that the buffer doesn't get destroyed while this stream
  25. * is in use.
  26. *
  27. * @param buffer Buffer to write into.
  28. */
  29. TBufferOutput(TBuffer& buffer);
  30. TBufferOutput(TBufferOutput&&) noexcept;
  31. TBufferOutput& operator=(TBufferOutput&&) noexcept;
  32. ~TBufferOutput() override;
  33. /**
  34. * @returns Buffer that this stream writes into.
  35. */
  36. TBuffer& Buffer() const noexcept;
  37. private:
  38. size_t DoNext(void** ptr) override;
  39. void DoUndo(size_t len) override;
  40. void DoWrite(const void* buf, size_t len) override;
  41. void DoWriteC(char c) override;
  42. private:
  43. THolder<TImpl> Impl_;
  44. };
  45. /**
  46. * Input stream that reads from an external `TBuffer`.
  47. */
  48. class TBufferInput: public IZeroCopyInputFastReadTo {
  49. public:
  50. /**
  51. * Constructs a stream that reads from an external buffer. It's up to the
  52. * user to make sure that the buffer doesn't get destroyed before this
  53. * stream.
  54. *
  55. * @param buffer External buffer to read from.
  56. */
  57. TBufferInput(const TBuffer& buffer);
  58. ~TBufferInput() override;
  59. const TBuffer& Buffer() const noexcept;
  60. void Rewind() noexcept;
  61. protected:
  62. size_t DoNext(const void** ptr, size_t len) override;
  63. void DoUndo(size_t len) override;
  64. private:
  65. const TBuffer& Buf_;
  66. size_t Readed_;
  67. };
  68. /**
  69. * Input/output stream that works with a `TBuffer`.
  70. */
  71. class TBufferStream: public TBufferOutput, public TBufferInput {
  72. public:
  73. /**
  74. * Constructs a stream that works with an internal buffer.
  75. *
  76. * @param buflen Initial size of the internal buffer.
  77. */
  78. inline TBufferStream(size_t buflen = 1024)
  79. : TBufferOutput(buflen)
  80. , TBufferInput(TBufferOutput::Buffer())
  81. {
  82. }
  83. /**
  84. * Constructs a stream that works with the provided buffer.
  85. *
  86. * @param buffer Buffer to work with.
  87. */
  88. inline TBufferStream(TBuffer& buffer)
  89. : TBufferOutput(buffer)
  90. , TBufferInput(TBufferOutput::Buffer())
  91. {
  92. }
  93. ~TBufferStream() override = default;
  94. using TBufferOutput::Buffer;
  95. };
  96. /** @} */