📄 istream
字号:
// istream standard header
#pragma once
#ifndef _ISTREAM_
#define _ISTREAM_
#ifndef RC_INVOKED
#include <ostream>
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)
#pragma warning(disable: 4189)
_STD_BEGIN
// TEMPLATE CLASS basic_istream
template<class _Elem,
class _Traits>
class basic_istream
: virtual public basic_ios<_Elem, _Traits>
{ // control extractions from a stream buffer
public:
typedef basic_istream<_Elem, _Traits> _Myt;
typedef basic_ios<_Elem, _Traits> _Myios;
typedef basic_streambuf<_Elem, _Traits> _Mysb;
typedef istreambuf_iterator<_Elem, _Traits> _Iter;
typedef ctype<_Elem> _Ctype;
typedef num_get<_Elem, _Iter> _Nget;
#ifndef _INTERNAL_IFSTRIP_
#if defined(__FORCE_INSTANCE)
explicit __CLR_OR_THIS_CALL basic_istream(_Mysb *_Strbuf,
bool _Isstd, bool _Noinit)
: _Chcount(0)
{ // construct from stream buffer pointer
if (!_Noinit)
_Myios::init(_Strbuf, _Isstd);
}
#endif /* defined(__FORCE_INSTANCE) */
#endif /* _INTERNAL_IFSTRIP_ */
explicit __CLR_OR_THIS_CALL basic_istream(_Mysb *_Strbuf,
bool _Isstd = false)
: _Chcount(0)
{ // construct from stream buffer pointer
_Myios::init(_Strbuf, _Isstd);
}
__CLR_OR_THIS_CALL basic_istream(_Uninitialized)
{ // construct uninitialized
ios_base::_Addstd(this);
}
__CLR_OR_THIS_CALL basic_istream(_Myt&& _Right)
: _Chcount(_Right._Chcount)
{ // construct by moving _Right
_Myios::init();
_Myios::move(_STD move(_Right));
_Right._Chcount = 0;
}
_Myt& __CLR_OR_THIS_CALL operator=(_Myt&& _Right)
{ // move from _Right
this->swap(_Right);
return (*this);
}
void __CLR_OR_THIS_CALL swap(_Myt& _Right)
{ // swap with _Right
_Myios::swap(_Right);
_STD swap(_Chcount, _Right._Chcount);
}
virtual __CLR_OR_THIS_CALL ~basic_istream()
{ // destroy the object
}
typedef typename _Traits::int_type int_type;
typedef typename _Traits::pos_type pos_type;
typedef typename _Traits::off_type off_type;
// TEMPLATE CLASS sentry
class _Sentry_base
{ // stores thread lock and reference to input stream
public:
__CLR_OR_THIS_CALL _Sentry_base(_Myt& _Istr)
: _Myistr(_Istr)
{ // lock the stream buffer, if there
if (_Myistr.rdbuf() != 0)
_Myistr.rdbuf()->_Lock();
}
__CLR_OR_THIS_CALL ~_Sentry_base()
{ // destroy after unlocking
if (_Myistr.rdbuf() != 0)
_Myistr.rdbuf()->_Unlock();
}
_Myt& _Myistr; // the input stream, for _Unlock call at destruction
private:
_Sentry_base& operator=(const _Sentry_base&);
};
class sentry
: public _Sentry_base
{ // stores thread lock and result of _Ipfx call
public:
explicit __CLR_OR_THIS_CALL sentry(_Myt& _Istr, bool _Noskip = false)
: _Sentry_base(_Istr)
{ // construct locking and calling _Ipfx
_Ok = this->_Myistr._Ipfx(_Noskip);
}
__CLR_OR_THIS_CALL _OPERATOR_BOOL() const
{ // test if _Ipfx succeeded
return (_Ok ? _CONVERTIBLE_TO_TRUE : 0);
}
private:
bool _Ok; // true if _Ipfx succeeded at construction
__CLR_OR_THIS_CALL sentry(const sentry&); // not defined
sentry& __CLR_OR_THIS_CALL operator=(const sentry&); // not defined
};
bool __CLR_OR_THIS_CALL _Ipfx(bool _Noskip = false)
{ // test stream state and skip whitespace as needed
if (ios_base::good())
{ // state okay, flush tied stream and skip whitespace
if (_Myios::tie() != 0)
_Myios::tie()->flush();
if (!_Noskip && ios_base::flags() & ios_base::skipws)
{ // skip whitespace
const _Ctype& _Ctype_fac = _USE(ios_base::getloc(), _Ctype);
_TRY_IO_BEGIN
int_type _Meta = _Myios::rdbuf()->sgetc();
for (; ; _Meta = _Myios::rdbuf()->snextc())
if (_Traits::eq_int_type(_Traits::eof(), _Meta))
{ // end of file, quit
_Myios::setstate(ios_base::eofbit);
break;
}
else if (!_Ctype_fac.is(_Ctype::space,
_Traits::to_char_type(_Meta)))
break; // not whitespace, quit
_CATCH_IO_END
}
if (ios_base::good())
return (true);
}
_Myios::setstate(ios_base::failbit);
return (false);
}
bool __CLR_OR_THIS_CALL ipfx(bool _Noskip = false)
{ // test stream state and skip whitespace as needed (retained)
return (_Ipfx(_Noskip));
}
void __CLR_OR_THIS_CALL isfx()
{ // perform any wrapup (retained)
}
#ifdef _M_CEE_PURE
_Myt& __CLR_OR_THIS_CALL operator>>(_Myt& (__clrcall *_Pfn)(_Myt&))
{ // call basic_istream manipulator
_DEBUG_POINTER(_Pfn);
return ((*_Pfn)(*this));
}
_Myt& __CLR_OR_THIS_CALL operator>>(_Myios& (__clrcall *_Pfn)(_Myios&))
{ // call basic_ios manipulator
_DEBUG_POINTER(_Pfn);
(*_Pfn)(*(_Myios *)this);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(ios_base& (__clrcall *_Pfn)(ios_base&))
{ // call ios_base manipulator
_DEBUG_POINTER(_Pfn);
(*_Pfn)(*(ios_base *)this);
return (*this);
}
#endif /* _M_CEE_PURE */
_Myt& __CLR_OR_THIS_CALL operator>>(_Myt& (__cdecl *_Pfn)(_Myt&))
{ // call basic_istream manipulator
_DEBUG_POINTER(_Pfn);
return ((*_Pfn)(*this));
}
_Myt& __CLR_OR_THIS_CALL operator>>(_Myios& (__cdecl *_Pfn)(_Myios&))
{ // call basic_ios manipulator
_DEBUG_POINTER(_Pfn);
(*_Pfn)(*(_Myios *)this);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(ios_base& (__cdecl *_Pfn)(ios_base&))
{ // call ios_base manipulator
_DEBUG_POINTER(_Pfn);
(*_Pfn)(*(ios_base *)this);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(_Bool& _Val)
{ // extract a boolean
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(ios_base::getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(short& _Val)
{ // extract a short
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
long _Tmp = 0;
const _Nget& _Nget_fac = _USE(ios_base::getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Tmp);
_CATCH_IO_END
if (_State & ios_base::failbit
|| _Tmp < SHRT_MIN || SHRT_MAX < _Tmp)
_State |= ios_base::failbit;
else
_Val = (short)_Tmp;
}
_Myios::setstate(_State);
return (*this);
}
/* NOTE:
If you are not using native wchar_t, the unsigned short extractor
is masked by an explicit specialization that treats an unsigned
short as a wide character.
To read or write unsigned shorts as integers with wchar_t streams,
make wchar_t a native type with the command line option /Zc:wchar_t.
*/
_Myt& __CLR_OR_THIS_CALL operator>>(unsigned short& _Val)
{ // extract an unsigned short
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(ios_base::getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(int& _Val)
{ // extract an int
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
long _Tmp = 0;
const _Nget& _Nget_fac = _USE(ios_base::getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Tmp);
_CATCH_IO_END
if (_State & ios_base::failbit
|| _Tmp < INT_MIN || INT_MAX < _Tmp)
_State |= ios_base::failbit;
else
_Val = _Tmp;
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(unsigned int& _Val)
{ // extract an unsigned int
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(ios_base::getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(long& _Val)
{ // extract a long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(ios_base::getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(unsigned long& _Val)
{ // extract an unsigned long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(ios_base::getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
#ifdef _LONGLONG
_Myt& __CLR_OR_THIS_CALL operator>>(_LONGLONG& _Val)
{ // extract a long long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(ios_base::getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(_ULONGLONG& _Val)
{ // extract an unsigned long long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(ios_base::getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
#endif /* _LONGLONG */
_Myt& __CLR_OR_THIS_CALL operator>>(float& _Val)
{ // extract a float
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(ios_base::getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator>>(double& _Val)
{ // extract a double
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to extract
const _Nget& _Nget_fac = _USE(ios_base::getloc(), _Nget);
_TRY_IO_BEGIN
_Nget_fac.get(_Iter(_Myios::rdbuf()), _Iter(0),
*this, _State, _Val);
_CATCH_IO_END
}
_Myios::setstate(_State);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -