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