libstdc++
iomanip
Go to the documentation of this file.
00001 // Standard stream manipulators -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
00004 // 2006, 2007, 2008, 2009, 2010, 2011
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /** @file include/iomanip
00028  *  This is a Standard C++ Library header.
00029  */
00030 
00031 //
00032 // ISO C++ 14882: 27.6.3  Standard manipulators
00033 //
00034 
00035 #ifndef _GLIBCXX_IOMANIP
00036 #define _GLIBCXX_IOMANIP 1
00037 
00038 #pragma GCC system_header
00039 
00040 #include <bits/c++config.h>
00041 #include <iosfwd>
00042 #include <bits/ios_base.h>
00043 
00044 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00045 #include <locale>
00046 #endif
00047 
00048 namespace std _GLIBCXX_VISIBILITY(default)
00049 {
00050 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00051 
00052   // [27.6.3] standard manipulators
00053   // Also see DR 183.
00054 
00055   struct _Resetiosflags { ios_base::fmtflags _M_mask; };
00056 
00057   /**
00058    *  @brief  Manipulator for @c setf.
00059    *  @param  __mask  A format flags mask.
00060    *
00061    *  Sent to a stream object, this manipulator resets the specified flags,
00062    *  via @e stream.setf(0,__mask).
00063   */
00064   inline _Resetiosflags 
00065   resetiosflags(ios_base::fmtflags __mask)
00066   { return { __mask }; }
00067 
00068   template<typename _CharT, typename _Traits>
00069     inline basic_istream<_CharT, _Traits>& 
00070     operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
00071     { 
00072       __is.setf(ios_base::fmtflags(0), __f._M_mask); 
00073       return __is; 
00074     }
00075 
00076   template<typename _CharT, typename _Traits>
00077     inline basic_ostream<_CharT, _Traits>& 
00078     operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
00079     { 
00080       __os.setf(ios_base::fmtflags(0), __f._M_mask); 
00081       return __os; 
00082     }
00083 
00084 
00085   struct _Setiosflags { ios_base::fmtflags _M_mask; };
00086 
00087   /**
00088    *  @brief  Manipulator for @c setf.
00089    *  @param  __mask  A format flags mask.
00090    *
00091    *  Sent to a stream object, this manipulator sets the format flags
00092    *  to @a __mask.
00093   */
00094   inline _Setiosflags 
00095   setiosflags(ios_base::fmtflags __mask)
00096   { return { __mask }; }
00097 
00098   template<typename _CharT, typename _Traits>
00099     inline basic_istream<_CharT, _Traits>& 
00100     operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
00101     { 
00102       __is.setf(__f._M_mask); 
00103       return __is; 
00104     }
00105 
00106   template<typename _CharT, typename _Traits>
00107     inline basic_ostream<_CharT, _Traits>& 
00108     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
00109     { 
00110       __os.setf(__f._M_mask); 
00111       return __os; 
00112     }
00113 
00114 
00115   struct _Setbase { int _M_base; };
00116 
00117   /**
00118    *  @brief  Manipulator for @c setf.
00119    *  @param  __base  A numeric base.
00120    *
00121    *  Sent to a stream object, this manipulator changes the
00122    *  @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
00123    *  is 8, 10, or 16, accordingly, and to 0 if @a __base is any other value.
00124   */
00125   inline _Setbase 
00126   setbase(int __base)
00127   { return { __base }; }
00128 
00129   template<typename _CharT, typename _Traits>
00130     inline basic_istream<_CharT, _Traits>& 
00131     operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
00132     {
00133       __is.setf(__f._M_base ==  8 ? ios_base::oct : 
00134         __f._M_base == 10 ? ios_base::dec : 
00135         __f._M_base == 16 ? ios_base::hex : 
00136         ios_base::fmtflags(0), ios_base::basefield);
00137       return __is; 
00138     }
00139   
00140   template<typename _CharT, typename _Traits>
00141     inline basic_ostream<_CharT, _Traits>& 
00142     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
00143     {
00144       __os.setf(__f._M_base ==  8 ? ios_base::oct : 
00145         __f._M_base == 10 ? ios_base::dec : 
00146         __f._M_base == 16 ? ios_base::hex : 
00147         ios_base::fmtflags(0), ios_base::basefield);
00148       return __os; 
00149     }
00150   
00151 
00152   template<typename _CharT>
00153     struct _Setfill { _CharT _M_c; };
00154 
00155   /**
00156    *  @brief  Manipulator for @c fill.
00157    *  @param  __c  The new fill character.
00158    *
00159    *  Sent to a stream object, this manipulator calls @c fill(__c) for that
00160    *  object.
00161   */
00162   template<typename _CharT>
00163     inline _Setfill<_CharT>
00164     setfill(_CharT __c)
00165     { return { __c }; }
00166 
00167   template<typename _CharT, typename _Traits>
00168     inline basic_istream<_CharT, _Traits>& 
00169     operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
00170     { 
00171       __is.fill(__f._M_c); 
00172       return __is; 
00173     }
00174 
00175   template<typename _CharT, typename _Traits>
00176     inline basic_ostream<_CharT, _Traits>& 
00177     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
00178     { 
00179       __os.fill(__f._M_c); 
00180       return __os; 
00181     }
00182 
00183 
00184   struct _Setprecision { int _M_n; };
00185 
00186   /**
00187    *  @brief  Manipulator for @c precision.
00188    *  @param  __n  The new precision.
00189    *
00190    *  Sent to a stream object, this manipulator calls @c precision(__n) for
00191    *  that object.
00192   */
00193   inline _Setprecision 
00194   setprecision(int __n)
00195   { return { __n }; }
00196 
00197   template<typename _CharT, typename _Traits>
00198     inline basic_istream<_CharT, _Traits>& 
00199     operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
00200     { 
00201       __is.precision(__f._M_n); 
00202       return __is; 
00203     }
00204 
00205   template<typename _CharT, typename _Traits>
00206     inline basic_ostream<_CharT, _Traits>& 
00207     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
00208     { 
00209       __os.precision(__f._M_n); 
00210       return __os; 
00211     }
00212 
00213 
00214   struct _Setw { int _M_n; };
00215 
00216   /**
00217    *  @brief  Manipulator for @c width.
00218    *  @param  __n  The new width.
00219    *
00220    *  Sent to a stream object, this manipulator calls @c width(__n) for
00221    *  that object.
00222   */
00223   inline _Setw 
00224   setw(int __n)
00225   { return { __n }; }
00226 
00227   template<typename _CharT, typename _Traits>
00228     inline basic_istream<_CharT, _Traits>& 
00229     operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
00230     {
00231       __is.width(__f._M_n);
00232       return __is; 
00233     }
00234 
00235   template<typename _CharT, typename _Traits>
00236     inline basic_ostream<_CharT, _Traits>& 
00237     operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
00238     {
00239       __os.width(__f._M_n);
00240       return __os; 
00241     }
00242 
00243 #ifdef __GXX_EXPERIMENTAL_CXX0X__
00244   
00245   template<typename _MoneyT>
00246     struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
00247 
00248   /**
00249    *  @brief  Extended manipulator for extracting money.
00250    *  @param  __mon  Either long double or a specialization of @c basic_string.
00251    *  @param  __intl A bool indicating whether international format 
00252    *                 is to be used.
00253    *
00254    *  Sent to a stream object, this manipulator extracts @a __mon.
00255   */
00256   template<typename _MoneyT>
00257     inline _Get_money<_MoneyT>
00258     get_money(_MoneyT& __mon, bool __intl = false)
00259     { return { __mon, __intl }; }
00260 
00261   template<typename _CharT, typename _Traits, typename _MoneyT>
00262     basic_istream<_CharT, _Traits>&
00263     operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
00264     {
00265       typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
00266       if (__cerb)
00267     {
00268       ios_base::iostate __err = ios_base::goodbit;
00269       __try
00270         {
00271           typedef istreambuf_iterator<_CharT, _Traits>   _Iter;
00272           typedef money_get<_CharT, _Iter>               _MoneyGet;
00273 
00274           const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
00275           __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
00276                __is, __err, __f._M_mon);
00277         }
00278       __catch(__cxxabiv1::__forced_unwind&)
00279         {
00280           __is._M_setstate(ios_base::badbit);
00281           __throw_exception_again;
00282         }
00283       __catch(...)
00284         { __is._M_setstate(ios_base::badbit); }
00285       if (__err)
00286         __is.setstate(__err);
00287     }
00288       return __is; 
00289     }
00290 
00291 
00292   template<typename _MoneyT>
00293     struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
00294 
00295   /**
00296    *  @brief  Extended manipulator for inserting money.
00297    *  @param  __mon  Either long double or a specialization of @c basic_string.
00298    *  @param  __intl A bool indicating whether international format 
00299    *                 is to be used.
00300    *
00301    *  Sent to a stream object, this manipulator inserts @a __mon.
00302   */
00303   template<typename _MoneyT>
00304     inline _Put_money<_MoneyT>
00305     put_money(const _MoneyT& __mon, bool __intl = false)
00306     { return { __mon, __intl }; }
00307 
00308   template<typename _CharT, typename _Traits, typename _MoneyT>
00309     basic_ostream<_CharT, _Traits>& 
00310     operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
00311     {
00312       typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
00313       if (__cerb)
00314     {
00315       ios_base::iostate __err = ios_base::goodbit;
00316       __try
00317         {
00318           typedef ostreambuf_iterator<_CharT, _Traits>   _Iter;
00319           typedef money_put<_CharT, _Iter>               _MoneyPut;
00320 
00321           const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
00322           if (__mp.put(_Iter(__os.rdbuf()), __f._M_intl, __os,
00323                __os.fill(), __f._M_mon).failed())
00324         __err |= ios_base::badbit;
00325         }
00326       __catch(__cxxabiv1::__forced_unwind&)
00327         {
00328           __os._M_setstate(ios_base::badbit);
00329           __throw_exception_again;
00330         }
00331       __catch(...)
00332         { __os._M_setstate(ios_base::badbit); }
00333       if (__err)
00334         __os.setstate(__err);
00335     }
00336       return __os; 
00337     }
00338 
00339 #endif
00340 
00341   // Inhibit implicit instantiations for required instantiations,
00342   // which are defined via explicit instantiations elsewhere.  
00343   // NB:  This syntax is a GNU extension.
00344 #if _GLIBCXX_EXTERN_TEMPLATE
00345   extern template ostream& operator<<(ostream&, _Setfill<char>);
00346   extern template ostream& operator<<(ostream&, _Setiosflags);
00347   extern template ostream& operator<<(ostream&, _Resetiosflags);
00348   extern template ostream& operator<<(ostream&, _Setbase);
00349   extern template ostream& operator<<(ostream&, _Setprecision);
00350   extern template ostream& operator<<(ostream&, _Setw);
00351   extern template istream& operator>>(istream&, _Setfill<char>);
00352   extern template istream& operator>>(istream&, _Setiosflags);
00353   extern template istream& operator>>(istream&, _Resetiosflags);
00354   extern template istream& operator>>(istream&, _Setbase);
00355   extern template istream& operator>>(istream&, _Setprecision);
00356   extern template istream& operator>>(istream&, _Setw);
00357 
00358 #ifdef _GLIBCXX_USE_WCHAR_T
00359   extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
00360   extern template wostream& operator<<(wostream&, _Setiosflags);
00361   extern template wostream& operator<<(wostream&, _Resetiosflags);
00362   extern template wostream& operator<<(wostream&, _Setbase);
00363   extern template wostream& operator<<(wostream&, _Setprecision);
00364   extern template wostream& operator<<(wostream&, _Setw);
00365   extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
00366   extern template wistream& operator>>(wistream&, _Setiosflags);
00367   extern template wistream& operator>>(wistream&, _Resetiosflags);
00368   extern template wistream& operator>>(wistream&, _Setbase);
00369   extern template wistream& operator>>(wistream&, _Setprecision);
00370   extern template wistream& operator>>(wistream&, _Setw);
00371 #endif
00372 #endif
00373 
00374 _GLIBCXX_END_NAMESPACE_VERSION
00375 } // namespace
00376 
00377 #endif /* _GLIBCXX_IOMANIP */