libstdc++
iomanip
Go to the documentation of this file.
00001 // Standard stream manipulators -*- C++ -*-
00002 
00003 // Copyright (C) 1997-2015 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 3, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file include/iomanip
00026  *  This is a Standard C++ Library header.
00027  */
00028 
00029 //
00030 // ISO C++ 14882: 27.6.3  Standard manipulators
00031 //
00032 
00033 #ifndef _GLIBCXX_IOMANIP
00034 #define _GLIBCXX_IOMANIP 1
00035 
00036 #pragma GCC system_header
00037 
00038 #include <bits/c++config.h>
00039 #include <iosfwd>
00040 #include <bits/ios_base.h>
00041 
00042 #if __cplusplus >= 201103L
00043 #include <locale>
00044 #if __cplusplus > 201103L
00045 #include <bits/quoted_string.h>
00046 #endif
00047 #endif
00048 
00049 namespace std _GLIBCXX_VISIBILITY(default)
00050 {
00051 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00052 
00053   // [27.6.3] standard manipulators
00054   // Also see DR 183.
00055 
00056   struct _Resetiosflags { ios_base::fmtflags _M_mask; };
00057 
00058   /**
00059    *  @brief  Manipulator for @c setf.
00060    *  @param  __mask  A format flags mask.
00061    *
00062    *  Sent to a stream object, this manipulator resets the specified flags,
00063    *  via @e stream.setf(0,__mask).
00064   */
00065   inline _Resetiosflags 
00066   resetiosflags(ios_base::fmtflags __mask)
00067   { return { __mask }; }
00068 
00069   template<typename _CharT, typename _Traits>
00070     inline basic_istream<_CharT, _Traits>& 
00071     operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
00072     { 
00073       __is.setf(ios_base::fmtflags(0), __f._M_mask); 
00074       return __is; 
00075     }
00076 
00077   template<typename _CharT, typename _Traits>
00078     inline basic_ostream<_CharT, _Traits>& 
00079     operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
00080     { 
00081       __os.setf(ios_base::fmtflags(0), __f._M_mask); 
00082       return __os; 
00083     }
00084 
00085 
00086   struct _Setiosflags { ios_base::fmtflags _M_mask; };
00087 
00088   /**
00089    *  @brief  Manipulator for @c setf.
00090    *  @param  __mask  A format flags mask.
00091    *
00092    *  Sent to a stream object, this manipulator sets the format flags
00093    *  to @a __mask.
00094   */
00095   inline _Setiosflags 
00096   setiosflags(ios_base::fmtflags __mask)
00097   { return { __mask }; }
00098 
00099   template<typename _CharT, typename _Traits>
00100     inline basic_istream<_CharT, _Traits>& 
00101     operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
00102     { 
00103       __is.setf(__f._M_mask); 
00104       return __is; 
00105     }
00106 
00107   template<typename _CharT, typename _Traits>
00108     inline basic_ostream<_CharT, _Traits>& 
00109     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
00110     { 
00111       __os.setf(__f._M_mask); 
00112       return __os; 
00113     }
00114 
00115 
00116   struct _Setbase { int _M_base; };
00117 
00118   /**
00119    *  @brief  Manipulator for @c setf.
00120    *  @param  __base  A numeric base.
00121    *
00122    *  Sent to a stream object, this manipulator changes the
00123    *  @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
00124    *  is 8, 10, or 16, accordingly, and to 0 if @a __base is any other value.
00125   */
00126   inline _Setbase 
00127   setbase(int __base)
00128   { return { __base }; }
00129 
00130   template<typename _CharT, typename _Traits>
00131     inline basic_istream<_CharT, _Traits>& 
00132     operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
00133     {
00134       __is.setf(__f._M_base ==  8 ? ios_base::oct : 
00135                 __f._M_base == 10 ? ios_base::dec : 
00136                 __f._M_base == 16 ? ios_base::hex : 
00137                 ios_base::fmtflags(0), ios_base::basefield);
00138       return __is; 
00139     }
00140   
00141   template<typename _CharT, typename _Traits>
00142     inline basic_ostream<_CharT, _Traits>& 
00143     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
00144     {
00145       __os.setf(__f._M_base ==  8 ? ios_base::oct : 
00146                 __f._M_base == 10 ? ios_base::dec : 
00147                 __f._M_base == 16 ? ios_base::hex : 
00148                 ios_base::fmtflags(0), ios_base::basefield);
00149       return __os; 
00150     }
00151   
00152 
00153   template<typename _CharT>
00154     struct _Setfill { _CharT _M_c; };
00155 
00156   /**
00157    *  @brief  Manipulator for @c fill.
00158    *  @param  __c  The new fill character.
00159    *
00160    *  Sent to a stream object, this manipulator calls @c fill(__c) for that
00161    *  object.
00162   */
00163   template<typename _CharT>
00164     inline _Setfill<_CharT>
00165     setfill(_CharT __c)
00166     { return { __c }; }
00167 
00168   template<typename _CharT, typename _Traits>
00169     inline basic_istream<_CharT, _Traits>& 
00170     operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
00171     { 
00172       __is.fill(__f._M_c); 
00173       return __is; 
00174     }
00175 
00176   template<typename _CharT, typename _Traits>
00177     inline basic_ostream<_CharT, _Traits>& 
00178     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
00179     { 
00180       __os.fill(__f._M_c); 
00181       return __os; 
00182     }
00183 
00184 
00185   struct _Setprecision { int _M_n; };
00186 
00187   /**
00188    *  @brief  Manipulator for @c precision.
00189    *  @param  __n  The new precision.
00190    *
00191    *  Sent to a stream object, this manipulator calls @c precision(__n) for
00192    *  that object.
00193   */
00194   inline _Setprecision 
00195   setprecision(int __n)
00196   { return { __n }; }
00197 
00198   template<typename _CharT, typename _Traits>
00199     inline basic_istream<_CharT, _Traits>& 
00200     operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
00201     { 
00202       __is.precision(__f._M_n); 
00203       return __is; 
00204     }
00205 
00206   template<typename _CharT, typename _Traits>
00207     inline basic_ostream<_CharT, _Traits>& 
00208     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
00209     { 
00210       __os.precision(__f._M_n); 
00211       return __os; 
00212     }
00213 
00214 
00215   struct _Setw { int _M_n; };
00216 
00217   /**
00218    *  @brief  Manipulator for @c width.
00219    *  @param  __n  The new width.
00220    *
00221    *  Sent to a stream object, this manipulator calls @c width(__n) for
00222    *  that object.
00223   */
00224   inline _Setw 
00225   setw(int __n)
00226   { return { __n }; }
00227 
00228   template<typename _CharT, typename _Traits>
00229     inline basic_istream<_CharT, _Traits>& 
00230     operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
00231     {
00232       __is.width(__f._M_n);
00233       return __is; 
00234     }
00235 
00236   template<typename _CharT, typename _Traits>
00237     inline basic_ostream<_CharT, _Traits>& 
00238     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
00239     {
00240       __os.width(__f._M_n);
00241       return __os; 
00242     }
00243 
00244 #if __cplusplus >= 201103L
00245   
00246   template<typename _MoneyT>
00247     struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
00248 
00249   /**
00250    *  @brief  Extended manipulator for extracting money.
00251    *  @param  __mon  Either long double or a specialization of @c basic_string.
00252    *  @param  __intl A bool indicating whether international format 
00253    *                 is to be used.
00254    *
00255    *  Sent to a stream object, this manipulator extracts @a __mon.
00256   */
00257   template<typename _MoneyT>
00258     inline _Get_money<_MoneyT>
00259     get_money(_MoneyT& __mon, bool __intl = false)
00260     { return { __mon, __intl }; }
00261 
00262   template<typename _CharT, typename _Traits, typename _MoneyT>
00263     basic_istream<_CharT, _Traits>&
00264     operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
00265     {
00266       typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
00267       if (__cerb)
00268         {
00269           ios_base::iostate __err = ios_base::goodbit;
00270           __try
00271             {
00272               typedef istreambuf_iterator<_CharT, _Traits>   _Iter;
00273               typedef money_get<_CharT, _Iter>               _MoneyGet;
00274 
00275               const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
00276               __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
00277                        __is, __err, __f._M_mon);
00278             }
00279           __catch(__cxxabiv1::__forced_unwind&)
00280             {
00281               __is._M_setstate(ios_base::badbit);
00282               __throw_exception_again;
00283             }
00284           __catch(...)
00285             { __is._M_setstate(ios_base::badbit); }
00286           if (__err)
00287             __is.setstate(__err);
00288         }
00289       return __is; 
00290     }
00291 
00292 
00293   template<typename _MoneyT>
00294     struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
00295 
00296   /**
00297    *  @brief  Extended manipulator for inserting money.
00298    *  @param  __mon  Either long double or a specialization of @c basic_string.
00299    *  @param  __intl A bool indicating whether international format 
00300    *                 is to be used.
00301    *
00302    *  Sent to a stream object, this manipulator inserts @a __mon.
00303   */
00304   template<typename _MoneyT>
00305     inline _Put_money<_MoneyT>
00306     put_money(const _MoneyT& __mon, bool __intl = false)
00307     { return { __mon, __intl }; }
00308 
00309   template<typename _CharT, typename _Traits, typename _MoneyT>
00310     basic_ostream<_CharT, _Traits>& 
00311     operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
00312     {
00313       typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
00314       if (__cerb)
00315         {
00316           ios_base::iostate __err = ios_base::goodbit;
00317           __try
00318             {
00319               typedef ostreambuf_iterator<_CharT, _Traits>   _Iter;
00320               typedef money_put<_CharT, _Iter>               _MoneyPut;
00321 
00322               const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
00323               if (__mp.put(_Iter(__os.rdbuf()), __f._M_intl, __os,
00324                            __os.fill(), __f._M_mon).failed())
00325                 __err |= ios_base::badbit;
00326             }
00327           __catch(__cxxabiv1::__forced_unwind&)
00328             {
00329               __os._M_setstate(ios_base::badbit);
00330               __throw_exception_again;
00331             }
00332           __catch(...)
00333             { __os._M_setstate(ios_base::badbit); }
00334           if (__err)
00335             __os.setstate(__err);
00336         }
00337       return __os; 
00338     }
00339 
00340   template<typename _CharT>
00341     struct _Put_time
00342     {
00343       const std::tm* _M_tmb;
00344       const _CharT* _M_fmt;
00345     };
00346 
00347   /**
00348    *  @brief  Extended manipulator for formatting time.
00349    *
00350    *  This manipulator uses time_put::put to format time.
00351    *  [ext.manip]
00352    *
00353    *  @param __tmb  struct tm time data to format.
00354    *  @param __fmt  format string.
00355    */
00356   template<typename _CharT>
00357     inline _Put_time<_CharT>
00358     put_time(const std::tm* __tmb, const _CharT* __fmt)
00359     { return { __tmb, __fmt }; }
00360 
00361   template<typename _CharT, typename _Traits>
00362     basic_ostream<_CharT, _Traits>&
00363     operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_time<_CharT> __f)
00364     {
00365       typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
00366       if (__cerb)
00367         {
00368           ios_base::iostate __err = ios_base::goodbit;
00369           __try
00370             {
00371               typedef ostreambuf_iterator<_CharT, _Traits>   _Iter;
00372               typedef time_put<_CharT, _Iter>                _TimePut;
00373 
00374               const _CharT* const __fmt_end = __f._M_fmt +
00375                 _Traits::length(__f._M_fmt);
00376 
00377               const _TimePut& __mp = use_facet<_TimePut>(__os.getloc());
00378               if (__mp.put(_Iter(__os.rdbuf()), __os, __os.fill(),
00379                            __f._M_tmb, __f._M_fmt, __fmt_end).failed())
00380                 __err |= ios_base::badbit;
00381             }
00382           __catch(__cxxabiv1::__forced_unwind&)
00383             {
00384               __os._M_setstate(ios_base::badbit);
00385               __throw_exception_again;
00386             }
00387           __catch(...)
00388             { __os._M_setstate(ios_base::badbit); }
00389           if (__err)
00390             __os.setstate(__err);
00391         }
00392       return __os;
00393     }
00394 
00395   template<typename _CharT>
00396     struct _Get_time
00397     {
00398       std::tm*      _M_tmb;
00399       const _CharT* _M_fmt;
00400     };
00401 
00402   /**
00403    *  @brief  Extended manipulator for extracting time.
00404    *
00405    *  This manipulator uses time_get::get to extract time.
00406    *  [ext.manip]
00407    *
00408    *  @param __tmb  struct to extract the time data to.
00409    *  @param __fmt  format string.
00410    */
00411   template<typename _CharT>
00412     inline _Get_time<_CharT>
00413     get_time(std::tm* __tmb, const _CharT* __fmt)
00414     { return { __tmb, __fmt }; }
00415 
00416   template<typename _CharT, typename _Traits>
00417     basic_istream<_CharT, _Traits>&
00418     operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f)
00419     {
00420       typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
00421       if (__cerb)
00422         {
00423           ios_base::iostate __err = ios_base::goodbit;
00424           __try
00425             {
00426               typedef istreambuf_iterator<_CharT, _Traits>   _Iter;
00427               typedef time_get<_CharT, _Iter>                _TimeGet;
00428 
00429               const _CharT* const __fmt_end = __f._M_fmt +
00430                 _Traits::length(__f._M_fmt);
00431 
00432               const _TimeGet& __mg = use_facet<_TimeGet>(__is.getloc());
00433               __mg.get(_Iter(__is.rdbuf()), _Iter(), __is,
00434                        __err, __f._M_tmb, __f._M_fmt, __fmt_end);
00435             }
00436           __catch(__cxxabiv1::__forced_unwind&)
00437             {
00438               __is._M_setstate(ios_base::badbit);
00439               __throw_exception_again;
00440             }
00441           __catch(...)
00442             { __is._M_setstate(ios_base::badbit); }
00443           if (__err)
00444             __is.setstate(__err);
00445         }
00446       return __is;
00447     }
00448 
00449 #if __cplusplus > 201103L
00450 
00451 #define __cpp_lib_quoted_string_io 201304
00452 
00453   /**
00454    * @brief Manipulator for quoted strings.
00455    * @param __string String to quote.
00456    * @param __delim  Character to quote string with.
00457    * @param __escape Escape character to escape itself or quote character.
00458    */
00459   template<typename _CharT>
00460     inline auto
00461     quoted(const _CharT* __string,
00462            _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
00463     {
00464       return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim,
00465                                                              __escape);
00466     }
00467 
00468   template<typename _CharT, typename _Traits, typename _Alloc>
00469     inline auto
00470     quoted(const basic_string<_CharT, _Traits, _Alloc>& __string,
00471            _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
00472     {
00473       return __detail::_Quoted_string<
00474                         const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
00475                                 __string, __delim, __escape);
00476     }
00477 
00478   template<typename _CharT, typename _Traits, typename _Alloc>
00479     inline auto
00480     quoted(basic_string<_CharT, _Traits, _Alloc>& __string,
00481            _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
00482     {
00483       return __detail::_Quoted_string<
00484                         basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
00485                                 __string, __delim, __escape);
00486     }
00487 
00488 #endif // __cplusplus > 201103L
00489 
00490 #endif // __cplusplus >= 201103L
00491 
00492   // Inhibit implicit instantiations for required instantiations,
00493   // which are defined via explicit instantiations elsewhere.  
00494   // NB:  This syntax is a GNU extension.
00495 #if _GLIBCXX_EXTERN_TEMPLATE
00496   extern template ostream& operator<<(ostream&, _Setfill<char>);
00497   extern template ostream& operator<<(ostream&, _Setiosflags);
00498   extern template ostream& operator<<(ostream&, _Resetiosflags);
00499   extern template ostream& operator<<(ostream&, _Setbase);
00500   extern template ostream& operator<<(ostream&, _Setprecision);
00501   extern template ostream& operator<<(ostream&, _Setw);
00502   extern template istream& operator>>(istream&, _Setfill<char>);
00503   extern template istream& operator>>(istream&, _Setiosflags);
00504   extern template istream& operator>>(istream&, _Resetiosflags);
00505   extern template istream& operator>>(istream&, _Setbase);
00506   extern template istream& operator>>(istream&, _Setprecision);
00507   extern template istream& operator>>(istream&, _Setw);
00508 
00509 #ifdef _GLIBCXX_USE_WCHAR_T
00510   extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
00511   extern template wostream& operator<<(wostream&, _Setiosflags);
00512   extern template wostream& operator<<(wostream&, _Resetiosflags);
00513   extern template wostream& operator<<(wostream&, _Setbase);
00514   extern template wostream& operator<<(wostream&, _Setprecision);
00515   extern template wostream& operator<<(wostream&, _Setw);
00516   extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
00517   extern template wistream& operator>>(wistream&, _Setiosflags);
00518   extern template wistream& operator>>(wistream&, _Resetiosflags);
00519   extern template wistream& operator>>(wistream&, _Setbase);
00520   extern template wistream& operator>>(wistream&, _Setprecision);
00521   extern template wistream& operator>>(wistream&, _Setw);
00522 #endif
00523 #endif
00524 
00525 _GLIBCXX_END_NAMESPACE_VERSION
00526 } // namespace
00527 
00528 #endif /* _GLIBCXX_IOMANIP */