1 | // |
2 | // FIFOBufferStream.h |
3 | // |
4 | // Library: Foundation |
5 | // Package: Streams |
6 | // Module: FIFOBufferStream |
7 | // |
8 | // Definition of the FIFOBufferStream class. |
9 | // |
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
11 | // and Contributors. |
12 | // |
13 | // SPDX-License-Identifier: BSL-1.0 |
14 | // |
15 | |
16 | |
17 | #ifndef Foundation_FIFOBufferStream_INCLUDED |
18 | #define Foundation_FIFOBufferStream_INCLUDED |
19 | |
20 | |
21 | #include "Poco/Foundation.h" |
22 | #include "Poco/FIFOBuffer.h" |
23 | #include "Poco/BufferedBidirectionalStreamBuf.h" |
24 | #include <istream> |
25 | #include <ostream> |
26 | |
27 | |
28 | namespace Poco { |
29 | |
30 | |
31 | class Foundation_API FIFOBufferStreamBuf: public BufferedBidirectionalStreamBuf |
32 | /// This is the streambuf class used for reading from and writing to a FIFOBuffer. |
33 | /// FIFOBuffer is enabled for emtpy/non-empty/full state transitions notifications. |
34 | { |
35 | public: |
36 | |
37 | FIFOBufferStreamBuf(); |
38 | /// Creates a FIFOBufferStreamBuf. |
39 | |
40 | explicit FIFOBufferStreamBuf(FIFOBuffer& fifoBuffer); |
41 | /// Creates a FIFOBufferStreamBuf and assigns the given buffer to it. |
42 | |
43 | FIFOBufferStreamBuf(char* pBuffer, std::size_t length); |
44 | /// Creates a FIFOBufferStreamBuf and assigns the given buffer to it. |
45 | |
46 | FIFOBufferStreamBuf(const char* pBuffer, std::size_t length); |
47 | /// Creates a FIFOBufferStreamBuf and assigns the given buffer to it. |
48 | |
49 | explicit FIFOBufferStreamBuf(std::size_t length); |
50 | /// Creates a FIFOBufferStreamBuf of the given length. |
51 | |
52 | ~FIFOBufferStreamBuf(); |
53 | /// Destroys the FIFOBufferStreamBuf. |
54 | |
55 | FIFOBuffer& fifoBuffer(); |
56 | /// Returns the underlying FIFO buffer reference. |
57 | |
58 | protected: |
59 | int readFromDevice(char* buffer, std::streamsize length); |
60 | int writeToDevice(const char* buffer, std::streamsize length); |
61 | |
62 | private: |
63 | enum |
64 | { |
65 | STREAM_BUFFER_SIZE = 1024 |
66 | }; |
67 | |
68 | FIFOBuffer* _pFIFOBuffer; |
69 | FIFOBuffer& _fifoBuffer; |
70 | }; |
71 | |
72 | |
73 | class Foundation_API FIFOIOS: public virtual std::ios |
74 | /// The base class for FIFOBufferInputStream and |
75 | /// FIFOBufferStream. |
76 | /// |
77 | /// This class is needed to ensure the correct initialization |
78 | /// order of the stream buffer and base classes. |
79 | { |
80 | public: |
81 | explicit FIFOIOS(FIFOBuffer& buffer); |
82 | /// Creates a FIFOIOS and assigns the given buffer to it. |
83 | |
84 | FIFOIOS(char* pBuffer, std::size_t length); |
85 | /// Creates a FIFOIOS and assigns the given buffer to it. |
86 | |
87 | FIFOIOS(const char* pBuffer, std::size_t length); |
88 | /// Creates a FIFOIOS and assigns the given buffer to it. |
89 | |
90 | explicit FIFOIOS(std::size_t length); |
91 | /// Creates a FIFOIOS of the given length. |
92 | |
93 | ~FIFOIOS(); |
94 | /// Destroys the FIFOIOS. |
95 | /// |
96 | /// Flushes the buffer. |
97 | |
98 | FIFOBufferStreamBuf* rdbuf(); |
99 | /// Returns a pointer to the internal FIFOBufferStreamBuf. |
100 | |
101 | void close(); |
102 | /// Flushes the stream. |
103 | |
104 | protected: |
105 | FIFOBufferStreamBuf _buf; |
106 | }; |
107 | |
108 | |
109 | class Foundation_API FIFOBufferStream: public FIFOIOS, public std::iostream |
110 | /// An output stream for writing to a FIFO. |
111 | { |
112 | public: |
113 | Poco::BasicEvent<bool>& readable; |
114 | Poco::BasicEvent<bool>& writable; |
115 | |
116 | explicit FIFOBufferStream(FIFOBuffer& buffer); |
117 | /// Creates the FIFOBufferStream with supplied buffer as initial value. |
118 | |
119 | FIFOBufferStream(char* pBuffer, std::size_t length); |
120 | /// Creates a FIFOBufferStream and assigns the given buffer to it. |
121 | |
122 | FIFOBufferStream(const char* pBuffer, std::size_t length); |
123 | /// Creates a FIFOBufferStream and assigns the given buffer to it. |
124 | |
125 | explicit FIFOBufferStream(std::size_t length); |
126 | /// Creates a FIFOBufferStream of the given length. |
127 | |
128 | ~FIFOBufferStream(); |
129 | /// Destroys the FIFOBufferStream. |
130 | /// |
131 | /// Flushes the buffer. |
132 | |
133 | private: |
134 | FIFOBufferStream(); |
135 | FIFOBufferStream(const FIFOBufferStream& other); |
136 | FIFOBufferStream& operator =(const FIFOBufferStream& other); |
137 | }; |
138 | |
139 | |
140 | /// |
141 | /// inlines |
142 | /// |
143 | |
144 | |
145 | inline FIFOBuffer& FIFOBufferStreamBuf::fifoBuffer() |
146 | { |
147 | return _fifoBuffer; |
148 | } |
149 | |
150 | |
151 | } // namespace Poco |
152 | |
153 | |
154 | #endif // Foundation_FIFOBufferStream_INCLUDED |
155 | |