📄 bufferedstreambuf.h
字号:
//
// BufferedStreamBuf.h
//
// $Id: //poco/Main/Foundation/include/Foundation/BufferedStreamBuf.h#5 $
//
// Definition of template BasicBufferedStreamBuf and class BufferedStreamBuf.
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#ifndef Foundation_BufferedStreamBuf_INCLUDED
#define Foundation_BufferedStreamBuf_INCLUDED
#ifndef Foundation_Foundation_INCLUDED
#include "Foundation/Foundation.h"
#endif
#ifndef STD_STREAMBUF_INCLUDED
#include <streambuf>
#define STD_STREAMBUF_INCLUDED
#endif
#ifndef STD_IOSFWD_INCLUDED
#include <iosfwd>
#define STD_IOSFWD_INCLUDED
#endif
#ifndef STD_IOS_INCLUDED
#include <ios>
#define STD_IOS_INCLUDED
#endif
Foundation_BEGIN
template<typename ch, typename tr>
class BasicBufferedStreamBuf: public std::basic_streambuf<ch, tr>
/// This is an implementation of a buffered streambuf
/// that greatly simplifies the implementation of
/// custom streambufs of various kinds.
/// Derived classes only have to override the methods
/// readFromDevice() or writeToDevice().
{
typedef std::basic_streambuf<ch, tr> Base;
typedef std::basic_ios<ch, tr> IOS;
typedef ch char_type;
typedef tr char_traits;
typedef typename Base::int_type int_type;
typedef typename Base::pos_type pos_type;
typedef typename Base::off_type off_type;
typedef typename IOS::openmode openmode;
public:
BasicBufferedStreamBuf(std::streamsize bufferSize, openmode mode)
{
_mode = mode;
_bufsize = bufferSize;
_pBuffer = new char_type[_bufsize];
this->setp(_pBuffer, _pBuffer + (_bufsize - 1));
this->setg(_pBuffer + 4, _pBuffer + 4, _pBuffer + 4);
}
~BasicBufferedStreamBuf()
{
delete _pBuffer;
}
virtual int_type overflow(int_type c)
{
if (!(_mode & IOS::out)) return char_traits::eof();
if (c != char_traits::eof())
{
*this->pptr() = char_traits::to_char_type(c);
this->pbump(1);
}
if (flush_buffer() == std::streamsize(-1)) return char_traits::eof();
return c;
}
virtual int_type underflow()
{
if (!(_mode & IOS::in)) return char_traits::eof();
if (this->gptr() && (this->gptr() < this->egptr()))
return char_traits::to_int_type(*this->gptr());
int putback = int(this->gptr() - this->eback());
if (putback > 4) putback = 4;
char_traits::copy(_pBuffer + (4 - putback), this->gptr() - putback, putback);
int n = readFromDevice(_pBuffer + 4, _bufsize - 4);
if (n <= 0) return char_traits::eof();
this->setg(_pBuffer + (4 - putback), _pBuffer + 4, _pBuffer + 4 + n);
// return next character
return char_traits::to_int_type(*this->gptr());
}
virtual int sync()
{
if (this->pptr() && this->pptr() > this->pbase())
{
if (flush_buffer() == -1) return -1;
}
return 0;
}
private:
virtual int readFromDevice(char_type* buffer, std::streamsize length)
{
return 0;
}
virtual int writeToDevice(const char_type* buffer, std::streamsize length)
{
return 0;
}
int flush_buffer()
{
int n = int(this->pptr() - this->pbase());
if (writeToDevice(this->pbase(), n) == n)
{
this->pbump(-n);
return n;
}
return -1;
}
std::streamsize _bufsize;
char_type* _pBuffer;
openmode _mode;
};
//
// We provide an instantiation for char
//
typedef BasicBufferedStreamBuf<char, std::char_traits<char> > BufferedStreamBuf;
Foundation_END
#endif // Foundation_BufferedStreamBuf_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -