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
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       typedef istreambuf_iterator<_CharT, _Traits> _Iter;
00266       typedef money_get<_CharT, _Iter> _MoneyGet;
00267       
00268       ios_base::iostate __err = ios_base::goodbit;
00269       const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
00270 
00271       __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
00272            __is, __err, __f._M_mon);
00273 
00274       if (ios_base::goodbit != __err)
00275     __is.setstate(__err);
00276 
00277       return __is; 
00278     }
00279 
00280 
00281   template<typename _MoneyT>
00282     struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
00283 
00284   /**
00285    *  @brief  Extended manipulator for inserting money.
00286    *  @param  mon  Either long double or a specialization of @c basic_string.
00287    *  @param  intl A bool indicating whether international format 
00288    *               is to be used.
00289    *
00290    *  Sent to a stream object, this manipulator inserts @a mon.
00291   */
00292   template<typename _MoneyT>
00293     inline _Put_money<_MoneyT>
00294     put_money(const _MoneyT& __mon, bool __intl = false)
00295     { return { __mon, __intl }; }
00296 
00297   template<typename _CharT, typename _Traits, typename _MoneyT>
00298     basic_ostream<_CharT, _Traits>& 
00299     operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
00300     {
00301       typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
00302       typedef money_put<_CharT, _Iter> _MoneyPut;
00303       
00304       const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
00305       const _Iter __end = __mp.put(_Iter(__os.rdbuf()), __f._M_intl,
00306                    __os, __os.fill(), __f._M_mon);
00307 
00308       if (__end.failed())
00309     __os.setstate(ios_base::badbit);
00310 
00311       return __os; 
00312     }
00313 
00314 #endif
00315 
00316   // Inhibit implicit instantiations for required instantiations,
00317   // which are defined via explicit instantiations elsewhere.  
00318   // NB:  This syntax is a GNU extension.
00319 #if _GLIBCXX_EXTERN_TEMPLATE
00320   extern template ostream& operator<<(ostream&, _Setfill<char>);
00321   extern template ostream& operator<<(ostream&, _Setiosflags);
00322   extern template ostream& operator<<(ostream&, _Resetiosflags);
00323   extern template ostream& operator<<(ostream&, _Setbase);
00324   extern template ostream& operator<<(ostream&, _Setprecision);
00325   extern template ostream& operator<<(ostream&, _Setw);
00326   extern template istream& operator>>(istream&, _Setfill<char>);
00327   extern template istream& operator>>(istream&, _Setiosflags);
00328   extern template istream& operator>>(istream&, _Resetiosflags);
00329   extern template istream& operator>>(istream&, _Setbase);
00330   extern template istream& operator>>(istream&, _Setprecision);
00331   extern template istream& operator>>(istream&, _Setw);
00332 
00333 #ifdef _GLIBCXX_USE_WCHAR_T
00334   extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
00335   extern template wostream& operator<<(wostream&, _Setiosflags);
00336   extern template wostream& operator<<(wostream&, _Resetiosflags);
00337   extern template wostream& operator<<(wostream&, _Setbase);
00338   extern template wostream& operator<<(wostream&, _Setprecision);
00339   extern template wostream& operator<<(wostream&, _Setw);
00340   extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
00341   extern template wistream& operator>>(wistream&, _Setiosflags);
00342   extern template wistream& operator>>(wistream&, _Resetiosflags);
00343   extern template wistream& operator>>(wistream&, _Setbase);
00344   extern template wistream& operator>>(wistream&, _Setprecision);
00345   extern template wistream& operator>>(wistream&, _Setw);
00346 #endif
00347 #endif
00348 
00349 _GLIBCXX_END_NAMESPACE_VERSION
00350 } // namespace
00351 
00352 #endif /* _GLIBCXX_IOMANIP */