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