]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/std/ostream
* many: Replace uses of __GXX_EXPERIMENTAL_CXX0X__ with __cplusplus.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / ostream
1 // Output streams -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010, 2011, 2012
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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
21
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
26
27 /** @file include/ostream
28 * This is a Standard C++ Library header.
29 */
30
31 //
32 // ISO C++ 14882: 27.6.2 Output streams
33 //
34
35 #ifndef _GLIBCXX_OSTREAM
36 #define _GLIBCXX_OSTREAM 1
37
38 #pragma GCC system_header
39
40 #include <ios>
41 #include <bits/ostream_insert.h>
42
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46
47 /**
48 * @brief Template class basic_ostream.
49 * @ingroup io
50 *
51 * @tparam _CharT Type of character stream.
52 * @tparam _Traits Traits for character type, defaults to
53 * char_traits<_CharT>.
54 *
55 * This is the base class for all output streams. It provides text
56 * formatting of all builtin types, and communicates with any class
57 * derived from basic_streambuf to do the actual output.
58 */
59 template<typename _CharT, typename _Traits>
60 class basic_ostream : virtual public basic_ios<_CharT, _Traits>
61 {
62 public:
63 // Types (inherited from basic_ios):
64 typedef _CharT char_type;
65 typedef typename _Traits::int_type int_type;
66 typedef typename _Traits::pos_type pos_type;
67 typedef typename _Traits::off_type off_type;
68 typedef _Traits traits_type;
69
70 // Non-standard Types:
71 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
72 typedef basic_ios<_CharT, _Traits> __ios_type;
73 typedef basic_ostream<_CharT, _Traits> __ostream_type;
74 typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
75 __num_put_type;
76 typedef ctype<_CharT> __ctype_type;
77
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 /// Safe prefix/suffix operations.
98 class sentry;
99 friend class sentry;
100
101 //@{
102 /**
103 * @brief Interface for manipulators.
104 *
105 * Manipulators such as @c std::endl and @c std::hex use these
106 * functions in constructs like "std::cout << std::endl". For more
107 * information, see the iomanip header.
108 */
109 __ostream_type&
110 operator<<(__ostream_type& (*__pf)(__ostream_type&))
111 {
112 // _GLIBCXX_RESOLVE_LIB_DEFECTS
113 // DR 60. What is a formatted input function?
114 // The inserters for manipulators are *not* formatted output functions.
115 return __pf(*this);
116 }
117
118 __ostream_type&
119 operator<<(__ios_type& (*__pf)(__ios_type&))
120 {
121 // _GLIBCXX_RESOLVE_LIB_DEFECTS
122 // DR 60. What is a formatted input function?
123 // The inserters for manipulators are *not* formatted output functions.
124 __pf(*this);
125 return *this;
126 }
127
128 __ostream_type&
129 operator<<(ios_base& (*__pf) (ios_base&))
130 {
131 // _GLIBCXX_RESOLVE_LIB_DEFECTS
132 // DR 60. What is a formatted input function?
133 // The inserters for manipulators are *not* formatted output functions.
134 __pf(*this);
135 return *this;
136 }
137 //@}
138
139 //@{
140 /**
141 * @name 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 /**
160 * @brief Integer arithmetic inserters
161 * @param __n A variable of builtin integral type.
162 * @return @c *this if successful
163 *
164 * These functions use the stream's current locale (specifically, the
165 * @c num_get facet) to perform numeric formatting.
166 */
167 __ostream_type&
168 operator<<(long __n)
169 { return _M_insert(__n); }
170
171 __ostream_type&
172 operator<<(unsigned long __n)
173 { return _M_insert(__n); }
174
175 __ostream_type&
176 operator<<(bool __n)
177 { return _M_insert(__n); }
178
179 __ostream_type&
180 operator<<(short __n);
181
182 __ostream_type&
183 operator<<(unsigned short __n)
184 {
185 // _GLIBCXX_RESOLVE_LIB_DEFECTS
186 // 117. basic_ostream uses nonexistent num_put member functions.
187 return _M_insert(static_cast<unsigned long>(__n));
188 }
189
190 __ostream_type&
191 operator<<(int __n);
192
193 __ostream_type&
194 operator<<(unsigned int __n)
195 {
196 // _GLIBCXX_RESOLVE_LIB_DEFECTS
197 // 117. basic_ostream uses nonexistent num_put member functions.
198 return _M_insert(static_cast<unsigned long>(__n));
199 }
200
201 #ifdef _GLIBCXX_USE_LONG_LONG
202 __ostream_type&
203 operator<<(long long __n)
204 { return _M_insert(__n); }
205
206 __ostream_type&
207 operator<<(unsigned long long __n)
208 { return _M_insert(__n); }
209 #endif
210 //@}
211
212 //@{
213 /**
214 * @brief Floating point arithmetic inserters
215 * @param __f A variable of builtin floating point type.
216 * @return @c *this if successful
217 *
218 * These functions use the stream's current locale (specifically, the
219 * @c num_get facet) to perform numeric formatting.
220 */
221 __ostream_type&
222 operator<<(double __f)
223 { return _M_insert(__f); }
224
225 __ostream_type&
226 operator<<(float __f)
227 {
228 // _GLIBCXX_RESOLVE_LIB_DEFECTS
229 // 117. basic_ostream uses nonexistent num_put member functions.
230 return _M_insert(static_cast<double>(__f));
231 }
232
233 __ostream_type&
234 operator<<(long double __f)
235 { return _M_insert(__f); }
236 //@}
237
238 /**
239 * @brief Pointer arithmetic inserters
240 * @param __p A variable of pointer type.
241 * @return @c *this if successful
242 *
243 * These functions use the stream's current locale (specifically, the
244 * @c num_get facet) to perform numeric formatting.
245 */
246 __ostream_type&
247 operator<<(const void* __p)
248 { return _M_insert(__p); }
249
250 /**
251 * @brief Extracting from another streambuf.
252 * @param __sb A pointer to a streambuf
253 *
254 * This function behaves like one of the basic arithmetic extractors,
255 * in that it also constructs a sentry object and has the same error
256 * handling behavior.
257 *
258 * If @p __sb is NULL, the stream will set failbit in its error state.
259 *
260 * Characters are extracted from @p __sb and inserted into @c *this
261 * until one of the following occurs:
262 *
263 * - the input stream reaches end-of-file,
264 * - insertion into the output sequence fails (in this case, the
265 * character that would have been inserted is not extracted), or
266 * - an exception occurs while getting a character from @p __sb, which
267 * sets failbit in the error state
268 *
269 * If the function inserts no characters, failbit is set.
270 */
271 __ostream_type&
272 operator<<(__streambuf_type* __sb);
273 //@}
274
275 //@{
276 /**
277 * @name Unformatted Output Functions
278 *
279 * All the unformatted output functions have some common behavior.
280 * Each starts by constructing a temporary object of type
281 * std::basic_ostream::sentry. This has several effects, concluding
282 * with the setting of a status flag; see the sentry documentation
283 * for more.
284 *
285 * If the sentry status is good, the function tries to generate
286 * whatever data is appropriate for the type of the argument.
287 *
288 * If an exception is thrown during insertion, ios_base::badbit
289 * will be turned on in the stream's error state. If badbit is on in
290 * the stream's exceptions mask, the exception will be rethrown
291 * without completing its actions.
292 */
293
294 /**
295 * @brief Simple insertion.
296 * @param __c The character to insert.
297 * @return *this
298 *
299 * Tries to insert @p __c.
300 *
301 * @note This function is not overloaded on signed char and
302 * unsigned char.
303 */
304 __ostream_type&
305 put(char_type __c);
306
307 /**
308 * @brief Core write functionality, without sentry.
309 * @param __s The array to insert.
310 * @param __n Maximum number of characters to insert.
311 */
312 void
313 _M_write(const char_type* __s, streamsize __n)
314 {
315 const streamsize __put = this->rdbuf()->sputn(__s, __n);
316 if (__put != __n)
317 this->setstate(ios_base::badbit);
318 }
319
320 /**
321 * @brief Character string insertion.
322 * @param __s The array to insert.
323 * @param __n Maximum number of characters to insert.
324 * @return *this
325 *
326 * Characters are copied from @p __s and inserted into the stream until
327 * one of the following happens:
328 *
329 * - @p __n characters are inserted
330 * - inserting into the output sequence fails (in this case, badbit
331 * will be set in the stream's error state)
332 *
333 * @note This function is not overloaded on signed char and
334 * unsigned char.
335 */
336 __ostream_type&
337 write(const char_type* __s, streamsize __n);
338 //@}
339
340 /**
341 * @brief Synchronizing the stream buffer.
342 * @return *this
343 *
344 * If @c rdbuf() is a null pointer, changes nothing.
345 *
346 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
347 * sets badbit.
348 */
349 __ostream_type&
350 flush();
351
352 /**
353 * @brief Getting the current write position.
354 * @return A file position object.
355 *
356 * If @c fail() is not false, returns @c pos_type(-1) to indicate
357 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
358 */
359 pos_type
360 tellp();
361
362 /**
363 * @brief Changing the current write position.
364 * @param __pos A file position object.
365 * @return *this
366 *
367 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
368 * that function fails, sets failbit.
369 */
370 __ostream_type&
371 seekp(pos_type);
372
373 /**
374 * @brief Changing the current write position.
375 * @param __off A file offset object.
376 * @param __dir The direction in which to seek.
377 * @return *this
378 *
379 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
380 * If that function fails, sets failbit.
381 */
382 __ostream_type&
383 seekp(off_type, ios_base::seekdir);
384
385 protected:
386 basic_ostream()
387 { this->init(0); }
388
389 template<typename _ValueT>
390 __ostream_type&
391 _M_insert(_ValueT __v);
392 };
393
394 /**
395 * @brief Performs setup work for output streams.
396 *
397 * Objects of this class are created before all of the standard
398 * inserters are run. It is responsible for <em>exception-safe prefix and
399 * suffix operations</em>.
400 */
401 template <typename _CharT, typename _Traits>
402 class basic_ostream<_CharT, _Traits>::sentry
403 {
404 // Data Members.
405 bool _M_ok;
406 basic_ostream<_CharT, _Traits>& _M_os;
407
408 public:
409 /**
410 * @brief The constructor performs preparatory work.
411 * @param __os The output stream to guard.
412 *
413 * If the stream state is good (@a __os.good() is true), then if the
414 * stream is tied to another output stream, @c is.tie()->flush()
415 * is called to synchronize the output sequences.
416 *
417 * If the stream state is still good, then the sentry state becomes
418 * true (@a okay).
419 */
420 explicit
421 sentry(basic_ostream<_CharT, _Traits>& __os);
422
423 /**
424 * @brief Possibly flushes the stream.
425 *
426 * If @c ios_base::unitbuf is set in @c os.flags(), and
427 * @c std::uncaught_exception() is true, the sentry destructor calls
428 * @c flush() on the output stream.
429 */
430 ~sentry()
431 {
432 // XXX MT
433 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
434 {
435 // Can't call flush directly or else will get into recursive lock.
436 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
437 _M_os.setstate(ios_base::badbit);
438 }
439 }
440
441 /**
442 * @brief Quick status checking.
443 * @return The sentry state.
444 *
445 * For ease of use, sentries may be converted to booleans. The
446 * return value is that of the sentry state (true == okay).
447 */
448 #if __cplusplus >= 201103L
449 explicit
450 #endif
451 operator bool() const
452 { return _M_ok; }
453 };
454
455 //@{
456 /**
457 * @brief Character inserters
458 * @param __out An output stream.
459 * @param __c A character.
460 * @return out
461 *
462 * Behaves like one of the formatted arithmetic inserters described in
463 * std::basic_ostream. After constructing a sentry object with good
464 * status, this function inserts a single character and any required
465 * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then
466 * called.
467 *
468 * If @p __c is of type @c char and the character type of the stream is not
469 * @c char, the character is widened before insertion.
470 */
471 template<typename _CharT, typename _Traits>
472 inline basic_ostream<_CharT, _Traits>&
473 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
474 { return __ostream_insert(__out, &__c, 1); }
475
476 template<typename _CharT, typename _Traits>
477 inline basic_ostream<_CharT, _Traits>&
478 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
479 { return (__out << __out.widen(__c)); }
480
481 // Specialization
482 template <class _Traits>
483 inline basic_ostream<char, _Traits>&
484 operator<<(basic_ostream<char, _Traits>& __out, char __c)
485 { return __ostream_insert(__out, &__c, 1); }
486
487 // Signed and unsigned
488 template<class _Traits>
489 inline basic_ostream<char, _Traits>&
490 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
491 { return (__out << static_cast<char>(__c)); }
492
493 template<class _Traits>
494 inline basic_ostream<char, _Traits>&
495 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
496 { return (__out << static_cast<char>(__c)); }
497 //@}
498
499 //@{
500 /**
501 * @brief String inserters
502 * @param __out An output stream.
503 * @param __s A character string.
504 * @return out
505 * @pre @p __s must be a non-NULL pointer
506 *
507 * Behaves like one of the formatted arithmetic inserters described in
508 * std::basic_ostream. After constructing a sentry object with good
509 * status, this function inserts @c traits::length(__s) characters starting
510 * at @p __s, widened if necessary, followed by any required padding (as
511 * determined by [22.2.2.2.2]). @c __out.width(0) is then called.
512 */
513 template<typename _CharT, typename _Traits>
514 inline basic_ostream<_CharT, _Traits>&
515 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
516 {
517 if (!__s)
518 __out.setstate(ios_base::badbit);
519 else
520 __ostream_insert(__out, __s,
521 static_cast<streamsize>(_Traits::length(__s)));
522 return __out;
523 }
524
525 template<typename _CharT, typename _Traits>
526 basic_ostream<_CharT, _Traits> &
527 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
528
529 // Partial specializations
530 template<class _Traits>
531 inline basic_ostream<char, _Traits>&
532 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
533 {
534 if (!__s)
535 __out.setstate(ios_base::badbit);
536 else
537 __ostream_insert(__out, __s,
538 static_cast<streamsize>(_Traits::length(__s)));
539 return __out;
540 }
541
542 // Signed and unsigned
543 template<class _Traits>
544 inline basic_ostream<char, _Traits>&
545 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
546 { return (__out << reinterpret_cast<const char*>(__s)); }
547
548 template<class _Traits>
549 inline basic_ostream<char, _Traits> &
550 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
551 { return (__out << reinterpret_cast<const char*>(__s)); }
552 //@}
553
554 // Standard basic_ostream manipulators
555
556 /**
557 * @brief Write a newline and flush the stream.
558 *
559 * This manipulator is often mistakenly used when a simple newline is
560 * desired, leading to poor buffering performance. See
561 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
562 * for more on this subject.
563 */
564 template<typename _CharT, typename _Traits>
565 inline basic_ostream<_CharT, _Traits>&
566 endl(basic_ostream<_CharT, _Traits>& __os)
567 { return flush(__os.put(__os.widen('\n'))); }
568
569 /**
570 * @brief Write a null character into the output sequence.
571 *
572 * <em>Null character</em> is @c CharT() by definition. For CharT
573 * of @c char, this correctly writes the ASCII @c NUL character
574 * string terminator.
575 */
576 template<typename _CharT, typename _Traits>
577 inline basic_ostream<_CharT, _Traits>&
578 ends(basic_ostream<_CharT, _Traits>& __os)
579 { return __os.put(_CharT()); }
580
581 /**
582 * @brief Flushes the output stream.
583 *
584 * This manipulator simply calls the stream's @c flush() member function.
585 */
586 template<typename _CharT, typename _Traits>
587 inline basic_ostream<_CharT, _Traits>&
588 flush(basic_ostream<_CharT, _Traits>& __os)
589 { return __os.flush(); }
590
591 #if __cplusplus >= 201103L
592 /**
593 * @brief Generic inserter for rvalue stream
594 * @param __os An input stream.
595 * @param __x A reference to the object being inserted.
596 * @return os
597 *
598 * This is just a forwarding function to allow insertion to
599 * rvalue streams since they won't bind to the inserter functions
600 * that take an lvalue reference.
601 */
602 template<typename _CharT, typename _Traits, typename _Tp>
603 inline basic_ostream<_CharT, _Traits>&
604 operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
605 { return (__os << __x); }
606 #endif // C++11
607
608 _GLIBCXX_END_NAMESPACE_VERSION
609 } // namespace std
610
611 #include <bits/ostream.tcc>
612
613 #endif /* _GLIBCXX_OSTREAM */