C++的流basic_streambuf
来源:互联网 发布:打字软件哪个好 编辑:程序博客网 时间:2024/05/17 09:09
不是个抽象类,但是他是C++流的基类.它提供了基本的缓冲区管理.它包含六个缓存区指针,三个分别是读指针(开头,结尾,当前),另外三个是写指针(开头,结尾,当前)
读开始指针 当前读指针 读结尾指针
_M_gbegin _M_gnext _M_gend
=========================================================
^ ^ ^
_M_pbegin _M_pnext _M_pend
写开始指针 当前写指针 写结尾指针
template <class_CharT,class_Traits>
classbasic_streambuf
{
friendclassbasic_istream<_CharT,_Traits>;
friendclassbasic_ostream<_CharT,_Traits>;
public: // Typedefs.
typedef_CharT char_type;
typedeftypename_Traits::int_type int_type;
typedeftypename_Traits::pos_type pos_type;
typedeftypename_Traits::off_type off_type;
typedef_Traits traits_type;
public: // Destructor.
virtual ~basic_streambuf() {}
public: // Locale-related functions.
localepubimbue(constlocale&__loc) {
this->imbue(__loc);
locale__tmp =_M_locale;
_M_locale =__loc;
return__tmp;
}
localegetloc()const {return_M_locale; }
public: // Buffer management.
//设置缓冲区和长度
basic_streambuf*pubsetbuf(char_type*__s,streamsize__n)
{ returnthis->setbuf(__s,__n); }
//设置缓冲区偏移量,简单调用seekoff()函数
pos_typepubseekoff(off_type__offset,ios_base::seekdir__way,
ios_base::openmode__mod = ios_base::in |ios_base::out)
{ returnthis->seekoff(__offset,__way,__mod); }
//设置缓冲区位置,简单调用seekpos()函数
pos_typepubseekpos(pos_type__sp,
ios_base::openmode__mod = ios_base::in |ios_base::out)
{ returnthis->seekpos(__sp,__mod); }
//放置缓冲区同步,简单调用sync()函数
intpubsync() {returnthis->sync(); }
public:
// 读取字符的公共成员函数
//比较当前指针和结尾指针,如果在结尾之前返回剩下的缓冲区大小.否则调用showmanyc()函数
streamsizein_avail() {
return_M_gnext <_M_gend ?_M_gend -_M_gnext :this->showmanyc();
}
// 移动到下一个字符,并返回下一字符
int_typesnextc() {
return_M_gend -_M_gnext > 1 ?_Traits::to_int_type(*++_M_gnext)
:this->_M_snextc_aux();
}
// 移动到下一个字符,并返回当前字符
int_typesbumpc() {
return_M_gnext <_M_gend ?_Traits::to_int_type(*_M_gnext++)
:this->uflow();
}
//并返回当前字符,指针不移动到
int_typesgetc() {
return_M_gnext <_M_gend ?_Traits::to_int_type(*_M_gnext)
:this->underflow();
}
// 获取n个字符串到参数__s
streamsizesgetn(char_type*__s,streamsize__n)
{ returnthis->xsgetn(__s,__n); }
// 比较当前前一个字符是否与参数__c相等,相等则返回,移动当前指针当前前一个字符并返回
int_typesputbackc(char_type__c) {
return_M_gbegin <_M_gnext &&_Traits::eq(__c, *(_M_gnext - 1))
? _Traits::to_int_type(*--_M_gnext)
: this->pbackfail(_Traits::to_int_type(__c));
}
//移动当前指针当前前一个字符并返回
int_typesungetc() {
return_M_gbegin <_M_gnext
? _Traits::to_int_type(*--_M_gnext)
: this->pbackfail();
}
public:
// 公共写字符成员函数
// 写入一个字符,
int_typesputc(char_type__c) {
return_M_pnext <_M_pend
? _Traits::to_int_type(*_M_pnext++ = __c)
: this->overflow(_Traits::to_int_type(__c));
}
// 写入长度为n的字串
streamsizesputn(constchar_type*__s,streamsize__n)
{ returnthis->xsputn(__s,__n); }
// 扩展写n个字符函数
streamsize_M_sputnc(char_type__c,streamsize__n)
{ returnthis->_M_xsputnc(__c,__n); }
public:
// 线程安全的锁
_STL_mutex_lock _M_lock;
protected:
// 缺省构造函数
basic_streambuf()
: _M_gbegin(0),_M_gnext(0),_M_gend(0),
_M_pbegin(0),_M_pnext(0),_M_pend(0),
_M_locale()
{
_M_lock._M_initialize();
}
protected:
// 保护的获取读指针的成员函数
char_type*eback()const {return_M_gbegin; } //开头读指针
char_type*gptr() const {return_M_gnext; } //当前读指针
char_type*egptr()const {return_M_gend; } // 结尾读指针
// 将读指针向移动n个字符
voidgbump(int__n) { _M_gnext +=__n; }
// 从新设置读的三个指针
voidsetg(char_type*__gbegin,char_type*__gnext,char_type*__gend) {
_M_gbegin =__gbegin;
_M_gnext =__gnext;
_M_gend =__gend;
}
protected:
// 保护的获取写指针的成员函数
char_type*pbase()const {return_M_pbegin; }// 开头读指针
char_type*pptr() const {return_M_pnext; } //当前读指针
char_type*epptr()const {return_M_pend; } // 结尾读指针
// 将写指针向移动n个字符
voidpbump(int__n) { _M_pnext +=__n; }
//从新设置读的三个指针
voidsetp(char_type*__pbegin,char_type*__pend) {
_M_pbegin =__pbegin;
_M_pnext =__pbegin;
_M_pend =__pend;
}
protected:
// 本地化虚函数
virtualvoidimbue(constlocale&) {}
protected:
//内存管理虚函数,当前置返回自己this
virtualbasic_streambuf*setbuf(char_type*,streamsize)
{ returnthis; }
// 通过一个整型偏移值,改变流的位置.该函数这里什么读没作,只返回-1,在子类中重载
virtualpos_typeseekoff(off_type,ios_base::seekdir,
ios_base::openmode =ios_base::in |ios_base::out)
{ returnpos_type(-1); }
// 通过前面获取流位置,改变流的位置.该函数这里什么读没作,只返回-1,在子类中重载
virtualpos_type
seekpos(pos_type,ios_base::openmode =ios_base::in |ios_base::out)
{ returnpos_type(-1); }
// 同步(刷新).缓冲区.所有的子类中重载
virtualintsync() { return 0; }
protected:
// 返回在在抵达文件结尾更低级可以读取字符数. (-1是特定值,意味着underflow将失败.)
// 函数在子类中重载
virtualstreamsizeshowmanyc()
{ return 0; }
// 读取n的字符.返回可读取的字符数
virtualstreamsizexsgetn(char_type*__s,streamsize__n);
// 当没有读取的位置时调用.如: gptr()返回为空时或者gptr() >= egptr().
virtualint_typeunderflow()
{ return_Traits::eof(); }
// 类似underflow(),但是使用在unbuffered输入.
virtualint_typeuflow() {
return_Traits::eq_int_type(this->underflow(),_Traits::eof())
? _Traits::eof()
: _Traits::to_int_type(*_M_gnext++);
}
// 当没有放置的位置时调用.如: gptr()返回为空时或者gptr() == eback().
virtualint_typepbackfail(int_type__c = _Traits::eof())
{ return_Traits::eof(); }
protected:
// 写n字符,返回写的字符数
virtualstreamsizexsputn(constchar_type*__s,streamsize__n);
// 当没有写位置时调用
virtualint_typeoverflow(int_type = _Traits::eof())
{ return_Traits::eof(); }
private:
char_type*_M_gbegin; // get 区的开始指针
char_type*_M_gnext; // get 区的当前指针
char_type*_M_gend; // get 区的结尾指针
char_type*_M_pbegin; // put 区的开始指针
char_type*_M_pnext; // put 区的当前指针
char_type*_M_pend; // put 区的结尾指针
locale_M_locale; // The streambuf's locale object
};
0 0
- C++的流basic_streambuf
- C++的流basic_streambuf
- C++的流basic_streambuf
- basic_streambuf Class
- 自定义basic_streambuf
- 自定义basic_streambuf
- 自定义basic_streambuf
- basic_streambuf源码参考
- error: 'std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _T
- c++/c 流处理错误的例子
- c语言的控制流
- C语言的文件流
- 【C++】流操作的基础
- c++/C 的用户手册
- <C/C++> IO流 cout/fstream的使用
- c的
- C,C++,C#,java的区别
- C,C++,C#,java的区别
- Oracle中decode函数用法含义
- [笔记] 《C专家编程》
- 关于springmvc无法引入js文件问题
- 生活管家app 测试1
- mac上搭建svn服务器
- C++的流basic_streambuf
- 线程池ThreadPoolExecutor使用简介
- AVL树的旋转
- 行内元素和块级元素的区别
- C++的流分析basic_filebuf
- 5款客服代码
- PythonChallenge 编程游戏-------第0关
- LINUX关闭防火墙
- js操作select,radio,checkbox,table