]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/iomanip
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / iomanip
CommitLineData
54c1bf78 1// Standard stream manipulators -*- C++ -*-
de96ac46 2
83ffe9cd 3// Copyright (C) 1997-2023 Free Software Foundation, Inc.
de96ac46
BK
4//
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
de96ac46
BK
9// any later version.
10
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.
15
748086b7
JJ
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.
19
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/>.
de96ac46 24
f910786b 25/** @file include/iomanip
0aa06b18 26 * This is a Standard C++ Library header.
2f9d51b8
PE
27 */
28
143c27b0
BK
29//
30// ISO C++ 14882: 27.6.3 Standard manipulators
31//
32
1143680e
SE
33#ifndef _GLIBCXX_IOMANIP
34#define _GLIBCXX_IOMANIP 1
54c1bf78
BK
35
36#pragma GCC system_header
37
18f176d0
AA
38#include <bits/requires_hosted.h> // iostreams
39
54c1bf78 40#include <bits/c++config.h>
f4e39278
PC
41#include <iosfwd>
42#include <bits/ios_base.h>
54c1bf78 43
734f5023 44#if __cplusplus >= 201103L
604b384d 45#include <locale>
25608e3a 46#if __cplusplus > 201103L
0ca7ba9a 47#include <bits/quoted_string.h>
25608e3a 48#endif
604b384d
PC
49#endif
50
12ffa228
BK
51namespace std _GLIBCXX_VISIBILITY(default)
52{
53_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 54
840ceb34
PE
55 // [27.6.3] standard manipulators
56 // Also see DR 183.
57
54c1bf78
BK
58 struct _Resetiosflags { ios_base::fmtflags _M_mask; };
59
840ceb34
PE
60 /**
61 * @brief Manipulator for @c setf.
93c66bc6 62 * @param __mask A format flags mask.
840ceb34
PE
63 *
64 * Sent to a stream object, this manipulator resets the specified flags,
93c66bc6 65 * via @e stream.setf(0,__mask).
840ceb34 66 */
33ac58d5 67 inline _Resetiosflags
54c1bf78 68 resetiosflags(ios_base::fmtflags __mask)
604b384d 69 { return { __mask }; }
54c1bf78 70
0d223e3a 71 template<typename _CharT, typename _Traits>
33ac58d5 72 inline basic_istream<_CharT, _Traits>&
681a6919 73 operator>>(basic_istream<_CharT, _Traits>& __is, _Resetiosflags __f)
33ac58d5
JW
74 {
75 __is.setf(ios_base::fmtflags(0), __f._M_mask);
76 return __is;
54c1bf78
BK
77 }
78
0d223e3a 79 template<typename _CharT, typename _Traits>
33ac58d5 80 inline basic_ostream<_CharT, _Traits>&
681a6919 81 operator<<(basic_ostream<_CharT, _Traits>& __os, _Resetiosflags __f)
33ac58d5
JW
82 {
83 __os.setf(ios_base::fmtflags(0), __f._M_mask);
84 return __os;
54c1bf78
BK
85 }
86
87
88 struct _Setiosflags { ios_base::fmtflags _M_mask; };
89
840ceb34
PE
90 /**
91 * @brief Manipulator for @c setf.
93c66bc6 92 * @param __mask A format flags mask.
840ceb34
PE
93 *
94 * Sent to a stream object, this manipulator sets the format flags
93c66bc6 95 * to @a __mask.
840ceb34 96 */
33ac58d5 97 inline _Setiosflags
54c1bf78 98 setiosflags(ios_base::fmtflags __mask)
604b384d 99 { return { __mask }; }
54c1bf78 100
0d223e3a 101 template<typename _CharT, typename _Traits>
33ac58d5 102 inline basic_istream<_CharT, _Traits>&
681a6919 103 operator>>(basic_istream<_CharT, _Traits>& __is, _Setiosflags __f)
33ac58d5
JW
104 {
105 __is.setf(__f._M_mask);
106 return __is;
54c1bf78
BK
107 }
108
0d223e3a 109 template<typename _CharT, typename _Traits>
33ac58d5 110 inline basic_ostream<_CharT, _Traits>&
f4e39278 111 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setiosflags __f)
33ac58d5
JW
112 {
113 __os.setf(__f._M_mask);
114 return __os;
54c1bf78
BK
115 }
116
117
118 struct _Setbase { int _M_base; };
119
840ceb34
PE
120 /**
121 * @brief Manipulator for @c setf.
93c66bc6 122 * @param __base A numeric base.
840ceb34
PE
123 *
124 * Sent to a stream object, this manipulator changes the
125 * @c ios_base::basefield flags to @c oct, @c dec, or @c hex when @a base
93c66bc6 126 * is 8, 10, or 16, accordingly, and to 0 if @a __base is any other value.
840ceb34 127 */
33ac58d5 128 inline _Setbase
54c1bf78 129 setbase(int __base)
604b384d 130 { return { __base }; }
54c1bf78 131
0d223e3a 132 template<typename _CharT, typename _Traits>
33ac58d5 133 inline basic_istream<_CharT, _Traits>&
681a6919 134 operator>>(basic_istream<_CharT, _Traits>& __is, _Setbase __f)
54c1bf78 135 {
33ac58d5
JW
136 __is.setf(__f._M_base == 8 ? ios_base::oct :
137 __f._M_base == 10 ? ios_base::dec :
138 __f._M_base == 16 ? ios_base::hex :
681a6919 139 ios_base::fmtflags(0), ios_base::basefield);
33ac58d5 140 return __is;
54c1bf78 141 }
33ac58d5 142
0d223e3a 143 template<typename _CharT, typename _Traits>
33ac58d5 144 inline basic_ostream<_CharT, _Traits>&
681a6919 145 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setbase __f)
54c1bf78 146 {
33ac58d5
JW
147 __os.setf(__f._M_base == 8 ? ios_base::oct :
148 __f._M_base == 10 ? ios_base::dec :
149 __f._M_base == 16 ? ios_base::hex :
54c1bf78 150 ios_base::fmtflags(0), ios_base::basefield);
33ac58d5 151 return __os;
54c1bf78 152 }
33ac58d5 153
54c1bf78 154
604b384d 155 template<typename _CharT>
54c1bf78
BK
156 struct _Setfill { _CharT _M_c; };
157
840ceb34
PE
158 /**
159 * @brief Manipulator for @c fill.
93c66bc6 160 * @param __c The new fill character.
840ceb34 161 *
93c66bc6 162 * Sent to a stream object, this manipulator calls @c fill(__c) for that
840ceb34
PE
163 * object.
164 */
604b384d
PC
165 template<typename _CharT>
166 inline _Setfill<_CharT>
54c1bf78 167 setfill(_CharT __c)
604b384d 168 { return { __c }; }
54c1bf78 169
0d223e3a 170 template<typename _CharT, typename _Traits>
33ac58d5 171 inline basic_istream<_CharT, _Traits>&
681a6919 172 operator>>(basic_istream<_CharT, _Traits>& __is, _Setfill<_CharT> __f)
33ac58d5
JW
173 {
174 __is.fill(__f._M_c);
175 return __is;
54c1bf78
BK
176 }
177
0d223e3a 178 template<typename _CharT, typename _Traits>
33ac58d5 179 inline basic_ostream<_CharT, _Traits>&
681a6919 180 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setfill<_CharT> __f)
33ac58d5
JW
181 {
182 __os.fill(__f._M_c);
183 return __os;
54c1bf78
BK
184 }
185
186
187 struct _Setprecision { int _M_n; };
188
840ceb34
PE
189 /**
190 * @brief Manipulator for @c precision.
93c66bc6 191 * @param __n The new precision.
840ceb34 192 *
93c66bc6 193 * Sent to a stream object, this manipulator calls @c precision(__n) for
840ceb34
PE
194 * that object.
195 */
33ac58d5 196 inline _Setprecision
54c1bf78 197 setprecision(int __n)
604b384d 198 { return { __n }; }
54c1bf78 199
0d223e3a 200 template<typename _CharT, typename _Traits>
33ac58d5 201 inline basic_istream<_CharT, _Traits>&
681a6919 202 operator>>(basic_istream<_CharT, _Traits>& __is, _Setprecision __f)
33ac58d5
JW
203 {
204 __is.precision(__f._M_n);
205 return __is;
54c1bf78
BK
206 }
207
0d223e3a 208 template<typename _CharT, typename _Traits>
33ac58d5 209 inline basic_ostream<_CharT, _Traits>&
681a6919 210 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setprecision __f)
33ac58d5
JW
211 {
212 __os.precision(__f._M_n);
213 return __os;
54c1bf78
BK
214 }
215
216
217 struct _Setw { int _M_n; };
218
840ceb34
PE
219 /**
220 * @brief Manipulator for @c width.
93c66bc6 221 * @param __n The new width.
840ceb34 222 *
93c66bc6 223 * Sent to a stream object, this manipulator calls @c width(__n) for
840ceb34
PE
224 * that object.
225 */
33ac58d5 226 inline _Setw
54c1bf78 227 setw(int __n)
604b384d 228 { return { __n }; }
54c1bf78 229
0d223e3a 230 template<typename _CharT, typename _Traits>
33ac58d5 231 inline basic_istream<_CharT, _Traits>&
681a6919 232 operator>>(basic_istream<_CharT, _Traits>& __is, _Setw __f)
604b384d
PC
233 {
234 __is.width(__f._M_n);
33ac58d5 235 return __is;
54c1bf78
BK
236 }
237
0d223e3a 238 template<typename _CharT, typename _Traits>
33ac58d5 239 inline basic_ostream<_CharT, _Traits>&
681a6919 240 operator<<(basic_ostream<_CharT, _Traits>& __os, _Setw __f)
604b384d
PC
241 {
242 __os.width(__f._M_n);
33ac58d5 243 return __os;
54c1bf78 244 }
a32e3c09 245
734f5023 246#if __cplusplus >= 201103L
33ac58d5 247
604b384d
PC
248 template<typename _MoneyT>
249 struct _Get_money { _MoneyT& _M_mon; bool _M_intl; };
250
251 /**
252 * @brief Extended manipulator for extracting money.
93c66bc6 253 * @param __mon Either long double or a specialization of @c basic_string.
33ac58d5 254 * @param __intl A bool indicating whether international format
93c66bc6 255 * is to be used.
604b384d 256 *
93c66bc6 257 * Sent to a stream object, this manipulator extracts @a __mon.
604b384d
PC
258 */
259 template<typename _MoneyT>
260 inline _Get_money<_MoneyT>
261 get_money(_MoneyT& __mon, bool __intl = false)
262 { return { __mon, __intl }; }
263
264 template<typename _CharT, typename _Traits, typename _MoneyT>
265 basic_istream<_CharT, _Traits>&
266 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_money<_MoneyT> __f)
267 {
1a30353a
PC
268 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
269 if (__cerb)
270 {
271 ios_base::iostate __err = ios_base::goodbit;
272 __try
273 {
274 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
275 typedef money_get<_CharT, _Iter> _MoneyGet;
276
277 const _MoneyGet& __mg = use_facet<_MoneyGet>(__is.getloc());
278 __mg.get(_Iter(__is.rdbuf()), _Iter(), __f._M_intl,
279 __is, __err, __f._M_mon);
280 }
281 __catch(__cxxabiv1::__forced_unwind&)
282 {
283 __is._M_setstate(ios_base::badbit);
284 __throw_exception_again;
285 }
286 __catch(...)
287 { __is._M_setstate(ios_base::badbit); }
f5ca3c34 288 if (__err)
1a30353a
PC
289 __is.setstate(__err);
290 }
33ac58d5 291 return __is;
604b384d
PC
292 }
293
294
295 template<typename _MoneyT>
296 struct _Put_money { const _MoneyT& _M_mon; bool _M_intl; };
297
298 /**
299 * @brief Extended manipulator for inserting money.
93c66bc6 300 * @param __mon Either long double or a specialization of @c basic_string.
33ac58d5 301 * @param __intl A bool indicating whether international format
93c66bc6 302 * is to be used.
604b384d 303 *
93c66bc6 304 * Sent to a stream object, this manipulator inserts @a __mon.
604b384d
PC
305 */
306 template<typename _MoneyT>
307 inline _Put_money<_MoneyT>
308 put_money(const _MoneyT& __mon, bool __intl = false)
309 { return { __mon, __intl }; }
310
311 template<typename _CharT, typename _Traits, typename _MoneyT>
33ac58d5 312 basic_ostream<_CharT, _Traits>&
604b384d
PC
313 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_money<_MoneyT> __f)
314 {
1a30353a
PC
315 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
316 if (__cerb)
317 {
f5ca3c34 318 ios_base::iostate __err = ios_base::goodbit;
1a30353a
PC
319 __try
320 {
321 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
322 typedef money_put<_CharT, _Iter> _MoneyPut;
f5ca3c34 323
1a30353a 324 const _MoneyPut& __mp = use_facet<_MoneyPut>(__os.getloc());
f5ca3c34
PC
325 if (__mp.put(_Iter(__os.rdbuf()), __f._M_intl, __os,
326 __os.fill(), __f._M_mon).failed())
327 __err |= ios_base::badbit;
1a30353a
PC
328 }
329 __catch(__cxxabiv1::__forced_unwind&)
330 {
331 __os._M_setstate(ios_base::badbit);
332 __throw_exception_again;
333 }
334 __catch(...)
335 { __os._M_setstate(ios_base::badbit); }
f5ca3c34
PC
336 if (__err)
337 __os.setstate(__err);
1a30353a 338 }
33ac58d5 339 return __os;
604b384d
PC
340 }
341
ffffc732
RS
342 template<typename _CharT>
343 struct _Put_time
344 {
345 const std::tm* _M_tmb;
346 const _CharT* _M_fmt;
347 };
348
349 /**
350 * @brief Extended manipulator for formatting time.
351 *
352 * This manipulator uses time_put::put to format time.
353 * [ext.manip]
354 *
355 * @param __tmb struct tm time data to format.
356 * @param __fmt format string.
357 */
358 template<typename _CharT>
359 inline _Put_time<_CharT>
360 put_time(const std::tm* __tmb, const _CharT* __fmt)
361 { return { __tmb, __fmt }; }
362
363 template<typename _CharT, typename _Traits>
364 basic_ostream<_CharT, _Traits>&
365 operator<<(basic_ostream<_CharT, _Traits>& __os, _Put_time<_CharT> __f)
366 {
367 typename basic_ostream<_CharT, _Traits>::sentry __cerb(__os);
368 if (__cerb)
369 {
370 ios_base::iostate __err = ios_base::goodbit;
371 __try
372 {
373 typedef ostreambuf_iterator<_CharT, _Traits> _Iter;
374 typedef time_put<_CharT, _Iter> _TimePut;
375
376 const _CharT* const __fmt_end = __f._M_fmt +
377 _Traits::length(__f._M_fmt);
378
379 const _TimePut& __mp = use_facet<_TimePut>(__os.getloc());
380 if (__mp.put(_Iter(__os.rdbuf()), __os, __os.fill(),
381 __f._M_tmb, __f._M_fmt, __fmt_end).failed())
382 __err |= ios_base::badbit;
383 }
384 __catch(__cxxabiv1::__forced_unwind&)
385 {
386 __os._M_setstate(ios_base::badbit);
387 __throw_exception_again;
388 }
389 __catch(...)
390 { __os._M_setstate(ios_base::badbit); }
391 if (__err)
392 __os.setstate(__err);
393 }
394 return __os;
395 }
396
0de0b10c
RS
397 template<typename _CharT>
398 struct _Get_time
399 {
400 std::tm* _M_tmb;
401 const _CharT* _M_fmt;
402 };
403
404 /**
405 * @brief Extended manipulator for extracting time.
406 *
407 * This manipulator uses time_get::get to extract time.
408 * [ext.manip]
409 *
410 * @param __tmb struct to extract the time data to.
411 * @param __fmt format string.
412 */
413 template<typename _CharT>
414 inline _Get_time<_CharT>
415 get_time(std::tm* __tmb, const _CharT* __fmt)
416 { return { __tmb, __fmt }; }
417
418 template<typename _CharT, typename _Traits>
419 basic_istream<_CharT, _Traits>&
420 operator>>(basic_istream<_CharT, _Traits>& __is, _Get_time<_CharT> __f)
421 {
422 typename basic_istream<_CharT, _Traits>::sentry __cerb(__is, false);
423 if (__cerb)
424 {
425 ios_base::iostate __err = ios_base::goodbit;
426 __try
427 {
428 typedef istreambuf_iterator<_CharT, _Traits> _Iter;
429 typedef time_get<_CharT, _Iter> _TimeGet;
430
431 const _CharT* const __fmt_end = __f._M_fmt +
432 _Traits::length(__f._M_fmt);
433
434 const _TimeGet& __mg = use_facet<_TimeGet>(__is.getloc());
435 __mg.get(_Iter(__is.rdbuf()), _Iter(), __is,
436 __err, __f._M_tmb, __f._M_fmt, __fmt_end);
437 }
438 __catch(__cxxabiv1::__forced_unwind&)
439 {
440 __is._M_setstate(ios_base::badbit);
441 __throw_exception_again;
442 }
443 __catch(...)
444 { __is._M_setstate(ios_base::badbit); }
445 if (__err)
446 __is.setstate(__err);
447 }
448 return __is;
449 }
450
7db54ccd 451#if __cplusplus >= 201402L
83ac9249 452
b8806796 453#define __cpp_lib_quoted_string_io 201304L
a15f7cb8 454
83ac9249
ESR
455 /**
456 * @brief Manipulator for quoted strings.
00541349 457 * @param __string String to quote.
83ac9249
ESR
458 * @param __delim Character to quote string with.
459 * @param __escape Escape character to escape itself or quote character.
460 */
461 template<typename _CharT>
462 inline auto
463 quoted(const _CharT* __string,
464 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
465 {
466 return __detail::_Quoted_string<const _CharT*, _CharT>(__string, __delim,
467 __escape);
468 }
469
470 template<typename _CharT, typename _Traits, typename _Alloc>
471 inline auto
472 quoted(const basic_string<_CharT, _Traits, _Alloc>& __string,
473 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
474 {
475 return __detail::_Quoted_string<
7db54ccd
JW
476 const basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
477 __string, __delim, __escape);
83ac9249
ESR
478 }
479
480 template<typename _CharT, typename _Traits, typename _Alloc>
481 inline auto
482 quoted(basic_string<_CharT, _Traits, _Alloc>& __string,
483 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
484 {
485 return __detail::_Quoted_string<
7db54ccd
JW
486 basic_string<_CharT, _Traits, _Alloc>&, _CharT>(
487 __string, __delim, __escape);
83ac9249
ESR
488 }
489
7db54ccd
JW
490#if __cplusplus >= 201703L
491 // _GLIBCXX_RESOLVE_LIB_DEFECTS
492 // 2785. quoted should work with basic_string_view
493 template<typename _CharT, typename _Traits>
494 inline auto
495 quoted(basic_string_view<_CharT, _Traits> __sv,
496 _CharT __delim = _CharT('"'), _CharT __escape = _CharT('\\'))
497 {
498 return __detail::_Quoted_string<
499 basic_string_view<_CharT, _Traits>, _CharT>(__sv, __delim, __escape);
500 }
501#endif // C++17
502#endif // C++14
83ac9249
ESR
503
504#endif // __cplusplus >= 201103L
604b384d 505
a32e3c09 506 // Inhibit implicit instantiations for required instantiations,
33ac58d5 507 // which are defined via explicit instantiations elsewhere.
a32e3c09 508 // NB: This syntax is a GNU extension.
3d7c150e 509#if _GLIBCXX_EXTERN_TEMPLATE
a32e3c09
BK
510 extern template ostream& operator<<(ostream&, _Setfill<char>);
511 extern template ostream& operator<<(ostream&, _Setiosflags);
512 extern template ostream& operator<<(ostream&, _Resetiosflags);
513 extern template ostream& operator<<(ostream&, _Setbase);
514 extern template ostream& operator<<(ostream&, _Setprecision);
515 extern template ostream& operator<<(ostream&, _Setw);
a32e3c09
BK
516 extern template istream& operator>>(istream&, _Setfill<char>);
517 extern template istream& operator>>(istream&, _Setiosflags);
518 extern template istream& operator>>(istream&, _Resetiosflags);
519 extern template istream& operator>>(istream&, _Setbase);
520 extern template istream& operator>>(istream&, _Setprecision);
521 extern template istream& operator>>(istream&, _Setw);
5112ae3a 522
3d7c150e 523#ifdef _GLIBCXX_USE_WCHAR_T
5112ae3a
BK
524 extern template wostream& operator<<(wostream&, _Setfill<wchar_t>);
525 extern template wostream& operator<<(wostream&, _Setiosflags);
526 extern template wostream& operator<<(wostream&, _Resetiosflags);
527 extern template wostream& operator<<(wostream&, _Setbase);
528 extern template wostream& operator<<(wostream&, _Setprecision);
529 extern template wostream& operator<<(wostream&, _Setw);
a32e3c09
BK
530 extern template wistream& operator>>(wistream&, _Setfill<wchar_t>);
531 extern template wistream& operator>>(wistream&, _Setiosflags);
532 extern template wistream& operator>>(wistream&, _Resetiosflags);
533 extern template wistream& operator>>(wistream&, _Setbase);
534 extern template wistream& operator>>(wistream&, _Setprecision);
535 extern template wistream& operator>>(wistream&, _Setw);
5112ae3a 536#endif
1bc8b0ad 537#endif
3cbc7af0 538
12ffa228
BK
539_GLIBCXX_END_NAMESPACE_VERSION
540} // namespace
54c1bf78 541
1143680e 542#endif /* _GLIBCXX_IOMANIP */