📄 unbufferedstreambuf.h
字号:
//
// UnufferedStreamBuf.h
//
// $Id: //poco/Main/Foundation/include/Foundation/UnbufferedStreamBuf.h#5 $
//
// Definition of template BasicUnbufferedStreamBuf and class UnbufferedStreamBuf.
//
// 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_UnbufferedStreamBuf_INCLUDED
#define Foundation_UnbufferedStreamBuf_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 BasicUnbufferedStreamBuf: public std::basic_streambuf<ch, tr>
/// This is an implementation of an unbuffered 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:
BasicUnbufferedStreamBuf()
{
_pb = char_traits::eof();
_ispb = false;
this->setp(0, 0);
this->setg(0, 0, 0);
}
~BasicUnbufferedStreamBuf()
{
}
virtual int_type overflow(int_type c)
{
if (c != char_traits::eof())
return writeToDevice(char_traits::to_char_type(c));
else
return c;
}
virtual int_type underflow()
{
if (_ispb)
{
return _pb;
}
else
{
int_type c = readFromDevice();
if (c != char_traits::eof())
{
_ispb = true;
_pb = c;
}
return c;
}
}
virtual int_type uflow()
{
if (_ispb)
{
_ispb = false;
return _pb;
}
else
{
int_type c = readFromDevice();
if (c != char_traits::eof())
{
_pb = c;
}
return c;
}
}
virtual int_type pbackfail(int_type c)
{
if (_ispb)
{
return char_traits::eof();
}
else
{
_ispb = true;
_pb = c;
return c;
}
}
virtual std::streamsize xsgetn(char_type* p, std::streamsize count)
/// Some platforms (for example, Compaq C++) have buggy implementations of
/// xsgetn that handle null buffers incorrectly.
/// Anyway, it does not hurt to provide an optimized implementation
/// of xsgetn for this streambuf implementation.
{
if (count == 0) return 0;
std::streamsize copied = 0;
int_type c = uflow();
while (count > 0 && c != char_traits::eof())
{
*p++ = char_traits::to_char_type(c);
++copied;
--count;
c = uflow();
}
return copied;
}
private:
virtual int_type readFromDevice()
{
return char_traits::eof();
}
virtual int_type writeToDevice(char_type c)
{
return char_traits::eof();
}
int_type _pb;
bool _ispb;
};
//
// We provide an instantiation for char
//
typedef BasicUnbufferedStreamBuf<char, std::char_traits<char> > UnbufferedStreamBuf;
Foundation_END
#endif // Foundation_UnbufferedStreamBuf_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -