chunked_buffer.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #pragma once
  2. #include <util/generic/strbuf.h>
  3. #include <util/generic/string.h>
  4. #include <util/stream/output.h>
  5. #include <memory>
  6. #include <deque>
  7. namespace NYql {
  8. class TChunkedBuffer {
  9. public:
  10. TChunkedBuffer() = default;
  11. TChunkedBuffer(const TChunkedBuffer&) = default;
  12. TChunkedBuffer(TChunkedBuffer&& other);
  13. TChunkedBuffer& operator=(TChunkedBuffer&& other);
  14. explicit TChunkedBuffer(TStringBuf buf, const std::shared_ptr<const void>& owner);
  15. explicit TChunkedBuffer(TString&& str);
  16. inline size_t ContigousSize() const {
  17. return Items_.empty() ? 0 : Front().Buf.size();
  18. }
  19. inline size_t Size() const {
  20. return Size_;
  21. }
  22. inline bool Empty() const {
  23. return Size_ == 0;
  24. }
  25. struct TChunk {
  26. TStringBuf Buf;
  27. std::shared_ptr<const void> Owner;
  28. };
  29. const TChunk& Front() const;
  30. size_t CopyTo(IOutputStream& dst, size_t toCopy = std::numeric_limits<size_t>::max()) const;
  31. TChunkedBuffer& Append(TStringBuf buf, const std::shared_ptr<const void>& owner);
  32. TChunkedBuffer& Append(TString&& str);
  33. TChunkedBuffer& Append(TChunkedBuffer&& other);
  34. TChunkedBuffer& Clear();
  35. TChunkedBuffer& Erase(size_t size);
  36. private:
  37. std::deque<TChunk> Items_;
  38. size_t Size_ = 0;
  39. };
  40. class TChunkedBufferOutput : public IOutputStream {
  41. public:
  42. explicit TChunkedBufferOutput(TChunkedBuffer& dst);
  43. private:
  44. virtual void DoWrite(const void *buf, size_t len) override;
  45. TChunkedBuffer& Dst_;
  46. };
  47. TChunkedBuffer CopyData(const TChunkedBuffer& src);
  48. TChunkedBuffer CopyData(TChunkedBuffer&& src);
  49. }