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