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 
39 namespace Pt {
40 
41 namespace System {
42 
55 class PT_SYSTEM_API IODevice : public Selectable
56 {
57  public:
58  typedef std::char_traits<char>::pos_type pos_type;
59  typedef std::char_traits<char>::off_type off_type;
60  typedef std::ios_base::seekdir seekdir;
61 
62  public:
64  virtual ~IODevice();
65 
68  void close();
69 
72  void setTimeout(std::size_t timeout);
73 
76  void beginRead(char* buffer, std::size_t n);
77 
80  std::size_t endRead();
81 
94  std::size_t read(char* buffer, std::size_t n);
95 
98  void beginWrite(const char* buffer, std::size_t n);
99 
102  std::size_t endWrite();
103 
112  std::size_t write(const char* buffer, std::size_t n);
113 
116  bool seekable() const;
117 
122  pos_type seek(off_type offset, seekdir sd);
123 
129  std::size_t peek(char* buffer, std::size_t n);
130 
137  void sync();
138 
147  pos_type position();
148 
151  bool isEof() const;
152 
159  { return _inputReady; }
160 
168  { return _outputReady; }
169 
172  bool isReading() const
173  { return _rbuf != 0; }
174 
177  bool isWriting() const
178  { return _wbuf != 0; }
179 
180  char* rbuf() const
181  { return _rbuf; }
182 
183  std::size_t rbuflen() const
184  { return _rbuflen; }
185 
186  std::size_t ravail() const
187  { return _ravail; }
188 
189  const char* wbuf() const
190  { return _wbuf; }
191 
192  std::size_t wbuflen() const
193  { return _wbuflen; }
194 
195  std::size_t wavail() const
196  { return _wavail; }
197 
200  EventLoop* loop() const
201  { return _loop; }
202 
203  protected:
205  IODevice();
206 
207  virtual void onClose() = 0;
208 
209  virtual void onSetTimeout(std::size_t timeout) = 0;
210 
211  virtual std::size_t onBeginRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof) = 0;
212 
213  virtual std::size_t onEndRead(EventLoop& loop, char* buffer, std::size_t n, bool& eof) = 0;
214 
215  virtual std::size_t onRead(char* buffer, std::size_t count, bool& eof) = 0;
216 
217  virtual std::size_t onBeginWrite(EventLoop& loop, const char* buffer, std::size_t n) = 0;
218 
219  virtual std::size_t onEndWrite(EventLoop& loop, const char* buffer, std::size_t n) = 0;
220 
221  virtual std::size_t onWrite(const char* buffer, std::size_t count) = 0;
222 
223  virtual std::size_t onPeek(char* buffer, std::size_t)
224  { return 0; }
225 
226  virtual bool onSeekable() const
227  { return false; }
228 
229  virtual pos_type onSeek(off_type, std::ios::seekdir)
230  { throw IOError("Could not seek on device"); }
231 
232  virtual void onSync() const
233  { }
234 
235  void setEof(bool eof);
236 
237  virtual void onAttach(EventLoop& loop);
238 
239  virtual void onDetach(EventLoop& loop);
240 
241  virtual void onCancel();
242 
243  protected:
244  EventLoop* _loop;
245  char* _rbuf;
246  std::size_t _rbuflen;
247  std::size_t _ravail;
248  const char* _wbuf;
249  std::size_t _wbuflen;
250  std::size_t _wavail;
251  Signal<IODevice&> _inputReady;
252  Signal<IODevice&> _outputReady;
253  Pt::varint_t _reserved;
254 
255  private:
256  bool _eof;
257 };
258 
259 } // namespace System
260 
261 } // namespace Pt
262 
263 #endif // Pt_System_IODevice_h
EventLoop * loop() const
Returns the used event loop.
Definition: IODevice.h:200
Dispatches operations through an event loop.
Definition: Selectable.h:44
Multicast Signal to call multiple slots.
Definition: Signal.h:109
Endpoint for I/O operations.
Definition: IODevice.h:55
bool isReading() const
Returns true if the device is reading.
Definition: IODevice.h:172
Signal< IODevice & > & inputReady()
Notifies about available data.
Definition: IODevice.h:158
Thread-safe event loop supporting I/O multiplexing and Timers.
Definition: EventLoop.h:72
Signal< IODevice & > & outputReady()
Notifies when data can be written.
Definition: IODevice.h:167
bool isWriting() const
Returns true if the device is writing.
Definition: IODevice.h:177