1// Standard stream manipulators -*- C++ -*- 2 3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005 4// 2006, 2007, 2008, 2009, 2010 5// Free Software Foundation, Inc. 6// 7// This file is part of the GNU ISO C++ Library. This library is free 8// software; you can redistribute it and/or modify it under the 9// terms of the GNU General Public License as published by the 10// Free Software Foundation; either version 3, or (at your option) 11// any later version. 12 13// This library is distributed in the hope that it will be useful, 14// but WITHOUT ANY WARRANTY; without even the implied warranty of 15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16// GNU General Public License for more details. 17 18// Under Section 7 of GPL version 3, you are granted additional 19// permissions described in the GCC Runtime Library Exception, version 20// 3.1, as published by the Free Software Foundation. 21 22// You should have received a copy of the GNU General Public License and 23// a copy of the GCC Runtime Library Exception along with this program; 24// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 25// <http://www.gnu.org/licenses/>. 26 27/** @file iomanip 28 * This is a Standard C++ Library header. 29 */ 30 31// 32// ISO C++ 14882: 27.6.3 Standard manipulators 33// 34 35#ifndef _GLIBCXX_IOMANIP 36#define _GLIBCXX_IOMANIP 1 37 38#pragma GCC system_header 39 40#include <bits/c++config.h> 41#include <iosfwd> 42#include <bits/ios_base.h> 43 44#ifdef __GXX_EXPERIMENTAL_CXX0X__ 45#include <locale> 46#endif 47 48_GLIBCXX_BEGIN_NAMESPACE(std) 49 50 // [27.6.3] standard manipulators 51 // Also see DR 183. 52 53 struct _Resetiosflags { ios_base::fmtflags _M_mask; }; 54 55 /** 56 * @brief Manipulator for @c setf. 57 * @param mask A format flags mask. 58 * 59 * Sent to a stream object, this manipulator resets the specified flags, 60 * via @e stream.setf(0,mask). 61 */ 62 inline _Resetiosflags 63 resetiosflags(ios_base::fmtflags __mask) 64 { return { __mask }; } 65 66 template<typename _CharT, typename _Traits> 67 inline basic_istream<_CharT, _Traits>& 68 operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f) 69 { 70 __is.setf(ios_base::fmtflags(0), __f._M_mask); 71 return __is; 72 } 73 74 template<typename _CharT, typename _Traits> 75 inline basic_ostream<_CharT, _Traits>& 76 operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f) 77 { 78 __os.setf(ios_base::fmtflags(0), __f._M_mask); 79 return __os; 80 } 81 82 83 struct _Setiosflags { ios_base::fmtflags _M_mask; }; 84 85 /** 86 * @brief Manipulator for @c setf. 87 * @param mask A format flags mask. 88 * 89 * Sent to a stream object, this manipulator sets the format flags 90 * to @a mask. 91 */ 92 inline _Setiosflags 93 setiosflags(ios_base::fmtflags __mask) 94 { return { __mask }; } 95 96 template<typename _CharT, typename _Traits> 97 inline basic_istream<_CharT, _Traits>& 98 operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f) 99 { 100 __is.setf(__f._M_mask); 101 return __is; 102 } 103 104 template<typename _CharT, typename _Traits> 105 inline basic_ostream<_CharT, _Traits>& 106 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f) 107 { 108 __os.setf(__f._M_mask); 109 return __os; 110 } 111 112 113 struct _Setbase { int _M_base; }; 114 115 /** 116 * @brief Manipulator for @c setf. 117 * @param base A numeric base. 118 * 119 * Sent to a stream object, this manipulator changes the 120 * @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base 121 * is 8, 10, or 16, accordingly, and to 0 if @a base is any other value. 122 */ 123 inline _Setbase 124 setbase(int __base) 125 { return { __base }; } 126 127 template<typename _CharT, typename _Traits> 128 inline basic_istream<_CharT, _Traits>& 129 operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f) 130 { 131 __is.setf(__f._M_base == 8 ? ios_base::oct : 132 __f._M_base == 10 ? ios_base::dec : 133 __f._M_base == 16 ? ios_base::hex : 134 ios_base::fmtflags(0), ios_base::basefield); 135 return __is; 136 } 137 138 template<typename _CharT, typename _Traits> 139 inline basic_ostream<_CharT, _Traits>& 140 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f) 141 { 142 __os.setf(__f._M_base == 8 ? ios_base::oct : 143 __f._M_base == 10 ? ios_base::dec : 144 __f._M_base == 16 ? ios_base::hex : 145 ios_base::fmtflags(0), ios_base::basefield); 146 return __os; 147 } 148 149 150 template<typename _CharT> 151 struct _Setfill { _CharT _M_c; }; 152 153 /** 154 * @brief Manipulator for @c fill. 155 * @param c The new fill character. 156 * 157 * Sent to a stream object, this manipulator calls @c fill(c) for that 158 * object. 159 */ 160 template<typename _CharT> 161 inline _Setfill<_CharT> 162 setfill(_CharT __c) 163 { return { __c }; } 164 165 template<typename _CharT, typename _Traits> 166 inline basic_istream<_CharT, _Traits>& 167 operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f) 168 { 169 __is.fill(__f._M_c); 170 return __is; 171 } 172 173 template<typename _CharT, typename _Traits> 174 inline basic_ostream<_CharT, _Traits>& 175 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f) 176 { 177 __os.fill(__f._M_c); 178 return __os; 179 } 180 181 182 struct _Setprecision { int _M_n; }; 183 184 /** 185 * @brief Manipulator for @c precision. 186 * @param n The new precision. 187 * 188 * Sent to a stream object, this manipulator calls @c precision(n) for 189 * that object. 190 */ 191 inline _Setprecision 192 setprecision(int __n) 193 { return { __n }; } 194 195 template<typename _CharT, typename _Traits> 196 inline basic_istream<_CharT, _Traits>& 197 operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f) 198 { 199 __is.precision(__f._M_n); 200 return __is; 201 } 202 203 template<typename _CharT, typename _Traits> 204 inline basic_ostream<_CharT, _Traits>& 205 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f) 206 { 207 __os.precision(__f._M_n); 208 return __os; 209 } 210 211 212 struct _Setw { int _M_n; }; 213 214 /** 215 * @brief Manipulator for @c width. 216 * @param n The new width. 217 * 218 * Sent to a stream object, this manipulator calls @c width(n) for 219 * that object. 220 */ 221 inline _Setw 222 setw(int __n) 223 { return { __n }; } 224 225 template<typename _CharT, typename _Traits> 226 inline basic_istream<_CharT, _Traits>& 227 operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f) 228 { 229 __is.width(__f._M_n); 230 return __is; 231 } 232 233 template<typename _CharT, typename _Traits> 234 inline basic_ostream<_CharT, _Traits>& 235 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f) 236 { 237 __os.width(__f._M_n); 238 return __os; 239 } 240 241#ifdef __GXX_EXPERIMENTAL_CXX0X__ 242 243 template<typename _MoneyT> 244 struct _Get_money { _MoneyT& _M_mon; bool _M_intl; }; 245 246 /** 247 * @brief Extended manipulator for extracting money. 248 * @param mon Either long double or a specialization of @c basic_string. 249 * @param intl A bool indicating whether international format 250 * is to be used. 251 * 252 * Sent to a stream object, this manipulator extracts @a mon. 253 */ 254 template<typename _MoneyT> 255 inline _Get_money<_MoneyT> 256 get_money(_MoneyT& __mon, bool __intl = false) 257 { return { __mon, __intl }; } 258 259 template<typename _CharT, typename _Traits, typename _MoneyT> 260 basic_istream<_CharT, _Traits>& 261 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f) 262 { 263 typedef istreambuf_iterator<_CharT, _Traits> _Iter; 264 typedef money_get<_CharT, _Iter> _MoneyGet; 265 266 ios_base::iostate __err = ios_base::goodbit; 267 const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc()); 268 269 __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl, 270 __is, __err, __f._M_mon); 271 272 if (ios_base::goodbit != __err) 273 __is.setstate(__err); 274 275 return __is; 276 } 277 278 279 template<typename _MoneyT> 280 struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; }; 281 282 /** 283 * @brief Extended manipulator for inserting money. 284 * @param mon Either long double or a specialization of @c basic_string. 285 * @param intl A bool indicating whether international format 286 * is to be used. 287 * 288 * Sent to a stream object, this manipulator inserts @a mon. 289 */ 290 template<typename _MoneyT> 291 inline _Put_money<_MoneyT> 292 put_money(const _MoneyT& __mon, bool __intl = false) 293 { return { __mon, __intl }; } 294 295 template<typename _CharT, typename _Traits, typename _MoneyT> 296 basic_ostream<_CharT, _Traits>& 297 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f) 298 { 299 typedef ostreambuf_iterator<_CharT, _Traits> _Iter; 300 typedef money_put<_CharT, _Iter> _MoneyPut; 301 302 const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc()); 303 const _Iter __end = __mp.put(_Iter(__os.rdbuf()), __f._M_intl, 304 __os, __os.fill(), __f._M_mon); 305 306 if (__end.failed()) 307 __os.setstate(ios_base::badbit); 308 309 return __os; 310 } 311 312#endif 313 314 // Inhibit implicit instantiations for required instantiations, 315 // which are defined via explicit instantiations elsewhere. 316 // NB: This syntax is a GNU extension. 317#if _GLIBCXX_EXTERN_TEMPLATE 318 extern template ostream& operator<<(ostream&, _Setfill<char>); 319 extern template ostream& operator<<(ostream&, _Setiosflags); 320 extern template ostream& operator<<(ostream&, _Resetiosflags); 321 extern template ostream& operator<<(ostream&, _Setbase); 322 extern template ostream& operator<<(ostream&, _Setprecision); 323 extern template ostream& operator<<(ostream&, _Setw); 324 extern template istream& operator>>(istream&, _Setfill<char>); 325 extern template istream& operator>>(istream&, _Setiosflags); 326 extern template istream& operator>>(istream&, _Resetiosflags); 327 extern template istream& operator>>(istream&, _Setbase); 328 extern template istream& operator>>(istream&, _Setprecision); 329 extern template istream& operator>>(istream&, _Setw); 330 331#ifdef _GLIBCXX_USE_WCHAR_T 332 extern template wostream& operator<<(wostream&, _Setfill<wchar_t>); 333 extern template wostream& operator<<(wostream&, _Setiosflags); 334 extern template wostream& operator<<(wostream&, _Resetiosflags); 335 extern template wostream& operator<<(wostream&, _Setbase); 336 extern template wostream& operator<<(wostream&, _Setprecision); 337 extern template wostream& operator<<(wostream&, _Setw); 338 extern template wistream& operator>>(wistream&, _Setfill<wchar_t>); 339 extern template wistream& operator>>(wistream&, _Setiosflags); 340 extern template wistream& operator>>(wistream&, _Resetiosflags); 341 extern template wistream& operator>>(wistream&, _Setbase); 342 extern template wistream& operator>>(wistream&, _Setprecision); 343 extern template wistream& operator>>(wistream&, _Setw); 344#endif 345#endif 346 347_GLIBCXX_END_NAMESPACE 348 349#endif /* _GLIBCXX_IOMANIP */ 350