std_iomanip.h revision 107606
197403Sobrien// Standard stream manipulators -*- C++ -*- 297403Sobrien 397403Sobrien// Copyright (C) 1997, 1998, 1999, 2001, 2002 Free Software Foundation, Inc. 497403Sobrien// 597403Sobrien// This file is part of the GNU ISO C++ Library. This library is free 697403Sobrien// software; you can redistribute it and/or modify it under the 797403Sobrien// terms of the GNU General Public License as published by the 897403Sobrien// Free Software Foundation; either version 2, or (at your option) 997403Sobrien// any later version. 1097403Sobrien 1197403Sobrien// This library is distributed in the hope that it will be useful, 1297403Sobrien// but WITHOUT ANY WARRANTY; without even the implied warranty of 1397403Sobrien// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1497403Sobrien// GNU General Public License for more details. 1597403Sobrien 1697403Sobrien// You should have received a copy of the GNU General Public License along 1797403Sobrien// with this library; see the file COPYING. If not, write to the Free 1897403Sobrien// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 1997403Sobrien// USA. 2097403Sobrien 2197403Sobrien// As a special exception, you may use this file as part of a free software 2297403Sobrien// library without restriction. Specifically, if other files instantiate 2397403Sobrien// templates or use macros or inline functions from this file, or you compile 2497403Sobrien// this file and link it with other files to produce an executable, this 2597403Sobrien// file does not by itself cause the resulting executable to be covered by 2697403Sobrien// the GNU General Public License. This exception does not however 2797403Sobrien// invalidate any other reasons why the executable file might be covered by 2897403Sobrien// the GNU General Public License. 2997403Sobrien 3097403Sobrien// 3197403Sobrien// ISO C++ 14882: 27.6.3 Standard manipulators 3297403Sobrien// 3397403Sobrien 3497403Sobrien/** @file iomanip 3597403Sobrien * This is a Standard C++ Library header. You should @c #include this header 3697403Sobrien * in your programs, rather than any of the "st[dl]_*.h" implementation files. 3797403Sobrien */ 3897403Sobrien 3997403Sobrien#ifndef _CPP_IOMANIP 4097403Sobrien#define _CPP_IOMANIP 1 4197403Sobrien 4297403Sobrien#pragma GCC system_header 4397403Sobrien 4497403Sobrien#include <bits/c++config.h> 4597403Sobrien#include <istream> 4697403Sobrien#include <functional> 4797403Sobrien 4897403Sobriennamespace std 4997403Sobrien{ 5097403Sobrien struct _Resetiosflags { ios_base::fmtflags _M_mask; }; 5197403Sobrien 5297403Sobrien inline _Resetiosflags 5397403Sobrien resetiosflags(ios_base::fmtflags __mask) 5497403Sobrien { 5597403Sobrien _Resetiosflags __x; 5697403Sobrien __x._M_mask = __mask; 5797403Sobrien return __x; 5897403Sobrien } 5997403Sobrien 6097403Sobrien template<typename _CharT, typename _Traits> 6197403Sobrien inline basic_istream<_CharT,_Traits>& 6297403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Resetiosflags __f) 6397403Sobrien { 6497403Sobrien __is.setf(ios_base::fmtflags(0), __f._M_mask); 6597403Sobrien return __is; 6697403Sobrien } 6797403Sobrien 6897403Sobrien template<typename _CharT, typename _Traits> 6997403Sobrien inline basic_ostream<_CharT,_Traits>& 7097403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Resetiosflags __f) 7197403Sobrien { 7297403Sobrien __os.setf(ios_base::fmtflags(0), __f._M_mask); 7397403Sobrien return __os; 7497403Sobrien } 7597403Sobrien 7697403Sobrien 7797403Sobrien struct _Setiosflags { ios_base::fmtflags _M_mask; }; 7897403Sobrien 7997403Sobrien inline _Setiosflags 8097403Sobrien setiosflags(ios_base::fmtflags __mask) 8197403Sobrien { 8297403Sobrien _Setiosflags __x; 8397403Sobrien __x._M_mask = __mask; 8497403Sobrien return __x; 8597403Sobrien } 8697403Sobrien 8797403Sobrien template<typename _CharT, typename _Traits> 8897403Sobrien inline basic_istream<_CharT,_Traits>& 8997403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setiosflags __f) 9097403Sobrien { 9197403Sobrien __is.setf(__f._M_mask); 9297403Sobrien return __is; 9397403Sobrien } 9497403Sobrien 9597403Sobrien template<typename _CharT, typename _Traits> 9697403Sobrien inline basic_ostream<_CharT,_Traits>& 9797403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setiosflags __f) 9897403Sobrien { 9997403Sobrien __os.setf(__f._M_mask); 10097403Sobrien return __os; 10197403Sobrien } 10297403Sobrien 10397403Sobrien 10497403Sobrien struct _Setbase { int _M_base; }; 10597403Sobrien 10697403Sobrien inline _Setbase 10797403Sobrien setbase(int __base) 10897403Sobrien { 10997403Sobrien _Setbase __x; 11097403Sobrien __x._M_base = __base; 11197403Sobrien return __x; 11297403Sobrien } 11397403Sobrien 11497403Sobrien template<typename _CharT, typename _Traits> 11597403Sobrien inline basic_istream<_CharT,_Traits>& 11697403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setbase __f) 11797403Sobrien { 11897403Sobrien __is.setf(__f._M_base == 8 ? ios_base::oct : 11997403Sobrien __f._M_base == 10 ? ios_base::dec : 12097403Sobrien __f._M_base == 16 ? ios_base::hex : 12197403Sobrien ios_base::fmtflags(0), ios_base::basefield); 12297403Sobrien return __is; 12397403Sobrien } 12497403Sobrien 12597403Sobrien template<typename _CharT, typename _Traits> 12697403Sobrien inline basic_ostream<_CharT,_Traits>& 12797403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setbase __f) 12897403Sobrien { 12997403Sobrien __os.setf(__f._M_base == 8 ? ios_base::oct : 13097403Sobrien __f._M_base == 10 ? ios_base::dec : 13197403Sobrien __f._M_base == 16 ? ios_base::hex : 13297403Sobrien ios_base::fmtflags(0), ios_base::basefield); 13397403Sobrien return __os; 13497403Sobrien } 13597403Sobrien 13697403Sobrien 13797403Sobrien template<typename _CharT> 13897403Sobrien struct _Setfill { _CharT _M_c; }; 13997403Sobrien 14097403Sobrien template<typename _CharT> 14197403Sobrien inline _Setfill<_CharT> 14297403Sobrien setfill(_CharT __c) 14397403Sobrien { 14497403Sobrien _Setfill<_CharT> __x; 14597403Sobrien __x._M_c = __c; 14697403Sobrien return __x; 14797403Sobrien } 14897403Sobrien 14997403Sobrien template<typename _CharT, typename _Traits> 15097403Sobrien inline basic_istream<_CharT,_Traits>& 15197403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setfill<_CharT> __f) 15297403Sobrien { 15397403Sobrien __is.fill(__f._M_c); 15497403Sobrien return __is; 15597403Sobrien } 15697403Sobrien 15797403Sobrien template<typename _CharT, typename _Traits> 15897403Sobrien inline basic_ostream<_CharT,_Traits>& 15997403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setfill<_CharT> __f) 16097403Sobrien { 16197403Sobrien __os.fill(__f._M_c); 16297403Sobrien return __os; 16397403Sobrien } 16497403Sobrien 16597403Sobrien 16697403Sobrien struct _Setprecision { int _M_n; }; 16797403Sobrien 16897403Sobrien inline _Setprecision 16997403Sobrien setprecision(int __n) 17097403Sobrien { 17197403Sobrien _Setprecision __x; 17297403Sobrien __x._M_n = __n; 17397403Sobrien return __x; 17497403Sobrien } 17597403Sobrien 17697403Sobrien template<typename _CharT, typename _Traits> 17797403Sobrien inline basic_istream<_CharT,_Traits>& 17897403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setprecision __f) 17997403Sobrien { 18097403Sobrien __is.precision(__f._M_n); 18197403Sobrien return __is; 18297403Sobrien } 18397403Sobrien 18497403Sobrien template<typename _CharT, typename _Traits> 18597403Sobrien inline basic_ostream<_CharT,_Traits>& 18697403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setprecision __f) 18797403Sobrien { 18897403Sobrien __os.precision(__f._M_n); 18997403Sobrien return __os; 19097403Sobrien } 19197403Sobrien 19297403Sobrien 19397403Sobrien struct _Setw { int _M_n; }; 19497403Sobrien 19597403Sobrien inline _Setw 19697403Sobrien setw(int __n) 19797403Sobrien { 19897403Sobrien _Setw __x; 19997403Sobrien __x._M_n = __n; 20097403Sobrien return __x; 20197403Sobrien } 20297403Sobrien 20397403Sobrien template<typename _CharT, typename _Traits> 20497403Sobrien inline basic_istream<_CharT,_Traits>& 20597403Sobrien operator>>(basic_istream<_CharT,_Traits>& __is, _Setw __f) 20697403Sobrien { 20797403Sobrien __is.width(__f._M_n); 20897403Sobrien return __is; 20997403Sobrien } 21097403Sobrien 21197403Sobrien template<typename _CharT, typename _Traits> 21297403Sobrien inline basic_ostream<_CharT,_Traits>& 21397403Sobrien operator<<(basic_ostream<_CharT,_Traits>& __os, _Setw __f) 21497403Sobrien { 21597403Sobrien __os.width(__f._M_n); 21697403Sobrien return __os; 21797403Sobrien } 21897403Sobrien 21997403Sobrien // Inhibit implicit instantiations for required instantiations, 22097403Sobrien // which are defined via explicit instantiations elsewhere. 22197403Sobrien // NB: This syntax is a GNU extension. 22297403Sobrien extern template ostream& operator<<(ostream&, _Setfill<char>); 22397403Sobrien extern template ostream& operator<<(ostream&, _Setiosflags); 22497403Sobrien extern template ostream& operator<<(ostream&, _Resetiosflags); 22597403Sobrien extern template ostream& operator<<(ostream&, _Setbase); 22697403Sobrien extern template ostream& operator<<(ostream&, _Setprecision); 22797403Sobrien extern template ostream& operator<<(ostream&, _Setw); 228107606Sobrien extern template istream& operator>>(istream&, _Setfill<char>); 229107606Sobrien extern template istream& operator>>(istream&, _Setiosflags); 230107606Sobrien extern template istream& operator>>(istream&, _Resetiosflags); 231107606Sobrien extern template istream& operator>>(istream&, _Setbase); 232107606Sobrien extern template istream& operator>>(istream&, _Setprecision); 233107606Sobrien extern template istream& operator>>(istream&, _Setw); 234107606Sobrien 235107606Sobrien#ifdef _GLIBCPP_USE_WCHAR_T 23697403Sobrien extern template wostream& operator<<(wostream&, _Setfill<wchar_t>); 23797403Sobrien extern template wostream& operator<<(wostream&, _Setiosflags); 23897403Sobrien extern template wostream& operator<<(wostream&, _Resetiosflags); 23997403Sobrien extern template wostream& operator<<(wostream&, _Setbase); 24097403Sobrien extern template wostream& operator<<(wostream&, _Setprecision); 24197403Sobrien extern template wostream& operator<<(wostream&, _Setw); 24297403Sobrien extern template wistream& operator>>(wistream&, _Setfill<wchar_t>); 24397403Sobrien extern template wistream& operator>>(wistream&, _Setiosflags); 24497403Sobrien extern template wistream& operator>>(wistream&, _Resetiosflags); 24597403Sobrien extern template wistream& operator>>(wistream&, _Setbase); 24697403Sobrien extern template wistream& operator>>(wistream&, _Setprecision); 24797403Sobrien extern template wistream& operator>>(wistream&, _Setw); 248107606Sobrien#endif 24997403Sobrien} // namespace std 25097403Sobrien 25197403Sobrien#endif 252