📄 ostream
字号:
// ostream standard header
#pragma once
#ifndef _OSTREAM_
#define _OSTREAM_
#ifndef RC_INVOKED
#include <ios>
#pragma pack(push,_CRT_PACKING)
#pragma warning(push,3)
#pragma warning(disable: 4189 4390)
_STD_BEGIN
// I/O EXCEPTION MACROS
#if _HAS_EXCEPTIONS
#define _TRY_IO_BEGIN _TRY_BEGIN /* begin try block */
#define _CATCH_IO_END _CATCH_ALL /* catch block for _Myios */ \
_Myios::setstate(ios_base::badbit, true); /* set badbit and rethrow */ \
_CATCH_END
#define _CATCH_IO_(x) _CATCH_ALL /* catch block for basic_ios x */ \
(x).setstate(ios_base::badbit, true); /* set badbit and rethrow */ \
_CATCH_END
#else /* _HAS_EXCEPTIONS */
#define _TRY_IO_BEGIN { /* begin try block */
#define _CATCH_IO_END } /* catch block for _Myios */
#define _CATCH_IO_(x) } /* catch block for basic_ios x */
#endif /* _HAS_EXCEPTIONS */
// TEMPLATE CLASS basic_ostream
template<class _Elem,
class _Traits>
class basic_ostream
: virtual public basic_ios<_Elem, _Traits>
{ // control insertions into a stream buffer
public:
typedef basic_ostream<_Elem, _Traits> _Myt;
typedef basic_ios<_Elem, _Traits> _Myios;
typedef basic_streambuf<_Elem, _Traits> _Mysb;
typedef ostreambuf_iterator<_Elem, _Traits> _Iter;
typedef num_put<_Elem, _Iter> _Nput;
explicit __CLR_OR_THIS_CALL basic_ostream(
basic_streambuf<_Elem, _Traits> *_Strbuf,
bool _Isstd = false)
{ // construct from a stream buffer pointer
_Myios::init(_Strbuf, _Isstd);
}
__CLR_OR_THIS_CALL basic_ostream(_Uninitialized, bool _Addit = true)
{ // construct uninitialized
if (_Addit)
ios_base::_Addstd(this); // suppress for basic_iostream
}
__CLR_OR_THIS_CALL basic_ostream(_Myt&& _Right)
{ // construct by moving _Right
_Myios::init();
_Myios::move(_STD move(_Right));
}
_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
if (this != &_Right)
_Myios::swap(_Right);
}
virtual __CLR_OR_THIS_CALL ~basic_ostream()
{ // destroy the object
}
typedef typename _Traits::int_type int_type;
typedef typename _Traits::pos_type pos_type;
typedef typename _Traits::off_type off_type;
class _Sentry_base
{ // stores thread lock and reference to output stream
public:
__CLR_OR_THIS_CALL _Sentry_base(_Myt& _Ostr)
: _Myostr(_Ostr)
{ // lock the stream buffer, if there
if (_Myostr.rdbuf() != 0)
_Myostr.rdbuf()->_Lock();
}
__CLR_OR_THIS_CALL ~_Sentry_base()
{ // destroy after unlocking
if (_Myostr.rdbuf() != 0)
_Myostr.rdbuf()->_Unlock();
}
_Myt& _Myostr; // the output stream, for _Unlock call at destruction
private:
_Sentry_base& operator=(const _Sentry_base&);
};
class sentry
: public _Sentry_base
{ // stores thread lock and state of stream
public:
explicit __CLR_OR_THIS_CALL sentry(_Myt& _Ostr)
: _Sentry_base(_Ostr)
{ // construct locking and testing stream
if (_Ostr.good() && _Ostr.tie() != 0)
_Ostr.tie()->flush();
_Ok = _Ostr.good(); // store test only after flushing tie
}
__CLR_OR_THIS_CALL ~sentry()
{ // destroy the object
#if _HAS_EXCEPTIONS
if (!uncaught_exception())
this->_Myostr._Osfx();
#else /* _HAS_EXCEPTIONS */
this->_Myostr._Osfx();
#endif /* _HAS_EXCEPTIONS */
}
__CLR_OR_THIS_CALL _OPERATOR_BOOL() const
{ // test if stream state okay
return (_Ok ? _CONVERTIBLE_TO_TRUE : 0);
}
private:
bool _Ok; // true if stream state okay 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 opfx()
{ // test stream state and flush tie stream as needed (retained)
if (ios_base::good() && _Myios::tie() != 0)
_Myios::tie()->flush();
return (ios_base::good());
}
void __CLR_OR_THIS_CALL osfx()
{ // perform any wrapup (retained)
_Osfx();
}
void __CLR_OR_THIS_CALL _Osfx()
{ // perform any wrapup
_TRY_BEGIN
if (ios_base::flags() & ios_base::unitbuf)
flush(); // flush stream as needed
_CATCH_ALL
_CATCH_END
}
#ifdef _M_CEE_PURE
_Myt& __CLR_OR_THIS_CALL operator<<(_Myt& (__clrcall *_Pfn)(_Myt&))
{ // call basic_ostream 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_ostream 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)
{ // insert a boolean
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);
_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), _Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator<<(short _Val)
{ // insert a short
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);
ios_base::fmtflags _Bfl =
ios_base::flags() & ios_base::basefield;
long _Tmp = (_Bfl == ios_base::oct
|| _Bfl == ios_base::hex)
? (long)(unsigned short)_Val : (long)_Val;
_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), _Tmp).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
/* NOTE:
If you are not using native wchar_t, the unsigned short inserter
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)
{ // insert an unsigned short
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);
_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), (unsigned long)_Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator<<(int _Val)
{ // insert an int
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);
ios_base::fmtflags _Bfl =
ios_base::flags() & ios_base::basefield;
long _Tmp = (_Bfl == ios_base::oct
|| _Bfl == ios_base::hex)
? (long)(unsigned int)_Val : (long)_Val;
_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), _Tmp).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator<<(unsigned int _Val)
{ // insert an unsigned int
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);
_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), (unsigned long)_Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator<<(long _Val)
{ // insert a long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);
_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), _Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
_Myt& __CLR_OR_THIS_CALL operator<<(unsigned long _Val)
{ // insert an unsigned long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
if (_Ok)
{ // state okay, use facet to insert
const _Nput& _Nput_fac = _USE(ios_base::getloc(), _Nput);
_TRY_IO_BEGIN
if (_Nput_fac.put(_Iter(_Myios::rdbuf()), *this,
_Myios::fill(), _Val).failed())
_State |= ios_base::badbit;
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}
#ifdef _LONGLONG
_Myt& __CLR_OR_THIS_CALL operator<<(_LONGLONG _Val)
{ // insert a long long
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -