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