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