streambuffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005 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 #ifndef cxxtools_System_StreamBuffer_h
29 #define cxxtools_System_StreamBuffer_h
30 
31 #include <ios>
32 #include <streambuf>
33 #include <cxxtools/iodevice.h>
34 
35 namespace cxxtools
36 {
37 
38 template <typename CharT>
39 class BasicStreamBuffer : public std::basic_streambuf<CharT>
40 {
41  public:
42  std::streamsize speekn(CharT* buffer, std::streamsize size)
43  { return this->xspeekn(buffer, size); }
44 
45  std::streamsize out_avail()
46  {
47  if( this->pptr() )
48  return this->pptr() - this->pbase();
49 
50  return this->showfull();
51  }
52 
53  protected:
54  virtual std::streamsize xspeekn(CharT* buffer, std::streamsize size)
55  {
56  if(size == 0)
57  return 0;
58 
59  buffer[0] = this->sgetc();
60  return 1;
61  }
62 
63  virtual std::streamsize showfull()
64  { return 0; }
65 };
66 
68 class StreamBuffer : public BasicStreamBuffer<char>,
69  public Connectable
70 {
71  public:
73  explicit StreamBuffer(IODevice& ioDevice, size_t bufferSize = 8192, bool extend = false);
74 
76  explicit StreamBuffer(size_t bufferSize = 8192, bool extend = false);
77 
78  ~StreamBuffer();
79 
80  void attach(IODevice& ioDevice);
81 
83  { return _ioDevice; }
84 
91  void beginRead();
92 
98  void endRead();
99 
113  size_t beginWrite();
114 
121  size_t endWrite();
122 
128  bool reading() const
129  { return _ioDevice && _ioDevice->reading(); }
130 
136  bool writing() const
137  { return _ioDevice && _ioDevice->writing(); }
138 
144  void discard();
145 
148  Signal<StreamBuffer&> inputReady;
149 
152  Signal<StreamBuffer&> outputReady;
153 
154  protected:
155  virtual int sync();
156 
157  virtual std::streamsize showfull();
158 
159  virtual std::streamsize xspeekn(char* buffer, std::streamsize size);
160 
161  virtual int_type underflow();
162 
163  virtual int_type overflow(int_type ch);
164 
165  virtual int_type pbackfail(int_type c);
166 
169  virtual pos_type seekoff(off_type offset, std::ios::seekdir sd, std::ios::openmode mode);
170 
173  virtual pos_type seekpos(pos_type p, std::ios::openmode mode );
174 
175  private:
176  void onRead(IODevice& dev);
177 
178  void onWrite(IODevice& dev);
179 
180  private:
181  IODevice* _ioDevice;
182  size_t _ibufferSize;
183  char* _ibuffer;
184  std::size_t _obufferSize;
185  char* _obuffer;
186  const size_t _pbmax;
187  bool _oextend;
188 };
189 
190 } // namespace cxxtools
191 
192 #endif