iodevice.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2007 Laurentiu-Gheorghe Crisan
3  * Copyright (C) 2006-2007 Marc Boris Duerner
4  * Copyright (C) 2006-2007 PTV AG
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * As a special exception, you may use this file as part of a free
12  * software library without restriction. Specifically, if other files
13  * instantiate templates or use macros or inline functions from this
14  * file, or you compile this file and link it with other files to
15  * produce an executable, this file does not by itself cause the
16  * resulting executable to be covered by the GNU General Public
17  * License. This exception does not however invalidate any other
18  * reasons why the executable file might be covered by the GNU Library
19  * General Public License.
20  *
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24  * Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public
27  * License along with this library; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29  */
30 #ifndef CXXTOOLS_IODevice_h
31 #define CXXTOOLS_IODevice_h
32 
33 #include <cxxtools/signal.h>
34 #include <cxxtools/ioerror.h>
35 #include <cxxtools/selectable.h>
36 #include <limits>
37 #include <ios>
38 
39 namespace cxxtools {
40 
42 {
43  IOS_Sync = 0,
44  IOS_Async = 1L << 0,
45  IOS_Read = 1L << 1,
46  IOS_Write = 1L << 2,
47  IOS_AtEnd = 1L << 3,
48  IOS_Append = 1L << 4,
49  IOS_Trunc = 1L << 5,
50  IOS_OpenModeEnd = 1L << 16
51 };
52 
54 { return IOS_OpenMode(static_cast<int>(a) & static_cast<int>(b)); }
55 
57 { return IOS_OpenMode(static_cast<int>(a) | static_cast<int>(b)); }
58 
60 { return IOS_OpenMode(static_cast<int>(a) ^ static_cast<int>(b)); }
61 
63 { return a = a | b; }
64 
66 { return a = a & b; }
67 
69 { return a = a ^ b; }
70 
72 { return IOS_OpenMode(~static_cast<int>(a)); }
73 
74 
75 class IODeviceImpl;
76 
89 class IODevice : public Selectable
90 {
91  public:
92  typedef std::char_traits<char>::pos_type pos_type;
93 
94  typedef std::char_traits<char>::off_type off_type;
95 
96  typedef std::ios_base::seekdir SeekDir;
97 
99 
100  static const OpenMode Sync = IOS_Sync;
101  static const OpenMode Async = IOS_Async;
102  static const OpenMode Read = IOS_Read;
103  static const OpenMode Write = IOS_Write;
104  static const OpenMode AtEnd = IOS_AtEnd;
105  static const OpenMode Append = IOS_Append;
106  static const OpenMode Trunc = IOS_Trunc;
107 
108  public:
110  virtual ~IODevice();
111 
112  void beginRead(char* buffer, size_t n);
113 
114  size_t endRead();
115 
117 
128  size_t read(char* buffer, size_t n);
129 
130  size_t beginWrite(const char* buffer, size_t n);
131 
132  size_t endWrite();
133 
135 
146  size_t write(const char* buffer, size_t n);
147 
150  void cancel();
151 
153 
158  bool seekable() const;
159 
161 
170  pos_type seek(off_type offset, std::ios::seekdir sd);
171 
173 
183  size_t peek(char* buffer, size_t n);
184 
186 
191  void sync();
192 
194 
201  pos_type position();
202 
204 
209  bool eof() const;
210 
213  bool async() const;
214 
220  Signal<IODevice&> inputReady;
221 
228  Signal<IODevice&> outputReady;
229 
230  virtual IODeviceImpl& ioimpl() = 0;
231 
232  bool reading() const
233  { return _rbuf != 0; }
234 
235  bool writing() const
236  { return _wbuf != 0; }
237 
238  char* rbuf() const
239  { return _rbuf; }
240 
241  size_t rbuflen() const
242  { return _rbuflen; }
243 
244  size_t ravail() const
245  { return _ravail; }
246 
247  const char* wbuf() const
248  { return _wbuf; }
249 
250  size_t wbuflen() const
251  { return _wbuflen; }
252 
253  size_t wavail() const
254  { return _wavail; }
255 
256  protected:
258  IODevice();
259 
260  virtual size_t onBeginRead(char* buffer, size_t n, bool& eof) = 0;
261 
262  virtual size_t onEndRead(bool& eof) = 0;
263 
265  virtual size_t onRead(char* buffer, size_t count, bool& eof) = 0;
266 
267  virtual size_t onBeginWrite(const char* buffer, size_t n) = 0;
268 
269  virtual size_t onEndWrite() = 0;
270 
272  virtual size_t onWrite(const char* buffer, size_t count) = 0;
273 
274  virtual void onCancel() = 0;
275 
277  virtual size_t onPeek(char*, size_t)
278  { return 0; }
279 
281  virtual bool onSeekable() const
282  { return false; }
283 
285  virtual pos_type onSeek(off_type, std::ios::seekdir)
286  { throw IOError("Could not seek on device."); }
287 
289  virtual void onSync() const
290  { }
291 
293  virtual size_t onSize() const
294  { return 0; }
295 
297  void setEof(bool eof);
298 
300  void setAsync(bool async);
301 
302  private:
303  bool _eof;
304  bool _async;
305 
306  protected:
307  char* _rbuf;
308  size_t _rbuflen;
309  size_t _ravail;
310  const char* _wbuf;
311  size_t _wbuflen;
312  size_t _wavail;
313  void* _reserved;
314 };
315 
316 } // namespace cxxtools
317 
318 #endif