You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

725 lines
19 KiB

5 years ago
// -*- C++ -*-
/***************************************************************************
*
* ostream - Declarations for the Standard Library ostream classes
*
* $Id: ostream 186353 2014-07-29 16:30:44Z ransin01 $
*
***************************************************************************
*
* Copyright (c) 1994-2001 Rogue Wave Software, Inc. All Rights Reserved.
*
* This computer software is owned by Rogue Wave Software, Inc. and is
* protected by U.S. copyright laws and other laws and by international
* treaties. This computer software is furnished by Rogue Wave Software,
* Inc. pursuant to a written license agreement and may be used, copied,
* transmitted, and stored only in accordance with the terms of such
* license and with the inclusion of the above copyright notice. This
* computer software or any other copies thereof may not be provided or
* otherwise made available to any other person.
*
* U.S. Government Restricted Rights. This computer software is provided
* with Restricted Rights. Use, duplication, or disclosure by the
* Government is subject to restrictions as set forth in subparagraph (c)
* (1) (ii) of The Rights in Technical Data and Computer Software clause
* at DFARS 252.227-7013 or subparagraphs (c) (1) and (2) of the
* Commercial Computer Software--Restricted Rights at 48 CFR 52.227-19,
* as applicable. Manufacturer is Rogue Wave Software, Inc., 5500
* Flatiron Parkway, Boulder, Colorado 80301 USA.
*
**************************************************************************/
#ifndef _RWSTD_OSTREAM_INCLUDED
#define _RWSTD_OSTREAM_INCLUDED
#include <ios>
#include <iosfwd>
#ifndef _RWSTD_NO_REDUNDANT_DEFINITIONS
# include <rw/_ioiter.h>
# include <streambuf>
#endif // _RWSTD_NO_REDUNDANT_DEFINITIONS
#include <rw/_numeric.h>
#include <rw/_defs.h>
_RWSTD_NAMESPACE_BEGIN (__rw)
// helper - implements insertion of arithmetic and pointer types
template <class _CharT, class _Traits, class _NativeType>
_STD::basic_ostream<_CharT, _Traits>&
__rw_insert (_STD::basic_ostream<_CharT, _Traits>&, _NativeType);
// helper - implements insertion of character strigs
template<class _CharT, class _Traits, class _StringT>
_STD::basic_ostream<_CharT, _Traits>&
__rw_insert (_STD::basic_ostream<_CharT, _Traits>&, _StringT*,
_STD::streamsize, _STD::streamsize);
template<class _CharT, class _Traits, class _StringT>
inline _STD::basic_ostream<_CharT, _Traits>&
__rw_insert (_STD::basic_ostream<_CharT, _Traits> &__strm, const _StringT *__s,
_STD::streamsize __len, _STD::streamsize __width)
{
return __rw_insert (__strm, _RWSTD_CONST_CAST (_StringT*, __s),
__len, __width);
}
_RWSTD_NAMESPACE_END // __rw
_RWSTD_NAMESPACE_BEGIN (std)
template<class _CharT, class _Traits>
class basic_ostream: virtual public basic_ios<_CharT, _Traits>
{
public:
typedef _CharT char_type;
typedef _Traits traits_type;
typedef _TYPENAME traits_type::int_type int_type;
typedef _TYPENAME traits_type::pos_type pos_type;
typedef _TYPENAME traits_type::off_type off_type;
typedef basic_ios<char_type, traits_type> ios_type;
// 27.6.2.2, p1
_EXPLICIT basic_ostream (basic_streambuf<char_type, traits_type> *__sb) {
this->init (__sb);
}
// implements sentry ctor
basic_ostream& _C_opfx ();
// allow access to ios_base::_C_bufmutex()
class sentry;
friend class sentry;
// 27.6.2.3
class sentry: public _RW::__rw_guard {
sentry (const sentry&); // not defined
sentry& operator= (const sentry&); // not defined
public:
_EXPLICIT
sentry (basic_ostream<char_type, traits_type> &__strm)
: _RW::__rw_guard (__strm.rdbuf () ? __strm._C_bufmutex () : 0),
_C_strm (__strm) {
_C_ok = _C_strm._C_opfx ().good ();
}
// 27.6.2.3, p4
~sentry () {
if ( ( _C_strm.flags () & ios_base::unitbuf
|| _C_strm._C_is_sync ()
&& _C_strm._C_is_std ()) // [w]cout, [w]cerr, [w]clog
&& !_UNCAUGHT_EXCEPTION ()
&& -1 == _C_strm.rdbuf ()->pubsync ()) {
// prevent exceptions from propagating
_C_strm.setstate (ios_base::badbit, 0);
}
}
operator bool () const {
return _C_ok;
}
private:
basic_ostream& _C_strm; // stream guarded by sentry
bool _C_ok; // is stream okay?
};
// 27.6.2.5 - Formatted output functions
// 27.6.2.5.3, p1
basic_ostream& operator<< (basic_ostream& (*__pf)(basic_ostream&)) {
return (*__pf)(*this);
}
// 27.6.2.5.3, p2
basic_ostream& operator<< (ios_base& (*__pf)(ios_base&)) {
return (*__pf)(*this), *this;
}
// 27.6.2.5.3, p4
basic_ostream& operator<< (ios_type& (*__pf)(ios_type&)) {
return (*__pf)(*this), *this;
}
// 27.6.2.5.2 - Arithmetic inserters
#ifndef _RWSTD_NO_BOOL
basic_ostream& operator<<(bool __val) {
return _RW::__rw_insert (*this, __val);
}
#endif // _RWSTD_NO_BOOL
basic_ostream& operator<< (short __val) {
return _RW::__rw_insert (*this, __val);
}
basic_ostream& operator<< (unsigned short __val) {
return *this << _RWSTD_STATIC_CAST (unsigned long, __val);
}
basic_ostream& operator<< (int __val) {
return _RW::__rw_insert (*this, __val);
}
basic_ostream& operator<< (unsigned int __val) {
return *this << _RWSTD_STATIC_CAST (unsigned long, __val);
}
basic_ostream& operator<< (long __val) {
return _RW::__rw_insert (*this, __val);
}
basic_ostream& operator<< (unsigned long __val) {
return _RW::__rw_insert (*this, __val);
}
basic_ostream& operator<< (float __val) {
return *this << static_cast<double>(__val);
}
basic_ostream& operator<< (double __val) {
return _RW::__rw_insert (*this, __val);
}
#ifndef _RWSTD_NO_LONG_DOUBLE
basic_ostream& operator<< (long double __val) {
return _RW::__rw_insert (*this, __val);
}
#endif // _RWSTD_NO_LONG_DOUBLE
#ifdef _RWSTD_LONG_LONG
// extension
basic_ostream& operator<< (unsigned _RWSTD_LONG_LONG __val) {
return _RW::__rw_insert (*this, __val);
}
// extension
basic_ostream& operator<< (_RWSTD_LONG_LONG __val) {
return _RW::__rw_insert (*this, __val);
}
#endif // _RWSTD_LONG_LONG
basic_ostream& operator<< (const void *__val) {
return _RW::__rw_insert (*this, __val);
}
// extension
basic_ostream& operator<< (basic_streambuf<char_type, traits_type>&);
// 27.6.2.6.3, p6
basic_ostream&
operator<< (basic_streambuf<char_type, traits_type> *__sb) {
return __sb ? *this << *__sb
: (this->setstate (ios_base::badbit), *this);
}
// 27.6.2.6 - Unformatted output functions
// 27.6.2.6, p2
basic_ostream& put (char_type __c) {
return _RW::__rw_insert (*this, &__c, 1, 0 /* width */);
}
// 27.6.2.6, p5
basic_ostream& write (const char_type *__s, streamsize __len) {
#if !defined (_MSC_VER) || (_MSC_VER > 1300)
// working around yet another of the variety of MSVC bugs
return _RW::__rw_insert (*this, __s, __len, 0 /* width */);
#else
return _RW::__rw_insert<char_type, traits_type, char_type>
(*this, __s, __len, 0 /* width */);
#endif // !defined (_MSC_VER) || (_MSC_VER > 1300)
}
// 27.6.2.6, p7
basic_ostream& flush ();
// 27.6.2.4 - Seek members
// 27.6.2.4, p1
pos_type tellp ();
// 27.6.2.4, p2
basic_ostream& seekp (pos_type);
// 27.6.2.4, p4
basic_ostream& seekp (off_type, ios_base::seekdir);
// is `*this' a [w]cout, [w]cerr?
bool _C_is_cout () const {
return false;
}
bool _C_is_cerr () const {
return false;
}
// is `*this' one of { [w]cout, [w]cerr, [w]clog }?
bool _C_is_std () const {
return false;
}
// pad output with fill chars, return number of chars written
streamsize _C_pad (streamsize);
};
#if defined (_MSC_VER) && _MSC_VER <= 1300
// explicit instantiation followed by explicit specialization is illegal
// according to 14.6.4.1, p7 but MSVC allows it (linker errors otherwise)
_RWSTD_INSTANTIATE_2 (class _RWSTD_EXPORT
basic_ostream<char, char_traits<char> >);
#ifndef _RWSTD_NO_WCHAR_T
_RWSTD_INSTANTIATE_2 (class _RWSTD_EXPORT
basic_ostream<wchar_t, char_traits<wchar_t> >);
#endif // _RWSTD_NO_WCHAR_T
#endif // defined (_MSC_VER) && _MSC_VER <= 1300
template<class _CharT, class _Traits>
inline streamsize
basic_ostream<_CharT, _Traits>::_C_pad (streamsize __len)
{
// note that __len can be negative
for (streamsize __i = 0; __i < __len; ++__i) {
if (traits_type::eq_int_type (this->rdbuf ()->sputc (this->fill ()),
traits_type::eof ())) {
return __i;
}
}
return __len;
}
// avoid having to bring standard iostreams into scope
extern _RWSTD_EXPORT const void* __rw_std_streams[];
_RWSTD_SPECIALIZED_FUNCTION
inline bool
basic_ostream<char, char_traits<char> >::_C_is_cout () const
{
// upcast to a virtual base necessary
return _RWSTD_STATIC_CAST (const ios_base*, this) == __rw_std_streams [1];
}
_RWSTD_SPECIALIZED_FUNCTION
inline bool
basic_ostream<char, char_traits<char> >::_C_is_cerr () const
{
// upcast to a virtual base necessary
return _RWSTD_STATIC_CAST (const ios_base*, this) == __rw_std_streams [2];
}
_RWSTD_SPECIALIZED_FUNCTION
inline bool
basic_ostream<char, char_traits <char> >::_C_is_std () const
{
// upcast to a virtual base necessary
return _C_is_cout () || _C_is_cerr ()
|| _RWSTD_STATIC_CAST (const ios_base*, this) == __rw_std_streams [3];
}
#ifndef _RWSTD_NO_WCHAR_T
_RWSTD_SPECIALIZED_FUNCTION
inline bool
basic_ostream<wchar_t, char_traits <wchar_t> >::_C_is_cout () const
{
// upcast to a virtual base necessary
return _RWSTD_STATIC_CAST (const ios_base*, this) == __rw_std_streams [5];
}
_RWSTD_SPECIALIZED_FUNCTION
inline bool
basic_ostream<wchar_t, char_traits <wchar_t> >::_C_is_cerr () const
{
// upcast to a virtual base necessary
return _RWSTD_STATIC_CAST (const ios_base*, this) == __rw_std_streams [6];
}
_RWSTD_SPECIALIZED_FUNCTION
inline bool
basic_ostream<wchar_t, char_traits <wchar_t> >::_C_is_std () const
{
// upcast to a virtual base necessary
return _C_is_cout () || _C_is_cerr ()
|| _RWSTD_STATIC_CAST (const ios_base*, this) == __rw_std_streams [7];
}
#endif // _RWSTD_NO_WCHAR_T
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
basic_ostream<_CharT, _Traits>::flush ()
{
if (this->rdbuf ()) {
_RWSTD_MT_GUARD (this->_C_bufmutex ());
if (-1 == this->rdbuf ()->pubsync ())
this->setstate (ios_base::badbit);
}
return *this;
}
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
basic_ostream<_CharT, _Traits>::seekp (pos_type __pos)
{
_RWSTD_ASSERT (0 != this->rdbuf ());
if (!this->fail ()) {
_RWSTD_MT_GUARD (this->_C_bufmutex ());
if (-1 == this->rdbuf ()->pubseekpos (__pos, ios_base::out))
this->setstate (ios_base::failbit); // lwg issue 129
}
return *this;
}
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
basic_ostream<_CharT, _Traits>::seekp (off_type __off, ios_base::seekdir __dir)
{
_RWSTD_ASSERT (0 != this->rdbuf ());
if (!this->fail ()) {
_RWSTD_MT_GUARD (this->_C_bufmutex ());
if (-1 == this->rdbuf()->pubseekoff (__off, __dir, ios_base::out))
this->setstate (ios_base::failbit); // lwg issue 129
}
return *this;
}
template<class _CharT, class _Traits>
inline _TYPENAME basic_ostream<_CharT, _Traits>::pos_type
basic_ostream<_CharT, _Traits>::tellp ()
{
_RWSTD_ASSERT (0 != this->rdbuf ());
if (!this->fail ()) {
_RWSTD_MT_GUARD (this->_C_bufmutex ());
return this->rdbuf()->pubseekoff (0, ios_base::cur, ios_base::out);
}
return pos_type (off_type (-1));
}
// 27.6.2.5.4 - Character inserter template functions
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
operator<< (basic_ostream<_CharT, _Traits> &__strm, _CharT __c)
{
_RW::__rw_insert (__strm, &__c, 1, __strm.width ()).width (0);
return __strm;
}
#ifndef _RWSTD_NO_OVERLOAD_OF_TEMPLATE_FUNCTION
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
operator<< (basic_ostream<_CharT, _Traits> &__strm, char __c)
{
_RW::__rw_insert (__strm, &__c, 1, __strm.width ()).width (0);
return __strm;
}
#ifndef _RWSTD_NO_FUNC_PARTIAL_SPEC
template <class _Traits>
inline basic_ostream<char, _Traits>&
operator<< (basic_ostream<char, _Traits> &__strm, char __c)
{
_RW::__rw_insert (__strm, &__c, 1, __strm.width ()).width (0);
return __strm;
}
#else // if defined (_RWSTD_NO_FUNC_PARTIAL_SPEC)
inline basic_ostream<char, char_traits<char> >&
operator<< (basic_ostream<char, char_traits<char> > &__strm, char __c)
{
_RW::__rw_insert (__strm, &__c, 1, __strm.width ()).width (0);
return __strm;
}
#endif // _RWSTD_NO_FUNC_PARTIAL_SPEC
#endif // _RWSTD_NO_OVERLOAD_OF_TEMPLATE_FUNCTION
_RWSTD_NAMESPACE_END // std
_RWSTD_NAMESPACE_BEGIN (__rw)
_RWSTD_INSTANTIATE_1 (
_RWSTD_EXPORT ostream&
__rw_insert (ostream&, char*, streamsize, streamsize)
);
#ifndef _RWSTD_NO_WCHAR_T
_RWSTD_INSTANTIATE_1 (
_RWSTD_EXPORT wostream&
__rw_insert (wostream&, wchar_t*, streamsize, streamsize)
);
_RWSTD_INSTANTIATE_1 (
_RWSTD_EXPORT wostream&
__rw_insert (wostream&, char*, streamsize, streamsize)
);
#endif // _RWSTD_NO_WCHAR_T
_RWSTD_NAMESPACE_END // __rw
_RWSTD_NAMESPACE_BEGIN (std)
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
operator<< (basic_ostream<_CharT, _Traits> &__strm, const _CharT *__s)
{
#if !defined (_MSC_VER) //|| _MSC_VER > 1300
_RW::__rw_insert (__strm, __s, _Traits::length (__s),
__strm.width ()).width (0);
#else
// template arguments explicitly provided to work around an MSVC bug
_RW::__rw_insert<_CharT, _Traits, _CharT>
(__strm, __s, _Traits::length (__s), __strm.width ()).width (0);
#endif
return __strm;
}
#ifndef _RWSTD_NO_OVERLOAD_OF_TEMPLATE_FUNCTION
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
operator<< (basic_ostream<_CharT, _Traits> &__strm, const char *__s)
{
_RW::__rw_insert (__strm, __s, char_traits<char>::length (__s),
__strm.width ()).width (0);
return __strm;
}
#ifndef _RWSTD_NO_FUNC_PARTIAL_SPEC
template <class _Traits>
inline basic_ostream<char, _Traits>&
operator<< (basic_ostream<char, _Traits> &__strm, const char *__s)
{
_RW::__rw_insert (__strm, __s, char_traits<char>::length (__s),
__strm.width ()).width (0);
return __strm;
}
#else // if defined (_RWSTD_NO_FUNC_PARTIAL_SPEC)
inline basic_ostream<char, char_traits<char> >&
operator<< (basic_ostream<char, char_traits<char> >& __strm, const char *__s)
{
_RW::__rw_insert (__strm, __s, char_traits<char>::length (__s),
__strm.width ()).width (0);
return __strm;
}
#endif // _RWSTD_NO_FUNC_PARTIAL_SPEC
#endif // _RWSTD_NO_OVERLOAD_OF_TEMPLATE_FUNCTION
#ifndef _RWSTD_NO_SIGNED_CHAR_IN_STREAMS
template <class _Traits>
inline basic_ostream<char, _Traits>&
operator<< (basic_ostream<char, _Traits> &__strm, unsigned char __c)
{
return __strm << _RWSTD_STATIC_CAST (char, __c);
}
template <class _Traits>
inline basic_ostream<char, _Traits>&
operator<< (basic_ostream<char, _Traits> &__strm, signed char __c)
{
return __strm << _RWSTD_STATIC_CAST (char, __c);
}
template <class _Traits>
inline basic_ostream<char, _Traits>&
operator<< (basic_ostream<char, _Traits> &__strm, const unsigned char *__s)
{
return __strm << _RWSTD_REINTERPRET_CAST (const char*, __s);
}
template <class _Traits>
inline basic_ostream<char, _Traits>&
operator<< (basic_ostream<char, _Traits>& __strm, const signed char *__s)
{
return __strm << _RWSTD_REINTERPRET_CAST (const char*, __s);
}
#endif // _RWSTD_NO_SIGNED_CHAR_IN_STREAMS
// 27.6.2.7, p1
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
endl (basic_ostream<_CharT, _Traits>& __strm)
{
return __strm.put (__strm.widen ('\n')).flush ();
}
// 27.6.2.7, p3
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
ends (basic_ostream<_CharT, _Traits>& __strm)
{
return __strm.put (_CharT ());
}
// 27.6.2.7, p5
template<class _CharT, class _Traits>
inline basic_ostream<_CharT, _Traits>&
flush (basic_ostream<_CharT, _Traits>& __strm)
{
return __strm.flush ();
}
// 21.3.7.9, p3 - defined here, declared inline in <string>
template<class _CharT, class _Traits, class _Allocator>
inline basic_ostream<_CharT, _Traits>&
operator<< (basic_ostream<_CharT, _Traits> & __strm,
const basic_string<_CharT, _Traits, _Allocator> &__str)
{
#if !defined (_MSC_VER) || (_MSC_VER > 1300)
// working around yet another of the variety of MSVC bugs
_RW::__rw_insert (__strm, __str.data (), __str.length (),
__strm.width ()).width (0);
#else
_RW::__rw_insert<_CharT, _Traits, _CharT>
(__strm, __str.data (), __str.length (), __strm.width ()).width (0);
#endif // !defined (_MSC_VER) || (_MSC_VER > 1200)
return __strm;
}
#if defined (_MSC_VER) && _MSV_VER <= 1300
// working around an MSVC 6.0 bug that causes it to pick the member
// operator<<(const void*) over the above if overloaded in user code
_RWSTD_SPECIALIZED_FUNCTION
inline ostream& endl (ostream& __strm)
{
return __strm.put (__strm.widen ('\n')).flush ();
}
_RWSTD_SPECIALIZED_FUNCTION
inline ostream& ends (ostream& __strm)
{
return __strm.put (char ());
}
_RWSTD_SPECIALIZED_FUNCTION
inline ostream& flush (ostream& __strm)
{
return __strm.flush ();
}
#ifndef _RWSTD_NO_WCHAR_T
_RWSTD_SPECIALIZED_FUNCTION
inline wostream& endl (wostream& __strm)
{
return __strm.put (__strm.widen ('\n')).flush ();
}
_RWSTD_SPECIALIZED_FUNCTION
inline wostream& ends (wostream& __strm)
{
return __strm.put (char ());
}
_RWSTD_SPECIALIZED_FUNCTION
inline wostream& flush (wostream& __strm)
{
return __strm.flush ();
}
#endif // _RWSTD_NO_WCHAR_T
#else
// working around an MSVC 6.0 bug that causes:
// - warning C4660: template-class specialization is already instantiated
// - many linker errors for (inline or otherwise) members of basic_ostream
_RWSTD_INSTANTIATE_2 (class _RWSTD_EXPORT
basic_ostream<char, char_traits<char> >);
#ifndef _RWSTD_NO_WCHAR_T
_RWSTD_INSTANTIATE_2 (class _RWSTD_EXPORT
basic_ostream<wchar_t, char_traits<wchar_t> >);
#endif // _RWSTD_NO_WCHAR_T
#endif // !_MSC_VER || _MSC_VER > 1300
_RWSTD_NAMESPACE_END // std
#if _RWSTD_DEFINE_TEMPLATE (BASIC_OSTREAM)
# include <ostream.cc>
#endif
#endif // _RWSTD_OSTREAM_INCLUDED