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/api.h>
35 #include <cxxtools/ioerror.h>
36 #include <cxxtools/selectable.h>
37 #include <limits>
38 #include <ios>
39 
40 namespace cxxtools {
41 
43 {
44  IOS_Sync = 0,
45  IOS_Async = 1L << 0,
46  IOS_Read = 1L << 1,
47  IOS_Write = 1L << 2,
48  IOS_AtEnd = 1L << 3,
49  IOS_Append = 1L << 4,
50  IOS_Trunc = 1L << 5,
51  IOS_OpenModeEnd = 1L << 16
52 };
53 
55 { return IOS_OpenMode(static_cast<int>(a) & static_cast<int>(b)); }
56 
58 { return IOS_OpenMode(static_cast<int>(a) | static_cast<int>(b)); }
59 
61 { return IOS_OpenMode(static_cast<int>(a) ^ static_cast<int>(b)); }
62 
64 { return a = a | b; }
65 
67 { return a = a & b; }
68 
70 { return a = a ^ b; }
71 
73 { return IOS_OpenMode(~static_cast<int>(a)); }
74 
75 
76 class IODeviceImpl;
77 
91 {
92  public:
93  typedef std::char_traits<char>::pos_type pos_type;
94 
95  typedef std::char_traits<char>::off_type off_type;
96 
97  typedef std::ios_base::seekdir SeekDir;
98 
100 
101  static const OpenMode Sync = IOS_Sync;
102  static const OpenMode Async = IOS_Async;
103  static const OpenMode Read = IOS_Read;
104  static const OpenMode Write = IOS_Write;
105  static const OpenMode AtEnd = IOS_AtEnd;
106  static const OpenMode Append = IOS_Append;
107  static const OpenMode Trunc = IOS_Trunc;
108 
109  public:
111  virtual ~IODevice();
112 
113  void beginRead(char* buffer, size_t n);
114 
115  size_t endRead();
116 
118 
129  size_t read(char* buffer, size_t n);
130 
131  size_t beginWrite(const char* buffer, size_t n);
132 
133  size_t endWrite();
134 
136 
147  size_t write(const char* buffer, size_t n);
148 
151  void cancel();
152 
154 
159  bool seekable() const;
160 
162 
171  pos_type seek(off_type offset, std::ios::seekdir sd);
172 
174 
184  size_t peek(char* buffer, size_t n);
185 
187 
192  void sync();
193 
195 
202  pos_type position();
203 
205 
210  bool eof() const;
211 
214  bool async() const;
215 
221  Signal<IODevice&> inputReady;
222 
229  Signal<IODevice&> outputReady;
230 
231  virtual IODeviceImpl& ioimpl() = 0;
232 
233  bool reading() const
234  { return _rbuf != 0; }
235 
236  bool writing() const
237  { return _wbuf != 0; }
238 
239  char* rbuf() const
240  { return _rbuf; }
241 
242  size_t rbuflen() const
243  { return _rbuflen; }
244 
245  size_t ravail() const
246  { return _ravail; }
247 
248  const char* wbuf() const
249  { return _wbuf; }
250 
251  size_t wbuflen() const
252  { return _wbuflen; }
253 
254  size_t wavail() const
255  { return _wavail; }
256 
257  protected:
259  IODevice();
260 
261  virtual size_t onBeginRead(char* buffer, size_t n, bool& eof) = 0;
262 
263  virtual size_t onEndRead(bool& eof) = 0;
264 
266  virtual size_t onRead(char* buffer, size_t count, bool& eof) = 0;
267 
268  virtual size_t onBeginWrite(const char* buffer, size_t n) = 0;
269 
270  virtual size_t onEndWrite() = 0;
271 
273  virtual size_t onWrite(const char* buffer, size_t count) = 0;
274 
275  virtual void onCancel() = 0;
276 
278  virtual size_t onPeek(char*, size_t)
279  { return 0; }
280 
282  virtual bool onSeekable() const
283  { return false; }
284 
286  virtual pos_type onSeek(off_type, std::ios::seekdir)
287  { throw IOError("Could not seek on device."); }
288 
290  virtual void onSync() const
291  { }
292 
294  virtual size_t onSize() const
295  { return 0; }
296 
298  void setEof(bool eof);
299 
301  void setAsync(bool async);
302 
303  private:
304  bool _eof;
305  bool _async;
306 
307  protected:
308  char* _rbuf;
309  size_t _rbuflen;
310  size_t _ravail;
311  const char* _wbuf;
312  size_t _wbuflen;
313  size_t _wavail;
314  void* _reserved;
315 };
316 
317 } // namespace cxxtools
318 
319 #endif