IODevice.h
1/*
2 * Copyright (C) 2006-2013 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_IODevice_h
30#define Pt_System_IODevice_h
31
32#include <Pt/System/Api.h>
33#include <Pt/System/IOError.h>
34#include <Pt/System/Selectable.h>
35#include <Pt/Types.h>
36#include <Pt/Signal.h>
37#include <ios>
38
39namespace Pt {
40
41namespace System {
42
95class PT_SYSTEM_API IODevice : public Selectable
96{
97 public:
98 typedef std::char_traits<char>::pos_type pos_type;
99 typedef std::char_traits<char>::off_type off_type;
100 typedef std::ios_base::seekdir seekdir;
101
102 public:
104 virtual ~IODevice();
105
108 void close();
109
112 void setTimeout(std::size_t timeout);
113
116 void beginRead(char* buffer, std::size_t n);
117
120 std::size_t endRead();
121
134 std::size_t read(char* buffer, std::size_t n);
135
138 void beginWrite(const char* buffer, std::size_t n);
139
142 std::size_t endWrite();
143
152 std::size_t write(const char* buffer, std::size_t n);
153
156 bool seekable() const;
157
162 pos_type seek(off_type offset, seekdir sd);
163
169 std::size_t peek(char* buffer, std::size_t n);
170
177 void sync();
178
187 pos_type position();
188
191 bool isEof() const;
192
199 { return _inputReady; }
200
208 { return _outputReady; }
209
212 bool isReading() const
213 { return _rbuf != 0; }
214
217 bool isWriting() const
218 { return _wbuf != 0; }
219
220 char* rbuf() const
221 { return _rbuf; }
222
223 std::size_t rbuflen() const
224 { return _rbuflen; }
225
226 std::size_t ravail() const
227 { return _ravail; }
228
229 const char* wbuf() const
230 { return _wbuf; }
231
232 std::size_t wbuflen() const
233 { return _wbuflen; }
234
235 std::size_t wavail() const
236 { return _wavail; }
237
241 { return parent(); }
242
243 protected:
246
247 virtual void onClose() = 0;
248
249 virtual void onSetTimeout(std::size_t timeout) = 0;
250
251 virtual std::size_t onBeginRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof) = 0;
252
253 virtual std::size_t onEndRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof) = 0;
254
255 virtual std::size_t onRead(char* buffer, std::size_t count, bool& eof) = 0;
256
257 virtual std::size_t onBeginWrite(EventLoop& loop, const char* buffer, std::size_t n) = 0;
258
259 virtual std::size_t onEndWrite(EventLoop& loop, const char* buffer, std::size_t n) = 0;
260
261 virtual std::size_t onWrite(const char* buffer, std::size_t count) = 0;
262
263 virtual std::size_t onPeek(char*, std::size_t)
264 { return 0; }
265
266 virtual bool onSeekable() const
267 { return false; }
268
269 virtual pos_type onSeek(off_type, std::ios::seekdir)
270 { throw IOError("Could not seek on device"); }
271
272 virtual void onSync() const
273 { }
274
275 void setEof(bool eof);
276
277 virtual void onCancel();
278
279 protected:
280 char* _rbuf;
281 std::size_t _rbuflen;
282 std::size_t _ravail;
283 const char* _wbuf;
284 std::size_t _wbuflen;
285 std::size_t _wavail;
286 Signal<IODevice&> _inputReady;
287 Signal<IODevice&> _outputReady;
288 Pt::varint_t _reserved;
289
290 private:
291 bool _eof;
292};
293
294} // namespace System
295
296} // namespace Pt
297
298#endif // Pt_System_IODevice_h
I/O error.
Definition IOError.h:44
Multicast Signal to call multiple slots.
Definition Signal.h:190
Event loop of a thread or process.
Definition EventLoop.h:83
void beginWrite(const char *buffer, std::size_t n)
Begins to write data.
std::size_t endRead()
Ends reading data.
bool isWriting() const
Returns true if the device is writing.
Definition IODevice.h:217
bool isEof() const
Returns if the device has reached EOF.
virtual void onCancel()
Blocks until operation has cancelled.
void setTimeout(std::size_t timeout)
Sets the timeout for blocking I/O in milliseconds.
void close()
Closes the device.
std::size_t endWrite()
Ends writing data.
std::size_t write(const char *buffer, std::size_t n)
Write data to I/O device.
pos_type seek(off_type offset, seekdir sd)
Moves the read position to the given offset.
virtual ~IODevice()
Destructor.
IODevice()
Default Constructor.
std::size_t peek(char *buffer, std::size_t n)
Peek data from I/O device without consuming them.
std::size_t read(char *buffer, std::size_t n)
Read data from I/O device.
pos_type position()
Returns the current I/O position.
bool seekable() const
Returns true if device is seekable.
void beginRead(char *buffer, std::size_t n)
Begins to read data.
bool isReading() const
Returns true if the device is reading.
Definition IODevice.h:212
void sync()
Synchronize device.
EventLoop * loop() const
Returns the used event loop.
Definition IODevice.h:240
Signal< IODevice & > & outputReady()
Notifies when data can be written.
Definition IODevice.h:207
Signal< IODevice & > & inputReady()
Notifies about available data.
Definition IODevice.h:198
Selectable()
Default Constructor.
EventLoop * parent() const
Returns the used event loop.
Definition Selectable.h:80
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33