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