libcommonc++  0.7
BasicBufferedStream.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_BasicBufferedStream_hxx
24 #define __ccxx_BasicBufferedStream_hxx
25 
26 #include <commonc++/Common.h++>
29 #include <commonc++/Stream.h++>
30 
31 #include <istream>
32 #include <string>
33 #include <streambuf>
34 
35 namespace ccxx {
36 
44 template <typename C> class BasicBufferedStream : public std::basic_iostream<C>
45 {
46  public:
47 
55  size_t bufferSize = DEFAULT_BUFFER_SIZE);
56 
58  static const size_t DEFAULT_BUFFER_SIZE;
59 
61  virtual ~BasicBufferedStream();
62 
68  virtual void close(IOMode mode = IOReadWrite);
69 
70  protected:
71 
77  class StreamBuf : public std::basic_streambuf<C>
78  {
79 #ifndef _MSC_VER
80  typedef typename std::char_traits<C>::int_type int_type;
81  typedef typename std::char_traits<C>::pos_type pos_type;
82  typedef typename std::char_traits<C>::off_type off_type;
83 #endif
84  enum StreamOp { OpNone, OpRead, OpReadSeek, OpWrite, OpWriteSeek,
85  OpSeek };
86 
87  public:
88 
89  StreamBuf(size_t bufferSize, Stream& stream);
90  ~StreamBuf();
91 
92  int sync();
93 
94  protected:
95 
96  int_type overflow(int_type c);
97  int_type underflow();
98 
99  pos_type seekpos(pos_type streampos, std::ios::openmode mode);
100  pos_type seekoff(off_type offset, std::ios::seekdir dir,
101  std::ios::openmode mode);
102  std::streamsize showmanyc();
103 
104  private:
105 
106  inline void _resetp();
107  inline void _resetg();
108 
109  Stream& _stream;
110  CircularBuffer<C> *_inbuf;
111  CircularBuffer<C> *_outbuf;
112  int64_t _readPos;
113  int64_t _writePos;
114  StreamOp _lastOp;
115  };
116 
118  Stream& _stream;
119  StreamBuf *_buf;
121 };
122 
123 #include <commonc++/BasicBufferedStreamImpl.h++>
124 
127 
128 } // namespace ccxx
129 
130 #endif // __ccxx_BasicBufferedStream_hxx
pos_type seekoff(off_type offset, std::ios::seekdir dir, std::ios::openmode mode)
StreamBuf(size_t bufferSize, Stream &stream)
IOMode
Stream I/O modes.
Definition: Stream.h++:36
pos_type seekpos(pos_type streampos, std::ios::openmode mode)
An unbuffered I/O stream.
Definition: Stream.h++:60
virtual ~BasicBufferedStream()
Destructor.
Read/write mode.
Definition: Stream.h++:42
BasicBufferedStream< byte_t > BufferedByteStream
Definition: BasicBufferedStream.h++:126
A buffered stream.
Definition: BasicBufferedStream.h++:44
BasicBufferedStream(Stream &stream, size_t bufferSize=DEFAULT_BUFFER_SIZE)
Construct a new BasicBufferedStream for the given stream and buffer size.
An implementation of basic_streambuf from the standard C++ library.
Definition: BasicBufferedStream.h++:77
BasicBufferedStream< char > BufferedCharStream
Definition: BasicBufferedStream.h++:125
virtual void close(IOMode mode=IOReadWrite)
Close the stream for reading, writing, or both.
static const size_t DEFAULT_BUFFER_SIZE
The default buffer size.
Definition: BasicBufferedStream.h++:58
Definition: AllocationMap.c++:25