1 | // |
2 | // FIFOBufferStream.cpp |
3 | // |
4 | // Library: Foundation |
5 | // Package: Streams |
6 | // Module: FIFOBufferStream |
7 | // |
8 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
9 | // and Contributors. |
10 | // |
11 | // SPDX-License-Identifier: BSL-1.0 |
12 | // |
13 | |
14 | |
15 | #include "Poco/FIFOBufferStream.h" |
16 | |
17 | |
18 | namespace Poco { |
19 | |
20 | |
21 | // |
22 | // FIFOBufferStreamBuf |
23 | // |
24 | |
25 | |
26 | FIFOBufferStreamBuf::FIFOBufferStreamBuf(): |
27 | BufferedBidirectionalStreamBuf(STREAM_BUFFER_SIZE + 4, std::ios::in | std::ios::out), |
28 | _pFIFOBuffer(new FIFOBuffer(STREAM_BUFFER_SIZE, true)), |
29 | _fifoBuffer(*_pFIFOBuffer) |
30 | { |
31 | } |
32 | |
33 | |
34 | FIFOBufferStreamBuf::FIFOBufferStreamBuf(FIFOBuffer& rFifoBuffer): |
35 | BufferedBidirectionalStreamBuf(rFifoBuffer.size() + 4, std::ios::in | std::ios::out), |
36 | _pFIFOBuffer(0), |
37 | _fifoBuffer(rFifoBuffer) |
38 | { |
39 | rFifoBuffer.setNotify(true); |
40 | } |
41 | |
42 | |
43 | FIFOBufferStreamBuf::FIFOBufferStreamBuf(char* pBuffer, std::size_t length): |
44 | BufferedBidirectionalStreamBuf(length + 4, std::ios::in | std::ios::out), |
45 | _pFIFOBuffer(new FIFOBuffer(pBuffer, length, true)), |
46 | _fifoBuffer(*_pFIFOBuffer) |
47 | { |
48 | } |
49 | |
50 | |
51 | FIFOBufferStreamBuf::FIFOBufferStreamBuf(const char* pBuffer, std::size_t length): |
52 | BufferedBidirectionalStreamBuf(length + 4, std::ios::in | std::ios::out), |
53 | _pFIFOBuffer(new FIFOBuffer(pBuffer, length, true)), |
54 | _fifoBuffer(*_pFIFOBuffer) |
55 | { |
56 | } |
57 | |
58 | |
59 | FIFOBufferStreamBuf::FIFOBufferStreamBuf(std::size_t length): |
60 | BufferedBidirectionalStreamBuf(length + 4, std::ios::in | std::ios::out), |
61 | _pFIFOBuffer(new FIFOBuffer(length, true)), |
62 | _fifoBuffer(*_pFIFOBuffer) |
63 | { |
64 | } |
65 | |
66 | |
67 | FIFOBufferStreamBuf::~FIFOBufferStreamBuf() |
68 | { |
69 | delete _pFIFOBuffer; |
70 | } |
71 | |
72 | |
73 | int FIFOBufferStreamBuf::readFromDevice(char* buffer, std::streamsize length) |
74 | { |
75 | poco_assert (length > 0); |
76 | return static_cast<int>(_fifoBuffer.read(buffer, static_cast<std::size_t>(length))); |
77 | } |
78 | |
79 | |
80 | int FIFOBufferStreamBuf::writeToDevice(const char* buffer, std::streamsize length) |
81 | { |
82 | poco_assert (length > 0); |
83 | return static_cast<int>(_fifoBuffer.write(buffer, static_cast<std::size_t>(length))); |
84 | } |
85 | |
86 | |
87 | // |
88 | // FIFOIOS |
89 | // |
90 | |
91 | |
92 | FIFOIOS::FIFOIOS(FIFOBuffer& fifoBuffer): _buf(fifoBuffer) |
93 | { |
94 | poco_ios_init(&_buf); |
95 | } |
96 | |
97 | |
98 | FIFOIOS::FIFOIOS(char* pBuffer, std::size_t length): _buf(pBuffer, length) |
99 | { |
100 | poco_ios_init(&_buf); |
101 | } |
102 | |
103 | |
104 | FIFOIOS::FIFOIOS(const char* pBuffer, std::size_t length): _buf(pBuffer, length) |
105 | { |
106 | poco_ios_init(&_buf); |
107 | } |
108 | |
109 | |
110 | FIFOIOS::FIFOIOS(std::size_t length): _buf(length) |
111 | { |
112 | poco_ios_init(&_buf); |
113 | } |
114 | |
115 | |
116 | FIFOIOS::~FIFOIOS() |
117 | { |
118 | try |
119 | { |
120 | _buf.sync(); |
121 | } |
122 | catch (...) |
123 | { |
124 | } |
125 | } |
126 | |
127 | |
128 | FIFOBufferStreamBuf* FIFOIOS::rdbuf() |
129 | { |
130 | return &_buf; |
131 | } |
132 | |
133 | |
134 | void FIFOIOS::close() |
135 | { |
136 | _buf.sync(); |
137 | } |
138 | |
139 | |
140 | // |
141 | // FIFOBufferStream |
142 | // |
143 | |
144 | |
145 | FIFOBufferStream::FIFOBufferStream(FIFOBuffer& fifoBuffer): |
146 | FIFOIOS(fifoBuffer), |
147 | std::iostream(&_buf), |
148 | readable(_buf.fifoBuffer().readable), |
149 | writable(_buf.fifoBuffer().writable) |
150 | { |
151 | } |
152 | |
153 | |
154 | FIFOBufferStream::FIFOBufferStream(char* pBuffer, std::size_t length): |
155 | FIFOIOS(pBuffer, length), |
156 | std::iostream(&_buf), |
157 | readable(_buf.fifoBuffer().readable), |
158 | writable(_buf.fifoBuffer().writable) |
159 | { |
160 | } |
161 | |
162 | |
163 | FIFOBufferStream::FIFOBufferStream(const char* pBuffer, std::size_t length): |
164 | FIFOIOS(pBuffer, length), |
165 | std::iostream(&_buf), |
166 | readable(_buf.fifoBuffer().readable), |
167 | writable(_buf.fifoBuffer().writable) |
168 | { |
169 | } |
170 | |
171 | |
172 | FIFOBufferStream::FIFOBufferStream(std::size_t length): |
173 | FIFOIOS(length), |
174 | std::iostream(&_buf), |
175 | readable(_buf.fifoBuffer().readable), |
176 | writable(_buf.fifoBuffer().writable) |
177 | { |
178 | } |
179 | |
180 | |
181 | FIFOBufferStream::~FIFOBufferStream() |
182 | { |
183 | } |
184 | |
185 | |
186 | } // namespace Poco |
187 | |