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/api.h>
34 #include <cxxtools/iodevice.h>
35 
36 namespace cxxtools {
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 
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 
82  IODevice* device();
83 
84  void beginRead();
85 
86  void endRead();
87 
88  size_t beginWrite();
89 
90  size_t endWrite();
91 
92  void discard();
93 
94  Signal<StreamBuffer&> inputReady;
95 
96  Signal<StreamBuffer&> outputReady;
97 
98  protected:
99  virtual int sync();
100 
101  virtual std::streamsize showfull();
102 
103  virtual std::streamsize xspeekn(char* buffer, std::streamsize size);
104 
105  virtual int_type underflow();
106 
107  virtual int_type overflow(int_type ch);
108 
109  virtual int_type pbackfail(int_type c);
110 
113  virtual pos_type seekoff(off_type offset, std::ios::seekdir sd, std::ios::openmode mode);
114 
117  virtual pos_type seekpos(pos_type p, std::ios::openmode mode );
118 
119  private:
120  void onRead(IODevice& dev);
121 
122  void onWrite(IODevice& dev);
123 
124  private:
125  IODevice* _ioDevice;
126  size_t _ibufferSize;
127  char* _ibuffer;
128  std::size_t _obufferSize;
129  char* _obuffer;
130  const size_t _pbmax;
131  bool _oextend;
132 };
133 
134 } // namespace cxxtools
135 
136 #endif