std_iomanip.h revision 117397
197403Sobrien// Standard stream manipulators -*- C++ -*- 297403Sobrien 3169691Skan// Copyright (C) 1997, 1998, 1999, 2001, 2002, 2003 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 19169691Skan// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 31169691Skan// 32169691Skan// ISO C++ 14882: 27.6.3 Standard manipulators 33169691Skan// 34169691Skan 3597403Sobrien/** @file iomanip 3697403Sobrien * This is a Standard C++ Library header. You should @c #include this header 3797403Sobrien * in your programs, rather than any of the "st[dl]_*.h" implementation files. 3897403Sobrien */ 39132720Skan 40132720Skan#ifndef _CPP_IOMANIP 4197403Sobrien#define _CPP_IOMANIP 1 4297403Sobrien 4397403Sobrien#pragma GCC system_header 4497403Sobrien 4597403Sobrien#include <bits/c++config.h> 4697403Sobrien#include <istream> 4797403Sobrien#include <functional> 48169691Skan 49169691Skannamespace std 50117397Skan{ 51117397Skan // [27.6.3] standard manipulators 52117397Skan // Also see DR 183. 5397403Sobrien 5497403Sobrien struct _Resetiosflags { ios_base::fmtflags _M_mask; }; 55117397Skan 56117397Skan /** 57117397Skan * @brief Manipulator for @c setf. 58117397Skan * @param mask A format flags mask. 59117397Skan * 60117397Skan * Sent to a stream object, this manipulator resets the specified flags, 61117397Skan * via @e stream.setf(0,mask). 6297403Sobrien */ 6397403Sobrien inline _Resetiosflags 6497403Sobrien resetiosflags(ios_base::fmtflags __mask) 6597403Sobrien { 6697403Sobrien _Resetiosflags __x; 6797403Sobrien __x._M_mask = __mask; 6897403Sobrien return __x; 6997403Sobrien } 7097403Sobrien 7197403Sobrien template<typename _CharT, typename _Traits> 7297403Sobrien inline basic_istream<_CharT,_Traits>& 7397403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Resetiosflags __f) 7497403Sobrien { 7597403Sobrien __is.setf(ios_base::fmtflags(0), __f._M_mask); 7697403Sobrien return __is; 7797403Sobrien } 7897403Sobrien 7997403Sobrien template<typename _CharT, typename _Traits> 8097403Sobrien inline basic_ostream<_CharT,_Traits>& 8197403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Resetiosflags __f) 8297403Sobrien { 8397403Sobrien __os.setf(ios_base::fmtflags(0), __f._M_mask); 8497403Sobrien return __os; 8597403Sobrien } 8697403Sobrien 8797403Sobrien 8897403Sobrien struct _Setiosflags { ios_base::fmtflags _M_mask; }; 89117397Skan 90117397Skan /** 91117397Skan * @brief Manipulator for @c setf. 92117397Skan * @param mask A format flags mask. 93117397Skan * 94117397Skan * Sent to a stream object, this manipulator sets the format flags 95117397Skan * to @a mask. 9697403Sobrien */ 9797403Sobrien inline _Setiosflags 9897403Sobrien setiosflags(ios_base::fmtflags __mask) 9997403Sobrien { 10097403Sobrien _Setiosflags __x; 10197403Sobrien __x._M_mask = __mask; 10297403Sobrien return __x; 10397403Sobrien } 10497403Sobrien 10597403Sobrien template<typename _CharT, typename _Traits> 10697403Sobrien inline basic_istream<_CharT,_Traits>& 10797403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setiosflags __f) 10897403Sobrien { 10997403Sobrien __is.setf(__f._M_mask); 11097403Sobrien return __is; 11197403Sobrien } 11297403Sobrien 11397403Sobrien template<typename _CharT, typename _Traits> 11497403Sobrien inline basic_ostream<_CharT,_Traits>& 11597403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setiosflags __f) 11697403Sobrien { 11797403Sobrien __os.setf(__f._M_mask); 11897403Sobrien return __os; 11997403Sobrien } 12097403Sobrien 12197403Sobrien 12297403Sobrien struct _Setbase { int _M_base; }; 123117397Skan 124117397Skan /** 125117397Skan * @brief Manipulator for @c setf. 126117397Skan * @param base A numeric base. 127117397Skan * 128117397Skan * Sent to a stream object, this manipulator changes the 129117397Skan * @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base 130117397Skan * is 8, 10, or 16, accordingly, and to 0 if @a base is any other value. 13197403Sobrien */ 13297403Sobrien inline _Setbase 13397403Sobrien setbase(int __base) 13497403Sobrien { 13597403Sobrien _Setbase __x; 13697403Sobrien __x._M_base = __base; 13797403Sobrien return __x; 13897403Sobrien } 13997403Sobrien 14097403Sobrien template<typename _CharT, typename _Traits> 14197403Sobrien inline basic_istream<_CharT,_Traits>& 14297403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setbase __f) 14397403Sobrien { 14497403Sobrien __is.setf(__f._M_base == 8 ? ios_base::oct : 14597403Sobrien __f._M_base == 10 ? ios_base::dec : 14697403Sobrien __f._M_base == 16 ? ios_base::hex : 14797403Sobrien ios_base::fmtflags(0), ios_base::basefield); 14897403Sobrien return __is; 14997403Sobrien } 15097403Sobrien 15197403Sobrien template<typename _CharT, typename _Traits> 15297403Sobrien inline basic_ostream<_CharT,_Traits>& 15397403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setbase __f) 15497403Sobrien { 15597403Sobrien __os.setf(__f._M_base == 8 ? ios_base::oct : 15697403Sobrien __f._M_base == 10 ? ios_base::dec : 15797403Sobrien __f._M_base == 16 ? ios_base::hex : 15897403Sobrien ios_base::fmtflags(0), ios_base::basefield); 15997403Sobrien return __os; 16097403Sobrien } 16197403Sobrien 16297403Sobrien 16397403Sobrien template<typename _CharT> 16497403Sobrien struct _Setfill { _CharT _M_c; }; 165117397Skan 166117397Skan /** 167117397Skan * @brief Manipulator for @c fill. 168117397Skan * @param c The new fill character. 169117397Skan * 170117397Skan * Sent to a stream object, this manipulator calls @c fill(c) for that 171117397Skan * object. 17297403Sobrien */ 17397403Sobrien template<typename _CharT> 17497403Sobrien inline _Setfill<_CharT> 17597403Sobrien setfill(_CharT __c) 17697403Sobrien { 17797403Sobrien _Setfill<_CharT> __x; 17897403Sobrien __x._M_c = __c; 17997403Sobrien return __x; 18097403Sobrien } 18197403Sobrien 18297403Sobrien template<typename _CharT, typename _Traits> 18397403Sobrien inline basic_istream<_CharT,_Traits>& 18497403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setfill<_CharT> __f) 18597403Sobrien { 18697403Sobrien __is.fill(__f._M_c); 18797403Sobrien return __is; 18897403Sobrien } 18997403Sobrien 19097403Sobrien template<typename _CharT, typename _Traits> 19197403Sobrien inline basic_ostream<_CharT,_Traits>& 19297403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setfill<_CharT> __f) 19397403Sobrien { 19497403Sobrien __os.fill(__f._M_c); 19597403Sobrien return __os; 19697403Sobrien } 19797403Sobrien 19897403Sobrien 19997403Sobrien struct _Setprecision { int _M_n; }; 200117397Skan 201117397Skan /** 202117397Skan * @brief Manipulator for @c precision. 203117397Skan * @param n The new precision. 204117397Skan * 205117397Skan * Sent to a stream object, this manipulator calls @c precision(n) for 206117397Skan * that object. 20797403Sobrien */ 20897403Sobrien inline _Setprecision 20997403Sobrien setprecision(int __n) 21097403Sobrien { 21197403Sobrien _Setprecision __x; 21297403Sobrien __x._M_n = __n; 21397403Sobrien return __x; 21497403Sobrien } 21597403Sobrien 21697403Sobrien template<typename _CharT, typename _Traits> 21797403Sobrien inline basic_istream<_CharT,_Traits>& 21897403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setprecision __f) 21997403Sobrien { 22097403Sobrien __is.precision(__f._M_n); 22197403Sobrien return __is; 22297403Sobrien } 22397403Sobrien 22497403Sobrien template<typename _CharT, typename _Traits> 22597403Sobrien inline basic_ostream<_CharT,_Traits>& 22697403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setprecision __f) 22797403Sobrien { 22897403Sobrien __os.precision(__f._M_n); 22997403Sobrien return __os; 23097403Sobrien } 23197403Sobrien 23297403Sobrien 23397403Sobrien struct _Setw { int _M_n; }; 234117397Skan 235117397Skan /** 236117397Skan * @brief Manipulator for @c width. 237117397Skan * @param n The new width. 238117397Skan * 239117397Skan * Sent to a stream object, this manipulator calls @c width(n) for 240117397Skan * that object. 24197403Sobrien */ 24297403Sobrien inline _Setw 24397403Sobrien setw(int __n) 24497403Sobrien { 24597403Sobrien _Setw __x; 24697403Sobrien __x._M_n = __n; 24797403Sobrien return __x; 24897403Sobrien } 24997403Sobrien 25097403Sobrien template<typename _CharT, typename _Traits> 25197403Sobrien inline basic_istream<_CharT,_Traits>& 25297403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setw __f) 25397403Sobrien { 25497403Sobrien __is.width(__f._M_n); 25597403Sobrien return __is; 25697403Sobrien } 25797403Sobrien 25897403Sobrien template<typename _CharT, typename _Traits> 25997403Sobrien inline basic_ostream<_CharT,_Traits>& 26097403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setw __f) 26197403Sobrien { 26297403Sobrien __os.width(__f._M_n); 26397403Sobrien return __os; 26497403Sobrien } 26597403Sobrien 26697403Sobrien // Inhibit implicit instantiations for required instantiations, 26797403Sobrien // which are defined via explicit instantiations elsewhere. 268132720Skan // NB: This syntax is a GNU extension. 26997403Sobrien#if _GLIBCPP_EXTERN_TEMPLATE 27097403Sobrien extern template ostream& operator<<(ostream&, _Setfill<char>); 27197403Sobrien extern template ostream& operator<<(ostream&, _Setiosflags); 27297403Sobrien extern template ostream& operator<<(ostream&, _Resetiosflags); 27397403Sobrien extern template ostream& operator<<(ostream&, _Setbase); 27497403Sobrien extern template ostream& operator<<(ostream&, _Setprecision); 275107606Sobrien extern template ostream& operator<<(ostream&, _Setw); 276107606Sobrien extern template istream& operator>>(istream&, _Setfill<char>); 277107606Sobrien extern template istream& operator>>(istream&, _Setiosflags); 278107606Sobrien extern template istream& operator>>(istream&, _Resetiosflags); 279107606Sobrien extern template istream& operator>>(istream&, _Setbase); 280107606Sobrien extern template istream& operator>>(istream&, _Setprecision); 281107606Sobrien extern template istream& operator>>(istream&, _Setw); 282132720Skan 28397403Sobrien#ifdef _GLIBCPP_USE_WCHAR_T 28497403Sobrien extern template wostream& operator<<(wostream&, _Setfill<wchar_t>); 28597403Sobrien extern template wostream& operator<<(wostream&, _Setiosflags); 28697403Sobrien extern template wostream& operator<<(wostream&, _Resetiosflags); 28797403Sobrien extern template wostream& operator<<(wostream&, _Setbase); 28897403Sobrien extern template wostream& operator<<(wostream&, _Setprecision); 28997403Sobrien extern template wostream& operator<<(wostream&, _Setw); 29097403Sobrien extern template wistream& operator>>(wistream&, _Setfill<wchar_t>); 29197403Sobrien extern template wistream& operator>>(wistream&, _Setiosflags); 29297403Sobrien extern template wistream& operator>>(wistream&, _Resetiosflags); 29397403Sobrien extern template wistream& operator>>(wistream&, _Setbase); 29497403Sobrien extern template wistream& operator>>(wistream&, _Setprecision); 295107606Sobrien extern template wistream& operator>>(wistream&, _Setw); 296117397Skan#endif 29797403Sobrien#endif 298169691Skan} // namespace std 299169691Skan 300132720Skan#endif 301