IOStream.h
1 /*
2  * Copyright (C) 2005-2012 Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_System_IOStream_h
30 #define Pt_System_IOStream_h
31 
32 #include <Pt/System/Api.h>
33 #include <Pt/System/IOBuffer.h>
34 #include <Pt/IOStream.h>
35 #include <cstddef>
36 
37 namespace Pt {
38 
39 namespace System {
40 
43 class IStream : public BasicIStream<char>
44 {
45  public:
48  explicit IStream(std::size_t bufferSize = 8192, bool extend = false);
49 
52  explicit IStream(IODevice& device, std::size_t bufferSize = 8192, bool extend = false);
53 
56  {}
57 
61  { return _buffer; }
62 
66  { return _buffer.device(); }
67 
70  void attach(IODevice& dev)
71  { _buffer.attach(dev); }
72 
75  void detach()
76  { _buffer.detach(); }
77 
80  void discard()
81  { _buffer.discard(); }
82 
85  void reset()
86  { _buffer.reset(); }
87 
88  private:
89  IOBuffer _buffer;
90 };
91 
94 class OStream : public BasicOStream<char>
95 {
96  public:
99  explicit OStream(std::size_t bufferSize = 8192, bool extend = false);
100 
103  explicit OStream(IODevice& device, std::size_t bufferSize = 8192, bool extend = false);
104 
107  {}
108 
112  { return _buffer; }
113 
117  { return _buffer.device(); }
118 
121  void attach(IODevice& dev)
122  { _buffer.attach(dev); }
123 
126  void detach()
127  { _buffer.detach(); }
128 
131  void discard()
132  { _buffer.discard(); }
133 
136  void reset()
137  { _buffer.reset(); }
138 
139  private:
140  IOBuffer _buffer;
141 };
142 
145 class IOStream : public BasicIOStream<char>
146 {
147  public:
150  explicit IOStream(std::size_t bufferSize = 8192, bool extend = false);
151 
154  explicit IOStream(IODevice& device, std::size_t bufferSize = 8192, bool extend = false);
155 
158  {}
159 
163  { return _buffer; }
164 
168  { return _buffer.device(); }
169 
172  void attach(IODevice& dev)
173  { _buffer.attach(dev); }
174 
177  void detach()
178  { _buffer.detach(); }
179 
182  void discard()
183  { _buffer.discard(); }
184 
187  void reset()
188  { _buffer.reset(); }
189 
190  private:
191  IOBuffer _buffer;
192 };
193 
194 
195 inline IStream::IStream(std::size_t bufferSize, bool extend)
196 : BasicIStream<char>(0)
197 , _buffer(bufferSize, extend)
198 {
199  this->setBuffer(&_buffer);
200 }
201 
202 
203 inline IStream::IStream(IODevice& device, std::size_t bufferSize, bool extend)
204 : BasicIStream<char>(0)
205 , _buffer(device, bufferSize, extend)
206 {
207  this->setBuffer(&_buffer);
208 }
209 
210 
211 inline OStream::OStream(std::size_t bufferSize, bool extend)
212 : BasicOStream<char>(0)
213 , _buffer(bufferSize, extend)
214 {
215  this->setBuffer(&_buffer);
216 }
217 
218 
219 inline OStream::OStream(IODevice& device, std::size_t bufferSize, bool extend)
220 : BasicOStream<char>(0)
221 , _buffer(device, bufferSize, extend)
222 {
223  this->setBuffer(&_buffer);
224 }
225 
226 
227 inline IOStream::IOStream(std::size_t bufferSize, bool extend)
228 : BasicIOStream<char>(0)
229 , _buffer(bufferSize, extend)
230 {
231  this->setBuffer(&_buffer);
232 }
233 
234 
235 inline IOStream::IOStream(IODevice& device, std::size_t bufferSize, bool extend)
236 : BasicIOStream<char>(0)
237 , _buffer(device, bufferSize, extend)
238 {
239  this->setBuffer(&_buffer);
240 }
241 
242 } // namespace System
243 
244 } // namespace Pt
245 
246 #endif // Pt_System_IOStream_h
void setBuffer(BasicStreamBuffer< char > *sb)
Sets the buffer.
Definition: IOStream.h:127
void discard()
Discards the buffer.
Definition: IOStream.h:182
~OStream()
Destructor.
Definition: IOStream.h:106
IOBuffer & ioBuffer()
Returns the stream buffer.
Definition: IOStream.h:111
void setBuffer(BasicStreamBuffer< char > *sb)
Sets the buffer.
Definition: IOStream.h:175
IODevice * device()
Returns the I/O device.
Definition: IOStream.h:65
IODevice * device()
Returns the I/O device.
Definition: IOStream.h:167
void detach()
Detach from I/O device.
Definition: IOStream.h:75
IOStream(std::size_t bufferSize=8192, bool extend=false)
Construct with buffer size.
Definition: IOStream.h:227
void reset()
Discards and detaches.
Definition: IOStream.h:136
OStream(std::size_t bufferSize=8192, bool extend=false)
Construct with buffer size.
Definition: IOStream.h:211
void reset()
Discards and detaches.
Input stream.
Definition: IOStream.h:57
IOBuffer & ioBuffer()
Returns the stream buffer.
Definition: IOStream.h:162
Output stream for I/O devices.
Definition: IOStream.h:94
Input/Output stream.
Definition: IOStream.h:141
void attach(IODevice &dev)
Attach to I/O device.
Definition: IOStream.h:121
void attach(IODevice &ioDevice)
Attach to I/O device.
IODevice * device()
Returns the I/O device.
Definition: IOStream.h:116
Implements std::streambuf for I/O devices.
Definition: IOBuffer.h:43
~IOStream()
Destructor.
Definition: IOStream.h:157
Endpoint for I/O operations.
Definition: IODevice.h:55
void detach()
Detach from I/O device.
Definition: IOStream.h:177
void discard()
Discards the buffer.
Input stream for I/O devices.
Definition: IOStream.h:43
Output stream.
Definition: IOStream.h:101
void detach()
Detach from I/O device.
Definition: IOStream.h:126
void detach()
Detach from I/O device.
Input/Output stream for I/O devices.
Definition: IOStream.h:145
IStream(std::size_t bufferSize=8192, bool extend=false)
Construct with buffer size.
Definition: IOStream.h:195
void discard()
Discards the buffer.
Definition: IOStream.h:131
void attach(IODevice &dev)
Attach to I/O device.
Definition: IOStream.h:172
void discard()
Discards the buffer.
Definition: IOStream.h:80
void attach(IODevice &dev)
Attach to I/O device.
Definition: IOStream.h:70
~IStream()
Destructor.
Definition: IOStream.h:55
IOBuffer & ioBuffer()
Returns the stream buffer.
Definition: IOStream.h:60
void setBuffer(BasicStreamBuffer< char > *sb)
Sets the buffer.
Definition: IOStream.h:87
IODevice * device()
Returns the I/O device.
Definition: IOBuffer.h:61
void reset()
Discards and detaches.
Definition: IOStream.h:85
void reset()
Discards and detaches.
Definition: IOStream.h:187