BasicBufferedStream.h++
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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 };
00126
00127 #endif // __ccxx_BasicBufferedStream_hxx
00128
00129