1*e4b17023SJohn Marino// Output streams -*- C++ -*- 2*e4b17023SJohn Marino 3*e4b17023SJohn Marino// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 4*e4b17023SJohn Marino// 2006, 2007, 2008, 2009, 2010, 2011 5*e4b17023SJohn Marino// Free Software Foundation, Inc. 6*e4b17023SJohn Marino// 7*e4b17023SJohn Marino// This file is part of the GNU ISO C++ Library. This library is free 8*e4b17023SJohn Marino// software; you can redistribute it and/or modify it under the 9*e4b17023SJohn Marino// terms of the GNU General Public License as published by the 10*e4b17023SJohn Marino// Free Software Foundation; either version 3, or (at your option) 11*e4b17023SJohn Marino// any later version. 12*e4b17023SJohn Marino 13*e4b17023SJohn Marino// This library is distributed in the hope that it will be useful, 14*e4b17023SJohn Marino// but WITHOUT ANY WARRANTY; without even the implied warranty of 15*e4b17023SJohn Marino// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*e4b17023SJohn Marino// GNU General Public License for more details. 17*e4b17023SJohn Marino 18*e4b17023SJohn Marino// Under Section 7 of GPL version 3, you are granted additional 19*e4b17023SJohn Marino// permissions described in the GCC Runtime Library Exception, version 20*e4b17023SJohn Marino// 3.1, as published by the Free Software Foundation. 21*e4b17023SJohn Marino 22*e4b17023SJohn Marino// You should have received a copy of the GNU General Public License and 23*e4b17023SJohn Marino// a copy of the GCC Runtime Library Exception along with this program; 24*e4b17023SJohn Marino// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 25*e4b17023SJohn Marino// <http://www.gnu.org/licenses/>. 26*e4b17023SJohn Marino 27*e4b17023SJohn Marino/** @file include/ostream 28*e4b17023SJohn Marino * This is a Standard C++ Library header. 29*e4b17023SJohn Marino */ 30*e4b17023SJohn Marino 31*e4b17023SJohn Marino// 32*e4b17023SJohn Marino// ISO C++ 14882: 27.6.2 Output streams 33*e4b17023SJohn Marino// 34*e4b17023SJohn Marino 35*e4b17023SJohn Marino#ifndef _GLIBCXX_OSTREAM 36*e4b17023SJohn Marino#define _GLIBCXX_OSTREAM 1 37*e4b17023SJohn Marino 38*e4b17023SJohn Marino#pragma GCC system_header 39*e4b17023SJohn Marino 40*e4b17023SJohn Marino#include <ios> 41*e4b17023SJohn Marino#include <bits/ostream_insert.h> 42*e4b17023SJohn Marino 43*e4b17023SJohn Marinonamespace std _GLIBCXX_VISIBILITY(default) 44*e4b17023SJohn Marino{ 45*e4b17023SJohn Marino_GLIBCXX_BEGIN_NAMESPACE_VERSION 46*e4b17023SJohn Marino 47*e4b17023SJohn Marino /** 48*e4b17023SJohn Marino * @brief Template class basic_ostream. 49*e4b17023SJohn Marino * @ingroup io 50*e4b17023SJohn Marino * 51*e4b17023SJohn Marino * This is the base class for all output streams. It provides text 52*e4b17023SJohn Marino * formatting of all builtin types, and communicates with any class 53*e4b17023SJohn Marino * derived from basic_streambuf to do the actual output. 54*e4b17023SJohn Marino */ 55*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 56*e4b17023SJohn Marino class basic_ostream : virtual public basic_ios<_CharT, _Traits> 57*e4b17023SJohn Marino { 58*e4b17023SJohn Marino public: 59*e4b17023SJohn Marino // Types (inherited from basic_ios): 60*e4b17023SJohn Marino typedef _CharT char_type; 61*e4b17023SJohn Marino typedef typename _Traits::int_type int_type; 62*e4b17023SJohn Marino typedef typename _Traits::pos_type pos_type; 63*e4b17023SJohn Marino typedef typename _Traits::off_type off_type; 64*e4b17023SJohn Marino typedef _Traits traits_type; 65*e4b17023SJohn Marino 66*e4b17023SJohn Marino // Non-standard Types: 67*e4b17023SJohn Marino typedef basic_streambuf<_CharT, _Traits> __streambuf_type; 68*e4b17023SJohn Marino typedef basic_ios<_CharT, _Traits> __ios_type; 69*e4b17023SJohn Marino typedef basic_ostream<_CharT, _Traits> __ostream_type; 70*e4b17023SJohn Marino typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> > 71*e4b17023SJohn Marino __num_put_type; 72*e4b17023SJohn Marino typedef ctype<_CharT> __ctype_type; 73*e4b17023SJohn Marino 74*e4b17023SJohn Marino /** 75*e4b17023SJohn Marino * @brief Base constructor. 76*e4b17023SJohn Marino * 77*e4b17023SJohn Marino * This ctor is almost never called by the user directly, rather from 78*e4b17023SJohn Marino * derived classes' initialization lists, which pass a pointer to 79*e4b17023SJohn Marino * their own stream buffer. 80*e4b17023SJohn Marino */ 81*e4b17023SJohn Marino explicit 82*e4b17023SJohn Marino basic_ostream(__streambuf_type* __sb) 83*e4b17023SJohn Marino { this->init(__sb); } 84*e4b17023SJohn Marino 85*e4b17023SJohn Marino /** 86*e4b17023SJohn Marino * @brief Base destructor. 87*e4b17023SJohn Marino * 88*e4b17023SJohn Marino * This does very little apart from providing a virtual base dtor. 89*e4b17023SJohn Marino */ 90*e4b17023SJohn Marino virtual 91*e4b17023SJohn Marino ~basic_ostream() { } 92*e4b17023SJohn Marino 93*e4b17023SJohn Marino /// Safe prefix/suffix operations. 94*e4b17023SJohn Marino class sentry; 95*e4b17023SJohn Marino friend class sentry; 96*e4b17023SJohn Marino 97*e4b17023SJohn Marino //@{ 98*e4b17023SJohn Marino /** 99*e4b17023SJohn Marino * @brief Interface for manipulators. 100*e4b17023SJohn Marino * 101*e4b17023SJohn Marino * Manipulators such as @c std::endl and @c std::hex use these 102*e4b17023SJohn Marino * functions in constructs like "std::cout << std::endl". For more 103*e4b17023SJohn Marino * information, see the iomanip header. 104*e4b17023SJohn Marino */ 105*e4b17023SJohn Marino __ostream_type& 106*e4b17023SJohn Marino operator<<(__ostream_type& (*__pf)(__ostream_type&)) 107*e4b17023SJohn Marino { 108*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 109*e4b17023SJohn Marino // DR 60. What is a formatted input function? 110*e4b17023SJohn Marino // The inserters for manipulators are *not* formatted output functions. 111*e4b17023SJohn Marino return __pf(*this); 112*e4b17023SJohn Marino } 113*e4b17023SJohn Marino 114*e4b17023SJohn Marino __ostream_type& 115*e4b17023SJohn Marino operator<<(__ios_type& (*__pf)(__ios_type&)) 116*e4b17023SJohn Marino { 117*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 118*e4b17023SJohn Marino // DR 60. What is a formatted input function? 119*e4b17023SJohn Marino // The inserters for manipulators are *not* formatted output functions. 120*e4b17023SJohn Marino __pf(*this); 121*e4b17023SJohn Marino return *this; 122*e4b17023SJohn Marino } 123*e4b17023SJohn Marino 124*e4b17023SJohn Marino __ostream_type& 125*e4b17023SJohn Marino operator<<(ios_base& (*__pf) (ios_base&)) 126*e4b17023SJohn Marino { 127*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 128*e4b17023SJohn Marino // DR 60. What is a formatted input function? 129*e4b17023SJohn Marino // The inserters for manipulators are *not* formatted output functions. 130*e4b17023SJohn Marino __pf(*this); 131*e4b17023SJohn Marino return *this; 132*e4b17023SJohn Marino } 133*e4b17023SJohn Marino //@} 134*e4b17023SJohn Marino 135*e4b17023SJohn Marino //@{ 136*e4b17023SJohn Marino /** 137*e4b17023SJohn Marino * @name Inserters 138*e4b17023SJohn Marino * 139*e4b17023SJohn Marino * All the @c operator<< functions (aka <em>formatted output 140*e4b17023SJohn Marino * functions</em>) have some common behavior. Each starts by 141*e4b17023SJohn Marino * constructing a temporary object of type std::basic_ostream::sentry. 142*e4b17023SJohn Marino * This can have several effects, concluding with the setting of a 143*e4b17023SJohn Marino * status flag; see the sentry documentation for more. 144*e4b17023SJohn Marino * 145*e4b17023SJohn Marino * If the sentry status is good, the function tries to generate 146*e4b17023SJohn Marino * whatever data is appropriate for the type of the argument. 147*e4b17023SJohn Marino * 148*e4b17023SJohn Marino * If an exception is thrown during insertion, ios_base::badbit 149*e4b17023SJohn Marino * will be turned on in the stream's error state without causing an 150*e4b17023SJohn Marino * ios_base::failure to be thrown. The original exception will then 151*e4b17023SJohn Marino * be rethrown. 152*e4b17023SJohn Marino */ 153*e4b17023SJohn Marino 154*e4b17023SJohn Marino //@{ 155*e4b17023SJohn Marino /** 156*e4b17023SJohn Marino * @brief Integer arithmetic inserters 157*e4b17023SJohn Marino * @param __n A variable of builtin integral type. 158*e4b17023SJohn Marino * @return @c *this if successful 159*e4b17023SJohn Marino * 160*e4b17023SJohn Marino * These functions use the stream's current locale (specifically, the 161*e4b17023SJohn Marino * @c num_get facet) to perform numeric formatting. 162*e4b17023SJohn Marino */ 163*e4b17023SJohn Marino __ostream_type& 164*e4b17023SJohn Marino operator<<(long __n) 165*e4b17023SJohn Marino { return _M_insert(__n); } 166*e4b17023SJohn Marino 167*e4b17023SJohn Marino __ostream_type& 168*e4b17023SJohn Marino operator<<(unsigned long __n) 169*e4b17023SJohn Marino { return _M_insert(__n); } 170*e4b17023SJohn Marino 171*e4b17023SJohn Marino __ostream_type& 172*e4b17023SJohn Marino operator<<(bool __n) 173*e4b17023SJohn Marino { return _M_insert(__n); } 174*e4b17023SJohn Marino 175*e4b17023SJohn Marino __ostream_type& 176*e4b17023SJohn Marino operator<<(short __n); 177*e4b17023SJohn Marino 178*e4b17023SJohn Marino __ostream_type& 179*e4b17023SJohn Marino operator<<(unsigned short __n) 180*e4b17023SJohn Marino { 181*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 182*e4b17023SJohn Marino // 117. basic_ostream uses nonexistent num_put member functions. 183*e4b17023SJohn Marino return _M_insert(static_cast<unsigned long>(__n)); 184*e4b17023SJohn Marino } 185*e4b17023SJohn Marino 186*e4b17023SJohn Marino __ostream_type& 187*e4b17023SJohn Marino operator<<(int __n); 188*e4b17023SJohn Marino 189*e4b17023SJohn Marino __ostream_type& 190*e4b17023SJohn Marino operator<<(unsigned int __n) 191*e4b17023SJohn Marino { 192*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 193*e4b17023SJohn Marino // 117. basic_ostream uses nonexistent num_put member functions. 194*e4b17023SJohn Marino return _M_insert(static_cast<unsigned long>(__n)); 195*e4b17023SJohn Marino } 196*e4b17023SJohn Marino 197*e4b17023SJohn Marino#ifdef _GLIBCXX_USE_LONG_LONG 198*e4b17023SJohn Marino __ostream_type& 199*e4b17023SJohn Marino operator<<(long long __n) 200*e4b17023SJohn Marino { return _M_insert(__n); } 201*e4b17023SJohn Marino 202*e4b17023SJohn Marino __ostream_type& 203*e4b17023SJohn Marino operator<<(unsigned long long __n) 204*e4b17023SJohn Marino { return _M_insert(__n); } 205*e4b17023SJohn Marino#endif 206*e4b17023SJohn Marino //@} 207*e4b17023SJohn Marino 208*e4b17023SJohn Marino //@{ 209*e4b17023SJohn Marino /** 210*e4b17023SJohn Marino * @brief Floating point arithmetic inserters 211*e4b17023SJohn Marino * @param __f A variable of builtin floating point type. 212*e4b17023SJohn Marino * @return @c *this if successful 213*e4b17023SJohn Marino * 214*e4b17023SJohn Marino * These functions use the stream's current locale (specifically, the 215*e4b17023SJohn Marino * @c num_get facet) to perform numeric formatting. 216*e4b17023SJohn Marino */ 217*e4b17023SJohn Marino __ostream_type& 218*e4b17023SJohn Marino operator<<(double __f) 219*e4b17023SJohn Marino { return _M_insert(__f); } 220*e4b17023SJohn Marino 221*e4b17023SJohn Marino __ostream_type& 222*e4b17023SJohn Marino operator<<(float __f) 223*e4b17023SJohn Marino { 224*e4b17023SJohn Marino // _GLIBCXX_RESOLVE_LIB_DEFECTS 225*e4b17023SJohn Marino // 117. basic_ostream uses nonexistent num_put member functions. 226*e4b17023SJohn Marino return _M_insert(static_cast<double>(__f)); 227*e4b17023SJohn Marino } 228*e4b17023SJohn Marino 229*e4b17023SJohn Marino __ostream_type& 230*e4b17023SJohn Marino operator<<(long double __f) 231*e4b17023SJohn Marino { return _M_insert(__f); } 232*e4b17023SJohn Marino //@} 233*e4b17023SJohn Marino 234*e4b17023SJohn Marino /** 235*e4b17023SJohn Marino * @brief Pointer arithmetic inserters 236*e4b17023SJohn Marino * @param __p A variable of pointer type. 237*e4b17023SJohn Marino * @return @c *this if successful 238*e4b17023SJohn Marino * 239*e4b17023SJohn Marino * These functions use the stream's current locale (specifically, the 240*e4b17023SJohn Marino * @c num_get facet) to perform numeric formatting. 241*e4b17023SJohn Marino */ 242*e4b17023SJohn Marino __ostream_type& 243*e4b17023SJohn Marino operator<<(const void* __p) 244*e4b17023SJohn Marino { return _M_insert(__p); } 245*e4b17023SJohn Marino 246*e4b17023SJohn Marino /** 247*e4b17023SJohn Marino * @brief Extracting from another streambuf. 248*e4b17023SJohn Marino * @param __sb A pointer to a streambuf 249*e4b17023SJohn Marino * 250*e4b17023SJohn Marino * This function behaves like one of the basic arithmetic extractors, 251*e4b17023SJohn Marino * in that it also constructs a sentry object and has the same error 252*e4b17023SJohn Marino * handling behavior. 253*e4b17023SJohn Marino * 254*e4b17023SJohn Marino * If @p __sb is NULL, the stream will set failbit in its error state. 255*e4b17023SJohn Marino * 256*e4b17023SJohn Marino * Characters are extracted from @p __sb and inserted into @c *this 257*e4b17023SJohn Marino * until one of the following occurs: 258*e4b17023SJohn Marino * 259*e4b17023SJohn Marino * - the input stream reaches end-of-file, 260*e4b17023SJohn Marino * - insertion into the output sequence fails (in this case, the 261*e4b17023SJohn Marino * character that would have been inserted is not extracted), or 262*e4b17023SJohn Marino * - an exception occurs while getting a character from @p __sb, which 263*e4b17023SJohn Marino * sets failbit in the error state 264*e4b17023SJohn Marino * 265*e4b17023SJohn Marino * If the function inserts no characters, failbit is set. 266*e4b17023SJohn Marino */ 267*e4b17023SJohn Marino __ostream_type& 268*e4b17023SJohn Marino operator<<(__streambuf_type* __sb); 269*e4b17023SJohn Marino //@} 270*e4b17023SJohn Marino 271*e4b17023SJohn Marino //@{ 272*e4b17023SJohn Marino /** 273*e4b17023SJohn Marino * @name Unformatted Output Functions 274*e4b17023SJohn Marino * 275*e4b17023SJohn Marino * All the unformatted output functions have some common behavior. 276*e4b17023SJohn Marino * Each starts by constructing a temporary object of type 277*e4b17023SJohn Marino * std::basic_ostream::sentry. This has several effects, concluding 278*e4b17023SJohn Marino * with the setting of a status flag; see the sentry documentation 279*e4b17023SJohn Marino * for more. 280*e4b17023SJohn Marino * 281*e4b17023SJohn Marino * If the sentry status is good, the function tries to generate 282*e4b17023SJohn Marino * whatever data is appropriate for the type of the argument. 283*e4b17023SJohn Marino * 284*e4b17023SJohn Marino * If an exception is thrown during insertion, ios_base::badbit 285*e4b17023SJohn Marino * will be turned on in the stream's error state. If badbit is on in 286*e4b17023SJohn Marino * the stream's exceptions mask, the exception will be rethrown 287*e4b17023SJohn Marino * without completing its actions. 288*e4b17023SJohn Marino */ 289*e4b17023SJohn Marino 290*e4b17023SJohn Marino /** 291*e4b17023SJohn Marino * @brief Simple insertion. 292*e4b17023SJohn Marino * @param __c The character to insert. 293*e4b17023SJohn Marino * @return *this 294*e4b17023SJohn Marino * 295*e4b17023SJohn Marino * Tries to insert @p __c. 296*e4b17023SJohn Marino * 297*e4b17023SJohn Marino * @note This function is not overloaded on signed char and 298*e4b17023SJohn Marino * unsigned char. 299*e4b17023SJohn Marino */ 300*e4b17023SJohn Marino __ostream_type& 301*e4b17023SJohn Marino put(char_type __c); 302*e4b17023SJohn Marino 303*e4b17023SJohn Marino /** 304*e4b17023SJohn Marino * @brief Core write functionality, without sentry. 305*e4b17023SJohn Marino * @param __s The array to insert. 306*e4b17023SJohn Marino * @param __n Maximum number of characters to insert. 307*e4b17023SJohn Marino */ 308*e4b17023SJohn Marino void 309*e4b17023SJohn Marino _M_write(const char_type* __s, streamsize __n) 310*e4b17023SJohn Marino { 311*e4b17023SJohn Marino const streamsize __put = this->rdbuf()->sputn(__s, __n); 312*e4b17023SJohn Marino if (__put != __n) 313*e4b17023SJohn Marino this->setstate(ios_base::badbit); 314*e4b17023SJohn Marino } 315*e4b17023SJohn Marino 316*e4b17023SJohn Marino /** 317*e4b17023SJohn Marino * @brief Character string insertion. 318*e4b17023SJohn Marino * @param __s The array to insert. 319*e4b17023SJohn Marino * @param __n Maximum number of characters to insert. 320*e4b17023SJohn Marino * @return *this 321*e4b17023SJohn Marino * 322*e4b17023SJohn Marino * Characters are copied from @p __s and inserted into the stream until 323*e4b17023SJohn Marino * one of the following happens: 324*e4b17023SJohn Marino * 325*e4b17023SJohn Marino * - @p __n characters are inserted 326*e4b17023SJohn Marino * - inserting into the output sequence fails (in this case, badbit 327*e4b17023SJohn Marino * will be set in the stream's error state) 328*e4b17023SJohn Marino * 329*e4b17023SJohn Marino * @note This function is not overloaded on signed char and 330*e4b17023SJohn Marino * unsigned char. 331*e4b17023SJohn Marino */ 332*e4b17023SJohn Marino __ostream_type& 333*e4b17023SJohn Marino write(const char_type* __s, streamsize __n); 334*e4b17023SJohn Marino //@} 335*e4b17023SJohn Marino 336*e4b17023SJohn Marino /** 337*e4b17023SJohn Marino * @brief Synchronizing the stream buffer. 338*e4b17023SJohn Marino * @return *this 339*e4b17023SJohn Marino * 340*e4b17023SJohn Marino * If @c rdbuf() is a null pointer, changes nothing. 341*e4b17023SJohn Marino * 342*e4b17023SJohn Marino * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1, 343*e4b17023SJohn Marino * sets badbit. 344*e4b17023SJohn Marino */ 345*e4b17023SJohn Marino __ostream_type& 346*e4b17023SJohn Marino flush(); 347*e4b17023SJohn Marino 348*e4b17023SJohn Marino /** 349*e4b17023SJohn Marino * @brief Getting the current write position. 350*e4b17023SJohn Marino * @return A file position object. 351*e4b17023SJohn Marino * 352*e4b17023SJohn Marino * If @c fail() is not false, returns @c pos_type(-1) to indicate 353*e4b17023SJohn Marino * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out). 354*e4b17023SJohn Marino */ 355*e4b17023SJohn Marino pos_type 356*e4b17023SJohn Marino tellp(); 357*e4b17023SJohn Marino 358*e4b17023SJohn Marino /** 359*e4b17023SJohn Marino * @brief Changing the current write position. 360*e4b17023SJohn Marino * @param __pos A file position object. 361*e4b17023SJohn Marino * @return *this 362*e4b17023SJohn Marino * 363*e4b17023SJohn Marino * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If 364*e4b17023SJohn Marino * that function fails, sets failbit. 365*e4b17023SJohn Marino */ 366*e4b17023SJohn Marino __ostream_type& 367*e4b17023SJohn Marino seekp(pos_type); 368*e4b17023SJohn Marino 369*e4b17023SJohn Marino /** 370*e4b17023SJohn Marino * @brief Changing the current write position. 371*e4b17023SJohn Marino * @param __off A file offset object. 372*e4b17023SJohn Marino * @param __dir The direction in which to seek. 373*e4b17023SJohn Marino * @return *this 374*e4b17023SJohn Marino * 375*e4b17023SJohn Marino * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir). 376*e4b17023SJohn Marino * If that function fails, sets failbit. 377*e4b17023SJohn Marino */ 378*e4b17023SJohn Marino __ostream_type& 379*e4b17023SJohn Marino seekp(off_type, ios_base::seekdir); 380*e4b17023SJohn Marino 381*e4b17023SJohn Marino protected: 382*e4b17023SJohn Marino basic_ostream() 383*e4b17023SJohn Marino { this->init(0); } 384*e4b17023SJohn Marino 385*e4b17023SJohn Marino template<typename _ValueT> 386*e4b17023SJohn Marino __ostream_type& 387*e4b17023SJohn Marino _M_insert(_ValueT __v); 388*e4b17023SJohn Marino }; 389*e4b17023SJohn Marino 390*e4b17023SJohn Marino /** 391*e4b17023SJohn Marino * @brief Performs setup work for output streams. 392*e4b17023SJohn Marino * 393*e4b17023SJohn Marino * Objects of this class are created before all of the standard 394*e4b17023SJohn Marino * inserters are run. It is responsible for <em>exception-safe prefix and 395*e4b17023SJohn Marino * suffix operations</em>. 396*e4b17023SJohn Marino */ 397*e4b17023SJohn Marino template <typename _CharT, typename _Traits> 398*e4b17023SJohn Marino class basic_ostream<_CharT, _Traits>::sentry 399*e4b17023SJohn Marino { 400*e4b17023SJohn Marino // Data Members. 401*e4b17023SJohn Marino bool _M_ok; 402*e4b17023SJohn Marino basic_ostream<_CharT, _Traits>& _M_os; 403*e4b17023SJohn Marino 404*e4b17023SJohn Marino public: 405*e4b17023SJohn Marino /** 406*e4b17023SJohn Marino * @brief The constructor performs preparatory work. 407*e4b17023SJohn Marino * @param __os The output stream to guard. 408*e4b17023SJohn Marino * 409*e4b17023SJohn Marino * If the stream state is good (@a __os.good() is true), then if the 410*e4b17023SJohn Marino * stream is tied to another output stream, @c is.tie()->flush() 411*e4b17023SJohn Marino * is called to synchronize the output sequences. 412*e4b17023SJohn Marino * 413*e4b17023SJohn Marino * If the stream state is still good, then the sentry state becomes 414*e4b17023SJohn Marino * true (@a okay). 415*e4b17023SJohn Marino */ 416*e4b17023SJohn Marino explicit 417*e4b17023SJohn Marino sentry(basic_ostream<_CharT, _Traits>& __os); 418*e4b17023SJohn Marino 419*e4b17023SJohn Marino /** 420*e4b17023SJohn Marino * @brief Possibly flushes the stream. 421*e4b17023SJohn Marino * 422*e4b17023SJohn Marino * If @c ios_base::unitbuf is set in @c os.flags(), and 423*e4b17023SJohn Marino * @c std::uncaught_exception() is true, the sentry destructor calls 424*e4b17023SJohn Marino * @c flush() on the output stream. 425*e4b17023SJohn Marino */ 426*e4b17023SJohn Marino ~sentry() 427*e4b17023SJohn Marino { 428*e4b17023SJohn Marino // XXX MT 429*e4b17023SJohn Marino if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception()) 430*e4b17023SJohn Marino { 431*e4b17023SJohn Marino // Can't call flush directly or else will get into recursive lock. 432*e4b17023SJohn Marino if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1) 433*e4b17023SJohn Marino _M_os.setstate(ios_base::badbit); 434*e4b17023SJohn Marino } 435*e4b17023SJohn Marino } 436*e4b17023SJohn Marino 437*e4b17023SJohn Marino /** 438*e4b17023SJohn Marino * @brief Quick status checking. 439*e4b17023SJohn Marino * @return The sentry state. 440*e4b17023SJohn Marino * 441*e4b17023SJohn Marino * For ease of use, sentries may be converted to booleans. The 442*e4b17023SJohn Marino * return value is that of the sentry state (true == okay). 443*e4b17023SJohn Marino */ 444*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 445*e4b17023SJohn Marino explicit 446*e4b17023SJohn Marino#endif 447*e4b17023SJohn Marino operator bool() const 448*e4b17023SJohn Marino { return _M_ok; } 449*e4b17023SJohn Marino }; 450*e4b17023SJohn Marino 451*e4b17023SJohn Marino //@{ 452*e4b17023SJohn Marino /** 453*e4b17023SJohn Marino * @brief Character inserters 454*e4b17023SJohn Marino * @param __out An output stream. 455*e4b17023SJohn Marino * @param __c A character. 456*e4b17023SJohn Marino * @return out 457*e4b17023SJohn Marino * 458*e4b17023SJohn Marino * Behaves like one of the formatted arithmetic inserters described in 459*e4b17023SJohn Marino * std::basic_ostream. After constructing a sentry object with good 460*e4b17023SJohn Marino * status, this function inserts a single character and any required 461*e4b17023SJohn Marino * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then 462*e4b17023SJohn Marino * called. 463*e4b17023SJohn Marino * 464*e4b17023SJohn Marino * If @p __c is of type @c char and the character type of the stream is not 465*e4b17023SJohn Marino * @c char, the character is widened before insertion. 466*e4b17023SJohn Marino */ 467*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 468*e4b17023SJohn Marino inline basic_ostream<_CharT, _Traits>& 469*e4b17023SJohn Marino operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c) 470*e4b17023SJohn Marino { return __ostream_insert(__out, &__c, 1); } 471*e4b17023SJohn Marino 472*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 473*e4b17023SJohn Marino inline basic_ostream<_CharT, _Traits>& 474*e4b17023SJohn Marino operator<<(basic_ostream<_CharT, _Traits>& __out, char __c) 475*e4b17023SJohn Marino { return (__out << __out.widen(__c)); } 476*e4b17023SJohn Marino 477*e4b17023SJohn Marino // Specialization 478*e4b17023SJohn Marino template <class _Traits> 479*e4b17023SJohn Marino inline basic_ostream<char, _Traits>& 480*e4b17023SJohn Marino operator<<(basic_ostream<char, _Traits>& __out, char __c) 481*e4b17023SJohn Marino { return __ostream_insert(__out, &__c, 1); } 482*e4b17023SJohn Marino 483*e4b17023SJohn Marino // Signed and unsigned 484*e4b17023SJohn Marino template<class _Traits> 485*e4b17023SJohn Marino inline basic_ostream<char, _Traits>& 486*e4b17023SJohn Marino operator<<(basic_ostream<char, _Traits>& __out, signed char __c) 487*e4b17023SJohn Marino { return (__out << static_cast<char>(__c)); } 488*e4b17023SJohn Marino 489*e4b17023SJohn Marino template<class _Traits> 490*e4b17023SJohn Marino inline basic_ostream<char, _Traits>& 491*e4b17023SJohn Marino operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c) 492*e4b17023SJohn Marino { return (__out << static_cast<char>(__c)); } 493*e4b17023SJohn Marino //@} 494*e4b17023SJohn Marino 495*e4b17023SJohn Marino //@{ 496*e4b17023SJohn Marino /** 497*e4b17023SJohn Marino * @brief String inserters 498*e4b17023SJohn Marino * @param __out An output stream. 499*e4b17023SJohn Marino * @param __s A character string. 500*e4b17023SJohn Marino * @return out 501*e4b17023SJohn Marino * @pre @p __s must be a non-NULL pointer 502*e4b17023SJohn Marino * 503*e4b17023SJohn Marino * Behaves like one of the formatted arithmetic inserters described in 504*e4b17023SJohn Marino * std::basic_ostream. After constructing a sentry object with good 505*e4b17023SJohn Marino * status, this function inserts @c traits::length(__s) characters starting 506*e4b17023SJohn Marino * at @p __s, widened if necessary, followed by any required padding (as 507*e4b17023SJohn Marino * determined by [22.2.2.2.2]). @c __out.width(0) is then called. 508*e4b17023SJohn Marino */ 509*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 510*e4b17023SJohn Marino inline basic_ostream<_CharT, _Traits>& 511*e4b17023SJohn Marino operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s) 512*e4b17023SJohn Marino { 513*e4b17023SJohn Marino if (!__s) 514*e4b17023SJohn Marino __out.setstate(ios_base::badbit); 515*e4b17023SJohn Marino else 516*e4b17023SJohn Marino __ostream_insert(__out, __s, 517*e4b17023SJohn Marino static_cast<streamsize>(_Traits::length(__s))); 518*e4b17023SJohn Marino return __out; 519*e4b17023SJohn Marino } 520*e4b17023SJohn Marino 521*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 522*e4b17023SJohn Marino basic_ostream<_CharT, _Traits> & 523*e4b17023SJohn Marino operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s); 524*e4b17023SJohn Marino 525*e4b17023SJohn Marino // Partial specializations 526*e4b17023SJohn Marino template<class _Traits> 527*e4b17023SJohn Marino inline basic_ostream<char, _Traits>& 528*e4b17023SJohn Marino operator<<(basic_ostream<char, _Traits>& __out, const char* __s) 529*e4b17023SJohn Marino { 530*e4b17023SJohn Marino if (!__s) 531*e4b17023SJohn Marino __out.setstate(ios_base::badbit); 532*e4b17023SJohn Marino else 533*e4b17023SJohn Marino __ostream_insert(__out, __s, 534*e4b17023SJohn Marino static_cast<streamsize>(_Traits::length(__s))); 535*e4b17023SJohn Marino return __out; 536*e4b17023SJohn Marino } 537*e4b17023SJohn Marino 538*e4b17023SJohn Marino // Signed and unsigned 539*e4b17023SJohn Marino template<class _Traits> 540*e4b17023SJohn Marino inline basic_ostream<char, _Traits>& 541*e4b17023SJohn Marino operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s) 542*e4b17023SJohn Marino { return (__out << reinterpret_cast<const char*>(__s)); } 543*e4b17023SJohn Marino 544*e4b17023SJohn Marino template<class _Traits> 545*e4b17023SJohn Marino inline basic_ostream<char, _Traits> & 546*e4b17023SJohn Marino operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s) 547*e4b17023SJohn Marino { return (__out << reinterpret_cast<const char*>(__s)); } 548*e4b17023SJohn Marino //@} 549*e4b17023SJohn Marino 550*e4b17023SJohn Marino // Standard basic_ostream manipulators 551*e4b17023SJohn Marino 552*e4b17023SJohn Marino /** 553*e4b17023SJohn Marino * @brief Write a newline and flush the stream. 554*e4b17023SJohn Marino * 555*e4b17023SJohn Marino * This manipulator is often mistakenly used when a simple newline is 556*e4b17023SJohn Marino * desired, leading to poor buffering performance. See 557*e4b17023SJohn Marino * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html 558*e4b17023SJohn Marino * for more on this subject. 559*e4b17023SJohn Marino */ 560*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 561*e4b17023SJohn Marino inline basic_ostream<_CharT, _Traits>& 562*e4b17023SJohn Marino endl(basic_ostream<_CharT, _Traits>& __os) 563*e4b17023SJohn Marino { return flush(__os.put(__os.widen('\n'))); } 564*e4b17023SJohn Marino 565*e4b17023SJohn Marino /** 566*e4b17023SJohn Marino * @brief Write a null character into the output sequence. 567*e4b17023SJohn Marino * 568*e4b17023SJohn Marino * <em>Null character</em> is @c CharT() by definition. For CharT 569*e4b17023SJohn Marino * of @c char, this correctly writes the ASCII @c NUL character 570*e4b17023SJohn Marino * string terminator. 571*e4b17023SJohn Marino */ 572*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 573*e4b17023SJohn Marino inline basic_ostream<_CharT, _Traits>& 574*e4b17023SJohn Marino ends(basic_ostream<_CharT, _Traits>& __os) 575*e4b17023SJohn Marino { return __os.put(_CharT()); } 576*e4b17023SJohn Marino 577*e4b17023SJohn Marino /** 578*e4b17023SJohn Marino * @brief Flushes the output stream. 579*e4b17023SJohn Marino * 580*e4b17023SJohn Marino * This manipulator simply calls the stream's @c flush() member function. 581*e4b17023SJohn Marino */ 582*e4b17023SJohn Marino template<typename _CharT, typename _Traits> 583*e4b17023SJohn Marino inline basic_ostream<_CharT, _Traits>& 584*e4b17023SJohn Marino flush(basic_ostream<_CharT, _Traits>& __os) 585*e4b17023SJohn Marino { return __os.flush(); } 586*e4b17023SJohn Marino 587*e4b17023SJohn Marino#ifdef __GXX_EXPERIMENTAL_CXX0X__ 588*e4b17023SJohn Marino /** 589*e4b17023SJohn Marino * @brief Generic inserter for rvalue stream 590*e4b17023SJohn Marino * @param __os An input stream. 591*e4b17023SJohn Marino * @param __x A reference to the object being inserted. 592*e4b17023SJohn Marino * @return os 593*e4b17023SJohn Marino * 594*e4b17023SJohn Marino * This is just a forwarding function to allow insertion to 595*e4b17023SJohn Marino * rvalue streams since they won't bind to the inserter functions 596*e4b17023SJohn Marino * that take an lvalue reference. 597*e4b17023SJohn Marino */ 598*e4b17023SJohn Marino template<typename _CharT, typename _Traits, typename _Tp> 599*e4b17023SJohn Marino inline basic_ostream<_CharT, _Traits>& 600*e4b17023SJohn Marino operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x) 601*e4b17023SJohn Marino { return (__os << __x); } 602*e4b17023SJohn Marino#endif // __GXX_EXPERIMENTAL_CXX0X__ 603*e4b17023SJohn Marino 604*e4b17023SJohn Marino_GLIBCXX_END_NAMESPACE_VERSION 605*e4b17023SJohn Marino} // namespace std 606*e4b17023SJohn Marino 607*e4b17023SJohn Marino#include <bits/ostream.tcc> 608*e4b17023SJohn Marino 609*e4b17023SJohn Marino#endif /* _GLIBCXX_OSTREAM */ 610