libstdc++
iomanip
Go to the documentation of this file.
00001 // Standard stream manipulators -*- C++ -*-
00002 
00003 // Copyright (C) 1997-2014 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 <sstream> // used in quoted.
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 #if __cplusplus > 201103L
00341 
00342 _GLIBCXX_END_NAMESPACE_VERSION
00343   namespace __detail {
00344   _GLIBCXX_BEGIN_NAMESPACE_VERSION
00345 
00346     /**
00347      * @brief Struct for delimited strings.
00348      */
00349     template<typename _String, typename _CharT>
00350       struct _Quoted_string
00351       {
00352     static_assert(is_reference<_String>::value
00353            || is_pointer<_String>::value,
00354               "String type must be pointer or reference");
00355 
00356     _Quoted_string(_String __str, _CharT __del, _CharT __esc)
00357     : _M_string(__str), _M_delim{__del}, _M_escape{__esc}
00358     { }
00359 
00360     _Quoted_string&
00361     operator=(_Quoted_string&) = delete;
00362 
00363     _String _M_string;
00364     _CharT _M_delim;
00365     _CharT _M_escape;
00366       };
00367 
00368     /**
00369      * @brief Inserter for quoted strings.
00370      *
00371      *  _GLIBCXX_RESOLVE_LIB_DEFECTS
00372      *  DR 2344 quoted()'s interaction with padding is unclear
00373      */
00374     template<typename _CharT, typename _Traits>
00375       auto&
00376       operator<<(std::basic_ostream<_CharT, _Traits>& __os,
00377          const _Quoted_string<const _CharT*, _CharT>& __str)
00378       {
00379     std::basic_ostringstream<_CharT, _Traits> __ostr;
00380     __ostr << __str._M_delim;
00381     for (const _CharT* __c = __str._M_string; *__c; ++__c)
00382       {
00383         if (*__c == __str._M_delim || *__c == __str._M_escape)
00384           __ostr << __str._M_escape;
00385         __ostr << *__c;
00386       }
00387     __ostr << __str._M_delim;
00388 
00389     return __os << __ostr.str();
00390       }
00391 
00392     /**
00393      * @brief Inserter for quoted strings.
00394      *
00395      *  _GLIBCXX_RESOLVE_LIB_DEFECTS
00396      *  DR 2344 quoted()'s interaction with padding is unclear
00397      */
00398     template<typename _CharT, typename _Traits, typename _String>
00399       auto&
00400       operator<<(std::basic_ostream<_CharT, _Traits>& __os,
00401          const _Quoted_string<_String, _CharT>& __str)
00402       {
00403     std::basic_ostringstream<_CharT, _Traits> __ostr;
00404     __ostr << __str._M_delim;
00405     for (auto& __c : __str._M_string)
00406       {
00407         if (__c == __str._M_delim || __c == __str._M_escape)
00408           __ostr << __str._M_escape;
00409         __ostr << __c;
00410       }
00411     __ostr << __str._M_delim;
00412 
00413     return __os << __ostr.str();
00414       }
00415 
00416     /**
00417      * @brief Extractor for delimited strings.
00418      *        The left and right delimiters can be different.
00419      */
00420     template<typename _CharT, typename _Traits, typename _Alloc>
00421       auto&
00422       operator>>(std::basic_istream<_CharT, _Traits>& __is,
00423          const _Quoted_string<basic_string<_CharT, _Traits, _Alloc>&,
00424                       _CharT>& __str)
00425       {
00426     _CharT __c;
00427     __is >> __c;
00428     if (!__is.good())
00429       return __is;
00430     if (__c != __str._M_delim)
00431       {
00432         __is.unget();
00433         __is >> __str._M_string;
00434         return __is;
00435       }
00436     __str._M_string.clear();
00437     std::ios_base::fmtflags __flags
00438       = __is.flags(__is.flags() & ~std::ios_base::skipws);
00439     do
00440       {
00441         __is >> __c;
00442         if (!__is.good())
00443           break;
00444         if (__c == __str._M_escape)
00445           {
00446         __is >> __c;
00447         if (!__is.good())
00448           break;
00449           }
00450         else if (__c == __str._M_delim)
00451           break;
00452         __str._M_string += __c;
00453       }
00454     while (true);
00455     __is.setf(__flags);
00456 
00457     return __is;
00458       }
00459   _GLIBCXX_END_NAMESPACE_VERSION
00460   } // namespace __detail
00461 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00462 
00463   /**
00464    * @brief Manipulator for quoted strings.
00465    * @param __str    String to quote.
00466    * @param __delim  Character to quote string with.
00467    * @param __escape Escape character to escape itself or quote character.
00468    */
00469   template<typename _CharT>
00470     inline auto
00471     quoted(const _CharT* __string,
00472        _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
00473     {
00474       return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim,
00475                                  __escape);
00476     }
00477 
00478   template<typename _CharT, typename _Traits, typename _Alloc>
00479     inline auto
00480     quoted(const basic_string<_CharT, _Traits, _Alloc>& __string,
00481        _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
00482     {
00483       return __detail::_Quoted_string<
00484             const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
00485                 __string, __delim, __escape);
00486     }
00487 
00488   template<typename _CharT, typename _Traits, typename _Alloc>
00489     inline auto
00490     quoted(basic_string<_CharT, _Traits, _Alloc>& __string,
00491        _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
00492     {
00493       return __detail::_Quoted_string<
00494             basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
00495                 __string, __delim, __escape);
00496     }
00497 
00498 #endif // __cplusplus > 201103L
00499 
00500 #endif // __cplusplus >= 201103L
00501 
00502   // Inhibit implicit instantiations for required instantiations,
00503   // which are defined via explicit instantiations elsewhere.  
00504   // NB:  This syntax is a GNU extension.
00505 #if _GLIBCXX_EXTERN_TEMPLATE
00506   extern template ostream& operator<<(ostream&, _Setfill<char>);
00507   extern template ostream& operator<<(ostream&, _Setiosflags);
00508   extern template ostream& operator<<(ostream&, _Resetiosflags);
00509   extern template ostream& operator<<(ostream&, _Setbase);
00510   extern template ostream& operator<<(ostream&, _Setprecision);
00511   extern template ostream& operator<<(ostream&, _Setw);
00512   extern template istream& operator>>(istream&, _Setfill<char>);
00513   extern template istream& operator>>(istream&, _Setiosflags);
00514   extern template istream& operator>>(istream&, _Resetiosflags);
00515   extern template istream& operator>>(istream&, _Setbase);
00516   extern template istream& operator>>(istream&, _Setprecision);
00517   extern template istream& operator>>(istream&, _Setw);
00518 
00519 #ifdef _GLIBCXX_USE_WCHAR_T
00520   extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
00521   extern template wostream& operator<<(wostream&, _Setiosflags);
00522   extern template wostream& operator<<(wostream&, _Resetiosflags);
00523   extern template wostream& operator<<(wostream&, _Setbase);
00524   extern template wostream& operator<<(wostream&, _Setprecision);
00525   extern template wostream& operator<<(wostream&, _Setw);
00526   extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
00527   extern template wistream& operator>>(wistream&, _Setiosflags);
00528   extern template wistream& operator>>(wistream&, _Resetiosflags);
00529   extern template wistream& operator>>(wistream&, _Setbase);
00530   extern template wistream& operator>>(wistream&, _Setprecision);
00531   extern template wistream& operator>>(wistream&, _Setw);
00532 #endif
00533 #endif
00534 
00535 _GLIBCXX_END_NAMESPACE_VERSION
00536 } // namespace
00537 
00538 #endif /* _GLIBCXX_IOMANIP */