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