[流解析 vs 2005]ios_base

vs 2005中ios_base继承自_Iosb<int>,主要提供流状态控制、格式控制、本地化等方面支持。

 

1、类型和数据定义

class ios_base : public _Iosb<int>
{
public:
	// 预定义类型
	_BITMASK(_Fmtflags, fmtflags);	// 其实是typedef int fmtflags  第一个方便理解
	_BITMASK(_Iostate, iostate);
	_BITMASK(_Openmode, openmode);
	_BITMASK(_Seekdir, seekdir);

	typedef std::streamoff streamoff;
	typedef std::streampos streampos;

	enum event
	{	// constants for ios events
		erase_event, imbue_event, copyfmt_event};

	typedef void (__CLRCALL_OR_CDECL *event_callback)(event, ios_base&, int);

	class failure	// 用于抛出异常。
		: public runtime_error
	{
	public:
		explicit failure(const string &_Message)
			: runtime_error(_Message) {}
		virtual ~failure() throw () {}
#if !_HAS_EXCEPTIONS
	protected:
		virtual void _Doraise() const		// exception中有相关信息
		{	// report the exception
			_RAISE(*this);
		}
#endif  /* _HAS_EXCEPTIONS */
	};

	class Init	// 作用?
	{	// controller for standard-stream initialization
	public:
		Init() { _Init_ctor(this); }
		~Init() { _Init_dtor(this); }
	private:
		static void __cdecl _Init_ctor(Init *);		// 位于iostream.cpp中
		static void __cdecl _Init_dtor(Init *);		// 位于iostream.cpp中
		static int& __cdecl _Init_cnt_func();		// 位于iostream.cpp中
		static int _Init_cnt;	// net constructions - destructions
	};

	// ...
private:
	iostate _Mystate;	// stream state
	iostate _Except;	// exception mask
	fmtflags _Fmtfl;	// format flags
	streamsize _Prec;	// field precision
	streamsize _Wide;	// field width
	_Iosarray *_Arr;	// ?? pointer to first node of long/pointer array
	_Fnarray *_Calls;	// ?? pointer to first node of call list
	locale *_Ploc;	// pointer to locale

	static int _Index;	// ?? source of unique indexes for long/pointer array
	static bool _Sync;	// ?? C/C++ synchronization flag (ignored)
};


其中:_Mystate是流状态,_Except是异常掩码,用于标识在流在哪些状态下抛出异常;_Fmtfl是格式;_Prec是精度;_Wide是宽度;_Arr 和 _Calls暂不明白; Ploc是本地化相关信息;_Index 和 _Sync 暂不明白。

 

二、流状态操作:

		// 流状态操作
		void clear(iostate _State, bool _Reraise)	// 第二个参数表示是否重新抛出异常
		{	// set state, possibly reraise exception
			_Mystate = (iostate)(_State & _Statmask);
			if ((_Mystate & _Except) == 0)
				;
			else if (_Reraise)
				_RERAISE;
			else if (_Mystate & _Except & badbit)
				_THROW_NCEE(failure, "ios_base::badbit set");
			else if (_Mystate & _Except & failbit)
				_THROW_NCEE(failure, "ios_base::failbit set");
			else
				_THROW_NCEE(failure, "ios_base::eofbit set");
		}
		void clear(iostate _State = goodbit)
		{
			clear(_State, false);
		}

		iostate rdstate() const;  // return (_Mystate);
		void setstate(iostate _State)
		{	// merge in state argument
			if (_State != goodbit)
				clear((iostate)((int)rdstate() | (int)_State), false);
		}

		bool good() const;	// return (rdstate() == goodbit);
		bool eof() const;	// return ((int)rdstate() & (int)eofbit);
		bool fail() const;	// return (((int)rdstate() & ((int)badbit | (int)failbit)) != 0);
		bool bad() const;	// return (((int)rdstate() & (int)badbit) != 0);

		// 异常掩码:用于确定哪些状态下抛出异常
		iostate exceptions() const;		// return (_Except);
		void exceptions(iostate _Newexcept)
		{
			_Except = (iostate)((int)_Newexcept & (int)_Statmask);
			clear(_Mystate);
		}


