template class std::basic_streambuf控制输入,输出字符序列.
之前几篇博客介绍各个stream并不负责实际的读写,而是委托给stream buffer完成.
处理stream buffer的一般性接口十分简单:
stream::rdbuf() 取得一个buffer的指针指向某个stream的buffer.
stream::rdbuf(buffer* ptr) 给某个stream设置一个buffer,并返回之前的buffer的指针.
stream buffer即负责Input,也负责Output.
因此在buffer内其实可以想象成2个数组一个负责Input(read缓冲区中的内容),一个负责Output(write入缓冲区).
Output缓冲区(向stream的buffer中写入数据):
output缓冲区由3个pointer维护,这三个pointer分别由函数pbase(),pptr()和epptr()取得
1,pbase()返回output stream缓冲区的开始.
2,pptr()获得一个指向当前output stream buffer 可以写入的位置.
3,epptr()返回的是output缓冲区的尾端,类似尾后迭代器.
Input缓冲区(从stream的buffer读取数据):
Input缓冲区也由3个pointer维护,这三个pointer可由eback(),gptr(),egptr().
1,eback()返回input缓冲区的起点.
2,gptr()返回一个指向当前input stream buffer 可以读取的位置.
3,egptr()返回的是input缓冲区的尾端,类似尾后迭代器.
Constructor:
protected:
basic_streambuf();
protected:
basic_streambuf(const basic_streambuf& rhs);
1, 默认构造函数为protected,意味着只有继承了该class的其他class才能使用.
2,拷贝构造函数也为protected,意味着只有继承了该class的其他class才能使用.
protected:
std::basic_streambuf::operator=
basic_streambuf& operator=( const basic_streambuf& other );
protected的拷贝复制运算符,同样也是只有继承了该class的其他class才能使用.
Output stream所使用的buffer接口:
std::basic_streambuf::setp
void setp( char_type* pbeg, char_type* pend );
前面我们介绍了pbase(), pptr(), epptr()函数用来返回指向缓冲区pointer,这些指针的开始位置,尾后位置都是通过该函数设置的:
pbeg:用来设置指向buffer开始的位置的指针.
pend: 用来设置指向buffer尾后位置的指针.
std::basic_streambuf::sputc
int_type sputc( char_type ch );
将字符ch拷贝进去缓冲区.
std::basic_streambuf::sputn
std::streamsize sputn( const char_type* s, std::streamsize count );
protected:
virtual std::streamsize xsputn( const char_type* s, std::streamsize count );
将字符序列s中的前count个字符拷贝到缓冲区,一般情况下是对std::basic_streambuf::sputc()调用count次,因此我们可以通过优化std::basic_streambuf::sputc来优化.
std::basic_streambuf::overflow
virtual int_type overflow( int_type ch = traits::eof() );
该函数是一个很特别的函数,根据标准由2种实现:
1,用来判断当前buffer区是否还有足够的空间存放字符,如果有足够的空间则拷贝字符进去buffer,否则返回 typename std::char_traits<charT>::eof();
2,仅仅用来返回typename std::char_traits<charT>::eof();缓存区空间的大小留给std::basic_streambuf::sputc()或者是std::basic_streambuf::xsputn()来判断.
Demo 1(未重写overflow):
#include <iostream> // std::cin, std::cout
#include <string>
#include <streambuf>
#include <array>
template<typename charT, typename traits = std::char_traits<charT>>
class StreamBuffer : public std::basic_streambuf<charT> {
public:
using char_type = typename traits::char_type;
StreamBuffer();
~StreamBuffer() = default;
StreamBuffer(const StreamBuffer<charT>& other) = delete;
StreamBuffer& operator=(const StreamBuffer<charT>& other) = delete;
void printBuffer()const;
private:
std::array<char_type, 10> buffer;
};
template<typename charT, typename tratis>
StreamBuffer<charT, tratis>::StreamBuffer()
{
//注意这里这里设置的是缓冲区的开始位置和最末位置
//可以通过pbase(), pptr(), epptr()来进行访问.
this->std::basic_streambuf<charT>::setp((this->buffer).data(), (this->buffer).data()+9);
}
template<typename charT, typename traits>
void StreamBuffer<charT, traits>::printBuffer()const
{
for (const char_type& value : (this->buffer)) {
std::cout << value << " ";
}
std::cout << std::endl;
}
int main()
{
StreamBuffer<char> buffer;
std::basic_ostream<char> ostream(&buffer);
ostream << "shihua";
buffer.printBuffer();
return 0;
}
Demo 2(重写overflow):
#include <iostream>
#include <streambuf>
#include <array>
#include <locale>
template<typename charT, typename traits = std::char_traits<charT>>
class StreamBuffer :public std::basic_streambuf<charT>
{
public:
using charType = typename traits::char_type;
using intType = typename traits::int_type;
StreamBuffer();
virtual ~StreamBuffer() = default;
StreamBuffer(const StreamBuffer<charT>& other) = delete;
StreamBuffer& operator=(const StreamBuffer<charT>& other) = delete;
void printBuffer()const;
protected:
virtual intType overflow(intType ch)override;
virtual std::streamsize xsputn(const charT* s, std::streamsize count)override;
private:
std::array<charType, 10> buffer;
unsigned long counter;
};
template<typename charT, typename traits>
StreamBuffer<charT, traits>::StreamBuffer()
:std::basic_streambuf<charT>(),
counter(buffer.size())
{
this->std::basic_streambuf<charT>::setp((this->buffer).data(), (this->buffer).data(), (this->buffer).data() + 10);
}
template<typename charT, typename traits>
void StreamBuffer<charT, traits>::printBuffer()const
{
for (const charType& value : (this->buffer)) {
if ( !typename traits::eq(value, traits::eof())) {
std::cout << value << " ";
}
}
std::cout << std::endl;
}
template<typename charT, typename traits>
typename StreamBuffer<charT, traits>::intType StreamBuffer<charT, traits>::overflow(typename StreamBuffer<charT, traits>::intType ch)
{
return (typename traits::eof());
}
template<typename charT, typename traits>
std::streamsize StreamBuffer<charT, traits>::xsputn(const charT* s, std::streamsize count)
{
if (count > 0) {
if (count < (this->buffer).size()) {
typename traits::copy(this->pptr(), s, count);
return count;
}else {
return (this->epptr() - this->pptr());
}
}
return (this->overflow(*(this->pptr())));
}
int main()
{
StreamBuffer<char> buffer;
std::basic_ostream<char> cout(&buffer);
cout << "shihua";
buffer.printBuffer();
return 0;
}
Input stream所用的buffer的接口:
std::basic_streambuf::in_avail
std::streamsize in_avail();
该函数返回当前buffer内还有多少有效字符未被读(read)出来.
std::basic_streambuf::uflow
virtual int_type uflow();
该函数的默认行为是: 调用std::basic_streambuf::underflow()并移动(前进)read pointer。但是一般情况下标准库对于std::basic_streambuf::underflow()的做法是令它返回EOF.
std::basic_streambuf::showmanyc
protected:
virtual std::streamsize showmanyc();
返回并估计当前缓冲区中还有多少个字符可以读.
其中需要注意的是 std::streamsize一般的实现是: typedef unsigned long long std::streamsize;
std::basic_streambuf::underflow
virtual int_type underflow();
默认被实现为返回EOF(typename std::char_traits<charType>::eof()).
std::basic_streambuf::sgetc
int_type sgetc();
读取一个字符(*gptr()),不会向前移动当前可读取位置.
std::basic_streambuf::sgetn
std::basic_streambuf::xsgetn
std::streamsize sgetn( char_type* s, std::streamsize count );
protected:
virtual std::streamsize xsgetn( char_type* s, std::streamsize count );
std::basic_streambuf::sgetn()其实是调用std::basic_streambuf::xsgetn().
从input stream buffer中读取count个字符存储到s指向的数组中去.
同时会调用std::basic_streambuf::gbump(count)使当前当前可读位置增加count.
std::basic_streambuf::gbump
void gbump( int count );
使得当前可读位置向前移动count个位置.
std::basic_streambuf::gset
void setg( char_type* pbeg, char_type* pend );
设置input stream buffer维护的3个pointer的初始化位置,该函数一般在构造函数中被调用.
eback() = pbeg; gptr()=pbeg; egptr()=pend;
Locales:
std::basic_streambuf::pubimbue
std::basic_streambuf::imbue
std::locale pubimbue( const std::locale& loc );
protected:
virtual void imbue( const std::locale& loc );
给当前stream buffer设置std::locale.
std::basic_streambuf::getloc
std::locale getloc() const;
返回当前std::basic_streambuf设置的std::locale.
Positioning:
std::basic_streambuf::pubsetbuf
std::basic_streambuf::setbuf
basic_streambuf<CharT, Traits>* pubsetbuf( char_type* s, std::streamsize n )
protected:
virtual basic_streambuf<CharT, Traits>* setbuf( char_type* s, std::streamsize n )
用我们给定的字符数组s的前n个作为缓冲区的存储介质.
std::basic_streambuf::pubseekoff
std::basic_streambuf::seekoff
pos_type pubseekoff( off_type off, ios_base::seekdir dir,
ios_base::openmode which = ios_base::in | ios_base::out );
protected:
virtual pos_type seekoff( off_type off, ios_base::seekdir dir,
ios_base::openmode which = ios_base::in | ios_base::out );
off:是偏移量.
dir:是定位到当前stream buffer的位置.
which:指定当前stream buffer的I/O方向.
通过dir定位到stream buffer的一个位置,然后通过offset指定的量偏移一定位置.
返回当前坐标位于buffer中的绝对位置.
std::basic_streambuf::pubseekpos
std::basic_streambuf::seekpos
pos_type pubseekpos( pos_type pos,
ios_base::openmode which = ios_base::in | ios_base::out );
protected:
virtual pos_type seekpos( pos_type pos,
ios_base::openmode which = ios_base::in | ios_base::out);
pos: 绝对位置.
which: I/O方向.
设置当前stream buffer中指向可读位置或者可写的位置到pos指定的绝对位置.