basic_ios定义出“与字符类型及其相应字符特性相关”的stream class共同属性,其中包括stream所用的缓冲器。
1、类型定义
// _Elem表示字符类型,可能是char或wchar_t // _Traits是字符特性类型,默认有char_traits<char> 和 char_traits<wchar_t> template<class _Elem, class _Traits> class basic_ios : public ios_base { // base class for basic_istream/basic_ostream public: typedef basic_ios<_Elem, _Traits> _Myt; typedef basic_ostream<_Elem, _Traits> _Myos; typedef basic_streambuf<_Elem, _Traits> _Mysb; typedef ctype<_Elem> _Ctype; typedef _Elem char_type; typedef _Traits traits_type; typedef typename _Traits::int_type int_type; typedef typename _Traits::pos_type pos_type; typedef typename _Traits::off_type off_type; // ... private: _Mysb *_Mystrbuf; // pointer to stream buffer _Myos *_Tiestr; // pointer to tied output stream _Elem _Fillch; // the fill character } template class basic_ios<char, char_traits<char> >; template class basic_ios<wchar_t, char_traits<wchar_t> >;
_Mystrbuf是流缓冲区指针,_Tiestr是关联的输出流指针,_Fillch是填充字符。
二、构造函数
public: explicit basic_ios(_Mysb *_Strbuf) { // construct from stream buffer pointer init(_Strbuf); } virtual ~basic_ios() { // destroy the object } protected: void init(_Mysb *_Strbuf = 0, bool _Isstd = false) { // initialize with stream buffer pointer _Init(); // initialize ios_base _Mystrbuf = _Strbuf; _Tiestr = 0; _Fillch = widen(' '); if (_Mystrbuf == 0) setstate(badbit); if (_Isstd) _Addstd(this); // special handling for standard streams else _Stdstr = 0; } basic_ios() { // default constructor, do nothing } private: basic_ios(const _Myt&); // not defined _Myt& operator=(const _Myt&); // not defined
可见,basic_ios不支持复制构造,而且也只能从派生类中调用其构造函数。唯一的显示构造函数,要求传入的参数是basic_streambuf<_Elem, _Traits>类型的流缓冲区指针。 basic_ios也不支持赋值。
三、操作:清除流状态、设置流状态、拷贝流标志、返回或设置流缓冲区指针、设置本地化相关信息、返回或设置填充字符、字符转换
// Clears all error flags. void clear(iostate _State = goodbit, bool _Reraise = false) { // set state, possibly reraise exception ios_base::clear((iostate)(_Mystrbuf == 0 ? (int)_State | (int)badbit : (int)_State), _Reraise); } void clear(io_state _State) // { // set state to _State clear((iostate)_State); } // Sets additional flags. void setstate(iostate _State, bool _Reraise = false) { // merge _State into state, possible reraise exception if (_State != goodbit) clear((iostate)((int)rdstate() | (int)_State), _Reraise); } void setstate(io_state _State) { // merge _State into state setstate((iostate)_State); } // Copies flags from one stream to another. _Myt& copyfmt(const _Myt& _Right) { // copy format parameters _Tiestr = _Right.tie(); _Fillch = _Right.fill(); ios_base::copyfmt(_Right); return (*this); } // Ensures that one stream is processed before another stream. _Myos * tie() const { // return tie pointer return (_Tiestr); } _Myos * tie(_Myos *_Newtie) { // set tie pointer _Myos *_Oldtie = _Tiestr; _Tiestr = _Newtie; return (_Oldtie); } // 返回或设置流缓冲区指针 _Mysb * rdbuf() const { // return stream buffer pointer return (_Mystrbuf); } _Mysb * rdbuf(_Mysb *_Strbuf) { // set stream buffer pointer _Mysb *_Oldstrbuf = _Mystrbuf; _Mystrbuf = _Strbuf; clear(); return (_Oldstrbuf); } // 设置本地化相关信息 locale imbue(const locale& _Loc) { // set locale to argument locale _Oldlocale = ios_base::imbue(_Loc); if (rdbuf() != 0) rdbuf()->pubimbue(_Loc); return (_Oldlocale); } // 返回或设置填充字符 _Elem fill() const { // return fill character return (_Fillch); } _Elem fill(_Elem _Newfill) { // set fill character _Elem _Oldfill = _Fillch; _Fillch = _Newfill; return (_Oldfill); } // 字符转换 char narrow(_Elem _Ch, char _Dflt = '\0') const { // convert _Ch to byte using imbued locale const _Ctype& _Ctype_fac = _USE(getloc(), _Ctype); return (_Ctype_fac.narrow(_Ch, _Dflt)); } _Elem widen(char _Byte) const { // convert _Byte to character using imbued locale const _Ctype& _Ctype_fac = _USE(getloc(), _Ctype); return (_Ctype_fac.widen(_Byte)); }