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