BasicBufferedStream.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2009  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_BasicBufferedStream_hxx
00024 #define __ccxx_BasicBufferedStream_hxx
00025 
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/CircularBuffer.h++>
00028 #include <commonc++/IOException.h++>
00029 #include <commonc++/Stream.h++>
00030 
00031 #include <istream>
00032 #include <string>
00033 #include <streambuf>
00034 
00035 namespace ccxx {
00036 
00044 template <typename C> class BasicBufferedStream
00045   : public std::basic_iostream<C>
00046 {
00047   public:
00048 
00055   BasicBufferedStream(Stream& stream,
00056                       size_t bufferSize = DEFAULT_BUFFER_SIZE);
00057 
00059   static const size_t DEFAULT_BUFFER_SIZE;
00060 
00062   virtual ~BasicBufferedStream() throw();
00063 
00068   virtual void close(IOMode mode = IOReadWrite) throw();
00069 
00070   protected:
00071 
00076   class StreamBuf : public std::basic_streambuf<C>
00077   {
00078 #ifndef _MSC_VER
00079     typedef typename std::char_traits<C>::int_type int_type;
00080     typedef typename std::char_traits<C>::pos_type pos_type;
00081     typedef typename std::char_traits<C>::off_type off_type;
00082 #endif
00083     enum StreamOp { OpNone, OpRead, OpReadSeek, OpWrite, OpWriteSeek,
00084                     OpSeek };
00085 
00086     public:
00087 
00088     StreamBuf(size_t bufferSize, Stream& stream);
00089     ~StreamBuf();
00090 
00091     int sync();
00092 
00093     protected:
00094 
00095     int_type overflow(int_type c);
00096     int_type underflow();
00097 
00098     pos_type seekpos(pos_type streampos, std::ios::openmode mode);
00099     pos_type seekoff(off_type offset, std::ios::seekdir dir,
00100                      std::ios::openmode mode);
00101     std::streamsize showmanyc();
00102 
00103     private:
00104 
00105     inline void _resetp();
00106     inline void _resetg();
00107 
00108     Stream& _stream;
00109     CircularBuffer<C> *_inbuf;
00110     CircularBuffer<C> *_outbuf;
00111     int64_t _readPos;
00112     int64_t _writePos;
00113     StreamOp _lastOp;
00114   };
00115 
00116   Stream& _stream;
00117   StreamBuf *_buf;
00118 };
00119 
00120 #include <commonc++/BasicBufferedStreamImpl.h++>
00121 
00122 typedef BasicBufferedStream<char> BufferedCharStream;
00123 typedef BasicBufferedStream<byte_t> BufferedByteStream;
00124 
00125 }; // namespace ccxx
00126 
00127 #endif // __ccxx_BasicBufferedStream_hxx
00128 
00129 /* end of header file */

Generated on Sat Apr 17 23:03:05 2010 for libcommonc++ by  doxygen 1.5.9