IOBuffer.h
1 /*
2  * Copyright (C) 2005-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_IOBuffer_h
30 #define Pt_System_IOBuffer_h
31 
32 #include <Pt/System/Api.h>
33 #include <Pt/System/IODevice.h>
34 #include <Pt/Signal.h>
35 #include <Pt/StreamBuffer.h>
36 
37 namespace Pt {
38 
39 namespace System {
40 
43 class PT_SYSTEM_API IOBuffer : public BasicStreamBuffer<char>
44  , public Connectable
45 {
46  public:
49  explicit IOBuffer(std::size_t bufferSize = 8192, bool extend = false);
50 
53  explicit IOBuffer(IODevice& ioDevice, std::size_t bufferSize = 8192, bool extend = false);
54 
57  ~IOBuffer();
58 
62  { return _ioDevice; }
63 
67  { return _inputReady; }
68 
72  { return _outputReady; }
73 
76  void attach(IODevice& ioDevice);
77 
80  void detach();
81 
84  void reset();
85 
88  void discard();
89 
92  void beginRead();
93 
95  void onRead(IODevice& dev);
96 
99  std::size_t endRead();
100 
103  void beginWrite();
104 
106  void onWrite(IODevice& dev);
107 
108  std::size_t endWrite();
109 
112  bool isReading() const;
113 
116  bool isWriting() const;
117 
118  protected:
120  void init(std::size_t bufferSize, bool extend);
121 
122  // inherit docs
123  virtual std::streamsize showmanyc();
124 
125  // inherit docs
126  virtual std::streamsize showfull();
127 
128  // inherit docs
129  virtual int sync();
130 
131  // inherit docs
132  virtual int_type underflow();
133 
134  // inherit docs
135  virtual int_type overflow(int_type ch);
136 
137  // inherit docs
138  virtual pos_type seekoff(off_type offset, std::ios::seekdir sd, std::ios::openmode mode);
139 
140  // inherit docs
141  virtual pos_type seekpos(pos_type p, std::ios::openmode mode );
142 
143  // inherit docs
144  virtual int_type pbackfail(int_type c);
145 
146  private:
147  Signal<IOBuffer&> _inputReady;
148  Signal<IOBuffer&> _outputReady;
149  IODevice* _ioDevice;
150  std::size_t _ibufferSize;
151  char* _ibuffer;
152  std::size_t _obufferSize;
153  char* _obuffer;
154  bool _oextend;
155 
156  static const int _pbmax = 4;
157 };
158 
159 } // namespace System
160 
161 } // namespace Pt
162 
163 #endif // Pt_System_IOBuffer_h
Connection Management for Signal and Slot Objects.
Definition: Connectable.h:49
Buffer for input and output streams.
Definition: StreamBuffer.h:51
Signal< IOBuffer & > & outputReady()
Notifies when a part of the buffer was written.
Definition: IOBuffer.h:71
Signal< IOBuffer & > & inputReady()
Notifies when input was read.
Definition: IOBuffer.h:66
Multicast Signal to call multiple slots.
Definition: Signal.h:109
Implements std::streambuf for I/O devices.
Definition: IOBuffer.h:43
Endpoint for I/O operations.
Definition: IODevice.h:55
IODevice * device()
Returns the I/O device.
Definition: IOBuffer.h:61