1 // Standard stream manipulators -*- C++ -*-
3 // Copyright (C) 1997-2013 Free Software Foundation, Inc.
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
25 /** @file include/iomanip
26 * This is a Standard C++ Library header.
30 // ISO C++ 14882: 27.6.3 Standard manipulators
33 #ifndef _GLIBCXX_IOMANIP
34 #define _GLIBCXX_IOMANIP 1
36 #pragma GCC system_header
38 #include <bits/c++config.h>
40 #include <bits/ios_base.h>
42 #if __cplusplus >= 201103L
46 namespace std _GLIBCXX_VISIBILITY(default)
48 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 // [27.6.3] standard manipulators
53 struct _Resetiosflags { ios_base::fmtflags _M_mask; };
56 * @brief Manipulator for @c setf.
57 * @param __mask A format flags mask.
59 * Sent to a stream object, this manipulator resets the specified flags,
60 * via @e stream.setf(0,__mask).
63 resetiosflags(ios_base::fmtflags __mask)
64 { return { __mask }; }
66 template<typename _CharT, typename _Traits>
67 inline basic_istream<_CharT, _Traits>&
68 operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
70 __is.setf(ios_base::fmtflags(0), __f._M_mask);
74 template<typename _CharT, typename _Traits>
75 inline basic_ostream<_CharT, _Traits>&
76 operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
78 __os.setf(ios_base::fmtflags(0), __f._M_mask);
83 struct _Setiosflags { ios_base::fmtflags _M_mask; };
86 * @brief Manipulator for @c setf.
87 * @param __mask A format flags mask.
89 * Sent to a stream object, this manipulator sets the format flags
93 setiosflags(ios_base::fmtflags __mask)
94 { return { __mask }; }
96 template<typename _CharT, typename _Traits>
97 inline basic_istream<_CharT, _Traits>&
98 operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
100 __is.setf(__f._M_mask);
104 template<typename _CharT, typename _Traits>
105 inline basic_ostream<_CharT, _Traits>&
106 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
108 __os.setf(__f._M_mask);
113 struct _Setbase { int _M_base; };
116 * @brief Manipulator for @c setf.
117 * @param __base A numeric base.
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.
125 { return { __base }; }
127 template<typename _CharT, typename _Traits>
128 inline basic_istream<_CharT, _Traits>&
129 operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
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);
138 template<typename _CharT, typename _Traits>
139 inline basic_ostream<_CharT, _Traits>&
140 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
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);
150 template<typename _CharT>
151 struct _Setfill { _CharT _M_c; };
154 * @brief Manipulator for @c fill.
155 * @param __c The new fill character.
157 * Sent to a stream object, this manipulator calls @c fill(__c) for that
160 template<typename _CharT>
161 inline _Setfill<_CharT>
165 template<typename _CharT, typename _Traits>
166 inline basic_istream<_CharT, _Traits>&
167 operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
173 template<typename _CharT, typename _Traits>
174 inline basic_ostream<_CharT, _Traits>&
175 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
182 struct _Setprecision { int _M_n; };
185 * @brief Manipulator for @c precision.
186 * @param __n The new precision.
188 * Sent to a stream object, this manipulator calls @c precision(__n) for
192 setprecision(int __n)
195 template<typename _CharT, typename _Traits>
196 inline basic_istream<_CharT, _Traits>&
197 operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
199 __is.precision(__f._M_n);
203 template<typename _CharT, typename _Traits>
204 inline basic_ostream<_CharT, _Traits>&
205 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
207 __os.precision(__f._M_n);
212 struct _Setw { int _M_n; };
215 * @brief Manipulator for @c width.
216 * @param __n The new width.
218 * Sent to a stream object, this manipulator calls @c width(__n) for
225 template<typename _CharT, typename _Traits>
226 inline basic_istream<_CharT, _Traits>&
227 operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
229 __is.width(__f._M_n);
233 template<typename _CharT, typename _Traits>
234 inline basic_ostream<_CharT, _Traits>&
235 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
237 __os.width(__f._M_n);
241 #if __cplusplus >= 201103L
243 template<typename _MoneyT>
244 struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
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
252 * Sent to a stream object, this manipulator extracts @a __mon.
254 template<typename _MoneyT>
255 inline _Get_money<_MoneyT>
256 get_money(_MoneyT& __mon, bool __intl = false)
257 { return { __mon, __intl }; }
259 template<typename _CharT, typename _Traits, typename _MoneyT>
260 basic_istream<_CharT, _Traits>&
261 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
263 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
266 ios_base::iostate __err = ios_base::goodbit;
269 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
270 typedef money_get<_CharT, _Iter> _MoneyGet;
272 const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
273 __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
274 __is, __err, __f._M_mon);
276 __catch(__cxxabiv1::__forced_unwind&)
278 __is._M_setstate(ios_base::badbit);
279 __throw_exception_again;
282 { __is._M_setstate(ios_base::badbit); }
284 __is.setstate(__err);
290 template<typename _MoneyT>
291 struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
294 * @brief Extended manipulator for inserting money.
295 * @param __mon Either long double or a specialization of @c basic_string.
296 * @param __intl A bool indicating whether international format
299 * Sent to a stream object, this manipulator inserts @a __mon.
301 template<typename _MoneyT>
302 inline _Put_money<_MoneyT>
303 put_money(const _MoneyT& __mon, bool __intl = false)
304 { return { __mon, __intl }; }
306 template<typename _CharT, typename _Traits, typename _MoneyT>
307 basic_ostream<_CharT, _Traits>&
308 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
310 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
313 ios_base::iostate __err = ios_base::goodbit;
316 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
317 typedef money_put<_CharT, _Iter> _MoneyPut;
319 const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
320 if (__mp.put(_Iter(__os.rdbuf()), __f._M_intl, __os,
321 __os.fill(), __f._M_mon).failed())
322 __err |= ios_base::badbit;
324 __catch(__cxxabiv1::__forced_unwind&)
326 __os._M_setstate(ios_base::badbit);
327 __throw_exception_again;
330 { __os._M_setstate(ios_base::badbit); }
332 __os.setstate(__err);
339 // Inhibit implicit instantiations for required instantiations,
340 // which are defined via explicit instantiations elsewhere.
341 // NB: This syntax is a GNU extension.
342 #if _GLIBCXX_EXTERN_TEMPLATE
343 extern template ostream& operator<<(ostream&, _Setfill<char>);
344 extern template ostream& operator<<(ostream&, _Setiosflags);
345 extern template ostream& operator<<(ostream&, _Resetiosflags);
346 extern template ostream& operator<<(ostream&, _Setbase);
347 extern template ostream& operator<<(ostream&, _Setprecision);
348 extern template ostream& operator<<(ostream&, _Setw);
349 extern template istream& operator>>(istream&, _Setfill<char>);
350 extern template istream& operator>>(istream&, _Setiosflags);
351 extern template istream& operator>>(istream&, _Resetiosflags);
352 extern template istream& operator>>(istream&, _Setbase);
353 extern template istream& operator>>(istream&, _Setprecision);
354 extern template istream& operator>>(istream&, _Setw);
356 #ifdef _GLIBCXX_USE_WCHAR_T
357 extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
358 extern template wostream& operator<<(wostream&, _Setiosflags);
359 extern template wostream& operator<<(wostream&, _Resetiosflags);
360 extern template wostream& operator<<(wostream&, _Setbase);
361 extern template wostream& operator<<(wostream&, _Setprecision);
362 extern template wostream& operator<<(wostream&, _Setw);
363 extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
364 extern template wistream& operator>>(wistream&, _Setiosflags);
365 extern template wistream& operator>>(wistream&, _Resetiosflags);
366 extern template wistream& operator>>(wistream&, _Setbase);
367 extern template wistream& operator>>(wistream&, _Setprecision);
368 extern template wistream& operator>>(wistream&, _Setw);
372 _GLIBCXX_END_NAMESPACE_VERSION
375 #endif /* _GLIBCXX_IOMANIP */