std_iomanip.h revision 169691
197403Sobrien// Standard stream manipulators -*- C++ -*- 297403Sobrien 3117397Skan// Copyright (C) 1997, 1998, 1999, 2001, 2002, 2003, 2005 4117397Skan// Free Software Foundation, Inc. 597403Sobrien// 697403Sobrien// This file is part of the GNU ISO C++ Library. This library is free 797403Sobrien// software; you can redistribute it and/or modify it under the 897403Sobrien// terms of the GNU General Public License as published by the 997403Sobrien// Free Software Foundation; either version 2, or (at your option) 1097403Sobrien// any later version. 1197403Sobrien 1297403Sobrien// This library is distributed in the hope that it will be useful, 1397403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of 1497403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1597403Sobrien// GNU General Public License for more details. 1697403Sobrien 1797403Sobrien// You should have received a copy of the GNU General Public License along 1897403Sobrien// with this library; see the file COPYING. If not, write to the Free 1997403Sobrien// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 2097403Sobrien// USA. 2197403Sobrien 2297403Sobrien// As a special exception, you may use this file as part of a free software 2397403Sobrien// library without restriction. Specifically, if other files instantiate 2497403Sobrien// templates or use macros or inline functions from this file, or you compile 2597403Sobrien// this file and link it with other files to produce an executable, this 2697403Sobrien// file does not by itself cause the resulting executable to be covered by 2797403Sobrien// the GNU General Public License. This exception does not however 2897403Sobrien// invalidate any other reasons why the executable file might be covered by 2997403Sobrien// the GNU General Public License. 3097403Sobrien 3197403Sobrien/** @file iomanip 3297403Sobrien * This is a Standard C++ Library header. 3397403Sobrien */ 3497403Sobrien 3597403Sobrien// 3697403Sobrien// ISO C++ 14882: 27.6.3 Standard manipulators 3797403Sobrien// 3897403Sobrien 3997403Sobrien#ifndef _GLIBCXX_IOMANIP 4097403Sobrien#define _GLIBCXX_IOMANIP 1 4197403Sobrien 4297403Sobrien#pragma GCC system_header 4397403Sobrien 4497403Sobrien#include <bits/c++config.h> 4597403Sobrien#include <istream> 4697403Sobrien#include <functional> 4797403Sobrien 4897403Sobrien_GLIBCXX_BEGIN_NAMESPACE(std) 4997403Sobrien 5097403Sobrien // [27.6.3] standard manipulators 51117397Skan // Also see DR 183. 52117397Skan 53117397Skan struct _Resetiosflags { ios_base::fmtflags _M_mask; }; 5497403Sobrien 5597403Sobrien /** 56117397Skan * @brief Manipulator for @c setf. 57117397Skan * @param mask A format flags mask. 58117397Skan * 59117397Skan * Sent to a stream object, this manipulator resets the specified flags, 60117397Skan * via @e stream.setf(0,mask). 61117397Skan */ 62117397Skan inline _Resetiosflags 6397403Sobrien resetiosflags(ios_base::fmtflags __mask) 6497403Sobrien { 6597403Sobrien _Resetiosflags __x; 6697403Sobrien __x._M_mask = __mask; 6797403Sobrien return __x; 6897403Sobrien } 6997403Sobrien 7097403Sobrien template<typename _CharT, typename _Traits> 7197403Sobrien inline basic_istream<_CharT,_Traits>& 7297403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Resetiosflags __f) 7397403Sobrien { 7497403Sobrien __is.setf(ios_base::fmtflags(0), __f._M_mask); 7597403Sobrien return __is; 7697403Sobrien } 7797403Sobrien 7897403Sobrien template<typename _CharT, typename _Traits> 7997403Sobrien inline basic_ostream<_CharT,_Traits>& 8097403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Resetiosflags __f) 8197403Sobrien { 8297403Sobrien __os.setf(ios_base::fmtflags(0), __f._M_mask); 8397403Sobrien return __os; 8497403Sobrien } 8597403Sobrien 8697403Sobrien 8797403Sobrien struct _Setiosflags { ios_base::fmtflags _M_mask; }; 8897403Sobrien 8997403Sobrien /** 90117397Skan * @brief Manipulator for @c setf. 91117397Skan * @param mask A format flags mask. 92117397Skan * 93117397Skan * Sent to a stream object, this manipulator sets the format flags 94117397Skan * to @a mask. 95117397Skan */ 96117397Skan inline _Setiosflags 9797403Sobrien setiosflags(ios_base::fmtflags __mask) 9897403Sobrien { 9997403Sobrien _Setiosflags __x; 10097403Sobrien __x._M_mask = __mask; 10197403Sobrien return __x; 10297403Sobrien } 10397403Sobrien 10497403Sobrien template<typename _CharT, typename _Traits> 10597403Sobrien inline basic_istream<_CharT,_Traits>& 10697403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setiosflags __f) 10797403Sobrien { 10897403Sobrien __is.setf(__f._M_mask); 10997403Sobrien return __is; 11097403Sobrien } 11197403Sobrien 11297403Sobrien template<typename _CharT, typename _Traits> 11397403Sobrien inline basic_ostream<_CharT,_Traits>& 11497403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setiosflags __f) 11597403Sobrien { 11697403Sobrien __os.setf(__f._M_mask); 11797403Sobrien return __os; 11897403Sobrien } 11997403Sobrien 12097403Sobrien 12197403Sobrien struct _Setbase { int _M_base; }; 12297403Sobrien 12397403Sobrien /** 124117397Skan * @brief Manipulator for @c setf. 125117397Skan * @param base A numeric base. 126117397Skan * 127117397Skan * Sent to a stream object, this manipulator changes the 128117397Skan * @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base 129117397Skan * is 8, 10, or 16, accordingly, and to 0 if @a base is any other value. 130117397Skan */ 131117397Skan inline _Setbase 13297403Sobrien setbase(int __base) 13397403Sobrien { 13497403Sobrien _Setbase __x; 13597403Sobrien __x._M_base = __base; 13697403Sobrien return __x; 13797403Sobrien } 13897403Sobrien 13997403Sobrien template<typename _CharT, typename _Traits> 14097403Sobrien inline basic_istream<_CharT,_Traits>& 14197403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setbase __f) 14297403Sobrien { 14397403Sobrien __is.setf(__f._M_base == 8 ? ios_base::oct : 14497403Sobrien __f._M_base == 10 ? ios_base::dec : 14597403Sobrien __f._M_base == 16 ? ios_base::hex : 14697403Sobrien ios_base::fmtflags(0), ios_base::basefield); 14797403Sobrien return __is; 14897403Sobrien } 14997403Sobrien 15097403Sobrien template<typename _CharT, typename _Traits> 15197403Sobrien inline basic_ostream<_CharT,_Traits>& 15297403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setbase __f) 15397403Sobrien { 15497403Sobrien __os.setf(__f._M_base == 8 ? ios_base::oct : 15597403Sobrien __f._M_base == 10 ? ios_base::dec : 15697403Sobrien __f._M_base == 16 ? ios_base::hex : 15797403Sobrien ios_base::fmtflags(0), ios_base::basefield); 15897403Sobrien return __os; 15997403Sobrien } 16097403Sobrien 16197403Sobrien 16297403Sobrien template<typename _CharT> 16397403Sobrien struct _Setfill { _CharT _M_c; }; 16497403Sobrien 16597403Sobrien /** 166117397Skan * @brief Manipulator for @c fill. 167117397Skan * @param c The new fill character. 168117397Skan * 169117397Skan * Sent to a stream object, this manipulator calls @c fill(c) for that 170117397Skan * object. 171117397Skan */ 172117397Skan template<typename _CharT> 17397403Sobrien inline _Setfill<_CharT> 17497403Sobrien setfill(_CharT __c) 17597403Sobrien { 17697403Sobrien _Setfill<_CharT> __x; 17797403Sobrien __x._M_c = __c; 17897403Sobrien return __x; 17997403Sobrien } 18097403Sobrien 18197403Sobrien template<typename _CharT, typename _Traits> 18297403Sobrien inline basic_istream<_CharT,_Traits>& 18397403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setfill<_CharT> __f) 18497403Sobrien { 18597403Sobrien __is.fill(__f._M_c); 18697403Sobrien return __is; 18797403Sobrien } 18897403Sobrien 18997403Sobrien template<typename _CharT, typename _Traits> 19097403Sobrien inline basic_ostream<_CharT,_Traits>& 19197403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setfill<_CharT> __f) 19297403Sobrien { 19397403Sobrien __os.fill(__f._M_c); 19497403Sobrien return __os; 19597403Sobrien } 19697403Sobrien 19797403Sobrien 19897403Sobrien struct _Setprecision { int _M_n; }; 19997403Sobrien 20097403Sobrien /** 201117397Skan * @brief Manipulator for @c precision. 202117397Skan * @param n The new precision. 203117397Skan * 204117397Skan * Sent to a stream object, this manipulator calls @c precision(n) for 205117397Skan * that object. 206117397Skan */ 207117397Skan inline _Setprecision 20897403Sobrien setprecision(int __n) 20997403Sobrien { 21097403Sobrien _Setprecision __x; 21197403Sobrien __x._M_n = __n; 21297403Sobrien return __x; 21397403Sobrien } 21497403Sobrien 21597403Sobrien template<typename _CharT, typename _Traits> 21697403Sobrien inline basic_istream<_CharT,_Traits>& 21797403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setprecision __f) 21897403Sobrien { 21997403Sobrien __is.precision(__f._M_n); 22097403Sobrien return __is; 22197403Sobrien } 22297403Sobrien 22397403Sobrien template<typename _CharT, typename _Traits> 22497403Sobrien inline basic_ostream<_CharT,_Traits>& 22597403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setprecision __f) 22697403Sobrien { 22797403Sobrien __os.precision(__f._M_n); 22897403Sobrien return __os; 22997403Sobrien } 23097403Sobrien 23197403Sobrien 23297403Sobrien struct _Setw { int _M_n; }; 23397403Sobrien 23497403Sobrien /** 235117397Skan * @brief Manipulator for @c width. 236117397Skan * @param n The new width. 237117397Skan * 238117397Skan * Sent to a stream object, this manipulator calls @c width(n) for 239117397Skan * that object. 240117397Skan */ 241117397Skan inline _Setw 24297403Sobrien setw(int __n) 24397403Sobrien { 24497403Sobrien _Setw __x; 24597403Sobrien __x._M_n = __n; 24697403Sobrien return __x; 24797403Sobrien } 24897403Sobrien 24997403Sobrien template<typename _CharT, typename _Traits> 25097403Sobrien inline basic_istream<_CharT,_Traits>& 25197403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setw __f) 25297403Sobrien { 25397403Sobrien __is.width(__f._M_n); 25497403Sobrien return __is; 25597403Sobrien } 25697403Sobrien 25797403Sobrien template<typename _CharT, typename _Traits> 25897403Sobrien inline basic_ostream<_CharT,_Traits>& 25997403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setw __f) 26097403Sobrien { 26197403Sobrien __os.width(__f._M_n); 26297403Sobrien return __os; 26397403Sobrien } 26497403Sobrien 26597403Sobrien // Inhibit implicit instantiations for required instantiations, 26697403Sobrien // which are defined via explicit instantiations elsewhere. 26797403Sobrien // NB: This syntax is a GNU extension. 26897403Sobrien#if _GLIBCXX_EXTERN_TEMPLATE 269117397Skan extern template ostream& operator<<(ostream&, _Setfill<char>); 27097403Sobrien extern template ostream& operator<<(ostream&, _Setiosflags); 27197403Sobrien extern template ostream& operator<<(ostream&, _Resetiosflags); 27297403Sobrien extern template ostream& operator<<(ostream&, _Setbase); 27397403Sobrien extern template ostream& operator<<(ostream&, _Setprecision); 27497403Sobrien extern template ostream& operator<<(ostream&, _Setw); 27597403Sobrien extern template istream& operator>>(istream&, _Setfill<char>); 276107606Sobrien extern template istream& operator>>(istream&, _Setiosflags); 277107606Sobrien extern template istream& operator>>(istream&, _Resetiosflags); 278107606Sobrien extern template istream& operator>>(istream&, _Setbase); 279107606Sobrien extern template istream& operator>>(istream&, _Setprecision); 280107606Sobrien extern template istream& operator>>(istream&, _Setw); 281107606Sobrien 282107606Sobrien#ifdef _GLIBCXX_USE_WCHAR_T 283107606Sobrien extern template wostream& operator<<(wostream&, _Setfill<wchar_t>); 28497403Sobrien extern template wostream& operator<<(wostream&, _Setiosflags); 28597403Sobrien extern template wostream& operator<<(wostream&, _Resetiosflags); 28697403Sobrien extern template wostream& operator<<(wostream&, _Setbase); 28797403Sobrien extern template wostream& operator<<(wostream&, _Setprecision); 28897403Sobrien extern template wostream& operator<<(wostream&, _Setw); 28997403Sobrien extern template wistream& operator>>(wistream&, _Setfill<wchar_t>); 29097403Sobrien extern template wistream& operator>>(wistream&, _Setiosflags); 29197403Sobrien extern template wistream& operator>>(wistream&, _Resetiosflags); 29297403Sobrien extern template wistream& operator>>(wistream&, _Setbase); 29397403Sobrien extern template wistream& operator>>(wistream&, _Setprecision); 29497403Sobrien extern template wistream& operator>>(wistream&, _Setw); 29597403Sobrien#endif 296107606Sobrien#endif 297117397Skan 29897403Sobrien_GLIBCXX_END_NAMESPACE 29997403Sobrien 30097403Sobrien#endif /* _GLIBCXX_IOMANIP */ 301