12116Sjkh// Standard stream manipulators -*- C++ -*-
22116Sjkh
32116Sjkh// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
42116Sjkh// 2006, 2007, 2008, 2009, 2010
52116Sjkh// Free Software Foundation, Inc.
62116Sjkh//
72116Sjkh// This file is part of the GNU ISO C++ Library.  This library is free
82116Sjkh// software; you can redistribute it and/or modify it under the
92116Sjkh// terms of the GNU General Public License as published by the
102116Sjkh// Free Software Foundation; either version 3, or (at your option)
118870Srgrimes// any later version.
122116Sjkh
132116Sjkh// This library is distributed in the hope that it will be useful,
142116Sjkh// but WITHOUT ANY WARRANTY; without even the implied warranty of
152116Sjkh// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
162116Sjkh// GNU General Public License for more details.
178870Srgrimes
182116Sjkh// Under Section 7 of GPL version 3, you are granted additional
192116Sjkh// permissions described in the GCC Runtime Library Exception, version
202116Sjkh// 3.1, as published by the Free Software Foundation.
212116Sjkh
222116Sjkh// You should have received a copy of the GNU General Public License and
232116Sjkh// a copy of the GCC Runtime Library Exception along with this program;
242116Sjkh// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
252116Sjkh// <http://www.gnu.org/licenses/>.
262116Sjkh
272116Sjkh/** @file iomanip
282116Sjkh *  This is a Standard C++ Library header.
292116Sjkh */
308870Srgrimes
312116Sjkh//
328870Srgrimes// ISO C++ 14882: 27.6.3  Standard manipulators
332116Sjkh//
342116Sjkh
352116Sjkh#ifndef _GLIBCXX_IOMANIP
362116Sjkh#define _GLIBCXX_IOMANIP 1
372116Sjkh
382116Sjkh#pragma GCC system_header
392116Sjkh
402116Sjkh#include <bits/c++config.h>
412116Sjkh#include <iosfwd>
422116Sjkh#include <bits/ios_base.h>
432116Sjkh
442116Sjkh#ifdef __GXX_EXPERIMENTAL_CXX0X__
452116Sjkh#include <locale>
462116Sjkh#endif
472116Sjkh
482116Sjkh_GLIBCXX_BEGIN_NAMESPACE(std)
492116Sjkh
502116Sjkh  // [27.6.3] standard manipulators
512116Sjkh  // Also see DR 183.
522116Sjkh
532116Sjkh  struct _Resetiosflags { ios_base::fmtflags _M_mask; };
542116Sjkh
558870Srgrimes  /**
562116Sjkh   *  @brief  Manipulator for @c setf.
578870Srgrimes   *  @param  mask  A format flags mask.
582116Sjkh   *
592116Sjkh   *  Sent to a stream object, this manipulator resets the specified flags,
602116Sjkh   *  via @e stream.setf(0,mask).
612116Sjkh  */
622116Sjkh  inline _Resetiosflags 
632116Sjkh  resetiosflags(ios_base::fmtflags __mask)
642116Sjkh  { return { __mask }; }
652116Sjkh
662116Sjkh  template<typename _CharT, typename _Traits>
672116Sjkh    inline basic_istream<_CharT, _Traits>& 
682116Sjkh    operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
692116Sjkh    { 
702116Sjkh      __is.setf(ios_base::fmtflags(0), __f._M_mask); 
712116Sjkh      return __is; 
722116Sjkh    }
732116Sjkh
742116Sjkh  template<typename _CharT, typename _Traits>
752116Sjkh    inline basic_ostream<_CharT, _Traits>& 
762116Sjkh    operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
772116Sjkh    { 
782116Sjkh      __os.setf(ios_base::fmtflags(0), __f._M_mask); 
792116Sjkh      return __os; 
802116Sjkh    }
812116Sjkh
822116Sjkh
832116Sjkh  struct _Setiosflags { ios_base::fmtflags _M_mask; };
842116Sjkh
852116Sjkh  /**
862116Sjkh   *  @brief  Manipulator for @c setf.
872116Sjkh   *  @param  mask  A format flags mask.
882116Sjkh   *
892116Sjkh   *  Sent to a stream object, this manipulator sets the format flags
902116Sjkh   *  to @a mask.
912116Sjkh  */
922116Sjkh  inline _Setiosflags 
932116Sjkh  setiosflags(ios_base::fmtflags __mask)
942116Sjkh  { return { __mask }; }
952116Sjkh
962116Sjkh  template<typename _CharT, typename _Traits>
972116Sjkh    inline basic_istream<_CharT, _Traits>& 
982116Sjkh    operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
992116Sjkh    { 
1002116Sjkh      __is.setf(__f._M_mask); 
1012116Sjkh      return __is; 
1022116Sjkh    }
1032116Sjkh
1042116Sjkh  template<typename _CharT, typename _Traits>
1052116Sjkh    inline basic_ostream<_CharT, _Traits>& 
1062116Sjkh    operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
1072116Sjkh    { 
1082116Sjkh      __os.setf(__f._M_mask); 
1092116Sjkh      return __os; 
1102116Sjkh    }
1112116Sjkh
1122116Sjkh
1132116Sjkh  struct _Setbase { int _M_base; };
1142116Sjkh
1152116Sjkh  /**
1162116Sjkh   *  @brief  Manipulator for @c setf.
1172116Sjkh   *  @param  base  A numeric base.
1182116Sjkh   *
1192116Sjkh   *  Sent to a stream object, this manipulator changes the
1202116Sjkh   *  @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
1212116Sjkh   *  is 8, 10, or 16, accordingly, and to 0 if @a base is any other value.
1222116Sjkh  */
1232116Sjkh  inline _Setbase 
1248870Srgrimes  setbase(int __base)
1252116Sjkh  { return { __base }; }
1268870Srgrimes
1272116Sjkh  template<typename _CharT, typename _Traits>
1282116Sjkh    inline basic_istream<_CharT, _Traits>& 
1292116Sjkh    operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
1302116Sjkh    {
1312116Sjkh      __is.setf(__f._M_base ==  8 ? ios_base::oct : 
1322116Sjkh		__f._M_base == 10 ? ios_base::dec : 
1332116Sjkh		__f._M_base == 16 ? ios_base::hex : 
1342116Sjkh		ios_base::fmtflags(0), ios_base::basefield);
1352116Sjkh      return __is; 
1368870Srgrimes    }
1372116Sjkh  
1382116Sjkh  template<typename _CharT, typename _Traits>
1392116Sjkh    inline basic_ostream<_CharT, _Traits>& 
1402116Sjkh    operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
1412116Sjkh    {
1422116Sjkh      __os.setf(__f._M_base ==  8 ? ios_base::oct : 
1432116Sjkh		__f._M_base == 10 ? ios_base::dec : 
1442116Sjkh		__f._M_base == 16 ? ios_base::hex : 
1452116Sjkh		ios_base::fmtflags(0), ios_base::basefield);
1462116Sjkh      return __os; 
1472116Sjkh    }
1482116Sjkh  
1492116Sjkh
1502116Sjkh  template<typename _CharT>
1512116Sjkh    struct _Setfill { _CharT _M_c; };
1522116Sjkh
1532116Sjkh  /**
1542116Sjkh   *  @brief  Manipulator for @c fill.
1552116Sjkh   *  @param  c  The new fill character.
1562116Sjkh   *
1572116Sjkh   *  Sent to a stream object, this manipulator calls @c fill(c) for that
1582116Sjkh   *  object.
1592116Sjkh  */
1602116Sjkh  template<typename _CharT>
1612116Sjkh    inline _Setfill<_CharT>
1622116Sjkh    setfill(_CharT __c)
1632116Sjkh    { return { __c }; }
1642116Sjkh
1652116Sjkh  template<typename _CharT, typename _Traits>
1662116Sjkh    inline basic_istream<_CharT, _Traits>& 
1672116Sjkh    operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
1682116Sjkh    { 
1692116Sjkh      __is.fill(__f._M_c); 
1702116Sjkh      return __is; 
1712116Sjkh    }
1722116Sjkh
1732116Sjkh  template<typename _CharT, typename _Traits>
1742116Sjkh    inline basic_ostream<_CharT, _Traits>& 
1752116Sjkh    operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
1762116Sjkh    { 
1772116Sjkh      __os.fill(__f._M_c); 
1782116Sjkh      return __os; 
1792116Sjkh    }
1802116Sjkh
1812116Sjkh
1822116Sjkh  struct _Setprecision { int _M_n; };
1832116Sjkh
1842116Sjkh  /**
1852116Sjkh   *  @brief  Manipulator for @c precision.
1862116Sjkh   *  @param  n  The new precision.
1872116Sjkh   *
1882116Sjkh   *  Sent to a stream object, this manipulator calls @c precision(n) for
1892116Sjkh   *  that object.
1902116Sjkh  */
1912116Sjkh  inline _Setprecision 
1922116Sjkh  setprecision(int __n)
1932116Sjkh  { return { __n }; }
1942116Sjkh
1952116Sjkh  template<typename _CharT, typename _Traits>
1962116Sjkh    inline basic_istream<_CharT, _Traits>& 
1972116Sjkh    operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
1982116Sjkh    { 
1992116Sjkh      __is.precision(__f._M_n); 
2002116Sjkh      return __is; 
2012116Sjkh    }
2022116Sjkh
2032116Sjkh  template<typename _CharT, typename _Traits>
2042116Sjkh    inline basic_ostream<_CharT, _Traits>& 
2052116Sjkh    operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
2062116Sjkh    { 
2072116Sjkh      __os.precision(__f._M_n); 
2082116Sjkh      return __os; 
2092116Sjkh    }
2102116Sjkh
2112116Sjkh
2122116Sjkh  struct _Setw { int _M_n; };
2132116Sjkh
2142116Sjkh  /**
2152116Sjkh   *  @brief  Manipulator for @c width.
2162116Sjkh   *  @param  n  The new width.
2172116Sjkh   *
2182116Sjkh   *  Sent to a stream object, this manipulator calls @c width(n) for
2192116Sjkh   *  that object.
2202116Sjkh  */
2212116Sjkh  inline _Setw 
2222116Sjkh  setw(int __n)
2232116Sjkh  { return { __n }; }
2242116Sjkh
2252116Sjkh  template<typename _CharT, typename _Traits>
2262116Sjkh    inline basic_istream<_CharT, _Traits>& 
2272116Sjkh    operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
2282116Sjkh    {
2292116Sjkh      __is.width(__f._M_n);
2302116Sjkh      return __is; 
2312116Sjkh    }
2322116Sjkh
2332116Sjkh  template<typename _CharT, typename _Traits>
2342116Sjkh    inline basic_ostream<_CharT, _Traits>& 
2352116Sjkh    operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
2362116Sjkh    {
2372116Sjkh      __os.width(__f._M_n);
2382116Sjkh      return __os; 
2392116Sjkh    }
2402116Sjkh
2412116Sjkh#ifdef __GXX_EXPERIMENTAL_CXX0X__
2422116Sjkh  
2432116Sjkh  template<typename _MoneyT>
2442116Sjkh    struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
2452116Sjkh
2462116Sjkh  /**
2472116Sjkh   *  @brief  Extended manipulator for extracting money.
2482116Sjkh   *  @param  mon  Either long double or a specialization of @c basic_string.
2492116Sjkh   *  @param  intl A bool indicating whether international format 
2502116Sjkh   *               is to be used.
2512116Sjkh   *
2522116Sjkh   *  Sent to a stream object, this manipulator extracts @a mon.
2532116Sjkh  */
2542116Sjkh  template<typename _MoneyT>
2552116Sjkh    inline _Get_money<_MoneyT>
2562116Sjkh    get_money(_MoneyT& __mon, bool __intl = false)
2572116Sjkh    { return { __mon, __intl }; }
2582116Sjkh
2592116Sjkh  template<typename _CharT, typename _Traits, typename _MoneyT>
2602116Sjkh    basic_istream<_CharT, _Traits>&
2612116Sjkh    operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
2622116Sjkh    {
2632116Sjkh      typedef istreambuf_iterator<_CharT, _Traits> _Iter;
2642116Sjkh      typedef money_get<_CharT, _Iter> _MoneyGet;
2652116Sjkh      
2662116Sjkh      ios_base::iostate __err = ios_base::goodbit;
2672116Sjkh      const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
2682116Sjkh
2692116Sjkh      __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
2702116Sjkh	       __is, __err, __f._M_mon);
2712116Sjkh
2722116Sjkh      if (ios_base::goodbit != __err)
2732116Sjkh	__is.setstate(__err);
2742116Sjkh
2752116Sjkh      return __is; 
2762116Sjkh    }
2772116Sjkh
2782116Sjkh
2792116Sjkh  template<typename _MoneyT>
2802116Sjkh    struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
2812116Sjkh
2822116Sjkh  /**
2832116Sjkh   *  @brief  Extended manipulator for inserting money.
2842116Sjkh   *  @param  mon  Either long double or a specialization of @c basic_string.
2852116Sjkh   *  @param  intl A bool indicating whether international format 
2862116Sjkh   *               is to be used.
2872116Sjkh   *
2882116Sjkh   *  Sent to a stream object, this manipulator inserts @a mon.
2892116Sjkh  */
2902116Sjkh  template<typename _MoneyT>
2912116Sjkh    inline _Put_money<_MoneyT>
2922116Sjkh    put_money(const _MoneyT& __mon, bool __intl = false)
2932116Sjkh    { return { __mon, __intl }; }
2942116Sjkh
2952116Sjkh  template<typename _CharT, typename _Traits, typename _MoneyT>
2962116Sjkh    basic_ostream<_CharT, _Traits>& 
2972116Sjkh    operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
2982116Sjkh    {
2992116Sjkh      typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
3002116Sjkh      typedef money_put<_CharT, _Iter> _MoneyPut;
3012116Sjkh      
3022116Sjkh      const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
3032116Sjkh      const _Iter __end = __mp.put(_Iter(__os.rdbuf()), __f._M_intl,
3042116Sjkh				   __os, __os.fill(), __f._M_mon);
3052116Sjkh
3062116Sjkh      if (__end.failed())
3072116Sjkh	__os.setstate(ios_base::badbit);
3082116Sjkh
3092116Sjkh      return __os; 
3108870Srgrimes    }
3112116Sjkh
3122116Sjkh#endif
3132116Sjkh
3142116Sjkh  // Inhibit implicit instantiations for required instantiations,
3152116Sjkh  // which are defined via explicit instantiations elsewhere.  
3162116Sjkh  // NB:  This syntax is a GNU extension.
3172116Sjkh#if _GLIBCXX_EXTERN_TEMPLATE
3182116Sjkh  extern template ostream& operator<<(ostream&, _Setfill<char>);
3192116Sjkh  extern template ostream& operator<<(ostream&, _Setiosflags);
3202116Sjkh  extern template ostream& operator<<(ostream&, _Resetiosflags);
3212116Sjkh  extern template ostream& operator<<(ostream&, _Setbase);
3222116Sjkh  extern template ostream& operator<<(ostream&, _Setprecision);
3232116Sjkh  extern template ostream& operator<<(ostream&, _Setw);
3242116Sjkh  extern template istream& operator>>(istream&, _Setfill<char>);
3252116Sjkh  extern template istream& operator>>(istream&, _Setiosflags);
3262116Sjkh  extern template istream& operator>>(istream&, _Resetiosflags);
3272116Sjkh  extern template istream& operator>>(istream&, _Setbase);
3282116Sjkh  extern template istream& operator>>(istream&, _Setprecision);
3292116Sjkh  extern template istream& operator>>(istream&, _Setw);
3302116Sjkh
3312116Sjkh#ifdef _GLIBCXX_USE_WCHAR_T
3322116Sjkh  extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
3332116Sjkh  extern template wostream& operator<<(wostream&, _Setiosflags);
3342116Sjkh  extern template wostream& operator<<(wostream&, _Resetiosflags);
3352116Sjkh  extern template wostream& operator<<(wostream&, _Setbase);
3362116Sjkh  extern template wostream& operator<<(wostream&, _Setprecision);
3372116Sjkh  extern template wostream& operator<<(wostream&, _Setw);
3382116Sjkh  extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
3392116Sjkh  extern template wistream& operator>>(wistream&, _Setiosflags);
3402116Sjkh  extern template wistream& operator>>(wistream&, _Resetiosflags);
3412116Sjkh  extern template wistream& operator>>(wistream&, _Setbase);
3422116Sjkh  extern template wistream& operator>>(wistream&, _Setprecision);
3432116Sjkh  extern template wistream& operator>>(wistream&, _Setw);
3442116Sjkh#endif
3452116Sjkh#endif
3462116Sjkh
3472116Sjkh_GLIBCXX_END_NAMESPACE
3482116Sjkh
3492116Sjkh#endif /* _GLIBCXX_IOMANIP */
3502116Sjkh