|
libstdc++
|
00001 // Output streams -*- C++ -*- 00002 00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 00004 // 2006, 2007, 2008, 2009, 2010, 2011, 2012 00005 // Free Software Foundation, Inc. 00006 // 00007 // This file is part of the GNU ISO C++ Library. This library is free 00008 // software; you can redistribute it and/or modify it under the 00009 // terms of the GNU General Public License as published by the 00010 // Free Software Foundation; either version 3, or (at your option) 00011 // any later version. 00012 00013 // This library is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 // GNU General Public License for more details. 00017 00018 // Under Section 7 of GPL version 3, you are granted additional 00019 // permissions described in the GCC Runtime Library Exception, version 00020 // 3.1, as published by the Free Software Foundation. 00021 00022 // You should have received a copy of the GNU General Public License and 00023 // a copy of the GCC Runtime Library Exception along with this program; 00024 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00025 // <http://www.gnu.org/licenses/>. 00026 00027 /** @file include/ostream 00028 * This is a Standard C++ Library header. 00029 */ 00030 00031 // 00032 // ISO C++ 14882: 27.6.2 Output streams 00033 // 00034 00035 #ifndef _GLIBCXX_OSTREAM 00036 #define _GLIBCXX_OSTREAM 1 00037 00038 #pragma GCC system_header 00039 00040 #include <ios> 00041 #include <bits/ostream_insert.h> 00042 00043 namespace std _GLIBCXX_VISIBILITY(default) 00044 { 00045 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00046 00047 /** 00048 * @brief Template class basic_ostream. 00049 * @ingroup io 00050 * 00051 * @tparam _CharT Type of character stream. 00052 * @tparam _Traits Traits for character type, defaults to 00053 * char_traits<_CharT>. 00054 * 00055 * This is the base class for all output streams. It provides text 00056 * formatting of all builtin types, and communicates with any class 00057 * derived from basic_streambuf to do the actual output. 00058 */ 00059 template<typename _CharT, typename _Traits> 00060 class basic_ostream : virtual public basic_ios<_CharT, _Traits> 00061 { 00062 public: 00063 // Types (inherited from basic_ios): 00064 typedef _CharT char_type; 00065 typedef typename _Traits::int_type int_type; 00066 typedef typename _Traits::pos_type pos_type; 00067 typedef typename _Traits::off_type off_type; 00068 typedef _Traits traits_type; 00069 00070 // Non-standard Types: 00071 typedef basic_streambuf<_CharT, _Traits> __streambuf_type; 00072 typedef basic_ios<_CharT, _Traits> __ios_type; 00073 typedef basic_ostream<_CharT, _Traits> __ostream_type; 00074 typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> > 00075 __num_put_type; 00076 typedef ctype<_CharT> __ctype_type; 00077 00078 /** 00079 * @brief Base constructor. 00080 * 00081 * This ctor is almost never called by the user directly, rather from 00082 * derived classes' initialization lists, which pass a pointer to 00083 * their own stream buffer. 00084 */ 00085 explicit 00086 basic_ostream(__streambuf_type* __sb) 00087 { this->init(__sb); } 00088 00089 /** 00090 * @brief Base destructor. 00091 * 00092 * This does very little apart from providing a virtual base dtor. 00093 */ 00094 virtual 00095 ~basic_ostream() { } 00096 00097 /// Safe prefix/suffix operations. 00098 class sentry; 00099 friend class sentry; 00100 00101 //@{ 00102 /** 00103 * @brief Interface for manipulators. 00104 * 00105 * Manipulators such as @c std::endl and @c std::hex use these 00106 * functions in constructs like "std::cout << std::endl". For more 00107 * information, see the iomanip header. 00108 */ 00109 __ostream_type& 00110 operator<<(__ostream_type& (*__pf)(__ostream_type&)) 00111 { 00112 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00113 // DR 60. What is a formatted input function? 00114 // The inserters for manipulators are *not* formatted output functions. 00115 return __pf(*this); 00116 } 00117 00118 __ostream_type& 00119 operator<<(__ios_type& (*__pf)(__ios_type&)) 00120 { 00121 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00122 // DR 60. What is a formatted input function? 00123 // The inserters for manipulators are *not* formatted output functions. 00124 __pf(*this); 00125 return *this; 00126 } 00127 00128 __ostream_type& 00129 operator<<(ios_base& (*__pf) (ios_base&)) 00130 { 00131 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00132 // DR 60. What is a formatted input function? 00133 // The inserters for manipulators are *not* formatted output functions. 00134 __pf(*this); 00135 return *this; 00136 } 00137 //@} 00138 00139 //@{ 00140 /** 00141 * @name Inserters 00142 * 00143 * All the @c operator<< functions (aka <em>formatted output 00144 * functions</em>) have some common behavior. Each starts by 00145 * constructing a temporary object of type std::basic_ostream::sentry. 00146 * This can have several effects, concluding with the setting of a 00147 * status flag; see the sentry documentation for more. 00148 * 00149 * If the sentry status is good, the function tries to generate 00150 * whatever data is appropriate for the type of the argument. 00151 * 00152 * If an exception is thrown during insertion, ios_base::badbit 00153 * will be turned on in the stream's error state without causing an 00154 * ios_base::failure to be thrown. The original exception will then 00155 * be rethrown. 00156 */ 00157 00158 //@{ 00159 /** 00160 * @brief Integer arithmetic inserters 00161 * @param __n A variable of builtin integral type. 00162 * @return @c *this if successful 00163 * 00164 * These functions use the stream's current locale (specifically, the 00165 * @c num_get facet) to perform numeric formatting. 00166 */ 00167 __ostream_type& 00168 operator<<(long __n) 00169 { return _M_insert(__n); } 00170 00171 __ostream_type& 00172 operator<<(unsigned long __n) 00173 { return _M_insert(__n); } 00174 00175 __ostream_type& 00176 operator<<(bool __n) 00177 { return _M_insert(__n); } 00178 00179 __ostream_type& 00180 operator<<(short __n); 00181 00182 __ostream_type& 00183 operator<<(unsigned short __n) 00184 { 00185 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00186 // 117. basic_ostream uses nonexistent num_put member functions. 00187 return _M_insert(static_cast<unsigned long>(__n)); 00188 } 00189 00190 __ostream_type& 00191 operator<<(int __n); 00192 00193 __ostream_type& 00194 operator<<(unsigned int __n) 00195 { 00196 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00197 // 117. basic_ostream uses nonexistent num_put member functions. 00198 return _M_insert(static_cast<unsigned long>(__n)); 00199 } 00200 00201 #ifdef _GLIBCXX_USE_LONG_LONG 00202 __ostream_type& 00203 operator<<(long long __n) 00204 { return _M_insert(__n); } 00205 00206 __ostream_type& 00207 operator<<(unsigned long long __n) 00208 { return _M_insert(__n); } 00209 #endif 00210 //@} 00211 00212 //@{ 00213 /** 00214 * @brief Floating point arithmetic inserters 00215 * @param __f A variable of builtin floating point type. 00216 * @return @c *this if successful 00217 * 00218 * These functions use the stream's current locale (specifically, the 00219 * @c num_get facet) to perform numeric formatting. 00220 */ 00221 __ostream_type& 00222 operator<<(double __f) 00223 { return _M_insert(__f); } 00224 00225 __ostream_type& 00226 operator<<(float __f) 00227 { 00228 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00229 // 117. basic_ostream uses nonexistent num_put member functions. 00230 return _M_insert(static_cast<double>(__f)); 00231 } 00232 00233 __ostream_type& 00234 operator<<(long double __f) 00235 { return _M_insert(__f); } 00236 //@} 00237 00238 /** 00239 * @brief Pointer arithmetic inserters 00240 * @param __p A variable of pointer type. 00241 * @return @c *this if successful 00242 * 00243 * These functions use the stream's current locale (specifically, the 00244 * @c num_get facet) to perform numeric formatting. 00245 */ 00246 __ostream_type& 00247 operator<<(const void* __p) 00248 { return _M_insert(__p); } 00249 00250 /** 00251 * @brief Extracting from another streambuf. 00252 * @param __sb A pointer to a streambuf 00253 * 00254 * This function behaves like one of the basic arithmetic extractors, 00255 * in that it also constructs a sentry object and has the same error 00256 * handling behavior. 00257 * 00258 * If @p __sb is NULL, the stream will set failbit in its error state. 00259 * 00260 * Characters are extracted from @p __sb and inserted into @c *this 00261 * until one of the following occurs: 00262 * 00263 * - the input stream reaches end-of-file, 00264 * - insertion into the output sequence fails (in this case, the 00265 * character that would have been inserted is not extracted), or 00266 * - an exception occurs while getting a character from @p __sb, which 00267 * sets failbit in the error state 00268 * 00269 * If the function inserts no characters, failbit is set. 00270 */ 00271 __ostream_type& 00272 operator<<(__streambuf_type* __sb); 00273 //@} 00274 00275 //@{ 00276 /** 00277 * @name Unformatted Output Functions 00278 * 00279 * All the unformatted output functions have some common behavior. 00280 * Each starts by constructing a temporary object of type 00281 * std::basic_ostream::sentry. This has several effects, concluding 00282 * with the setting of a status flag; see the sentry documentation 00283 * for more. 00284 * 00285 * If the sentry status is good, the function tries to generate 00286 * whatever data is appropriate for the type of the argument. 00287 * 00288 * If an exception is thrown during insertion, ios_base::badbit 00289 * will be turned on in the stream's error state. If badbit is on in 00290 * the stream's exceptions mask, the exception will be rethrown 00291 * without completing its actions. 00292 */ 00293 00294 /** 00295 * @brief Simple insertion. 00296 * @param __c The character to insert. 00297 * @return *this 00298 * 00299 * Tries to insert @p __c. 00300 * 00301 * @note This function is not overloaded on signed char and 00302 * unsigned char. 00303 */ 00304 __ostream_type& 00305 put(char_type __c); 00306 00307 /** 00308 * @brief Core write functionality, without sentry. 00309 * @param __s The array to insert. 00310 * @param __n Maximum number of characters to insert. 00311 */ 00312 void 00313 _M_write(const char_type* __s, streamsize __n) 00314 { 00315 const streamsize __put = this->rdbuf()->sputn(__s, __n); 00316 if (__put != __n) 00317 this->setstate(ios_base::badbit); 00318 } 00319 00320 /** 00321 * @brief Character string insertion. 00322 * @param __s The array to insert. 00323 * @param __n Maximum number of characters to insert. 00324 * @return *this 00325 * 00326 * Characters are copied from @p __s and inserted into the stream until 00327 * one of the following happens: 00328 * 00329 * - @p __n characters are inserted 00330 * - inserting into the output sequence fails (in this case, badbit 00331 * will be set in the stream's error state) 00332 * 00333 * @note This function is not overloaded on signed char and 00334 * unsigned char. 00335 */ 00336 __ostream_type& 00337 write(const char_type* __s, streamsize __n); 00338 //@} 00339 00340 /** 00341 * @brief Synchronizing the stream buffer. 00342 * @return *this 00343 * 00344 * If @c rdbuf() is a null pointer, changes nothing. 00345 * 00346 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1, 00347 * sets badbit. 00348 */ 00349 __ostream_type& 00350 flush(); 00351 00352 /** 00353 * @brief Getting the current write position. 00354 * @return A file position object. 00355 * 00356 * If @c fail() is not false, returns @c pos_type(-1) to indicate 00357 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out). 00358 */ 00359 pos_type 00360 tellp(); 00361 00362 /** 00363 * @brief Changing the current write position. 00364 * @param __pos A file position object. 00365 * @return *this 00366 * 00367 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If 00368 * that function fails, sets failbit. 00369 */ 00370 __ostream_type& 00371 seekp(pos_type); 00372 00373 /** 00374 * @brief Changing the current write position. 00375 * @param __off A file offset object. 00376 * @param __dir The direction in which to seek. 00377 * @return *this 00378 * 00379 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir). 00380 * If that function fails, sets failbit. 00381 */ 00382 __ostream_type& 00383 seekp(off_type, ios_base::seekdir); 00384 00385 protected: 00386 basic_ostream() 00387 { this->init(0); } 00388 00389 template<typename _ValueT> 00390 __ostream_type& 00391 _M_insert(_ValueT __v); 00392 }; 00393 00394 /** 00395 * @brief Performs setup work for output streams. 00396 * 00397 * Objects of this class are created before all of the standard 00398 * inserters are run. It is responsible for <em>exception-safe prefix and 00399 * suffix operations</em>. 00400 */ 00401 template <typename _CharT, typename _Traits> 00402 class basic_ostream<_CharT, _Traits>::sentry 00403 { 00404 // Data Members. 00405 bool _M_ok; 00406 basic_ostream<_CharT, _Traits>& _M_os; 00407 00408 public: 00409 /** 00410 * @brief The constructor performs preparatory work. 00411 * @param __os The output stream to guard. 00412 * 00413 * If the stream state is good (@a __os.good() is true), then if the 00414 * stream is tied to another output stream, @c is.tie()->flush() 00415 * is called to synchronize the output sequences. 00416 * 00417 * If the stream state is still good, then the sentry state becomes 00418 * true (@a okay). 00419 */ 00420 explicit 00421 sentry(basic_ostream<_CharT, _Traits>& __os); 00422 00423 /** 00424 * @brief Possibly flushes the stream. 00425 * 00426 * If @c ios_base::unitbuf is set in @c os.flags(), and 00427 * @c std::uncaught_exception() is true, the sentry destructor calls 00428 * @c flush() on the output stream. 00429 */ 00430 ~sentry() 00431 { 00432 // XXX MT 00433 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception()) 00434 { 00435 // Can't call flush directly or else will get into recursive lock. 00436 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1) 00437 _M_os.setstate(ios_base::badbit); 00438 } 00439 } 00440 00441 /** 00442 * @brief Quick status checking. 00443 * @return The sentry state. 00444 * 00445 * For ease of use, sentries may be converted to booleans. The 00446 * return value is that of the sentry state (true == okay). 00447 */ 00448 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00449 explicit 00450 #endif 00451 operator bool() const 00452 { return _M_ok; } 00453 }; 00454 00455 //@{ 00456 /** 00457 * @brief Character inserters 00458 * @param __out An output stream. 00459 * @param __c A character. 00460 * @return out 00461 * 00462 * Behaves like one of the formatted arithmetic inserters described in 00463 * std::basic_ostream. After constructing a sentry object with good 00464 * status, this function inserts a single character and any required 00465 * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then 00466 * called. 00467 * 00468 * If @p __c is of type @c char and the character type of the stream is not 00469 * @c char, the character is widened before insertion. 00470 */ 00471 template<typename _CharT, typename _Traits> 00472 inline basic_ostream<_CharT, _Traits>& 00473 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c) 00474 { return __ostream_insert(__out, &__c, 1); } 00475 00476 template<typename _CharT, typename _Traits> 00477 inline basic_ostream<_CharT, _Traits>& 00478 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c) 00479 { return (__out << __out.widen(__c)); } 00480 00481 // Specialization 00482 template <class _Traits> 00483 inline basic_ostream<char, _Traits>& 00484 operator<<(basic_ostream<char, _Traits>& __out, char __c) 00485 { return __ostream_insert(__out, &__c, 1); } 00486 00487 // Signed and unsigned 00488 template<class _Traits> 00489 inline basic_ostream<char, _Traits>& 00490 operator<<(basic_ostream<char, _Traits>& __out, signed char __c) 00491 { return (__out << static_cast<char>(__c)); } 00492 00493 template<class _Traits> 00494 inline basic_ostream<char, _Traits>& 00495 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c) 00496 { return (__out << static_cast<char>(__c)); } 00497 //@} 00498 00499 //@{ 00500 /** 00501 * @brief String inserters 00502 * @param __out An output stream. 00503 * @param __s A character string. 00504 * @return out 00505 * @pre @p __s must be a non-NULL pointer 00506 * 00507 * Behaves like one of the formatted arithmetic inserters described in 00508 * std::basic_ostream. After constructing a sentry object with good 00509 * status, this function inserts @c traits::length(__s) characters starting 00510 * at @p __s, widened if necessary, followed by any required padding (as 00511 * determined by [22.2.2.2.2]). @c __out.width(0) is then called. 00512 */ 00513 template<typename _CharT, typename _Traits> 00514 inline basic_ostream<_CharT, _Traits>& 00515 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s) 00516 { 00517 if (!__s) 00518 __out.setstate(ios_base::badbit); 00519 else 00520 __ostream_insert(__out, __s, 00521 static_cast<streamsize>(_Traits::length(__s))); 00522 return __out; 00523 } 00524 00525 template<typename _CharT, typename _Traits> 00526 basic_ostream<_CharT, _Traits> & 00527 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s); 00528 00529 // Partial specializations 00530 template<class _Traits> 00531 inline basic_ostream<char, _Traits>& 00532 operator<<(basic_ostream<char, _Traits>& __out, const char* __s) 00533 { 00534 if (!__s) 00535 __out.setstate(ios_base::badbit); 00536 else 00537 __ostream_insert(__out, __s, 00538 static_cast<streamsize>(_Traits::length(__s))); 00539 return __out; 00540 } 00541 00542 // Signed and unsigned 00543 template<class _Traits> 00544 inline basic_ostream<char, _Traits>& 00545 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s) 00546 { return (__out << reinterpret_cast<const char*>(__s)); } 00547 00548 template<class _Traits> 00549 inline basic_ostream<char, _Traits> & 00550 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s) 00551 { return (__out << reinterpret_cast<const char*>(__s)); } 00552 //@} 00553 00554 // Standard basic_ostream manipulators 00555 00556 /** 00557 * @brief Write a newline and flush the stream. 00558 * 00559 * This manipulator is often mistakenly used when a simple newline is 00560 * desired, leading to poor buffering performance. See 00561 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html 00562 * for more on this subject. 00563 */ 00564 template<typename _CharT, typename _Traits> 00565 inline basic_ostream<_CharT, _Traits>& 00566 endl(basic_ostream<_CharT, _Traits>& __os) 00567 { return flush(__os.put(__os.widen('\n'))); } 00568 00569 /** 00570 * @brief Write a null character into the output sequence. 00571 * 00572 * <em>Null character</em> is @c CharT() by definition. For CharT 00573 * of @c char, this correctly writes the ASCII @c NUL character 00574 * string terminator. 00575 */ 00576 template<typename _CharT, typename _Traits> 00577 inline basic_ostream<_CharT, _Traits>& 00578 ends(basic_ostream<_CharT, _Traits>& __os) 00579 { return __os.put(_CharT()); } 00580 00581 /** 00582 * @brief Flushes the output stream. 00583 * 00584 * This manipulator simply calls the stream's @c flush() member function. 00585 */ 00586 template<typename _CharT, typename _Traits> 00587 inline basic_ostream<_CharT, _Traits>& 00588 flush(basic_ostream<_CharT, _Traits>& __os) 00589 { return __os.flush(); } 00590 00591 #ifdef __GXX_EXPERIMENTAL_CXX0X__ 00592 /** 00593 * @brief Generic inserter for rvalue stream 00594 * @param __os An input stream. 00595 * @param __x A reference to the object being inserted. 00596 * @return os 00597 * 00598 * This is just a forwarding function to allow insertion to 00599 * rvalue streams since they won't bind to the inserter functions 00600 * that take an lvalue reference. 00601 */ 00602 template<typename _CharT, typename _Traits, typename _Tp> 00603 inline basic_ostream<_CharT, _Traits>& 00604 operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x) 00605 { return (__os << __x); } 00606 #endif // __GXX_EXPERIMENTAL_CXX0X__ 00607 00608 _GLIBCXX_END_NAMESPACE_VERSION 00609 } // namespace std 00610 00611 #include <bits/ostream.tcc> 00612 00613 #endif /* _GLIBCXX_OSTREAM */