libstdc++
ios_base.h
Go to the documentation of this file.
1 // Iostreams base classes -*- C++ -*-
2 
3 // Copyright (C) 1997-2025 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 #ifdef _GLIBCXX_SYSHDR
38 #pragma GCC system_header
39 #endif
40 
41 #include <ext/atomicity.h>
42 #include <bits/localefwd.h>
43 #include <bits/locale_classes.h>
44 
45 #if __cplusplus < 201103L
46 # include <stdexcept>
47 #else
48 # include <system_error>
49 #endif
50 
51 namespace std _GLIBCXX_VISIBILITY(default)
52 {
53 _GLIBCXX_BEGIN_NAMESPACE_VERSION
54 
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
60  {
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__
82  };
83 
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)); }
88 
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)); }
93 
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)); }
98 
99  _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
100  inline _Ios_Fmtflags
101  operator~(_Ios_Fmtflags __a) _GLIBCXX_NOTHROW
102  { return _Ios_Fmtflags(~static_cast<int>(__a)); }
103 
104  _GLIBCXX14_CONSTEXPR
105  inline const _Ios_Fmtflags&
106  operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
107  { return __a = __a | __b; }
108 
109  _GLIBCXX14_CONSTEXPR
110  inline const _Ios_Fmtflags&
111  operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
112  { return __a = __a & __b; }
113 
114  _GLIBCXX14_CONSTEXPR
115  inline const _Ios_Fmtflags&
116  operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) _GLIBCXX_NOTHROW
117  { return __a = __a ^ __b; }
118 
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
126 
127  enum __attribute__((__flag_enum__)) _Ios_Openmode
128  {
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__
139  };
140 
141 #undef _NOREPLACE_UNUSED
142 
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)); }
147 
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)); }
152 
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)); }
157 
158  _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
159  inline _Ios_Openmode
160  operator~(_Ios_Openmode __a) _GLIBCXX_NOTHROW
161  { return _Ios_Openmode(~static_cast<int>(__a)); }
162 
163  _GLIBCXX14_CONSTEXPR
164  inline const _Ios_Openmode&
165  operator|=(_Ios_Openmode& __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
166  { return __a = __a | __b; }
167 
168  _GLIBCXX14_CONSTEXPR
169  inline const _Ios_Openmode&
170  operator&=(_Ios_Openmode& __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
171  { return __a = __a & __b; }
172 
173  _GLIBCXX14_CONSTEXPR
174  inline const _Ios_Openmode&
175  operator^=(_Ios_Openmode& __a, _Ios_Openmode __b) _GLIBCXX_NOTHROW
176  { return __a = __a ^ __b; }
177 
178 
179  enum _Ios_Iostate
180  {
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__
188  };
189 
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)); }
194 
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)); }
199 
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)); }
204 
205  _GLIBCXX_NODISCARD _GLIBCXX_CONSTEXPR
206  inline _Ios_Iostate
207  operator~(_Ios_Iostate __a) _GLIBCXX_NOTHROW
208  { return _Ios_Iostate(~static_cast<int>(__a)); }
209 
210  _GLIBCXX14_CONSTEXPR
211  inline const _Ios_Iostate&
212  operator|=(_Ios_Iostate& __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
213  { return __a = __a | __b; }
214 
215  _GLIBCXX14_CONSTEXPR
216  inline const _Ios_Iostate&
217  operator&=(_Ios_Iostate& __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
218  { return __a = __a & __b; }
219 
220  _GLIBCXX14_CONSTEXPR
221  inline const _Ios_Iostate&
222  operator^=(_Ios_Iostate& __a, _Ios_Iostate __b) _GLIBCXX_NOTHROW
223  { return __a = __a ^ __b; }
224 
225 
226  enum _Ios_Seekdir
227  {
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
232  };
233 
234 #if __cplusplus >= 201103L
235  /// I/O error code
236  enum class io_errc { stream = 1 };
237 
238  template <> struct is_error_code_enum<io_errc> : public true_type { };
239 
240  [[__nodiscard__, __gnu__::__const__]]
241  const error_category&
242  iostream_category() noexcept;
243 
244  [[__nodiscard__]]
245  inline error_code
246  make_error_code(io_errc __e) noexcept
247  { return error_code(static_cast<int>(__e), iostream_category()); }
248 
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
254 
255  // 27.4.2 Class ios_base
256  /**
257  * @brief The base of the I/O class hierarchy.
258  * @ingroup io
259  *
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).
264  */
265  class ios_base
266  {
267 #if _GLIBCXX_USE_CXX11_ABI
268 #if __cplusplus < 201103L
269  // Type that is layout-compatible with std::system_error
271  {
272  // Type that is layout-compatible with std::error_code
273  struct error_code
274  {
275  error_code() { }
276  private:
277  int _M_value;
278  const void* _M_cat;
279  } _M_code;
280  };
281 #endif
282 #endif
283  public:
284 
285  /**
286  * @brief These are thrown to indicate problems with io.
287  * @ingroup exceptions
288  *
289  * 27.4.2.1.1 Class ios_base::failure
290  */
291 #if _GLIBCXX_USE_CXX11_ABI
292  class _GLIBCXX_ABI_TAG_CXX11 failure : public system_error
293  {
294  public:
295  explicit
296  failure(const string& __str);
297 
298 #if __cplusplus >= 201103L
299  explicit
300  failure(const string&, const error_code&);
301 
302  explicit
303  failure(const char*, const error_code& = io_errc::stream);
304 #endif
305 
306  virtual
307  ~failure() throw();
308 
309  virtual const char*
310  what() const throw();
311  };
312 #else
313  class failure : public exception
314  {
315  public:
316  // _GLIBCXX_RESOLVE_LIB_DEFECTS
317  // 48. Use of non-existent exception constructor
318  explicit
319  failure(const string& __str) throw();
320 
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();
325 
326  virtual const char*
327  what() const throw();
328 
329 #if __cplusplus >= 201103L
330  // Define the new members required by C++11,
331  // even though the error_code cannot be stored.
332 
333  explicit
334  failure(const string& __s, const error_code&) noexcept
335  : failure(__s)
336  { }
337 
338  explicit
339  failure(const char* __s, const error_code& = error_code{})
340  : failure(string(__s))
341  { }
342 
343  // Stand-in for system_error::code() but returning by value.
344  error_code code() const noexcept { return error_code{}; }
345 #endif
346 
347  private:
348  string _M_msg;
349  };
350 #endif
351 
352  // 27.4.2.1.2 Type ios_base::fmtflags
353  /**
354  * @brief This is a bitmask type.
355  *
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
377  */
378  typedef _Ios_Fmtflags fmtflags;
379 
380  /// Insert/extract @c bool in alphabetic rather than numeric format.
381  static const fmtflags boolalpha = _S_boolalpha;
382 
383  /// Converts integer input or generates integer output in decimal base.
384  static const fmtflags dec = _S_dec;
385 
386  /// Generate floating-point output in fixed-point notation.
387  static const fmtflags fixed = _S_fixed;
388 
389  /// Converts integer input or generates integer output in hexadecimal base.
390  static const fmtflags hex = _S_hex;
391 
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;
396 
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;
400 
401  /// Converts integer input or generates integer output in octal base.
402  static const fmtflags oct = _S_oct;
403 
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;
407 
408  /// Generates floating-point output in scientific notation.
409  static const fmtflags scientific = _S_scientific;
410 
411  /// Generates a prefix indicating the numeric base of generated integer
412  /// output.
413  static const fmtflags showbase = _S_showbase;
414 
415  /// Generates a decimal-point character unconditionally in generated
416  /// floating-point output.
417  static const fmtflags showpoint = _S_showpoint;
418 
419  /// Generates a + sign in non-negative generated numeric output.
420  static const fmtflags showpos = _S_showpos;
421 
422  /// Skips leading white space before certain input operations.
423  static const fmtflags skipws = _S_skipws;
424 
425  /// Flushes output after each output operation.
426  static const fmtflags unitbuf = _S_unitbuf;
427 
428  /// Replaces certain lowercase letters with their uppercase equivalents
429  /// in generated output.
430  static const fmtflags uppercase = _S_uppercase;
431 
432  /// A mask of left|right|internal. Useful for the 2-arg form of @c setf.
433  static const fmtflags adjustfield = _S_adjustfield;
434 
435  /// A mask of dec|oct|hex. Useful for the 2-arg form of @c setf.
436  static const fmtflags basefield = _S_basefield;
437 
438  /// A mask of scientific|fixed. Useful for the 2-arg form of @c setf.
439  static const fmtflags floatfield = _S_floatfield;
440 
441  // 27.4.2.1.3 Type ios_base::iostate
442  /**
443  * @brief This is a bitmask type.
444  *
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
452  */
453  typedef _Ios_Iostate iostate;
454 
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;
458 
459  /// Indicates that an input operation reached the end of an input sequence.
460  static const iostate eofbit = _S_eofbit;
461 
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;
466 
467  /// Indicates all is well.
468  static const iostate goodbit = _S_goodbit;
469 
470  // 27.4.2.1.4 Type ios_base::openmode
471  /**
472  * @brief This is a bitmask type.
473  *
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
483  */
484  typedef _Ios_Openmode openmode;
485 
486  /// Seek to end before each write.
487  static const openmode app = _S_app;
488 
489  /// Open and seek to end immediately after opening.
490  static const openmode ate = _S_ate;
491 
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;
496 
497  /// Open for input. Default for @c ifstream and fstream.
498  static const openmode in = _S_in;
499 
500  /// Open for output. Default for @c ofstream and fstream.
501  static const openmode out = _S_out;
502 
503  /// Truncate an existing stream when opening. Default for @c ofstream.
504  static const openmode trunc = _S_trunc;
505 
506  static const openmode __noreplace = _S_noreplace;
507 
508 #ifdef __glibcxx_ios_noreplace // C++ >= 23 && HOSTED
509  /// Open a file in exclusive mode.
510  static const openmode noreplace = _S_noreplace;
511 #endif
512 
513  // 27.4.2.1.5 Type ios_base::seekdir
514  /**
515  * @brief This is an enumerated type.
516  *
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.
522  */
523  typedef _Ios_Seekdir seekdir;
524 
525  /// Request a seek relative to the beginning of the stream.
526  static const seekdir beg = _S_beg;
527 
528  /// Request a seek relative to the current position within the sequence.
529  static const seekdir cur = _S_cur;
530 
531  /// Request a seek relative to the current end of the sequence.
532  static const seekdir end = _S_end;
533 
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");
542 
543  typedef std::streampos streampos
544  _GLIBCXX_DEPRECATED_SUGGEST("std::streampos");
545  typedef std::streamoff streamoff
546  _GLIBCXX_DEPRECATED_SUGGEST("std::streamoff");
547 #endif
548 
549  // Callbacks;
550  /**
551  * @brief The set of events that may be passed to an event callback.
552  *
553  * erase_event is used during ~ios() and copyfmt(). imbue_event is used
554  * during imbue(). copyfmt_event is used during copyfmt().
555  */
556  enum event
557  {
558  erase_event,
559  imbue_event,
560  copyfmt_event
561  };
562 
563  /**
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.
568  *
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().
572  */
573  typedef void (*event_callback) (event __e, ios_base& __b, int __i);
574 
575  /**
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.
579  *
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.
584  */
585  void
586  register_callback(event_callback __fn, int __index);
587 
588  protected:
589  streamsize _M_precision;
590  streamsize _M_width;
591  fmtflags _M_flags;
592  iostate _M_exception;
593  iostate _M_streambuf_state;
594 
595  // 27.4.2.6 Members for callbacks
596  // 27.4.2.6 ios_base callbacks
597  struct _Callback_list
598  {
599  // Data Members
600  _Callback_list* _M_next;
602  int _M_index;
603  _Atomic_word _M_refcount; // 0 means one reference.
604 
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) { }
608 
609  void
610  _M_add_reference() { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
611 
612  // 0 => OK to delete.
613  int
614  _M_remove_reference()
615  {
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)
620  {
621  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount);
622  }
623  return __res;
624  }
625  };
626 
627  _Callback_list* _M_callbacks;
628 
629  void
630  _M_call_callbacks(event __ev) throw();
631 
632  void
633  _M_dispose_callbacks(void) throw();
634 
635  // 27.4.2.5 Members for iword/pword storage
636  struct _Words
637  {
638  void* _M_pword;
639  long _M_iword;
640  _Words() : _M_pword(0), _M_iword(0) { }
641  };
642 
643  // Only for failed iword/pword calls.
644  _Words _M_word_zero;
645 
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];
650 
651  // Allocated storage.
652  int _M_word_size;
653  _Words* _M_word;
654 
655  _Words&
656  _M_grow_words(int __index, bool __iword);
657 
658  // Members for locale and locale caching.
659  locale _M_ios_locale;
660 
661  void
662  _M_init() throw();
663 
664  public:
665 
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
671  {
672  friend class ios_base;
673  public:
674  Init();
675  ~Init();
676 
677 #if __cplusplus >= 201103L
678  Init(const Init&) = default;
679  Init& operator=(const Init&) = default;
680 #endif
681 
682  private:
683  static _Atomic_word _S_refcount;
684  static bool _S_synced_with_stdio;
685  };
686 
687  // [27.4.2.2] fmtflags state functions
688  /**
689  * @brief Access to format flags.
690  * @return The format control flags for both input and output.
691  */
692  _GLIBCXX_NODISCARD
693  fmtflags
694  flags() const
695  { return _M_flags; }
696 
697  /**
698  * @brief Setting new format flags all at once.
699  * @param __fmtfl The new flags to set.
700  * @return The previous format control flags.
701  *
702  * This function overwrites all the format flags with @a __fmtfl.
703  */
704  fmtflags
705  flags(fmtflags __fmtfl)
706  {
707  fmtflags __old = _M_flags;
708  _M_flags = __fmtfl;
709  return __old;
710  }
711 
712  /**
713  * @brief Setting new format flags.
714  * @param __fmtfl Additional flags to set.
715  * @return The previous format control flags.
716  *
717  * This function sets additional flags in format control. Flags that
718  * were previously set remain set.
719  */
720  fmtflags
721  setf(fmtflags __fmtfl)
722  {
723  fmtflags __old = _M_flags;
724  _M_flags |= __fmtfl;
725  return __old;
726  }
727 
728  /**
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.
733  *
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.
736  */
737  fmtflags
738  setf(fmtflags __fmtfl, fmtflags __mask)
739  {
740  fmtflags __old = _M_flags;
741  _M_flags &= ~__mask;
742  _M_flags |= (__fmtfl & __mask);
743  return __old;
744  }
745 
746  /**
747  * @brief Clearing format flags.
748  * @param __mask The flags to unset.
749  *
750  * This function clears @a __mask in the format flags.
751  */
752  void
753  unsetf(fmtflags __mask)
754  { _M_flags &= ~__mask; }
755 
756  /**
757  * @brief Flags access.
758  * @return The precision to generate on certain output operations.
759  *
760  * Be careful if you try to give a definition of @a precision here; see
761  * DR 189.
762  */
763  _GLIBCXX_NODISCARD
764  streamsize
765  precision() const
766  { return _M_precision; }
767 
768  /**
769  * @brief Changing flags.
770  * @param __prec The new precision value.
771  * @return The previous value of precision().
772  */
773  streamsize
775  {
776  streamsize __old = _M_precision;
777  _M_precision = __prec;
778  return __old;
779  }
780 
781  /**
782  * @brief Flags access.
783  * @return The minimum field width to generate on output operations.
784  *
785  * <em>Minimum field width</em> refers to the number of characters.
786  */
787  _GLIBCXX_NODISCARD
788  streamsize
789  width() const
790  { return _M_width; }
791 
792  /**
793  * @brief Changing flags.
794  * @param __wide The new width value.
795  * @return The previous value of width().
796  */
797  streamsize
799  {
800  streamsize __old = _M_width;
801  _M_width = __wide;
802  return __old;
803  }
804 
805  // [27.4.2.4] ios_base static members
806  /**
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.
810  *
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
815  */
816  static bool
817  sync_with_stdio(bool __sync = true);
818 
819  // [27.4.2.3] ios_base locale functions
820  /**
821  * @brief Setting a new locale.
822  * @param __loc The new locale.
823  * @return The previous locale.
824  *
825  * Sets the new locale for this stream, and then invokes each callback
826  * with imbue_event.
827  */
828  locale
829  imbue(const locale& __loc) throw();
830 
831  /**
832  * @brief Locale access
833  * @return A copy of the current locale.
834  *
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.
838  */
839  _GLIBCXX_NODISCARD
840  locale
841  getloc() const
842  { return _M_ios_locale; }
843 
844  /**
845  * @brief Locale access
846  * @return A reference to the current locale.
847  *
848  * Like getloc above, but returns a reference instead of
849  * generating a copy.
850  */
851  const locale&
852  _M_getloc() const
853  { return _M_ios_locale; }
854 
855  // [27.4.2.5] ios_base storage functions
856  /**
857  * @brief Access to unique indices.
858  * @return An integer different from all previous calls.
859  *
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.
865  *
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.
869  */
870  static int
871  xalloc() throw();
872 
873  /**
874  * @brief Access to integer array.
875  * @param __ix Index into the array.
876  * @return A reference to an integer associated with the index.
877  *
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.
881  *
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.
885  */
886  long&
887  iword(int __ix)
888  {
889  _Words& __word = ((unsigned)__ix < (unsigned)_M_word_size)
890  ? _M_word[__ix] : _M_grow_words(__ix, true);
891  return __word._M_iword;
892  }
893 
894  /**
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.
898  *
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.
902  *
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.
906  */
907  void*&
908  pword(int __ix)
909  {
910  _Words& __word = ((unsigned)__ix < (unsigned)_M_word_size)
911  ? _M_word[__ix] : _M_grow_words(__ix, false);
912  return __word._M_pword;
913  }
914 
915  // Destructor
916  /**
917  * Invokes each callback with erase_event. Destroys local storage.
918  *
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).
923  */
924  virtual ~ios_base();
925 
926  protected:
927  ios_base() throw ();
928 
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&);
934 
935  ios_base&
936  operator=(const ios_base&);
937 #else
938  public:
939  ios_base(const ios_base&) = delete;
940 
941  ios_base&
942  operator=(const ios_base&) = delete;
943 
944  protected:
945  void
946  _M_move(ios_base&) noexcept;
947 
948  void
949  _M_swap(ios_base& __rhs) noexcept;
950 #endif
951  };
952 
953  // [27.4.5.1] fmtflags manipulators
954  /// Calls base.setf(ios_base::boolalpha).
955  inline ios_base&
957  {
959  return __base;
960  }
961 
962  /// Calls base.unsetf(ios_base::boolalpha).
963  inline ios_base&
965  {
966  __base.unsetf(ios_base::boolalpha);
967  return __base;
968  }
969 
970  /// Calls base.setf(ios_base::showbase).
971  inline ios_base&
973  {
975  return __base;
976  }
977 
978  /// Calls base.unsetf(ios_base::showbase).
979  inline ios_base&
981  {
982  __base.unsetf(ios_base::showbase);
983  return __base;
984  }
985 
986  /// Calls base.setf(ios_base::showpoint).
987  inline ios_base&
989  {
991  return __base;
992  }
993 
994  /// Calls base.unsetf(ios_base::showpoint).
995  inline ios_base&
997  {
998  __base.unsetf(ios_base::showpoint);
999  return __base;
1000  }
1001 
1002  /// Calls base.setf(ios_base::showpos).
1003  inline ios_base&
1005  {
1006  __base.setf(ios_base::showpos);
1007  return __base;
1008  }
1009 
1010  /// Calls base.unsetf(ios_base::showpos).
1011  inline ios_base&
1013  {
1014  __base.unsetf(ios_base::showpos);
1015  return __base;
1016  }
1017 
1018  /// Calls base.setf(ios_base::skipws).
1019  inline ios_base&
1021  {
1022  __base.setf(ios_base::skipws);
1023  return __base;
1024  }
1025 
1026  /// Calls base.unsetf(ios_base::skipws).
1027  inline ios_base&
1029  {
1030  __base.unsetf(ios_base::skipws);
1031  return __base;
1032  }
1033 
1034  /// Calls base.setf(ios_base::uppercase).
1035  inline ios_base&
1037  {
1039  return __base;
1040  }
1041 
1042  /// Calls base.unsetf(ios_base::uppercase).
1043  inline ios_base&
1045  {
1046  __base.unsetf(ios_base::uppercase);
1047  return __base;
1048  }
1049 
1050  /// Calls base.setf(ios_base::unitbuf).
1051  inline ios_base&
1053  {
1054  __base.setf(ios_base::unitbuf);
1055  return __base;
1056  }
1057 
1058  /// Calls base.unsetf(ios_base::unitbuf).
1059  inline ios_base&
1061  {
1062  __base.unsetf(ios_base::unitbuf);
1063  return __base;
1064  }
1065 
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)
1070  {
1072  return __base;
1073  }
1074 
1075  /// Calls base.setf(ios_base::left, ios_base::adjustfield).
1076  inline ios_base&
1078  {
1080  return __base;
1081  }
1082 
1083  /// Calls base.setf(ios_base::right, ios_base::adjustfield).
1084  inline ios_base&
1086  {
1088  return __base;
1089  }
1090 
1091  // [27.4.5.3] basefield manipulators
1092  /// Calls base.setf(ios_base::dec, ios_base::basefield).
1093  inline ios_base&
1095  {
1097  return __base;
1098  }
1099 
1100  /// Calls base.setf(ios_base::hex, ios_base::basefield).
1101  inline ios_base&
1103  {
1105  return __base;
1106  }
1107 
1108  /// Calls base.setf(ios_base::oct, ios_base::basefield).
1109  inline ios_base&
1111  {
1113  return __base;
1114  }
1115 
1116  // [27.4.5.4] floatfield manipulators
1117  /// Calls base.setf(ios_base::fixed, ios_base::floatfield).
1118  inline ios_base&
1120  {
1122  return __base;
1123  }
1124 
1125  /// Calls base.setf(ios_base::scientific, ios_base::floatfield).
1126  inline ios_base&
1128  {
1130  return __base;
1131  }
1132 
1133 #if __cplusplus >= 201103L
1134  // New C++11 floatfield manipulators
1135 
1136  /// Calls
1137  /// base.setf(ios_base::fixed|ios_base::scientific, ios_base::floatfield)
1138  inline ios_base&
1140  {
1142  return __base;
1143  }
1144 
1145  /// Calls @c base.unsetf(ios_base::floatfield)
1146  inline ios_base&
1148  {
1149  __base.unsetf(ios_base::floatfield);
1150  return __base;
1151  }
1152 #endif
1153 
1154 _GLIBCXX_END_NAMESPACE_VERSION
1155 } // namespace
1156 
1157 #endif /* _IOS_BASE_H */
error_condition make_error_condition(future_errc __errc) noexcept
Overload of make_error_condition for future_errc.
Definition: future:102
error_code make_error_code(future_errc __errc) noexcept
Overload of make_error_code for future_errc.
Definition: future:96
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:116
ISO C++ entities toplevel namespace is std.
constexpr bitset< _Nb > operator&(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition: bitset:1562
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition: postypes.h:73
ios_base & nouppercase(ios_base &__base)
Calls base.unsetf(ios_base::uppercase).
Definition: ios_base.h:1044
ios_base & showpoint(ios_base &__base)
Calls base.setf(ios_base::showpoint).
Definition: ios_base.h:988
ios_base & right(ios_base &__base)
Calls base.setf(ios_base::right, ios_base::adjustfield).
Definition: ios_base.h:1085
ios_base & nounitbuf(ios_base &__base)
Calls base.unsetf(ios_base::unitbuf).
Definition: ios_base.h:1060
ios_base & hexfloat(ios_base &__base)
Calls base.setf(ios_base::fixed|ios_basescientific, ios_base::floatfield)
Definition: ios_base.h:1139
ios_base & scientific(ios_base &__base)
Calls base.setf(ios_base::scientific, ios_base::floatfield).
Definition: ios_base.h:1127
ios_base & noskipws(ios_base &__base)
Calls base.unsetf(ios_base::skipws).
Definition: ios_base.h:1028
ios_base & dec(ios_base &__base)
Calls base.setf(ios_base::dec, ios_base::basefield).
Definition: ios_base.h:1094
long long streamoff
Type used by fpos, char_traits<char>, and char_traits<wchar_t>.
Definition: postypes.h:68
io_errc
I/O error code.
Definition: ios_base.h:236
ios_base & showbase(ios_base &__base)
Calls base.setf(ios_base::showbase).
Definition: ios_base.h:972
ios_base & oct(ios_base &__base)
Calls base.setf(ios_base::oct, ios_base::basefield).
Definition: ios_base.h:1110
ios_base & hex(ios_base &__base)
Calls base.setf(ios_base::hex, ios_base::basefield).
Definition: ios_base.h:1102
constexpr bitset< _Nb > operator|(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition: bitset:1572
ios_base & defaultfloat(ios_base &__base)
Calls base.unsetf(ios_base::floatfield)
Definition: ios_base.h:1147
constexpr bitset< _Nb > operator^(const bitset< _Nb > &__x, const bitset< _Nb > &__y) noexcept
Global bitwise operations on bitsets.
Definition: bitset:1582
ios_base & showpos(ios_base &__base)
Calls base.setf(ios_base::showpos).
Definition: ios_base.h:1004
ios_base & left(ios_base &__base)
Calls base.setf(ios_base::left, ios_base::adjustfield).
Definition: ios_base.h:1077
ios_base & boolalpha(ios_base &__base)
Calls base.setf(ios_base::boolalpha).
Definition: ios_base.h:956
ios_base & noshowpos(ios_base &__base)
Calls base.unsetf(ios_base::showpos).
Definition: ios_base.h:1012
ios_base & uppercase(ios_base &__base)
Calls base.setf(ios_base::uppercase).
Definition: ios_base.h:1036
ios_base & unitbuf(ios_base &__base)
Calls base.setf(ios_base::unitbuf).
Definition: ios_base.h:1052
ios_base & noshowpoint(ios_base &__base)
Calls base.unsetf(ios_base::showpoint).
Definition: ios_base.h:996
ios_base & skipws(ios_base &__base)
Calls base.setf(ios_base::skipws).
Definition: ios_base.h:1020
ios_base & noboolalpha(ios_base &__base)
Calls base.unsetf(ios_base::boolalpha).
Definition: ios_base.h:964
ios_base & noshowbase(ios_base &__base)
Calls base.unsetf(ios_base::showbase).
Definition: ios_base.h:980
ios_base & fixed(ios_base &__base)
Calls base.setf(ios_base::fixed, ios_base::floatfield).
Definition: ios_base.h:1119
constexpr _Iterator __base(_Iterator __it)
One of two subclasses of exception.
Definition: stdexcept:222
An exception type that includes an error_code value.
Definition: system_error:559
Base class for all library exceptions.
Definition: exception.h:62
The base of the I/O class hierarchy.
Definition: ios_base.h:266
fmtflags setf(fmtflags __fmtfl)
Setting new format flags.
Definition: ios_base.h:721
static const fmtflags skipws
Skips leading white space before certain input operations.
Definition: ios_base.h:423
_Ios_Fmtflags fmtflags
This is a bitmask type.
Definition: ios_base.h:378
_Ios_Iostate iostate
This is a bitmask type.
Definition: ios_base.h:453
static const fmtflags hex
Converts integer input or generates integer output in hexadecimal base.
Definition: ios_base.h:390
static const fmtflags right
Adds fill characters on the left (initial positions) of certain generated output. (I....
Definition: ios_base.h:406
static const seekdir cur
Request a seek relative to the current position within the sequence.
Definition: ios_base.h:529
static const fmtflags uppercase
Replaces certain lowercase letters with their uppercase equivalents in generated output.
Definition: ios_base.h:430
static const fmtflags basefield
A mask of dec|oct|hex. Useful for the 2-arg form of setf.
Definition: ios_base.h:436
static const seekdir beg
Request a seek relative to the beginning of the stream.
Definition: ios_base.h:526
streamsize precision() const
Flags access.
Definition: ios_base.h:765
long & iword(int __ix)
Access to integer array.
Definition: ios_base.h:887
locale imbue(const locale &__loc)
Setting a new locale.
const locale & _M_getloc() const
Locale access.
Definition: ios_base.h:852
static const fmtflags dec
Converts integer input or generates integer output in decimal base.
Definition: ios_base.h:384
static int xalloc()
Access to unique indices.
event
The set of events that may be passed to an event callback.
Definition: ios_base.h:557
fmtflags flags(fmtflags __fmtfl)
Setting new format flags all at once.
Definition: ios_base.h:705
void unsetf(fmtflags __mask)
Clearing format flags.
Definition: ios_base.h:753
static const fmtflags showpoint
Generates a decimal-point character unconditionally in generated floating-point output.
Definition: ios_base.h:417
static const seekdir end
Request a seek relative to the current end of the sequence.
Definition: ios_base.h:532
void register_callback(event_callback __fn, int __index)
Add the callback __fn with parameter __index.
void *& pword(int __ix)
Access to void pointer array.
Definition: ios_base.h:908
static const openmode in
Open for input. Default for ifstream and fstream.
Definition: ios_base.h:498
static const fmtflags showbase
Generates a prefix indicating the numeric base of generated integer output.
Definition: ios_base.h:413
void(* event_callback)(event __e, ios_base &__b, int __i)
The type of an event callback function.
Definition: ios_base.h:573
static const fmtflags internal
Adds fill characters at a designated internal point in certain generated output, or identical to righ...
Definition: ios_base.h:395
static const openmode out
Open for output. Default for ofstream and fstream.
Definition: ios_base.h:501
static const fmtflags boolalpha
Insert/extract bool in alphabetic rather than numeric format.
Definition: ios_base.h:381
virtual ~ios_base()
fmtflags flags() const
Access to format flags.
Definition: ios_base.h:694
static const iostate eofbit
Indicates that an input operation reached the end of an input sequence.
Definition: ios_base.h:460
static const fmtflags floatfield
A mask of scientific|fixed. Useful for the 2-arg form of setf.
Definition: ios_base.h:439
fmtflags setf(fmtflags __fmtfl, fmtflags __mask)
Setting new format flags.
Definition: ios_base.h:738
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:495
static const iostate goodbit
Indicates all is well.
Definition: ios_base.h:468
static const fmtflags unitbuf
Flushes output after each output operation.
Definition: ios_base.h:426
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:457
_Ios_Openmode openmode
This is a bitmask type.
Definition: ios_base.h:484
streamsize width() const
Flags access.
Definition: ios_base.h:789
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:387
static const fmtflags oct
Converts integer input or generates integer output in octal base.
Definition: ios_base.h:402
static const openmode app
Seek to end before each write.
Definition: ios_base.h:487
_Ios_Seekdir seekdir
This is an enumerated type.
Definition: ios_base.h:523
streamsize width(streamsize __wide)
Changing flags.
Definition: ios_base.h:798
static const fmtflags left
Adds fill characters on the right (final positions) of certain generated output. (I....
Definition: ios_base.h:399
static const fmtflags showpos
Generates a + sign in non-negative generated numeric output.
Definition: ios_base.h:420
static const openmode ate
Open and seek to end immediately after opening.
Definition: ios_base.h:490
locale getloc() const
Locale access.
Definition: ios_base.h:841
static const openmode trunc
Truncate an existing stream when opening. Default for ofstream.
Definition: ios_base.h:504
streamsize precision(streamsize __prec)
Changing flags.
Definition: ios_base.h:774
static const iostate failbit
Indicates that an input operation failed to read the expected characters, or that an output operation...
Definition: ios_base.h:465
static const fmtflags scientific
Generates floating-point output in scientific notation.
Definition: ios_base.h:409
static const fmtflags adjustfield
A mask of left|right|internal. Useful for the 2-arg form of setf.
Definition: ios_base.h:433
These are thrown to indicate problems with io.
Definition: ios_base.h:314
virtual const char * what() const
Container class for localization functionality.
Class representing stream positions.
Definition: postypes.h:88