libstdc++: Add [[nodiscard]] to iostream members
[official-gcc.git] / libstdc++-v3 / include / bits / ios_base.h
blob1418b18830a37177adc82c0c3bab0b39c8d2dabd
1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997-2024 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
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 bits/ios_base.h
26 * This is an internal header file, included by other library headers.
27 * Do not attempt to use it directly. @headername{ios}
31 // ISO C++ 14882: 27.4 Iostreams base classes
34 #ifndef _IOS_BASE_H
35 #define _IOS_BASE_H 1
37 #ifdef _GLIBCXX_SYSHDR
38 #pragma GCC system_header
39 #endif
41 #include <ext/atomicity.h>
42 #include <bits/localefwd.h>
43 #include <bits/locale_classes.h>
45 #if __cplusplus < 201103L
46 # include <stdexcept>
47 #else
48 # include <system_error>
49 #endif
51 namespace std _GLIBCXX_VISIBILITY(default)
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
55 // The following definitions of bitmask types are enums, not ints,
56 // as permitted (but not required) in the standard, in order to provide
57 // better type safety in iostream calls. A side effect is that in C++98
58 // expressions involving them are not compile-time constants.
59 enum _Ios_Fmtflags
61 _S_boolalpha = 1L << 0,
62 _S_dec = 1L << 1,
63 _S_fixed = 1L << 2,
64 _S_hex = 1L << 3,
65 _S_internal = 1L << 4,
66 _S_left = 1L << 5,
67 _S_oct = 1L << 6,
68 _S_right = 1L << 7,
69 _S_scientific = 1L << 8,
70 _S_showbase = 1L << 9,
71 _S_showpoint = 1L << 10,
72 _S_showpos = 1L << 11,
73 _S_skipws = 1L << 12,
74 _S_unitbuf = 1L << 13,
75 _S_uppercase = 1L << 14,
76 _S_adjustfield = _S_left | _S_right | _S_internal,
77 _S_basefield = _S_dec | _S_oct | _S_hex,
78 _S_floatfield = _S_scientific | _S_fixed,
79 _S_ios_fmtflags_end = 1L << 16,
80 _S_ios_fmtflags_max = __INT_MAX__,
81 _S_ios_fmtflags_min = ~__INT_MAX__
84 _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
85 inline _Ios_Fmtflags
86 operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
87 { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); }
89 _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
90 inline _Ios_Fmtflags
91 operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
92 { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); }
94 _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
95 inline _Ios_Fmtflags
96 operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
97 { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); }
99 _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
100 inline _Ios_Fmtflags
101 operator~(_Ios_Fmtflags __a) _GLIBCXX_NOTHROW
102 { return _Ios_Fmtflags(~static_cast<int>(__a)); }
104 _GLIBCXX14_CONSTEXPR
105 inline const _Ios_Fmtflags&
106 operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
107 { return __a = __a | __b; }
109 _GLIBCXX14_CONSTEXPR
110 inline const _Ios_Fmtflags&
111 operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
112 { return __a = __a & __b; }
114 _GLIBCXX14_CONSTEXPR
115 inline const _Ios_Fmtflags&
116 operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
117 { return __a = __a ^ __b; }
119 // If std::ios_base::noreplace isn't available, -Wswitch should ignore
120 // _S_noreplace.
121 #ifdef __glibcxx_ios_noreplace
122 #define _NOREPLACE_UNUSED
123 #else
124 #define _NOREPLACE_UNUSED __attribute__((__unused__))
125 #endif
127 enum __attribute__((__flag_enum__)) _Ios_Openmode
129 _S_app = 1L << 0,
130 _S_ate = 1L << 1,
131 _S_bin = 1L << 2,
132 _S_in = 1L << 3,
133 _S_out = 1L << 4,
134 _S_trunc = 1L << 5,
135 _S_noreplace _NOREPLACE_UNUSED = 1L << 6,
136 _S_ios_openmode_end __attribute__((__unused__)) = 1L << 16,
137 _S_ios_openmode_max __attribute__((__unused__)) = __INT_MAX__,
138 _S_ios_openmode_min __attribute__((__unused__)) = ~__INT_MAX__
141 #undef _NOREPLACE_UNUSED
143 _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
144 inline _Ios_Openmode
145 operator&(_Ios_Openmode __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
146 { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); }
148 _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
149 inline _Ios_Openmode
150 operator|(_Ios_Openmode __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
151 { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); }
153 _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
154 inline _Ios_Openmode
155 operator^(_Ios_Openmode __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
156 { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); }
158 _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
159 inline _Ios_Openmode
160 operator~(_Ios_Openmode __a) _GLIBCXX_NOTHROW
161 { return _Ios_Openmode(~static_cast<int>(__a)); }
163 _GLIBCXX14_CONSTEXPR
164 inline const _Ios_Openmode&
165 operator|=(_Ios_Openmode& __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
166 { return __a = __a | __b; }
168 _GLIBCXX14_CONSTEXPR
169 inline const _Ios_Openmode&
170 operator&=(_Ios_Openmode& __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
171 { return __a = __a & __b; }
173 _GLIBCXX14_CONSTEXPR
174 inline const _Ios_Openmode&
175 operator^=(_Ios_Openmode& __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
176 { return __a = __a ^ __b; }
179 enum _Ios_Iostate
181 _S_goodbit = 0,
182 _S_badbit = 1L << 0,
183 _S_eofbit = 1L << 1,
184 _S_failbit = 1L << 2,
185 _S_ios_iostate_end = 1L << 16,
186 _S_ios_iostate_max = __INT_MAX__,
187 _S_ios_iostate_min = ~__INT_MAX__
190 _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
191 inline _Ios_Iostate
192 operator&(_Ios_Iostate __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
193 { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); }
195 _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
196 inline _Ios_Iostate
197 operator|(_Ios_Iostate __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
198 { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); }
200 _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
201 inline _Ios_Iostate
202 operator^(_Ios_Iostate __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
203 { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); }
205 _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
206 inline _Ios_Iostate
207 operator~(_Ios_Iostate __a) _GLIBCXX_NOTHROW
208 { return _Ios_Iostate(~static_cast<int>(__a)); }
210 _GLIBCXX14_CONSTEXPR
211 inline const _Ios_Iostate&
212 operator|=(_Ios_Iostate& __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
213 { return __a = __a | __b; }
215 _GLIBCXX14_CONSTEXPR
216 inline const _Ios_Iostate&
217 operator&=(_Ios_Iostate& __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
218 { return __a = __a & __b; }
220 _GLIBCXX14_CONSTEXPR
221 inline const _Ios_Iostate&
222 operator^=(_Ios_Iostate& __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
223 { return __a = __a ^ __b; }
226 enum _Ios_Seekdir
228 _S_beg = 0,
229 _S_cur = _GLIBCXX_STDIO_SEEK_CUR,
230 _S_end = _GLIBCXX_STDIO_SEEK_END,
231 _S_ios_seekdir_end = 1L << 16
234 #if __cplusplus >= 201103L
235 /// I/O error code
236 enum class io_errc { stream = 1 };
238 template <> struct is_error_code_enum<io_errc> : public true_type { };
240 [[__nodiscard__, __gnu__::__const__]]
241 const error_category&
242 iostream_category() noexcept;
244 [[__nodiscard__]]
245 inline error_code
246 make_error_code(io_errc __e) noexcept
247 { return error_code(static_cast<int>(__e), iostream_category()); }
249 [[__nodiscard__]]
250 inline error_condition
251 make_error_condition(io_errc __e) noexcept
252 { return error_condition(static_cast<int>(__e), iostream_category()); }
253 #endif
255 // 27.4.2 Class ios_base
257 * @brief The base of the I/O class hierarchy.
258 * @ingroup io
260 * This class defines everything that can be defined about I/O that does
261 * not depend on the type of characters being input or output. Most
262 * people will only see @c ios_base when they need to specify the full
263 * name of the various I/O flags (e.g., the openmodes).
265 class ios_base
267 #if _GLIBCXX_USE_CXX11_ABI
268 #if __cplusplus < 201103L
269 // Type that is layout-compatible with std::system_error
270 struct system_error : std::runtime_error
272 // Type that is layout-compatible with std::error_code
273 struct error_code
275 error_code() { }
276 private:
277 int _M_value;
278 const void* _M_cat;
279 } _M_code;
281 #endif
282 #endif
283 public:
285 /**
286 * @brief These are thrown to indicate problems with io.
287 * @ingroup exceptions
289 * 27.4.2.1.1 Class ios_base::failure
291 #if _GLIBCXX_USE_CXX11_ABI
292 class _GLIBCXX_ABI_TAG_CXX11 failure : public system_error
294 public:
295 explicit
296 failure(const string& __str);
298 #if __cplusplus >= 201103L
299 explicit
300 failure(const string&, const error_code&);
302 explicit
303 failure(const char*, const error_code& = io_errc::stream);
304 #endif
306 virtual
307 ~failure() throw();
309 virtual const char*
310 what() const throw();
312 #else
313 class failure : public exception
315 public:
316 // _GLIBCXX_RESOLVE_LIB_DEFECTS
317 // 48. Use of non-existent exception constructor
318 explicit
319 failure(const string& __str) throw();
321 // This declaration is not useless:
322 // http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Vague-Linkage.html
323 virtual
324 ~failure() throw();
326 virtual const char*
327 what() const throw();
329 #if __cplusplus >= 201103L
330 // Define the new members required by C++11,
331 // even though the error_code cannot be stored.
333 explicit
334 failure(const string& __s, const error_code&) noexcept
335 : failure(__s)
338 explicit
339 failure(const char* __s, const error_code& = error_code{})
340 : failure(string(__s))
343 // Stand-in for system_error::code() but returning by value.
344 error_code code() const noexcept { return error_code{}; }
345 #endif
347 private:
348 string _M_msg;
350 #endif
352 // 27.4.2.1.2 Type ios_base::fmtflags
354 * @brief This is a bitmask type.
356 * @c @a _Ios_Fmtflags is implementation-defined, but it is valid to
357 * perform bitwise operations on these values and expect the Right
358 * Thing to happen. Defined objects of type fmtflags are:
359 * - boolalpha
360 * - dec
361 * - fixed
362 * - hex
363 * - internal
364 * - left
365 * - oct
366 * - right
367 * - scientific
368 * - showbase
369 * - showpoint
370 * - showpos
371 * - skipws
372 * - unitbuf
373 * - uppercase
374 * - adjustfield
375 * - basefield
376 * - floatfield
378 typedef _Ios_Fmtflags fmtflags;
380 /// Insert/extract @c bool in alphabetic rather than numeric format.
381 static const fmtflags boolalpha = _S_boolalpha;
383 /// Converts integer input or generates integer output in decimal base.
384 static const fmtflags dec = _S_dec;
386 /// Generate floating-point output in fixed-point notation.
387 static const fmtflags fixed = _S_fixed;
389 /// Converts integer input or generates integer output in hexadecimal base.
390 static const fmtflags hex = _S_hex;
392 /// Adds fill characters at a designated internal point in certain
393 /// generated output, or identical to @c right if no such point is
394 /// designated.
395 static const fmtflags internal = _S_internal;
397 /// Adds fill characters on the right (final positions) of certain
398 /// generated output. (I.e., the thing you print is flush left.)
399 static const fmtflags left = _S_left;
401 /// Converts integer input or generates integer output in octal base.
402 static const fmtflags oct = _S_oct;
404 /// Adds fill characters on the left (initial positions) of certain
405 /// generated output. (I.e., the thing you print is flush right.)
406 static const fmtflags right = _S_right;
408 /// Generates floating-point output in scientific notation.
409 static const fmtflags scientific = _S_scientific;
411 /// Generates a prefix indicating the numeric base of generated integer
412 /// output.
413 static const fmtflags showbase = _S_showbase;
415 /// Generates a decimal-point character unconditionally in generated
416 /// floating-point output.
417 static const fmtflags showpoint = _S_showpoint;
419 /// Generates a + sign in non-negative generated numeric output.
420 static const fmtflags showpos = _S_showpos;
422 /// Skips leading white space before certain input operations.
423 static const fmtflags skipws = _S_skipws;
425 /// Flushes output after each output operation.
426 static const fmtflags unitbuf = _S_unitbuf;
428 /// Replaces certain lowercase letters with their uppercase equivalents
429 /// in generated output.
430 static const fmtflags uppercase = _S_uppercase;
432 /// A mask of left|right|internal. Useful for the 2-arg form of @c setf.
433 static const fmtflags adjustfield = _S_adjustfield;
435 /// A mask of dec|oct|hex. Useful for the 2-arg form of @c setf.
436 static const fmtflags basefield = _S_basefield;
438 /// A mask of scientific|fixed. Useful for the 2-arg form of @c setf.
439 static const fmtflags floatfield = _S_floatfield;
441 // 27.4.2.1.3 Type ios_base::iostate
443 * @brief This is a bitmask type.
445 * @c @a _Ios_Iostate is implementation-defined, but it is valid to
446 * perform bitwise operations on these values and expect the Right
447 * Thing to happen. Defined objects of type iostate are:
448 * - badbit
449 * - eofbit
450 * - failbit
451 * - goodbit
453 typedef _Ios_Iostate iostate;
455 /// Indicates a loss of integrity in an input or output sequence (such
456 /// as an irrecoverable read error from a file).
457 static const iostate badbit = _S_badbit;
459 /// Indicates that an input operation reached the end of an input sequence.
460 static const iostate eofbit = _S_eofbit;
462 /// Indicates that an input operation failed to read the expected
463 /// characters, or that an output operation failed to generate the
464 /// desired characters.
465 static const iostate failbit = _S_failbit;
467 /// Indicates all is well.
468 static const iostate goodbit = _S_goodbit;
470 // 27.4.2.1.4 Type ios_base::openmode
472 * @brief This is a bitmask type.
474 * @c @a _Ios_Openmode is implementation-defined, but it is valid to
475 * perform bitwise operations on these values and expect the Right
476 * Thing to happen. Defined objects of type openmode are:
477 * - app
478 * - ate
479 * - binary
480 * - in
481 * - out
482 * - trunc
484 typedef _Ios_Openmode openmode;
486 /// Seek to end before each write.
487 static const openmode app = _S_app;
489 /// Open and seek to end immediately after opening.
490 static const openmode ate = _S_ate;
492 /// Perform input and output in binary mode (as opposed to text mode).
493 /// This is probably not what you think it is; see
494 /// https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
495 static const openmode binary = _S_bin;
497 /// Open for input. Default for @c ifstream and fstream.
498 static const openmode in = _S_in;
500 /// Open for output. Default for @c ofstream and fstream.
501 static const openmode out = _S_out;
503 /// Truncate an existing stream when opening. Default for @c ofstream.
504 static const openmode trunc = _S_trunc;
506 static const openmode __noreplace = _S_noreplace;
508 #ifdef __glibcxx_ios_noreplace // C++ >= 23 && HOSTED
509 /// Open a file in exclusive mode.
510 static const openmode noreplace = _S_noreplace;
511 #endif
513 // 27.4.2.1.5 Type ios_base::seekdir
515 * @brief This is an enumerated type.
517 * @c @a _Ios_Seekdir is implementation-defined. Defined values
518 * of type seekdir are:
519 * - beg
520 * - cur, equivalent to @c SEEK_CUR in the C standard library.
521 * - end, equivalent to @c SEEK_END in the C standard library.
523 typedef _Ios_Seekdir seekdir;
525 /// Request a seek relative to the beginning of the stream.
526 static const seekdir beg = _S_beg;
528 /// Request a seek relative to the current position within the sequence.
529 static const seekdir cur = _S_cur;
531 /// Request a seek relative to the current end of the sequence.
532 static const seekdir end = _S_end;
534 #if __cplusplus <= 201402L
535 // Annex D.6 (removed in C++17)
536 typedef int io_state
537 _GLIBCXX_DEPRECATED_SUGGEST("std::iostate");
538 typedef int open_mode
539 _GLIBCXX_DEPRECATED_SUGGEST("std::openmode");
540 typedef int seek_dir
541 _GLIBCXX_DEPRECATED_SUGGEST("std::seekdir");
543 typedef std::streampos streampos
544 _GLIBCXX_DEPRECATED_SUGGEST("std::streampos");
545 typedef std::streamoff streamoff
546 _GLIBCXX_DEPRECATED_SUGGEST("std::streamoff");
547 #endif
549 // Callbacks;
551 * @brief The set of events that may be passed to an event callback.
553 * erase_event is used during ~ios() and copyfmt(). imbue_event is used
554 * during imbue(). copyfmt_event is used during copyfmt().
556 enum event
558 erase_event,
559 imbue_event,
560 copyfmt_event
564 * @brief The type of an event callback function.
565 * @param __e One of the members of the event enum.
566 * @param __b Reference to the ios_base object.
567 * @param __i The integer provided when the callback was registered.
569 * Event callbacks are user defined functions that get called during
570 * several ios_base and basic_ios functions, specifically imbue(),
571 * copyfmt(), and ~ios().
573 typedef void (*event_callback) (event __e, ios_base& __b, int __i);
576 * @brief Add the callback __fn with parameter __index.
577 * @param __fn The function to add.
578 * @param __index The integer to pass to the function when invoked.
580 * Registers a function as an event callback with an integer parameter to
581 * be passed to the function when invoked. Multiple copies of the
582 * function are allowed. If there are multiple callbacks, they are
583 * invoked in the order they were registered.
585 void
586 register_callback(event_callback __fn, int __index);
588 protected:
589 streamsize _M_precision;
590 streamsize _M_width;
591 fmtflags _M_flags;
592 iostate _M_exception;
593 iostate _M_streambuf_state;
595 // 27.4.2.6 Members for callbacks
596 // 27.4.2.6 ios_base callbacks
597 struct _Callback_list
599 // Data Members
600 _Callback_list* _M_next;
601 ios_base::event_callback _M_fn;
602 int _M_index;
603 _Atomic_word _M_refcount; // 0 means one reference.
605 _Callback_list(ios_base::event_callback __fn, int __index,
606 _Callback_list* __cb)
607 : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
609 void
610 _M_add_reference() { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
612 // 0 => OK to delete.
614 _M_remove_reference()
616 // Be race-detector-friendly. For more info see bits/c++config.
617 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount);
618 int __res = __gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1);
619 if (__res == 0)
621 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount);
623 return __res;
627 _Callback_list* _M_callbacks;
629 void
630 _M_call_callbacks(event __ev) throw();
632 void
633 _M_dispose_callbacks(void) throw();
635 // 27.4.2.5 Members for iword/pword storage
636 struct _Words
638 void* _M_pword;
639 long _M_iword;
640 _Words() : _M_pword(0), _M_iword(0) { }
643 // Only for failed iword/pword calls.
644 _Words _M_word_zero;
646 // Guaranteed storage.
647 // The first 5 iword and pword slots are reserved for internal use.
648 enum { _S_local_word_size = 8 };
649 _Words _M_local_word[_S_local_word_size];
651 // Allocated storage.
652 int _M_word_size;
653 _Words* _M_word;
655 _Words&
656 _M_grow_words(int __index, bool __iword);
658 // Members for locale and locale caching.
659 locale _M_ios_locale;
661 void
662 _M_init() throw();
664 public:
666 // 27.4.2.1.6 Class ios_base::Init
667 // Used to initialize standard streams. In theory, g++ could use
668 // -finit-priority to order this stuff correctly without going
669 // through these machinations.
670 class Init
672 friend class ios_base;
673 public:
674 Init();
675 ~Init();
677 #if __cplusplus >= 201103L
678 Init(const Init&) = default;
679 Init& operator=(const Init&) = default;
680 #endif
682 private:
683 static _Atomic_word _S_refcount;
684 static bool _S_synced_with_stdio;
687 // [27.4.2.2] fmtflags state functions
689 * @brief Access to format flags.
690 * @return The format control flags for both input and output.
692 _GLIBCXX_NODISCARD
693 fmtflags
694 flags() const
695 { return _M_flags; }
698 * @brief Setting new format flags all at once.
699 * @param __fmtfl The new flags to set.
700 * @return The previous format control flags.
702 * This function overwrites all the format flags with @a __fmtfl.
704 fmtflags
705 flags(fmtflags __fmtfl)
707 fmtflags __old = _M_flags;
708 _M_flags = __fmtfl;
709 return __old;
713 * @brief Setting new format flags.
714 * @param __fmtfl Additional flags to set.
715 * @return The previous format control flags.
717 * This function sets additional flags in format control. Flags that
718 * were previously set remain set.
720 fmtflags
721 setf(fmtflags __fmtfl)
723 fmtflags __old = _M_flags;
724 _M_flags |= __fmtfl;
725 return __old;
729 * @brief Setting new format flags.
730 * @param __fmtfl Additional flags to set.
731 * @param __mask The flags mask for @a fmtfl.
732 * @return The previous format control flags.
734 * This function clears @a mask in the format flags, then sets
735 * @a fmtfl @c & @a mask. An example mask is @c ios_base::adjustfield.
737 fmtflags
738 setf(fmtflags __fmtfl, fmtflags __mask)
740 fmtflags __old = _M_flags;
741 _M_flags &= ~__mask;
742 _M_flags |= (__fmtfl & __mask);
743 return __old;
747 * @brief Clearing format flags.
748 * @param __mask The flags to unset.
750 * This function clears @a __mask in the format flags.
752 void
753 unsetf(fmtflags __mask)
754 { _M_flags &= ~__mask; }
757 * @brief Flags access.
758 * @return The precision to generate on certain output operations.
760 * Be careful if you try to give a definition of @a precision here; see
761 * DR 189.
763 _GLIBCXX_NODISCARD
764 streamsize
765 precision() const
766 { return _M_precision; }
769 * @brief Changing flags.
770 * @param __prec The new precision value.
771 * @return The previous value of precision().
773 streamsize
774 precision(streamsize __prec)
776 streamsize __old = _M_precision;
777 _M_precision = __prec;
778 return __old;
782 * @brief Flags access.
783 * @return The minimum field width to generate on output operations.
785 * <em>Minimum field width</em> refers to the number of characters.
787 _GLIBCXX_NODISCARD
788 streamsize
789 width() const
790 { return _M_width; }
793 * @brief Changing flags.
794 * @param __wide The new width value.
795 * @return The previous value of width().
797 streamsize
798 width(streamsize __wide)
800 streamsize __old = _M_width;
801 _M_width = __wide;
802 return __old;
805 // [27.4.2.4] ios_base static members
807 * @brief Interaction with the standard C I/O objects.
808 * @param __sync Whether to synchronize or not.
809 * @return True if the standard streams were previously synchronized.
811 * The synchronization referred to is @e only that between the standard
812 * C facilities (e.g., stdout) and the standard C++ objects (e.g.,
813 * cout). User-declared streams are unaffected. See
814 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
816 static bool
817 sync_with_stdio(bool __sync = true);
819 // [27.4.2.3] ios_base locale functions
821 * @brief Setting a new locale.
822 * @param __loc The new locale.
823 * @return The previous locale.
825 * Sets the new locale for this stream, and then invokes each callback
826 * with imbue_event.
828 locale
829 imbue(const locale& __loc) throw();
832 * @brief Locale access
833 * @return A copy of the current locale.
835 * If @c imbue(loc) has previously been called, then this function
836 * returns @c loc. Otherwise, it returns a copy of @c std::locale(),
837 * the global C++ locale.
839 _GLIBCXX_NODISCARD
840 locale
841 getloc() const
842 { return _M_ios_locale; }
845 * @brief Locale access
846 * @return A reference to the current locale.
848 * Like getloc above, but returns a reference instead of
849 * generating a copy.
851 const locale&
852 _M_getloc() const
853 { return _M_ios_locale; }
855 // [27.4.2.5] ios_base storage functions
857 * @brief Access to unique indices.
858 * @return An integer different from all previous calls.
860 * This function returns a unique integer every time it is called. It
861 * can be used for any purpose, but is primarily intended to be a unique
862 * index for the iword and pword functions. The expectation is that an
863 * application calls xalloc in order to obtain an index in the iword and
864 * pword arrays that can be used without fear of conflict.
866 * The implementation maintains a static variable that is incremented and
867 * returned on each invocation. xalloc is guaranteed to return an index
868 * that is safe to use in the iword and pword arrays.
870 static int
871 xalloc() throw();
874 * @brief Access to integer array.
875 * @param __ix Index into the array.
876 * @return A reference to an integer associated with the index.
878 * The iword function provides access to an array of integers that can be
879 * used for any purpose. The array grows as required to hold the
880 * supplied index. All integers in the array are initialized to 0.
882 * The implementation reserves several indices. You should use xalloc to
883 * obtain an index that is safe to use. Also note that since the array
884 * can grow dynamically, it is not safe to hold onto the reference.
886 long&
887 iword(int __ix)
889 _Words& __word = ((unsigned)__ix < (unsigned)_M_word_size)
890 ? _M_word[__ix] : _M_grow_words(__ix, true);
891 return __word._M_iword;
895 * @brief Access to void pointer array.
896 * @param __ix Index into the array.
897 * @return A reference to a void* associated with the index.
899 * The pword function provides access to an array of pointers that can be
900 * used for any purpose. The array grows as required to hold the
901 * supplied index. All pointers in the array are initialized to 0.
903 * The implementation reserves several indices. You should use xalloc to
904 * obtain an index that is safe to use. Also note that since the array
905 * can grow dynamically, it is not safe to hold onto the reference.
907 void*&
908 pword(int __ix)
910 _Words& __word = ((unsigned)__ix < (unsigned)_M_word_size)
911 ? _M_word[__ix] : _M_grow_words(__ix, false);
912 return __word._M_pword;
915 // Destructor
917 * Invokes each callback with erase_event. Destroys local storage.
919 * Note that the ios_base object for the standard streams never gets
920 * destroyed. As a result, any callbacks registered with the standard
921 * streams will not get invoked with erase_event (unless copyfmt is
922 * used).
924 virtual ~ios_base();
926 protected:
927 ios_base() throw ();
929 #if __cplusplus < 201103L
930 // _GLIBCXX_RESOLVE_LIB_DEFECTS
931 // 50. Copy constructor and assignment operator of ios_base
932 private:
933 ios_base(const ios_base&);
935 ios_base&
936 operator=(const ios_base&);
937 #else
938 public:
939 ios_base(const ios_base&) = delete;
941 ios_base&
942 operator=(const ios_base&) = delete;
944 protected:
945 void
946 _M_move(ios_base&) noexcept;
948 void
949 _M_swap(ios_base& __rhs) noexcept;
950 #endif
953 // [27.4.5.1] fmtflags manipulators
954 /// Calls base.setf(ios_base::boolalpha).
955 inline ios_base&
956 boolalpha(ios_base& __base)
958 __base.setf(ios_base::boolalpha);
959 return __base;
962 /// Calls base.unsetf(ios_base::boolalpha).
963 inline ios_base&
964 noboolalpha(ios_base& __base)
966 __base.unsetf(ios_base::boolalpha);
967 return __base;
970 /// Calls base.setf(ios_base::showbase).
971 inline ios_base&
972 showbase(ios_base& __base)
974 __base.setf(ios_base::showbase);
975 return __base;
978 /// Calls base.unsetf(ios_base::showbase).
979 inline ios_base&
980 noshowbase(ios_base& __base)
982 __base.unsetf(ios_base::showbase);
983 return __base;
986 /// Calls base.setf(ios_base::showpoint).
987 inline ios_base&
988 showpoint(ios_base& __base)
990 __base.setf(ios_base::showpoint);
991 return __base;
994 /// Calls base.unsetf(ios_base::showpoint).
995 inline ios_base&
996 noshowpoint(ios_base& __base)
998 __base.unsetf(ios_base::showpoint);
999 return __base;
1002 /// Calls base.setf(ios_base::showpos).
1003 inline ios_base&
1004 showpos(ios_base& __base)
1006 __base.setf(ios_base::showpos);
1007 return __base;
1010 /// Calls base.unsetf(ios_base::showpos).
1011 inline ios_base&
1012 noshowpos(ios_base& __base)
1014 __base.unsetf(ios_base::showpos);
1015 return __base;
1018 /// Calls base.setf(ios_base::skipws).
1019 inline ios_base&
1020 skipws(ios_base& __base)
1022 __base.setf(ios_base::skipws);
1023 return __base;
1026 /// Calls base.unsetf(ios_base::skipws).
1027 inline ios_base&
1028 noskipws(ios_base& __base)
1030 __base.unsetf(ios_base::skipws);
1031 return __base;
1034 /// Calls base.setf(ios_base::uppercase).
1035 inline ios_base&
1036 uppercase(ios_base& __base)
1038 __base.setf(ios_base::uppercase);
1039 return __base;
1042 /// Calls base.unsetf(ios_base::uppercase).
1043 inline ios_base&
1044 nouppercase(ios_base& __base)
1046 __base.unsetf(ios_base::uppercase);
1047 return __base;
1050 /// Calls base.setf(ios_base::unitbuf).
1051 inline ios_base&
1052 unitbuf(ios_base& __base)
1054 __base.setf(ios_base::unitbuf);
1055 return __base;
1058 /// Calls base.unsetf(ios_base::unitbuf).
1059 inline ios_base&
1060 nounitbuf(ios_base& __base)
1062 __base.unsetf(ios_base::unitbuf);
1063 return __base;
1066 // [27.4.5.2] adjustfield manipulators
1067 /// Calls base.setf(ios_base::internal, ios_base::adjustfield).
1068 inline ios_base&
1069 internal(ios_base& __base)
1071 __base.setf(ios_base::internal, ios_base::adjustfield);
1072 return __base;
1075 /// Calls base.setf(ios_base::left, ios_base::adjustfield).
1076 inline ios_base&
1077 left(ios_base& __base)
1079 __base.setf(ios_base::left, ios_base::adjustfield);
1080 return __base;
1083 /// Calls base.setf(ios_base::right, ios_base::adjustfield).
1084 inline ios_base&
1085 right(ios_base& __base)
1087 __base.setf(ios_base::right, ios_base::adjustfield);
1088 return __base;
1091 // [27.4.5.3] basefield manipulators
1092 /// Calls base.setf(ios_base::dec, ios_base::basefield).
1093 inline ios_base&
1094 dec(ios_base& __base)
1096 __base.setf(ios_base::dec, ios_base::basefield);
1097 return __base;
1100 /// Calls base.setf(ios_base::hex, ios_base::basefield).
1101 inline ios_base&
1102 hex(ios_base& __base)
1104 __base.setf(ios_base::hex, ios_base::basefield);
1105 return __base;
1108 /// Calls base.setf(ios_base::oct, ios_base::basefield).
1109 inline ios_base&
1110 oct(ios_base& __base)
1112 __base.setf(ios_base::oct, ios_base::basefield);
1113 return __base;
1116 // [27.4.5.4] floatfield manipulators
1117 /// Calls base.setf(ios_base::fixed, ios_base::floatfield).
1118 inline ios_base&
1119 fixed(ios_base& __base)
1121 __base.setf(ios_base::fixed, ios_base::floatfield);
1122 return __base;
1125 /// Calls base.setf(ios_base::scientific, ios_base::floatfield).
1126 inline ios_base&
1127 scientific(ios_base& __base)
1129 __base.setf(ios_base::scientific, ios_base::floatfield);
1130 return __base;
1133 #if __cplusplus >= 201103L
1134 // New C++11 floatfield manipulators
1136 /// Calls
1137 /// base.setf(ios_base::fixed|ios_base::scientific, ios_base::floatfield)
1138 inline ios_base&
1139 hexfloat(ios_base& __base)
1141 __base.setf(ios_base::fixed | ios_base::scientific, ios_base::floatfield);
1142 return __base;
1145 /// Calls @c base.unsetf(ios_base::floatfield)
1146 inline ios_base&
1147 defaultfloat(ios_base& __base)
1149 __base.unsetf(ios_base::floatfield);
1150 return __base;
1152 #endif
1154 _GLIBCXX_END_NAMESPACE_VERSION
1155 } // namespace
1157 #endif /* _IOS_BASE_H */