holder.h 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #pragma once
  2. #include <util/generic/ptr.h>
  3. #include <utility>
  4. #include <type_traits>
  5. class IInputStream;
  6. class IOutputStream;
  7. namespace NPrivate {
  8. template <class Stream, bool isInput = std::is_base_of<IInputStream, Stream>::value>
  9. struct TStreamBase {
  10. using TType = IInputStream;
  11. };
  12. template <class Stream>
  13. struct TStreamBase<Stream, false> {
  14. using TType = IOutputStream;
  15. };
  16. } // namespace NPrivate
  17. /**
  18. * An ownership-gaining wrapper for proxy streams.
  19. *
  20. * Example usage:
  21. * \code
  22. * TCountingInput* input = new THoldingStream<TCountingInput>(new TStringInput(s));
  23. * \encode
  24. *
  25. * In this example, resulting counting input also owns a string input that it
  26. * was constructed on top of.
  27. */
  28. template <class Base, class StreamBase = typename ::NPrivate::TStreamBase<Base>::TType>
  29. class THoldingStream: private THolder<StreamBase>, public Base {
  30. public:
  31. template <class... Args>
  32. inline THoldingStream(THolder<StreamBase> stream, Args&&... args)
  33. : THolder<StreamBase>(std::move(stream))
  34. , Base(this->Get(), std::forward<Args>(args)...)
  35. {
  36. }
  37. };