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
37namespace Pt {
38
39namespace System {
40
58class PT_SYSTEM_API IOBuffer : public BasicStreamBuffer<char>
59 , public Connectable
60{
61 public:
64 explicit IOBuffer(std::size_t bufferSize = 8192, bool extend = false);
65
68 explicit IOBuffer(IODevice& ioDevice, std::size_t bufferSize = 8192, bool extend = false);
69
73
77 { return _ioDevice; }
78
82 { return _inputReady; }
83
87 { return _outputReady; }
88
91 void attach(IODevice& ioDevice);
92
95 void detach();
96
99 void reset();
100
103 void discard();
104
107 void beginRead();
108
110 void onRead(IODevice& dev);
111
114 std::size_t endRead();
115
119
121 void onWrite(IODevice& dev);
122
125 std::size_t endWrite();
126
129 bool isReading() const;
130
133 bool isWriting() const;
134
135 protected:
137 void init(std::size_t bufferSize, bool extend);
138
139 // inherit docs
140 virtual std::streamsize showmanyc();
141
142 // inherit docs
143 virtual std::streamsize showfull();
144
145 // inherit docs
146 virtual int sync();
147
148 // inherit docs
149 virtual int_type underflow();
150
151 // inherit docs
152 virtual int_type overflow(int_type ch);
153
154 // inherit docs
155 virtual pos_type seekoff(off_type offset, std::ios::seekdir sd, std::ios::openmode mode);
156
157 // inherit docs
158 virtual pos_type seekpos(pos_type p, std::ios::openmode mode );
159
160 // inherit docs
161 virtual int_type pbackfail(int_type c);
162
163 private:
164 Signal<IOBuffer&> _inputReady;
165 Signal<IOBuffer&> _outputReady;
166 IODevice* _ioDevice;
167 std::size_t _ibufferSize;
168 char* _ibuffer;
169 std::size_t _obufferSize;
170 char* _obuffer;
171 bool _oextend;
172
173 static const int _pbmax = 4;
174};
175
176} // namespace System
177
178} // namespace Pt
179
180#endif // Pt_System_IOBuffer_h
BasicStreamBuffer()
Definition StreamBuffer.h:139
Connectable()
Default constructor.
Multicast Signal to call multiple slots.
Definition Signal.h:190
Signal< IOBuffer & > & outputReady()
Notifies when a part of the buffer was written.
Definition IOBuffer.h:86
IOBuffer(std::size_t bufferSize=8192, bool extend=false)
Construct with buffer size.
std::size_t endRead()
Ends to read.
bool isWriting() const
Returns true if a write operation is running.
void beginWrite()
Begins to write buffered data to the I/O device.
void beginRead()
Begins to read from the I/O device into the buffer.
void discard()
Discards the buffer.
virtual std::streamsize showfull()
Returns the number of characters buffered for output.
std::size_t endWrite()
Ends writing buffered data to the I/O device.
IODevice * device()
Returns the I/O device.
Definition IOBuffer.h:76
IOBuffer(IODevice &ioDevice, std::size_t bufferSize=8192, bool extend=false)
Construct with I/O device.
Signal< IOBuffer & > & inputReady()
Notifies when input was read.
Definition IOBuffer.h:81
~IOBuffer()
Destructor.
void detach()
Detach from I/O device.
void attach(IODevice &ioDevice)
Attach to I/O device.
bool isReading() const
Returns true if a read operation is running.
void reset()
Discards and detaches.
Endpoint for I/O operations.
Definition IODevice.h:96
System programming
Definition Connection.h:26
Core module.
Definition Allocator.h:33