stream.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #pragma once
  2. #include "buffered.h"
  3. #include <library/cpp/yson_pull/detail/macros.h>
  4. #include <library/cpp/yson_pull/exceptions.h>
  5. #include <util/stream/output.h>
  6. #include <util/stream/file.h>
  7. #include <util/system/file.h>
  8. namespace NYsonPull {
  9. namespace NDetail {
  10. namespace NOutput {
  11. class TStream: public TBuffered<TStream> {
  12. IOutputStream* Output;
  13. public:
  14. TStream(IOutputStream* output, size_t buffer_size)
  15. : TBuffered<TStream>(buffer_size)
  16. , Output(output)
  17. {
  18. }
  19. void write(TStringBuf data) {
  20. Output->Write(data);
  21. }
  22. };
  23. template <typename TOutput>
  24. class TOwned: public TBuffered<TOwned<TOutput>> {
  25. TOutput Output;
  26. public:
  27. template <typename... Args>
  28. TOwned(size_t buffer_size, Args&&... args)
  29. : TBuffered<TOwned>(buffer_size)
  30. , Output(std::forward<Args>(args)...)
  31. {
  32. }
  33. void write(TStringBuf data) {
  34. Output.Write(data);
  35. }
  36. };
  37. class TFHandle: public TOwned<TUnbufferedFileOutput> {
  38. public:
  39. TFHandle(int fd, size_t buffer_size)
  40. : TOwned<TUnbufferedFileOutput>(buffer_size, Duplicate(fd))
  41. {
  42. }
  43. };
  44. }
  45. } // namespace NDetail
  46. }