TeeStream.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // TeeStream.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/TeeStream.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Streams
  8. // Module: TeeStream
  9. //
  10. // Definition of the TeeStream class.
  11. //
  12. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_TeeStream_INCLUDED
  18. #define Foundation_TeeStream_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/UnbufferedStreamBuf.h"
  21. #include <vector>
  22. #include <istream>
  23. #include <ostream>
  24. namespace Poco {
  25. class Foundation_API TeeStreamBuf: public UnbufferedStreamBuf
  26. /// This stream buffer copies all data written to or
  27. /// read from it to one or multiple output streams.
  28. {
  29. public:
  30. TeeStreamBuf();
  31. /// Creates an unconnected CountingStreamBuf.
  32. /// Use addStream() to attach output streams.
  33. TeeStreamBuf(std::istream& istr);
  34. /// Creates the CountingStreamBuf and connects it
  35. /// to the given input stream.
  36. TeeStreamBuf(std::ostream& ostr);
  37. /// Creates the CountingStreamBuf and connects it
  38. /// to the given output stream.
  39. ~TeeStreamBuf();
  40. /// Destroys the CountingStream.
  41. void addStream(std::ostream& ostr);
  42. /// Adds the given output stream.
  43. protected:
  44. int readFromDevice();
  45. int writeToDevice(char c);
  46. private:
  47. typedef std::vector<std::ostream*> StreamVec;
  48. std::istream* _pIstr;
  49. StreamVec _streams;
  50. };
  51. class Foundation_API TeeIOS: public virtual std::ios
  52. /// The base class for TeeInputStream and TeeOutputStream.
  53. ///
  54. /// This class is needed to ensure the correct initialization
  55. /// order of the stream buffer and base classes.
  56. {
  57. public:
  58. TeeIOS();
  59. /// Creates the basic stream and leaves it unconnected.
  60. TeeIOS(std::istream& istr);
  61. /// Creates the basic stream and connects it
  62. /// to the given input stream.
  63. TeeIOS(std::ostream& ostr);
  64. /// Creates the basic stream and connects it
  65. /// to the given output stream.
  66. ~TeeIOS();
  67. /// Destroys the stream.
  68. void addStream(std::ostream& ostr);
  69. /// Adds the given output stream.
  70. TeeStreamBuf* rdbuf();
  71. /// Returns a pointer to the underlying streambuf.
  72. protected:
  73. TeeStreamBuf _buf;
  74. };
  75. class Foundation_API TeeInputStream: public TeeIOS, public std::istream
  76. /// This stream copies all characters read through it
  77. /// to one or multiple output streams.
  78. {
  79. public:
  80. TeeInputStream(std::istream& istr);
  81. /// Creates the TeeInputStream and connects it
  82. /// to the given input stream.
  83. ~TeeInputStream();
  84. /// Destroys the TeeInputStream.
  85. };
  86. class Foundation_API TeeOutputStream: public TeeIOS, public std::ostream
  87. /// This stream copies all characters written to it
  88. /// to one or multiple output streams.
  89. {
  90. public:
  91. TeeOutputStream();
  92. /// Creates an unconnected TeeOutputStream.
  93. TeeOutputStream(std::ostream& ostr);
  94. /// Creates the TeeOutputStream and connects it
  95. /// to the given input stream.
  96. ~TeeOutputStream();
  97. /// Destroys the TeeOutputStream.
  98. };
  99. } // namespace Poco
  100. #endif // Foundation_TeeStream_INCLUDED