三、流格式化标志操作:

		// 流格式化标志操作
		fmtflags flags() const		// return (_Fmtfl);
		fmtflags flags(fmtflags _Newfmtflags)
		{	// set format flags to argument
			fmtflags _Oldfmtflags = _Fmtfl;
			_Fmtfl = (fmtflags)((int)_Newfmtflags & (int)_Fmtmask);
			return (_Oldfmtflags);
		}
		fmtflags setf(fmtflags _Newfmtflags, fmtflags _Mask)
		{	// merge in format flags argument under mask argument
			ios_base::fmtflags _Oldfmtflags = _Fmtfl;
			_Fmtfl = (fmtflags)(((int)_Fmtfl & (int)~_Mask)
				| ((int)_Newfmtflags & (int)_Mask & (int)_Fmtmask));
			return (_Oldfmtflags);
		}
		void unsetf(fmtflags _Mask)
		{	// clear format flags under mask argument
			_Fmtfl = (fmtflags)((int)_Fmtfl & (int)~_Mask);
		}
		streamsize precision(streamsize _Newprecision)
		{	// set precision to argument
			streamsize _Oldprecision = _Prec;
			_Prec = _Newprecision;
			return (_Oldprecision);
		}
		streamsize width() const;	// return (_Wide);
		streamsize width(streamsize _Newwidth)
		{	// set width to argument
			streamsize _Oldwidth = _Wide;
			_Wide = _Newwidth;
			return (_Oldwidth);
		}


四、本地化相关处理:

		// 本地化相关处理
		locale getloc() const
		{	// get locale
			return (*_Ploc);
		}
		locale imbue(const locale& _Loc)
		{	// set locale to argument
			locale _Oldlocale = *_Ploc;
			*_Ploc = _Loc;
			_Callfns(imbue_event);	// _Calls成员变量中维护了一系列函数
			return (_Oldlocale);
		}

五、
五、成员变量_Arr相关处理:

	struct _Iosarray
	{	// list element for open-ended sparse array of longs/pointers
	public:
		_Iosarray(int _Idx, _Iosarray *_Link)
			: _Next(_Link), _Index(_Idx), _Lo(0), _Vp(0)
		{	// construct node for index _Idx and link it in
		}

		_Iosarray *_Next;	// pointer to next node
		int _Index;	// index of this node
		long _Lo;	// stored long value
		void *_Vp;	// stored pointer value
	};

	long& iword(int _Idx)
	{	// return reference to long element
		return (_Findarr(_Idx)._Lo);
	}
	void *& pword(int _Idx)
	{	// return reference to pointer element
		return (_Findarr(_Idx)._Vp);
	}
	_Iosarray& _Findarr(int _Idx)
	{	// locate or make a variable array element
		_Iosarray *_Ptr, *_Q;

		for (_Ptr = _Arr, _Q = 0; _Ptr != 0; _Ptr = _Ptr->_Next)
			if (_Ptr->_Index == _Idx)
				return (*_Ptr);	// found element, return it
			else if (_Q == 0 && _Ptr->_Lo == 0 && _Ptr->_Vp == 0)
				_Q = _Ptr;	// found recycling candidate

		if (_Q != 0)
		{	// recycle existing element
			_Q->_Index = _Idx;
			return (*_Q);
		}

		_Arr = _NEW_CRT _Iosarray(_Idx, _Arr);	// make a new element
		return (*_Arr);
	}


六、成员变量_Calls处理:

	struct _Fnarray
	{	// list element for open-ended sparse array of event handlers
		_Fnarray(int _Idx, event_callback _Pnew, _Fnarray *_Link)
			: _Next(_Link), _Index(_Idx), _Pfn(_Pnew)
		{	// construct node for index _Idx and link it in
		}

		_Fnarray *_Next;	// pointer to next node
		int _Index;	// index of this node
		event_callback _Pfn;	// pointer to event handler
	};
	void register_callback(event_callback _Pfn, int _Idx)
	{	// register event handler
		_Calls = _NEW_CRT _Fnarray(_Idx, _Pfn, _Calls);
	}


七、运算符重载:

		ios_base& operator=(const ios_base& _Right)
		{	// assign state and format stuff from _Right
			if (this != &_Right)
			{	// worth doing
				_Mystate = _Right._Mystate;
				copyfmt(_Right);
			}
			return (*this);
		}
		operator void *() const;	// eg: if(cin)
		bool operator!() const;		// eg: if(!cin)


 

你可能感兴趣的:([流解析 vs 2005]ios_base)