libstdc++
iomanip
Go to the documentation of this file.
00001 // Standard stream manipulators -*- C++ -*-
00002 
00003 // Copyright (C) 1997-2013 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 #endif
00045 
00046 namespace std _GLIBCXX_VISIBILITY(default)
00047 {
00048 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00049 
00050   // [27.6.3] standard manipulators
00051   // Also see DR 183.
00052 
00053   struct _Resetiosflags { ios_base::fmtflags _M_mask; };
00054 
00055   /**
00056    *  @brief  Manipulator for @c setf.
00057    *  @param  __mask  A format flags mask.
00058    *
00059    *  Sent to a stream object, this manipulator resets the specified flags,
00060    *  via @e stream.setf(0,__mask).
00061   */
00062   inline _Resetiosflags 
00063   resetiosflags(ios_base::fmtflags __mask)
00064   { return { __mask }; }
00065 
00066   template<typename _CharT, typename _Traits>
00067     inline basic_istream<_CharT, _Traits>& 
00068     operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
00069     { 
00070       __is.setf(ios_base::fmtflags(0), __f._M_mask); 
00071       return __is; 
00072     }
00073 
00074   template<typename _CharT, typename _Traits>
00075     inline basic_ostream<_CharT, _Traits>& 
00076     operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
00077     { 
00078       __os.setf(ios_base::fmtflags(0), __f._M_mask); 
00079       return __os; 
00080     }
00081 
00082 
00083   struct _Setiosflags { ios_base::fmtflags _M_mask; };
00084 
00085   /**
00086    *  @brief  Manipulator for @c setf.
00087    *  @param  __mask  A format flags mask.
00088    *
00089    *  Sent to a stream object, this manipulator sets the format flags
00090    *  to @a __mask.
00091   */
00092   inline _Setiosflags 
00093   setiosflags(ios_base::fmtflags __mask)
00094   { return { __mask }; }
00095 
00096   template<typename _CharT, typename _Traits>
00097     inline basic_istream<_CharT, _Traits>& 
00098     operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
00099     { 
00100       __is.setf(__f._M_mask); 
00101       return __is; 
00102     }
00103 
00104   template<typename _CharT, typename _Traits>
00105     inline basic_ostream<_CharT, _Traits>& 
00106     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
00107     { 
00108       __os.setf(__f._M_mask); 
00109       return __os; 
00110     }
00111 
00112 
00113   struct _Setbase { int _M_base; };
00114 
00115   /**
00116    *  @brief  Manipulator for @c setf.
00117    *  @param  __base  A numeric base.
00118    *
00119    *  Sent to a stream object, this manipulator changes the
00120    *  @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
00121    *  is 8, 10, or 16, accordingly, and to 0 if @a __base is any other value.
00122   */
00123   inline _Setbase 
00124   setbase(int __base)
00125   { return { __base }; }
00126 
00127   template<typename _CharT, typename _Traits>
00128     inline basic_istream<_CharT, _Traits>& 
00129     operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
00130     {
00131       __is.setf(__f._M_base ==  8 ? ios_base::oct : 
00132         __f._M_base == 10 ? ios_base::dec : 
00133         __f._M_base == 16 ? ios_base::hex : 
00134         ios_base::fmtflags(0), ios_base::basefield);
00135       return __is; 
00136     }
00137   
00138   template<typename _CharT, typename _Traits>
00139     inline basic_ostream<_CharT, _Traits>& 
00140     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
00141     {
00142       __os.setf(__f._M_base ==  8 ? ios_base::oct : 
00143         __f._M_base == 10 ? ios_base::dec : 
00144         __f._M_base == 16 ? ios_base::hex : 
00145         ios_base::fmtflags(0), ios_base::basefield);
00146       return __os; 
00147     }
00148   
00149 
00150   template<typename _CharT>
00151     struct _Setfill { _CharT _M_c; };
00152 
00153   /**
00154    *  @brief  Manipulator for @c fill.
00155    *  @param  __c  The new fill character.
00156    *
00157    *  Sent to a stream object, this manipulator calls @c fill(__c) for that
00158    *  object.
00159   */
00160   template<typename _CharT>
00161     inline _Setfill<_CharT>
00162     setfill(_CharT __c)
00163     { return { __c }; }
00164 
00165   template<typename _CharT, typename _Traits>
00166     inline basic_istream<_CharT, _Traits>& 
00167     operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
00168     { 
00169       __is.fill(__f._M_c); 
00170       return __is; 
00171     }
00172 
00173   template<typename _CharT, typename _Traits>
00174     inline basic_ostream<_CharT, _Traits>& 
00175     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
00176     { 
00177       __os.fill(__f._M_c); 
00178       return __os; 
00179     }
00180 
00181 
00182   struct _Setprecision { int _M_n; };
00183 
00184   /**
00185    *  @brief  Manipulator for @c precision.
00186    *  @param  __n  The new precision.
00187    *
00188    *  Sent to a stream object, this manipulator calls @c precision(__n) for
00189    *  that object.
00190   */
00191   inline _Setprecision 
00192   setprecision(int __n)
00193   { return { __n }; }
00194 
00195   template<typename _CharT, typename _Traits>
00196     inline basic_istream<_CharT, _Traits>& 
00197     operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
00198     { 
00199       __is.precision(__f._M_n); 
00200       return __is; 
00201     }
00202 
00203   template<typename _CharT, typename _Traits>
00204     inline basic_ostream<_CharT, _Traits>& 
00205     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
00206     { 
00207       __os.precision(__f._M_n); 
00208       return __os; 
00209     }
00210 
00211 
00212   struct _Setw { int _M_n; };
00213 
00214   /**
00215    *  @brief  Manipulator for @c width.
00216    *  @param  __n  The new width.
00217    *
00218    *  Sent to a stream object, this manipulator calls @c width(__n) for
00219    *  that object.
00220   */
00221   inline _Setw 
00222   setw(int __n)
00223   { return { __n }; }
00224 
00225   template<typename _CharT, typename _Traits>
00226     inline basic_istream<_CharT, _Traits>& 
00227     operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
00228     {
00229       __is.width(__f._M_n);
00230       return __is; 
00231     }
00232 
00233   template<typename _CharT, typename _Traits>
00234     inline basic_ostream<_CharT, _Traits>& 
00235     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
00236     {
00237       __os.width(__f._M_n);
00238       return __os; 
00239     }
00240 
00241 #if __cplusplus >= 201103L
00242   
00243   template<typename _MoneyT>
00244     struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
00245 
00246   /**
00247    *  @brief  Extended manipulator for extracting money.
00248    *  @param  __mon  Either long double or a specialization of @c basic_string.
00249    *  @param  __intl A bool indicating whether international format 
00250    *                 is to be used.
00251    *
00252    *  Sent to a stream object, this manipulator extracts @a __mon.
00253   */
00254   template<typename _MoneyT>
00255     inline _Get_money<_MoneyT>
00256     get_money(_MoneyT& __mon, bool __intl = false)
00257     { return { __mon, __intl }; }
00258 
00259   template<typename _CharT, typename _Traits, typename _MoneyT>
00260     basic_istream<_CharT, _Traits>&
00261     operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
00262     {
00263       typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
00264       if (__cerb)
00265     {
00266       ios_base::iostate __err = ios_base::goodbit;
00267       __try
00268         {
00269           typedef istreambuf_iterator<_CharT, _Traits>   _Iter;
00270           typedef money_get<_CharT, _Iter>               _MoneyGet;
00271 
00272           const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
00273           __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
00274                __is, __err, __f._M_mon);
00275         }
00276       __catch(__cxxabiv1::__forced_unwind&)
00277         {
00278           __is._M_setstate(ios_base::badbit);
00279           __throw_exception_again;
00280         }
00281       __catch(...)
00282         { __is._M_setstate(ios_base::badbit); }
00283       if (__err)
00284         __is.setstate(__err);
00285     }
00286       return __is; 
00287     }
00288 
00289 
00290   template<typename _MoneyT>
00291     struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
00292 
00293   /**
00294    *  @brief  Extended manipulator for inserting money.
00295    *  @param  __mon  Either long double or a specialization of @c basic_string.
00296    *  @param  __intl A bool indicating whether international format 
00297    *                 is to be used.
00298    *
00299    *  Sent to a stream object, this manipulator inserts @a __mon.
00300   */
00301   template<typename _MoneyT>
00302     inline _Put_money<_MoneyT>
00303     put_money(const _MoneyT& __mon, bool __intl = false)
00304     { return { __mon, __intl }; }
00305 
00306   template<typename _CharT, typename _Traits, typename _MoneyT>
00307     basic_ostream<_CharT, _Traits>& 
00308     operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
00309     {
00310       typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
00311       if (__cerb)
00312     {
00313       ios_base::iostate __err = ios_base::goodbit;
00314       __try
00315         {
00316           typedef ostreambuf_iterator<_CharT, _Traits>   _Iter;
00317           typedef money_put<_CharT, _Iter>               _MoneyPut;
00318 
00319           const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
00320           if (__mp.put(_Iter(__os.rdbuf()), __f._M_intl, __os,
00321                __os.fill(), __f._M_mon).failed())
00322         __err |= ios_base::badbit;
00323         }
00324       __catch(__cxxabiv1::__forced_unwind&)
00325         {
00326           __os._M_setstate(ios_base::badbit);
00327           __throw_exception_again;
00328         }
00329       __catch(...)
00330         { __os._M_setstate(ios_base::badbit); }
00331       if (__err)
00332         __os.setstate(__err);
00333     }
00334       return __os; 
00335     }
00336 
00337 #endif
00338 
00339   // Inhibit implicit instantiations for required instantiations,
00340   // which are defined via explicit instantiations elsewhere.  
00341   // NB:  This syntax is a GNU extension.
00342 #if _GLIBCXX_EXTERN_TEMPLATE
00343   extern template ostream& operator<<(ostream&, _Setfill<char>);
00344   extern template ostream& operator<<(ostream&, _Setiosflags);
00345   extern template ostream& operator<<(ostream&, _Resetiosflags);
00346   extern template ostream& operator<<(ostream&, _Setbase);
00347   extern template ostream& operator<<(ostream&, _Setprecision);
00348   extern template ostream& operator<<(ostream&, _Setw);
00349   extern template istream& operator>>(istream&, _Setfill<char>);
00350   extern template istream& operator>>(istream&, _Setiosflags);
00351   extern template istream& operator>>(istream&, _Resetiosflags);
00352   extern template istream& operator>>(istream&, _Setbase);
00353   extern template istream& operator>>(istream&, _Setprecision);
00354   extern template istream& operator>>(istream&, _Setw);
00355 
00356 #ifdef _GLIBCXX_USE_WCHAR_T
00357   extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
00358   extern template wostream& operator<<(wostream&, _Setiosflags);
00359   extern template wostream& operator<<(wostream&, _Resetiosflags);
00360   extern template wostream& operator<<(wostream&, _Setbase);
00361   extern template wostream& operator<<(wostream&, _Setprecision);
00362   extern template wostream& operator<<(wostream&, _Setw);
00363   extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
00364   extern template wistream& operator>>(wistream&, _Setiosflags);
00365   extern template wistream& operator>>(wistream&, _Resetiosflags);
00366   extern template wistream& operator>>(wistream&, _Setbase);
00367   extern template wistream& operator>>(wistream&, _Setprecision);
00368   extern template wistream& operator>>(wistream&, _Setw);
00369 #endif
00370 #endif
00371 
00372 _GLIBCXX_END_NAMESPACE_VERSION
00373 } // namespace
00374 
00375 #endif /* _GLIBCXX_IOMANIP */