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
97class PT_SYSTEM_API IODevice : public Selectable
98{
99 public:
100 typedef std::char_traits<char>::pos_type pos_type;
101 typedef std::char_traits<char>::off_type off_type;
102 typedef std::ios_base::seekdir seekdir;
103
104 public:
106 virtual ~IODevice();
107
110 void close();
111
114 void setTimeout(std::size_t timeout);
115
118 void beginRead(char* buffer, std::size_t n);
119
122 std::size_t endRead();
123
136 std::size_t read(char* buffer, std::size_t n);
137
140 void beginWrite(const char* buffer, std::size_t n);
141
144 std::size_t endWrite();
145
154 std::size_t write(const char* buffer, std::size_t n);
155
158 bool seekable() const;
159
164 pos_type seek(off_type offset, seekdir sd);
165
171 std::size_t peek(char* buffer, std::size_t n);
172
179 void sync();
180
189 pos_type position();
190
193 bool isEof() const;
194
201 { return _inputReady; }
202
210 { return _outputReady; }
211
214 bool isReading() const
215 { return _rbuf != 0; }
216
219 bool isWriting() const
220 { return _wbuf != 0; }
221
222 char* rbuf() const
223 { return _rbuf; }
224
225 std::size_t rbuflen() const
226 { return _rbuflen; }
227
228 std::size_t ravail() const
229 { return _ravail; }
230
231 const char* wbuf() const
232 { return _wbuf; }
233
234 std::size_t wbuflen() const
235 { return _wbuflen; }
236
237 std::size_t wavail() const
238 { return _wavail; }
239
243 { return parent(); }
244
245 protected:
248
249 virtual void onClose() = 0;
250
251 virtual void onSetTimeout(std::size_t timeout) = 0;
252
253 virtual std::size_t onBeginRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof) = 0;
254
255 virtual std::size_t onEndRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof) = 0;
256
257 virtual std::size_t onRead(char* buffer, std::size_t count, bool& eof) = 0;
258
259 virtual std::size_t onBeginWrite(EventLoop& loop, const char* buffer, std::size_t n) = 0;
260
261 virtual std::size_t onEndWrite(EventLoop& loop, const char* buffer, std::size_t n) = 0;
262
263 virtual std::size_t onWrite(const char* buffer, std::size_t count) = 0;
264
265 virtual std::size_t onPeek(char*, std::size_t)
266 { return 0; }
267
268 virtual bool onSeekable() const
269 { return false; }
270
271 virtual pos_type onSeek(off_type, std::ios::seekdir)
272 { throw IOError("Could not seek on device"); }
273
274 virtual void onSync() const
275 { }
276
277 void setEof(bool eof);
278
279 virtual void onCancel();
280
281 protected:
282 char* _rbuf;
283 std::size_t _rbuflen;
284 std::size_t _ravail;
285 const char* _wbuf;
286 std::size_t _wbuflen;
287 std::size_t _wavail;
288 Signal<IODevice&> _inputReady;
289 Signal<IODevice&> _outputReady;
290 Pt::varint_t _reserved;
291
292 private:
293 bool _eof;
294};
295
296} // namespace System
297
298} // namespace Pt
299
300#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:219
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:214
void sync()
Synchronize device.
EventLoop * loop() const
Returns the used event loop.
Definition IODevice.h:242
Signal< IODevice & > & outputReady()
Notifies when data can be written.
Definition IODevice.h:209
Signal< IODevice & > & inputReady()
Notifies about available data.
Definition IODevice.h:200
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