libstdc++
ios_base.h
Go to the documentation of this file.
1// Iostreams base classes -*- C++ -*-
2
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.
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
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/>.
24
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}
28 */
29
30//
31// ISO C++ 14882: 27.4 Iostreams base classes
32//
33
34#ifndef _IOS_BASE_H
35#define _IOS_BASE_H 1
36
37#pragma GCC system_header
38
39#include <ext/atomicity.h>
40#include <bits/localefwd.h>
41#include <bits/locale_classes.h>
42
43#if __cplusplus < 201103L
44# include <stdexcept>
45#else
46# include <system_error>
47#endif
48
49namespace std _GLIBCXX_VISIBILITY(default)
50{
51_GLIBCXX_BEGIN_NAMESPACE_VERSION
52
53 // The following definitions of bitmask types are enums, not ints,
54 // as permitted (but not required) in the standard, in order to provide
55 // better type safety in iostream calls. A side effect is that in C++98
56 // expressions involving them are not compile-time constants.
57 enum _Ios_Fmtflags
58 {
59 _S_boolalpha = 1L << 0,
60 _S_dec = 1L << 1,
61 _S_fixed = 1L << 2,
62 _S_hex = 1L << 3,
63 _S_internal = 1L << 4,
64 _S_left = 1L << 5,
65 _S_oct = 1L << 6,
66 _S_right = 1L << 7,
67 _S_scientific = 1L << 8,
68 _S_showbase = 1L << 9,
69 _S_showpoint = 1L << 10,
70 _S_showpos = 1L << 11,
71 _S_skipws = 1L << 12,
72 _S_unitbuf = 1L << 13,
73 _S_uppercase = 1L << 14,
74 _S_adjustfield = _S_left | _S_right | _S_internal,
75 _S_basefield = _S_dec | _S_oct | _S_hex,
76 _S_floatfield = _S_scientific | _S_fixed,
77 _S_ios_fmtflags_end = 1L << 16,
78 _S_ios_fmtflags_max = __INT_MAX__,
79 _S_ios_fmtflags_min = ~__INT_MAX__
80 };
81
82 inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
83 operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
84 { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); }
85
86 inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
87 operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
88 { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); }
89
90 inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
91 operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
92 { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); }
93
94 inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
95 operator~(_Ios_Fmtflags __a)
96 { return _Ios_Fmtflags(~static_cast<int>(__a)); }
97
98 inline const _Ios_Fmtflags&
99 operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
100 { return __a = __a | __b; }
101
102 inline const _Ios_Fmtflags&
103 operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
104 { return __a = __a & __b; }
105
106 inline const _Ios_Fmtflags&
107 operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
108 { return __a = __a ^ __b; }
109
110
111 enum _Ios_Openmode
112 {
113 _S_app = 1L << 0,
114 _S_ate = 1L << 1,
115 _S_bin = 1L << 2,
116 _S_in = 1L << 3,
117 _S_out = 1L << 4,
118 _S_trunc = 1L << 5,
119 _S_noreplace = 1L << 6,
120 _S_ios_openmode_end = 1L << 16,
121 _S_ios_openmode_max = __INT_MAX__,
122 _S_ios_openmode_min = ~__INT_MAX__
123 };
124
125 inline _GLIBCXX_CONSTEXPR _Ios_Openmode
126 operator&(_Ios_Openmode __a, _Ios_Openmode __b)
127 { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); }
128
129 inline _GLIBCXX_CONSTEXPR _Ios_Openmode
130 operator|(_Ios_Openmode __a, _Ios_Openmode __b)
131 { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); }
132
133 inline _GLIBCXX_CONSTEXPR _Ios_Openmode
134 operator^(_Ios_Openmode __a, _Ios_Openmode __b)
135 { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); }
136
137 inline _GLIBCXX_CONSTEXPR _Ios_Openmode
138 operator~(_Ios_Openmode __a)
139 { return _Ios_Openmode(~static_cast<int>(__a)); }
140
141 inline const _Ios_Openmode&
142 operator|=(_Ios_Openmode& __a, _Ios_Openmode __b)
143 { return __a = __a | __b; }
144
145 inline const _Ios_Openmode&
146 operator&=(_Ios_Openmode& __a, _Ios_Openmode __b)
147 { return __a = __a & __b; }
148
149 inline const _Ios_Openmode&
150 operator^=(_Ios_Openmode& __a, _Ios_Openmode __b)
151 { return __a = __a ^ __b; }
152
153
154 enum _Ios_Iostate
155 {
156 _S_goodbit = 0,
157 _S_badbit = 1L << 0,
158 _S_eofbit = 1L << 1,
159 _S_failbit = 1L << 2,
160 _S_ios_iostate_end = 1L << 16,
161 _S_ios_iostate_max = __INT_MAX__,
162 _S_ios_iostate_min = ~__INT_MAX__
163 };
164
165 inline _GLIBCXX_CONSTEXPR _Ios_Iostate
166 operator&(_Ios_Iostate __a, _Ios_Iostate __b)
167 { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); }
168
169 inline _GLIBCXX_CONSTEXPR _Ios_Iostate
170 operator|(_Ios_Iostate __a, _Ios_Iostate __b)
171 { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); }
172
173 inline _GLIBCXX_CONSTEXPR _Ios_Iostate
174 operator^(_Ios_Iostate __a, _Ios_Iostate __b)
175 { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); }
176
177 inline _GLIBCXX_CONSTEXPR _Ios_Iostate
178 operator~(_Ios_Iostate __a)
179 { return _Ios_Iostate(~static_cast<int>(__a)); }
180
181 inline const _Ios_Iostate&
182 operator|=(_Ios_Iostate& __a, _Ios_Iostate __b)
183 { return __a = __a | __b; }
184
185 inline const _Ios_Iostate&
186 operator&=(_Ios_Iostate& __a, _Ios_Iostate __b)
187 { return __a = __a & __b; }
188
189 inline const _Ios_Iostate&
190 operator^=(_Ios_Iostate& __a, _Ios_Iostate __b)
191 { return __a = __a ^ __b; }
192
193
194 enum _Ios_Seekdir
195 {
196 _S_beg = 0,
197 _S_cur = _GLIBCXX_STDIO_SEEK_CUR,
198 _S_end = _GLIBCXX_STDIO_SEEK_END,
199 _S_ios_seekdir_end = 1L << 16
200 };
201
202#if __cplusplus >= 201103L
203 /// I/O error code
204 enum class io_errc { stream = 1 };
205
206 template <> struct is_error_code_enum<io_errc> : public true_type { };
207
208 [[__nodiscard__, __gnu__::__const__]]
209 const error_category&
210 iostream_category() noexcept;
211
212 [[__nodiscard__]]
213 inline error_code
214 make_error_code(io_errc __e) noexcept
215 { return error_code(static_cast<int>(__e), iostream_category()); }
216
217 [[__nodiscard__]]
218 inline error_condition
219 make_error_condition(io_errc __e) noexcept
220 { return error_condition(static_cast<int>(__e), iostream_category()); }
221#endif
222
223 // 27.4.2 Class ios_base
224 /**
225 * @brief The base of the I/O class hierarchy.
226 * @ingroup io
227 *
228 * This class defines everything that can be defined about I/O that does
229 * not depend on the type of characters being input or output. Most
230 * people will only see @c ios_base when they need to specify the full
231 * name of the various I/O flags (e.g., the openmodes).
232 */
234 {
235#if _GLIBCXX_USE_CXX11_ABI
236#if __cplusplus < 201103L
237 // Type that is layout-compatible with std::system_error
239 {
240 // Type that is layout-compatible with std::error_code
241 struct error_code
242 {
243 error_code() { }
244 private:
245 int _M_value;
246 const void* _M_cat;
247 } _M_code;
248 };
249#endif
250#endif
251 public:
252
253 /**
254 * @brief These are thrown to indicate problems with io.
255 * @ingroup exceptions
256 *
257 * 27.4.2.1.1 Class ios_base::failure
258 */
259#if _GLIBCXX_USE_CXX11_ABI
260 class _GLIBCXX_ABI_TAG_CXX11 failure : public system_error
261 {
262 public:
263 explicit
264 failure(const string& __str);
265
266#if __cplusplus >= 201103L
267 explicit
268 failure(const string&, const error_code&);
269
270 explicit
271 failure(const char*, const error_code& = io_errc::stream);
272#endif
273
274 virtual
275 ~failure() throw();
276
277 virtual const char*
278 what() const throw();
279 };
280#else
281 class failure : public exception
282 {
283 public:
284 // _GLIBCXX_RESOLVE_LIB_DEFECTS
285 // 48. Use of non-existent exception constructor
286 explicit
287 failure(const string& __str) throw();
288
289 // This declaration is not useless:
290 // http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Vague-Linkage.html
291 virtual
292 ~failure() throw();
293
294 virtual const char*
295 what() const throw();
296
297#if __cplusplus >= 201103L
298 // Define the new members required by C++11,
299 // even though the error_code cannot be stored.
300
301 explicit
302 failure(const string& __s, const error_code&) noexcept
303 : failure(__s)
304 { }
305
306 explicit
307 failure(const char* __s, const error_code& = error_code{})
308 : failure(string(__s))
309 { }
310
311 // Stand-in for system_error::code() but returning by value.
312 error_code code() const noexcept { return error_code{}; }
313#endif
314
315 private:
316 string _M_msg;
317 };
318#endif
319
320 // 27.4.2.1.2 Type ios_base::fmtflags
321 /**
322 * @brief This is a bitmask type.
323 *
324 * @c @a _Ios_Fmtflags is implementation-defined, but it is valid to
325 * perform bitwise operations on these values and expect the Right
326 * Thing to happen. Defined objects of type fmtflags are:
327 * - boolalpha
328 * - dec
329 * - fixed
330 * - hex
331 * - internal
332 * - left
333 * - oct
334 * - right
335 * - scientific
336 * - showbase
337 * - showpoint
338 * - showpos
339 * - skipws
340 * - unitbuf
341 * - uppercase
342 * - adjustfield
343 * - basefield
344 * - floatfield
345 */
346 typedef _Ios_Fmtflags fmtflags;
347
348 /// Insert/extract @c bool in alphabetic rather than numeric format.
349 static const fmtflags boolalpha = _S_boolalpha;
350
351 /// Converts integer input or generates integer output in decimal base.
352 static const fmtflags dec = _S_dec;
353
354 /// Generate floating-point output in fixed-point notation.
355 static const fmtflags fixed = _S_fixed;
356
357 /// Converts integer input or generates integer output in hexadecimal base.
358 static const fmtflags hex = _S_hex;
359
360 /// Adds fill characters at a designated internal point in certain
361 /// generated output, or identical to @c right if no such point is
362 /// designated.
363 static const fmtflags internal = _S_internal;
364
365 /// Adds fill characters on the right (final positions) of certain
366 /// generated output. (I.e., the thing you print is flush left.)
367 static const fmtflags left = _S_left;
368
369 /// Converts integer input or generates integer output in octal base.
370 static const fmtflags oct = _S_oct;
371
372 /// Adds fill characters on the left (initial positions) of certain
373 /// generated output. (I.e., the thing you print is flush right.)
374 static const fmtflags right = _S_right;
375
376 /// Generates floating-point output in scientific notation.
377 static const fmtflags scientific = _S_scientific;
378
379 /// Generates a prefix indicating the numeric base of generated integer
380 /// output.
381 static const fmtflags showbase = _S_showbase;
382
383 /// Generates a decimal-point character unconditionally in generated
384 /// floating-point output.
385 static const fmtflags showpoint = _S_showpoint;
386
387 /// Generates a + sign in non-negative generated numeric output.
388 static const fmtflags showpos = _S_showpos;
389
390 /// Skips leading white space before certain input operations.
391 static const fmtflags skipws = _S_skipws;
392
393 /// Flushes output after each output operation.
394 static const fmtflags unitbuf = _S_unitbuf;
395
396 /// Replaces certain lowercase letters with their uppercase equivalents
397 /// in generated output.
398 static const fmtflags uppercase = _S_uppercase;
399
400 /// A mask of left|right|internal. Useful for the 2-arg form of @c setf.
401 static const fmtflags adjustfield = _S_adjustfield;
402
403 /// A mask of dec|oct|hex. Useful for the 2-arg form of @c setf.
404 static const fmtflags basefield = _S_basefield;
405
406 /// A mask of scientific|fixed. Useful for the 2-arg form of @c setf.
407 static const fmtflags floatfield = _S_floatfield;
408
409 // 27.4.2.1.3 Type ios_base::iostate
410 /**
411 * @brief This is a bitmask type.
412 *
413 * @c @a _Ios_Iostate is implementation-defined, but it is valid to
414 * perform bitwise operations on these values and expect the Right
415 * Thing to happen. Defined objects of type iostate are:
416 * - badbit
417 * - eofbit
418 * - failbit
419 * - goodbit
420 */
421 typedef _Ios_Iostate iostate;
422
423 /// Indicates a loss of integrity in an input or output sequence (such
424 /// as an irrecoverable read error from a file).
425 static const iostate badbit = _S_badbit;
426
427 /// Indicates that an input operation reached the end of an input sequence.
428 static const iostate eofbit = _S_eofbit;
429
430 /// Indicates that an input operation failed to read the expected
431 /// characters, or that an output operation failed to generate the
432 /// desired characters.
433 static const iostate failbit = _S_failbit;
434
435 /// Indicates all is well.
436 static const iostate goodbit = _S_goodbit;
437
438 // 27.4.2.1.4 Type ios_base::openmode
439 /**
440 * @brief This is a bitmask type.
441 *
442 * @c @a _Ios_Openmode is implementation-defined, but it is valid to
443 * perform bitwise operations on these values and expect the Right
444 * Thing to happen. Defined objects of type openmode are:
445 * - app
446 * - ate
447 * - binary
448 * - in
449 * - out
450 * - trunc
451 */
452 typedef _Ios_Openmode openmode;
453
454 /// Seek to end before each write.
455 static const openmode app = _S_app;
456
457 /// Open and seek to end immediately after opening.
458 static const openmode ate = _S_ate;
459
460 /// Perform input and output in binary mode (as opposed to text mode).
461 /// This is probably not what you think it is; see
462 /// https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
463 static const openmode binary = _S_bin;
464
465 /// Open for input. Default for @c ifstream and fstream.
466 static const openmode in = _S_in;
467
468 /// Open for output. Default for @c ofstream and fstream.
469 static const openmode out = _S_out;
470
471 /// Truncate an existing stream when opening. Default for @c ofstream.
472 static const openmode trunc = _S_trunc;
473
474 static const openmode __noreplace = _S_noreplace;
475
476#ifdef __glibcxx_ios_noreplace // C++ >= 23 && HOSTED
477 /// Open a file in exclusive mode.
478 static const openmode noreplace = _S_noreplace;
479#endif
480
481 // 27.4.2.1.5 Type ios_base::seekdir
482 /**
483 * @brief This is an enumerated type.
484 *
485 * @c @a _Ios_Seekdir is implementation-defined. Defined values
486 * of type seekdir are:
487 * - beg
488 * - cur, equivalent to @c SEEK_CUR in the C standard library.
489 * - end, equivalent to @c SEEK_END in the C standard library.
490 */
491 typedef _Ios_Seekdir seekdir;
492
493 /// Request a seek relative to the beginning of the stream.
494 static const seekdir beg = _S_beg;
495
496 /// Request a seek relative to the current position within the sequence.
497 static const seekdir cur = _S_cur;
498
499 /// Request a seek relative to the current end of the sequence.
500 static const seekdir end = _S_end;
501
502#if __cplusplus <= 201402L
503 // Annex D.6 (removed in C++17)
504 typedef int io_state
505 _GLIBCXX_DEPRECATED_SUGGEST("std::iostate");
506 typedef int open_mode
507 _GLIBCXX_DEPRECATED_SUGGEST("std::openmode");
508 typedef int seek_dir
509 _GLIBCXX_DEPRECATED_SUGGEST("std::seekdir");
510
512 _GLIBCXX_DEPRECATED_SUGGEST("std::streampos");
514 _GLIBCXX_DEPRECATED_SUGGEST("std::streamoff");
515#endif
516
517 // Callbacks;
518 /**
519 * @brief The set of events that may be passed to an event callback.
520 *
521 * erase_event is used during ~ios() and copyfmt(). imbue_event is used
522 * during imbue(). copyfmt_event is used during copyfmt().
523 */
524 enum event
525 {
526 erase_event,
527 imbue_event,
528 copyfmt_event
529 };
530
531 /**
532 * @brief The type of an event callback function.
533 * @param __e One of the members of the event enum.
534 * @param __b Reference to the ios_base object.
535 * @param __i The integer provided when the callback was registered.
536 *
537 * Event callbacks are user defined functions that get called during
538 * several ios_base and basic_ios functions, specifically imbue(),
539 * copyfmt(), and ~ios().
540 */
541 typedef void (*event_callback) (event __e, ios_base& __b, int __i);
542
543 /**
544 * @brief Add the callback __fn with parameter __index.
545 * @param __fn The function to add.
546 * @param __index The integer to pass to the function when invoked.
547 *
548 * Registers a function as an event callback with an integer parameter to
549 * be passed to the function when invoked. Multiple copies of the
550 * function are allowed. If there are multiple callbacks, they are
551 * invoked in the order they were registered.
552 */
553 void
555
556 protected:
557 streamsize _M_precision;
558 streamsize _M_width;
559 fmtflags _M_flags;
560 iostate _M_exception;
561 iostate _M_streambuf_state;
562
563 // 27.4.2.6 Members for callbacks
564 // 27.4.2.6 ios_base callbacks
565 struct _Callback_list
566 {
567 // Data Members
568 _Callback_list* _M_next;
570 int _M_index;
571 _Atomic_word _M_refcount; // 0 means one reference.
572
573 _Callback_list(ios_base::event_callback __fn, int __index,
574 _Callback_list* __cb)
575 : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
576
577 void
578 _M_add_reference() { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
579
580 // 0 => OK to delete.
581 int
582 _M_remove_reference()
583 {
584 // Be race-detector-friendly. For more info see bits/c++config.
585 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount);
586 int __res = __gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1);
587 if (__res == 0)
588 {
589 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount);
590 }
591 return __res;
592 }
593 };
594
595 _Callback_list* _M_callbacks;
596
597 void
598 _M_call_callbacks(event __ev) throw();
599
600 void
601 _M_dispose_callbacks(void) throw();
602
603 // 27.4.2.5 Members for iword/pword storage
604 struct _Words
605 {
606 void* _M_pword;
607 long _M_iword;
608 _Words() : _M_pword(0), _M_iword(0) { }
609 };
610
611 // Only for failed iword/pword calls.
612 _Words _M_word_zero;
613
614 // Guaranteed storage.
615 // The first 5 iword and pword slots are reserved for internal use.
616 enum { _S_local_word_size = 8 };
617 _Words _M_local_word[_S_local_word_size];
618
619 // Allocated storage.
620 int _M_word_size;
621 _Words* _M_word;
622
623 _Words&
624 _M_grow_words(int __index, bool __iword);
625
626 // Members for locale and locale caching.
627 locale _M_ios_locale;
628
629 void
630 _M_init() throw();
631
632 public:
633
634 // 27.4.2.1.6 Class ios_base::Init
635 // Used to initialize standard streams. In theory, g++ could use
636 // -finit-priority to order this stuff correctly without going
637 // through these machinations.
638 class Init
639 {
640 friend class ios_base;
641 public:
642 Init();
643 ~Init();
644
645#if __cplusplus >= 201103L
646 Init(const Init&) = default;
647 Init& operator=(const Init&) = default;
648#endif
649
650 private:
651 static _Atomic_word _S_refcount;
652 static bool _S_synced_with_stdio;
653 };
654
655 // [27.4.2.2] fmtflags state functions
656 /**
657 * @brief Access to format flags.
658 * @return The format control flags for both input and output.
659 */
661 flags() const
662 { return _M_flags; }
663
664 /**
665 * @brief Setting new format flags all at once.
666 * @param __fmtfl The new flags to set.
667 * @return The previous format control flags.
668 *
669 * This function overwrites all the format flags with @a __fmtfl.
670 */
672 flags(fmtflags __fmtfl)
673 {
674 fmtflags __old = _M_flags;
675 _M_flags = __fmtfl;
676 return __old;
677 }
678
679 /**
680 * @brief Setting new format flags.
681 * @param __fmtfl Additional flags to set.
682 * @return The previous format control flags.
683 *
684 * This function sets additional flags in format control. Flags that
685 * were previously set remain set.
686 */
688 setf(fmtflags __fmtfl)
689 {
690 fmtflags __old = _M_flags;
691 _M_flags |= __fmtfl;
692 return __old;
693 }
694
695 /**
696 * @brief Setting new format flags.
697 * @param __fmtfl Additional flags to set.
698 * @param __mask The flags mask for @a fmtfl.
699 * @return The previous format control flags.
700 *
701 * This function clears @a mask in the format flags, then sets
702 * @a fmtfl @c & @a mask. An example mask is @c ios_base::adjustfield.
703 */
705 setf(fmtflags __fmtfl, fmtflags __mask)
706 {
707 fmtflags __old = _M_flags;
708 _M_flags &= ~__mask;
709 _M_flags |= (__fmtfl & __mask);
710 return __old;
711 }
712
713 /**
714 * @brief Clearing format flags.
715 * @param __mask The flags to unset.
716 *
717 * This function clears @a __mask in the format flags.
718 */
719 void
720 unsetf(fmtflags __mask)
721 { _M_flags &= ~__mask; }
722
723 /**
724 * @brief Flags access.
725 * @return The precision to generate on certain output operations.
726 *
727 * Be careful if you try to give a definition of @a precision here; see
728 * DR 189.
729 */
731 precision() const
732 { return _M_precision; }
733
734 /**
735 * @brief Changing flags.
736 * @param __prec The new precision value.
737 * @return The previous value of precision().
738 */
741 {
742 streamsize __old = _M_precision;
743 _M_precision = __prec;
744 return __old;
745 }
746
747 /**
748 * @brief Flags access.
749 * @return The minimum field width to generate on output operations.
750 *
751 * <em>Minimum field width</em> refers to the number of characters.
752 */
754 width() const
755 { return _M_width; }
756
757 /**
758 * @brief Changing flags.
759 * @param __wide The new width value.
760 * @return The previous value of width().
761 */
764 {
765 streamsize __old = _M_width;
766 _M_width = __wide;
767 return __old;
768 }
769
770 // [27.4.2.4] ios_base static members
771 /**
772 * @brief Interaction with the standard C I/O objects.
773 * @param __sync Whether to synchronize or not.
774 * @return True if the standard streams were previously synchronized.
775 *
776 * The synchronization referred to is @e only that between the standard
777 * C facilities (e.g., stdout) and the standard C++ objects (e.g.,
778 * cout). User-declared streams are unaffected. See
779 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/fstreams.html#std.io.filestreams.binary
780 */
781 static bool
782 sync_with_stdio(bool __sync = true);
783
784 // [27.4.2.3] ios_base locale functions
785 /**
786 * @brief Setting a new locale.
787 * @param __loc The new locale.
788 * @return The previous locale.
789 *
790 * Sets the new locale for this stream, and then invokes each callback
791 * with imbue_event.
792 */
793 locale
794 imbue(const locale& __loc) throw();
795
796 /**
797 * @brief Locale access
798 * @return A copy of the current locale.
799 *
800 * If @c imbue(loc) has previously been called, then this function
801 * returns @c loc. Otherwise, it returns a copy of @c std::locale(),
802 * the global C++ locale.
803 */
804 locale
805 getloc() const
806 { return _M_ios_locale; }
807
808 /**
809 * @brief Locale access
810 * @return A reference to the current locale.
811 *
812 * Like getloc above, but returns a reference instead of
813 * generating a copy.
814 */
815 const locale&
816 _M_getloc() const
817 { return _M_ios_locale; }
818
819 // [27.4.2.5] ios_base storage functions
820 /**
821 * @brief Access to unique indices.
822 * @return An integer different from all previous calls.
823 *
824 * This function returns a unique integer every time it is called. It
825 * can be used for any purpose, but is primarily intended to be a unique
826 * index for the iword and pword functions. The expectation is that an
827 * application calls xalloc in order to obtain an index in the iword and
828 * pword arrays that can be used without fear of conflict.
829 *
830 * The implementation maintains a static variable that is incremented and
831 * returned on each invocation. xalloc is guaranteed to return an index
832 * that is safe to use in the iword and pword arrays.
833 */
834 static int
835 xalloc() throw();
836
837 /**
838 * @brief Access to integer array.
839 * @param __ix Index into the array.
840 * @return A reference to an integer associated with the index.
841 *
842 * The iword function provides access to an array of integers that can be
843 * used for any purpose. The array grows as required to hold the
844 * supplied index. All integers in the array are initialized to 0.
845 *
846 * The implementation reserves several indices. You should use xalloc to
847 * obtain an index that is safe to use. Also note that since the array
848 * can grow dynamically, it is not safe to hold onto the reference.
849 */
850 long&
851 iword(int __ix)
852 {
853 _Words& __word = ((unsigned)__ix < (unsigned)_M_word_size)
854 ? _M_word[__ix] : _M_grow_words(__ix, true);
855 return __word._M_iword;
856 }
857
858 /**
859 * @brief Access to void pointer array.
860 * @param __ix Index into the array.
861 * @return A reference to a void* associated with the index.
862 *
863 * The pword function provides access to an array of pointers that can be
864 * used for any purpose. The array grows as required to hold the
865 * supplied index. All pointers in the array are initialized to 0.
866 *
867 * The implementation reserves several indices. You should use xalloc to
868 * obtain an index that is safe to use. Also note that since the array
869 * can grow dynamically, it is not safe to hold onto the reference.
870 */
871 void*&
872 pword(int __ix)
873 {
874 _Words& __word = ((unsigned)__ix < (unsigned)_M_word_size)
875 ? _M_word[__ix] : _M_grow_words(__ix, false);
876 return __word._M_pword;
877 }
878
879 // Destructor
880 /**
881 * Invokes each callback with erase_event. Destroys local storage.
882 *
883 * Note that the ios_base object for the standard streams never gets
884 * destroyed. As a result, any callbacks registered with the standard
885 * streams will not get invoked with erase_event (unless copyfmt is
886 * used).
887 */
888 virtual ~ios_base();
889
890 protected:
891 ios_base() throw ();
892
893#if __cplusplus < 201103L
894 // _GLIBCXX_RESOLVE_LIB_DEFECTS
895 // 50. Copy constructor and assignment operator of ios_base
896 private:
897 ios_base(const ios_base&);
898
899 ios_base&
900 operator=(const ios_base&);
901#else
902 public:
903 ios_base(const ios_base&) = delete;
904
905 ios_base&
906 operator=(const ios_base&) = delete;
907
908 protected:
909 void
910 _M_move(ios_base&) noexcept;
911
912 void
913 _M_swap(ios_base& __rhs) noexcept;
914#endif
915 };
916
917 // [27.4.5.1] fmtflags manipulators
918 /// Calls base.setf(ios_base::boolalpha).
919 inline ios_base&
921 {
922 __base.setf(ios_base::boolalpha);
923 return __base;
924 }
925
926 /// Calls base.unsetf(ios_base::boolalpha).
927 inline ios_base&
929 {
930 __base.unsetf(ios_base::boolalpha);
931 return __base;
932 }
933
934 /// Calls base.setf(ios_base::showbase).
935 inline ios_base&
937 {
938 __base.setf(ios_base::showbase);
939 return __base;
940 }
941
942 /// Calls base.unsetf(ios_base::showbase).
943 inline ios_base&
945 {
946 __base.unsetf(ios_base::showbase);
947 return __base;
948 }
949
950 /// Calls base.setf(ios_base::showpoint).
951 inline ios_base&
953 {
954 __base.setf(ios_base::showpoint);
955 return __base;
956 }
957
958 /// Calls base.unsetf(ios_base::showpoint).
959 inline ios_base&
961 {
962 __base.unsetf(ios_base::showpoint);
963 return __base;
964 }
965
966 /// Calls base.setf(ios_base::showpos).
967 inline ios_base&
969 {
970 __base.setf(ios_base::showpos);
971 return __base;
972 }
973
974 /// Calls base.unsetf(ios_base::showpos).
975 inline ios_base&
977 {
978 __base.unsetf(ios_base::showpos);
979 return __base;
980 }
981
982 /// Calls base.setf(ios_base::skipws).
983 inline ios_base&
985 {
986 __base.setf(ios_base::skipws);
987 return __base;
988 }
989
990 /// Calls base.unsetf(ios_base::skipws).
991 inline ios_base&
993 {
994 __base.unsetf(ios_base::skipws);
995 return __base;
996 }
997
998 /// Calls base.setf(ios_base::uppercase).
999 inline ios_base&
1001 {
1002 __base.setf(ios_base::uppercase);
1003 return __base;
1004 }
1005
1006 /// Calls base.unsetf(ios_base::uppercase).
1007 inline ios_base&
1009 {
1010 __base.unsetf(ios_base::uppercase);
1011 return __base;
1012 }
1013
1014 /// Calls base.setf(ios_base::unitbuf).
1015 inline ios_base&
1017 {
1018 __base.setf(ios_base::unitbuf);
1019 return __base;
1020 }
1021
1022 /// Calls base.unsetf(ios_base::unitbuf).
1023 inline ios_base&
1025 {
1026 __base.unsetf(ios_base::unitbuf);
1027 return __base;
1028 }
1029
1030 // [27.4.5.2] adjustfield manipulators
1031 /// Calls base.setf(ios_base::internal, ios_base::adjustfield).
1032 inline ios_base&
1034 {
1036 return __base;
1037 }
1038
1039 /// Calls base.setf(ios_base::left, ios_base::adjustfield).
1040 inline ios_base&
1042 {
1044 return __base;
1045 }
1046
1047 /// Calls base.setf(ios_base::right, ios_base::adjustfield).
1048 inline ios_base&
1050 {
1052 return __base;
1053 }
1054
1055 // [27.4.5.3] basefield manipulators
1056 /// Calls base.setf(ios_base::dec, ios_base::basefield).
1057 inline ios_base&
1058 dec(ios_base& __base)
1059 {
1060 __base.setf(ios_base::dec, ios_base::basefield);
1061 return __base;
1062 }
1063
1064 /// Calls base.setf(ios_base::hex, ios_base::basefield).
1065 inline ios_base&
1066 hex(ios_base& __base)
1067 {
1068 __base.setf(ios_base::hex, ios_base::basefield);
1069 return __base;
1070 }
1071
1072 /// Calls base.setf(ios_base::oct, ios_base::basefield).
1073 inline ios_base&
1074 oct(ios_base& __base)
1075 {
1076 __base.setf(ios_base::oct, ios_base::basefield);
1077 return __base;
1078 }
1079
1080 // [27.4.5.4] floatfield manipulators
1081 /// Calls base.setf(ios_base::fixed, ios_base::floatfield).
1082 inline ios_base&
1084 {
1086 return __base;
1087 }
1088
1089 /// Calls base.setf(ios_base::scientific, ios_base::floatfield).
1090 inline ios_base&
1092 {
1094 return __base;
1095 }
1096
1097#if __cplusplus >= 201103L
1098 // New C++11 floatfield manipulators
1099
1100 /// Calls
1101 /// base.setf(ios_base::fixed|ios_base::scientific, ios_base::floatfield)
1102 inline ios_base&
1104 {
1106 return __base;
1107 }
1108
1109 /// Calls @c base.unsetf(ios_base::floatfield)
1110 inline ios_base&
1112 {
1113 __base.unsetf(ios_base::floatfield);
1114 return __base;
1115 }
1116#endif
1117
1118_GLIBCXX_END_NAMESPACE_VERSION
1119} // namespace
1120
1121#endif /* _IOS_BASE_H */
error_condition make_error_condition(future_errc __errc) noexcept
Overload of make_error_condition for future_errc.
Definition future:100
error_code make_error_code(future_errc __errc) noexcept
Overload of make_error_code for future_errc.
Definition future:94
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:111
ISO C++ entities toplevel namespace is std.
ios_base & scientific(ios_base &__base)
Calls base.setf(ios_base::scientific, ios_base::floatfield).
Definition ios_base.h:1091
ios_base & internal(ios_base &__base)
Calls base.setf(ios_base::internal, ios_base::adjustfield).
Definition ios_base.h:1033
ios_base & noskipws(ios_base &__base)
Calls base.unsetf(ios_base::skipws).
Definition ios_base.h:992
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition postypes.h:68
ios_base & hex(ios_base &__base)
Calls base.setf(ios_base::hex, ios_base::basefield).
Definition ios_base.h:1066
ios_base & left(ios_base &__base)
Calls base.setf(ios_base::left, ios_base::adjustfield).
Definition ios_base.h:1041
ios_base & noshowpoint(ios_base &__base)
Calls base.unsetf(ios_base::showpoint).
Definition ios_base.h:960
ios_base & unitbuf(ios_base &__base)
Calls base.setf(ios_base::unitbuf).
Definition ios_base.h:1016
ios_base & nounitbuf(ios_base &__base)
Calls base.unsetf(ios_base::unitbuf).
Definition ios_base.h:1024
ios_base & showbase(ios_base &__base)
Calls base.setf(ios_base::showbase).
Definition ios_base.h:936
long long streamoff
Type used by fpos, char_traits<char>, and char_traits<wchar_t>.
Definition postypes.h:64
io_errc
I/O error code.
Definition ios_base.h:204
ios_base & noshowpos(ios_base &__base)
Calls base.unsetf(ios_base::showpos).
Definition ios_base.h:976
ios_base & nouppercase(ios_base &__base)
Calls base.unsetf(ios_base::uppercase).
Definition ios_base.h:1008
ios_base & noshowbase(ios_base &__base)
Calls base.unsetf(ios_base::showbase).
Definition ios_base.h:944
ios_base & oct(ios_base &__base)
Calls base.setf(ios_base::oct, ios_base::basefield).
Definition ios_base.h:1074
ios_base & dec(ios_base &__base)
Calls base.setf(ios_base::dec, ios_base::basefield).
Definition ios_base.h:1058
ios_base & right(ios_base &__base)
Calls base.setf(ios_base::right, ios_base::adjustfield).
Definition ios_base.h:1049
constexpr bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1573
ios_base & showpoint(ios_base &__base)
Calls base.setf(ios_base::showpoint).
Definition ios_base.h:952
ios_base & skipws(ios_base &__base)
Calls base.setf(ios_base::skipws).
Definition ios_base.h:984
ios_base & fixed(ios_base &__base)
Calls base.setf(ios_base::fixed, ios_base::floatfield).
Definition ios_base.h:1083
ios_base & showpos(ios_base &__base)
Calls base.setf(ios_base::showpos).
Definition ios_base.h:968
ios_base & hexfloat(ios_base &__base)
Calls base.setf(ios_base::fixed|ios_basescientific, ios_base::floatfield)
Definition ios_base.h:1103
ios_base & defaultfloat(ios_base &__base)
Calls base.unsetf(ios_base::floatfield)
Definition ios_base.h:1111
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1563
ios_base & uppercase(ios_base &__base)
Calls base.setf(ios_base::uppercase).
Definition ios_base.h:1000
ios_base & boolalpha(ios_base &__base)
Calls base.setf(ios_base::boolalpha).
Definition ios_base.h:920
ios_base & noboolalpha(ios_base &__base)
Calls base.unsetf(ios_base::boolalpha).
Definition ios_base.h:928
constexpr bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition bitset:1553
One of two subclasses of exception.
Definition stdexcept:220
An exception type that includes an error_code value.
Definition system_error:557
Base class for all library exceptions.
Definition exception.h:60
The base of the I/O class hierarchy.
Definition ios_base.h:234
fmtflags setf(fmtflags __fmtfl)
Setting new format flags.
Definition ios_base.h:688
static const fmtflags skipws
Skips leading white space before certain input operations.
Definition ios_base.h:391
_Ios_Fmtflags fmtflags
This is a bitmask type.
Definition ios_base.h:346
_Ios_Iostate iostate
This is a bitmask type.
Definition ios_base.h:421
static const fmtflags hex
Converts integer input or generates integer output in hexadecimal base.
Definition ios_base.h:358
static const fmtflags right
Adds fill characters on the left (initial positions) of certain generated output. (I....
Definition ios_base.h:374
static const seekdir cur
Request a seek relative to the current position within the sequence.
Definition ios_base.h:497
static const fmtflags uppercase
Replaces certain lowercase letters with their uppercase equivalents in generated output.
Definition ios_base.h:398
static const fmtflags basefield
A mask of dec|oct|hex. Useful for the 2-arg form of setf.
Definition ios_base.h:404
void *& pword(int __ix)
Access to void pointer array.
Definition ios_base.h:872
static const seekdir beg
Request a seek relative to the beginning of the stream.
Definition ios_base.h:494
streamsize precision() const
Flags access.
Definition ios_base.h:731
locale imbue(const locale &__loc)
Setting a new locale.
static const fmtflags dec
Converts integer input or generates integer output in decimal base.
Definition ios_base.h:352
static int xalloc()
Access to unique indices.
event
The set of events that may be passed to an event callback.
Definition ios_base.h:525
fmtflags flags(fmtflags __fmtfl)
Setting new format flags all at once.
Definition ios_base.h:672
void unsetf(fmtflags __mask)
Clearing format flags.
Definition ios_base.h:720
static const fmtflags showpoint
Generates a decimal-point character unconditionally in generated floating-point output.
Definition ios_base.h:385
static const seekdir end
Request a seek relative to the current end of the sequence.
Definition ios_base.h:500
void register_callback(event_callback __fn, int __index)
Add the callback __fn with parameter __index.
static const openmode in
Open for input. Default for ifstream and fstream.
Definition ios_base.h:466
static const fmtflags showbase
Generates a prefix indicating the numeric base of generated integer output.
Definition ios_base.h:381
void(* event_callback)(event __e, ios_base &__b, int __i)
The type of an event callback function.
Definition ios_base.h:541
static const fmtflags internal
Adds fill characters at a designated internal point in certain generated output, or identical to righ...
Definition ios_base.h:363
long & iword(int __ix)
Access to integer array.
Definition ios_base.h:851
static const openmode out
Open for output. Default for ofstream and fstream.
Definition ios_base.h:469
static const fmtflags boolalpha
Insert/extract bool in alphabetic rather than numeric format.
Definition ios_base.h:349
virtual ~ios_base()
fmtflags flags() const
Access to format flags.
Definition ios_base.h:661
static const iostate eofbit
Indicates that an input operation reached the end of an input sequence.
Definition ios_base.h:428
static const fmtflags floatfield
A mask of scientific|fixed. Useful for the 2-arg form of setf.
Definition ios_base.h:407
fmtflags setf(fmtflags __fmtfl, fmtflags __mask)
Setting new format flags.
Definition ios_base.h:705
static const openmode binary
Perform input and output in binary mode (as opposed to text mode). This is probably not what you thin...
Definition ios_base.h:463
static const iostate goodbit
Indicates all is well.
Definition ios_base.h:436
const locale & _M_getloc() const
Locale access.
Definition ios_base.h:816
static const fmtflags unitbuf
Flushes output after each output operation.
Definition ios_base.h:394
static const iostate badbit
Indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error fro...
Definition ios_base.h:425
_Ios_Openmode openmode
This is a bitmask type.
Definition ios_base.h:452
streamsize width() const
Flags access.
Definition ios_base.h:754
static bool sync_with_stdio(bool __sync=true)
Interaction with the standard C I/O objects.
static const fmtflags fixed
Generate floating-point output in fixed-point notation.
Definition ios_base.h:355
static const fmtflags oct
Converts integer input or generates integer output in octal base.
Definition ios_base.h:370
static const openmode app
Seek to end before each write.
Definition ios_base.h:455
_Ios_Seekdir seekdir
This is an enumerated type.
Definition ios_base.h:491
streamsize width(streamsize __wide)
Changing flags.
Definition ios_base.h:763
static const fmtflags left
Adds fill characters on the right (final positions) of certain generated output. (I....
Definition ios_base.h:367
static const fmtflags showpos
Generates a + sign in non-negative generated numeric output.
Definition ios_base.h:388
static const openmode ate
Open and seek to end immediately after opening.
Definition ios_base.h:458
locale getloc() const
Locale access.
Definition ios_base.h:805
static const openmode trunc
Truncate an existing stream when opening. Default for ofstream.
Definition ios_base.h:472
streamsize precision(streamsize __prec)
Changing flags.
Definition ios_base.h:740
static const iostate failbit
Indicates that an input operation failed to read the expected characters, or that an output operation...
Definition ios_base.h:433
static const fmtflags scientific
Generates floating-point output in scientific notation.
Definition ios_base.h:377
static const fmtflags adjustfield
A mask of left|right|internal. Useful for the 2-arg form of setf.
Definition ios_base.h:401
These are thrown to indicate problems with io.
Definition ios_base.h:282
virtual const char * what() const
Container class for localization functionality.
Class representing stream positions.
Definition postypes.h:83