1 // Output streams -*- C++ -*- 2 3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 4 // Free Software Foundation, Inc. 5 // 6 // This file is part of the GNU ISO C++ Library. This library is free 7 // software; you can redistribute it and/or modify it under the 8 // terms of the GNU General Public License as published by the 9 // Free Software Foundation; either version 2, or (at your option) 10 // any later version. 11 12 // This library is distributed in the hope that it will be useful, 13 // but WITHOUT ANY WARRANTY; without even the implied warranty of 14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 // GNU General Public License for more details. 16 17 // You should have received a copy of the GNU General Public License along 18 // with this library; see the file COPYING. If not, write to the Free 19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 20 // USA. 21 22 // As a special exception, you may use this file as part of a free software 23 // library without restriction. Specifically, if other files instantiate 24 // templates or use macros or inline functions from this file, or you compile 25 // this file and link it with other files to produce an executable, this 26 // file does not by itself cause the resulting executable to be covered by 27 // the GNU General Public License. This exception does not however 28 // invalidate any other reasons why the executable file might be covered by 29 // the GNU General Public License. 30 31 // 32 // ISO C++ 14882: 27.6.2 Output streams 33 // 34 35 /** @file ostream 36 * This is a Standard C++ Library header. You should @c #include this header 37 * in your programs, rather than any of the "st[dl]_*.h" implementation files. 38 */ 39 40 #ifndef _CPP_OSTREAM 41 #define _CPP_OSTREAM 1 42 43 #pragma GCC system_header 44 45 #include <ios> 46 47 namespace std 48 { 49 // 27.6.2.1 Template class basic_ostream 50 template<typename _CharT, typename _Traits> 51 class basic_ostream : virtual public basic_ios<_CharT, _Traits> 52 { 53 public: 54 // Types (inherited from basic_ios (27.4.4)): 55 typedef _CharT char_type; 56 typedef typename _Traits::int_type int_type; 57 typedef typename _Traits::pos_type pos_type; 58 typedef typename _Traits::off_type off_type; 59 typedef _Traits traits_type; 60 61 // Non-standard Types: 62 typedef basic_streambuf<_CharT, _Traits> __streambuf_type; 63 typedef basic_ios<_CharT, _Traits> __ios_type; 64 typedef basic_ostream<_CharT, _Traits> __ostream_type; 65 typedef ostreambuf_iterator<_CharT, _Traits> __ostreambuf_iter; 66 typedef num_put<_CharT, __ostreambuf_iter> __numput_type; 67 typedef ctype<_CharT> __ctype_type; 68 69 // 27.6.2.2 Constructor/destructor: 70 explicit 71 basic_ostream(__streambuf_type* __sb) 72 { this->init(__sb); } 73 74 virtual 75 ~basic_ostream() { } 76 77 // 27.6.2.3 Prefix/suffix: 78 class sentry; 79 friend class sentry; 80 81 // 27.6.2.5 Formatted output: 82 // 27.6.2.5.3 basic_ostream::operator<< 83 __ostream_type& 84 operator<<(__ostream_type& (*__pf)(__ostream_type&)); 85 86 __ostream_type& 87 operator<<(__ios_type& (*__pf)(__ios_type&)); 88 89 __ostream_type& 90 operator<<(ios_base& (*__pf) (ios_base&)); 91 92 // 27.6.2.5.2 Arithmetic Inserters 93 __ostream_type& 94 operator<<(long __n); 95 96 __ostream_type& 97 operator<<(unsigned long __n); 98 99 __ostream_type& 100 operator<<(bool __n); 101 102 __ostream_type& 103 operator<<(short __n) 104 { 105 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield; 106 if (__fmt & ios_base::oct || __fmt & ios_base::hex) 107 return this->operator<<(static_cast<unsigned long> 108 (static_cast<unsigned short>(__n))); 109 else 110 return this->operator<<(static_cast<long>(__n)); 111 } 112 113 __ostream_type& 114 operator<<(unsigned short __n) 115 { return this->operator<<(static_cast<unsigned long>(__n)); } 116 117 __ostream_type& 118 operator<<(int __n) 119 { 120 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield; 121 if (__fmt & ios_base::oct || __fmt & ios_base::hex) 122 return this->operator<<(static_cast<unsigned long> 123 (static_cast<unsigned int>(__n))); 124 else 125 return this->operator<<(static_cast<long>(__n)); 126 } 127 128 __ostream_type& 129 operator<<(unsigned int __n) 130 { return this->operator<<(static_cast<unsigned long>(__n)); } 131 132 #ifdef _GLIBCPP_USE_LONG_LONG 133 __ostream_type& 134 operator<<(long long __n); 135 136 __ostream_type& 137 operator<<(unsigned long long __n); 138 #endif 139 140 __ostream_type& 141 operator<<(double __f); 142 143 __ostream_type& 144 operator<<(float __f) 145 { return this->operator<<(static_cast<double>(__f)); } 146 147 __ostream_type& 148 operator<<(long double __f); 149 150 __ostream_type& 151 operator<<(const void* __p); 152 153 __ostream_type& 154 operator<<(__streambuf_type* __sb); 155 156 // Unformatted output: 157 __ostream_type& 158 put(char_type __c); 159 160 __ostream_type& 161 write(const char_type* __s, streamsize __n); 162 163 __ostream_type& 164 flush(); 165 166 // Seeks: 167 pos_type 168 tellp(); 169 170 __ostream_type& 171 seekp(pos_type); 172 173 __ostream_type& 174 seekp(off_type, ios_base::seekdir); 175 }; 176 177 // 27.6.2.3 Class basic_ostream::sentry 178 template <typename _CharT, typename _Traits> 179 class basic_ostream<_CharT, _Traits>::sentry 180 { 181 // Data Members: 182 bool _M_ok; 183 basic_ostream<_CharT,_Traits>& _M_os; 184 185 public: 186 explicit 187 sentry(basic_ostream<_CharT,_Traits>& __os); 188 189 ~sentry() 190 { 191 // XXX MT 192 if (_M_os.flags() & ios_base::unitbuf && !uncaught_exception()) 193 { 194 // Can't call flush directly or else will get into recursive lock. 195 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1) 196 _M_os.setstate(ios_base::badbit); 197 } 198 } 199 200 operator bool() 201 { return _M_ok; } 202 }; 203 204 template<typename _CharT, typename _Traits> 205 basic_ostream<_CharT, _Traits>& 206 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c); 207 208 template<typename _CharT, typename _Traits> 209 basic_ostream<_CharT, _Traits>& 210 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c) 211 { return (__out << __out.widen(__c)); } 212 213 // Specialization 214 template <class _Traits> 215 basic_ostream<char, _Traits>& 216 operator<<(basic_ostream<char, _Traits>& __out, char __c); 217 218 // Signed and unsigned 219 template<class _Traits> 220 basic_ostream<char, _Traits>& 221 operator<<(basic_ostream<char, _Traits>& __out, signed char __c) 222 { return (__out << static_cast<char>(__c)); } 223 224 template<class _Traits> 225 basic_ostream<char, _Traits>& 226 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c) 227 { return (__out << static_cast<char>(__c)); } 228 229 template<typename _CharT, typename _Traits> 230 basic_ostream<_CharT, _Traits>& 231 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s); 232 233 template<typename _CharT, typename _Traits> 234 basic_ostream<_CharT, _Traits> & 235 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s); 236 237 // Partial specializationss 238 template<class _Traits> 239 basic_ostream<char, _Traits>& 240 operator<<(basic_ostream<char, _Traits>& __out, const char* __s); 241 242 // Signed and unsigned 243 template<class _Traits> 244 basic_ostream<char, _Traits>& 245 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s) 246 { return (__out << reinterpret_cast<const char*>(__s)); } 247 248 template<class _Traits> 249 basic_ostream<char, _Traits> & 250 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s) 251 { return (__out << reinterpret_cast<const char*>(__s)); } 252 253 // 27.6.2.7 Standard basic_ostream manipulators 254 template<typename _CharT, typename _Traits> 255 basic_ostream<_CharT, _Traits>& 256 endl(basic_ostream<_CharT, _Traits>& __os) 257 { return flush(__os.put(__os.widen('\n'))); } 258 259 template<typename _CharT, typename _Traits> 260 basic_ostream<_CharT, _Traits>& 261 ends(basic_ostream<_CharT, _Traits>& __os) 262 { return __os.put(_CharT()); } 263 264 template<typename _CharT, typename _Traits> 265 basic_ostream<_CharT, _Traits>& 266 flush(basic_ostream<_CharT, _Traits>& __os) 267 { return __os.flush(); } 268 269 } // namespace std 270 271 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT 272 # define export 273 #endif 274 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS 275 # include <bits/ostream.tcc> 276 #endif 277 278 #endif /* _CPP_OSTREAM */ 279