1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 27.4 Iostreams base classes
36 * This is an internal header file, included by other library headers.
37 * You should not attempt to use it directly.
43 #pragma GCC system_header
45 #include <bits/atomicity.h>
46 #include <bits/localefwd.h>
47 #include <bits/locale_classes.h>
51 // The following definitions of bitmask types are enums, not ints,
52 // as permitted (but not required) in the standard, in order to provide
53 // better type safety in iostream calls. A side effect is that
54 // expressions involving them are no longer compile-time constants.
57 _S_boolalpha
= 1L << 0,
61 _S_internal
= 1L << 4,
65 _S_scientific
= 1L << 8,
66 _S_showbase
= 1L << 9,
67 _S_showpoint
= 1L << 10,
68 _S_showpos
= 1L << 11,
70 _S_unitbuf
= 1L << 13,
71 _S_uppercase
= 1L << 14,
72 _S_adjustfield
= _S_left
| _S_right
| _S_internal
,
73 _S_basefield
= _S_dec
| _S_oct
| _S_hex
,
74 _S_floatfield
= _S_scientific
| _S_fixed
,
75 _S_ios_fmtflags_end
= 1L << 16
79 operator&(_Ios_Fmtflags __a
, _Ios_Fmtflags __b
)
80 { return _Ios_Fmtflags(static_cast<int>(__a
) & static_cast<int>(__b
)); }
83 operator|(_Ios_Fmtflags __a
, _Ios_Fmtflags __b
)
84 { return _Ios_Fmtflags(static_cast<int>(__a
) | static_cast<int>(__b
)); }
87 operator^(_Ios_Fmtflags __a
, _Ios_Fmtflags __b
)
88 { return _Ios_Fmtflags(static_cast<int>(__a
) ^ static_cast<int>(__b
)); }
91 operator|=(_Ios_Fmtflags
& __a
, _Ios_Fmtflags __b
)
92 { return __a
= __a
| __b
; }
95 operator&=(_Ios_Fmtflags
& __a
, _Ios_Fmtflags __b
)
96 { return __a
= __a
& __b
; }
99 operator^=(_Ios_Fmtflags
& __a
, _Ios_Fmtflags __b
)
100 { return __a
= __a
^ __b
; }
103 operator~(_Ios_Fmtflags __a
)
104 { return _Ios_Fmtflags(~static_cast<int>(__a
)); }
115 _S_ios_openmode_end
= 1L << 16
119 operator&(_Ios_Openmode __a
, _Ios_Openmode __b
)
120 { return _Ios_Openmode(static_cast<int>(__a
) & static_cast<int>(__b
)); }
123 operator|(_Ios_Openmode __a
, _Ios_Openmode __b
)
124 { return _Ios_Openmode(static_cast<int>(__a
) | static_cast<int>(__b
)); }
127 operator^(_Ios_Openmode __a
, _Ios_Openmode __b
)
128 { return _Ios_Openmode(static_cast<int>(__a
) ^ static_cast<int>(__b
)); }
130 inline _Ios_Openmode
&
131 operator|=(_Ios_Openmode
& __a
, _Ios_Openmode __b
)
132 { return __a
= __a
| __b
; }
134 inline _Ios_Openmode
&
135 operator&=(_Ios_Openmode
& __a
, _Ios_Openmode __b
)
136 { return __a
= __a
& __b
; }
138 inline _Ios_Openmode
&
139 operator^=(_Ios_Openmode
& __a
, _Ios_Openmode __b
)
140 { return __a
= __a
^ __b
; }
143 operator~(_Ios_Openmode __a
)
144 { return _Ios_Openmode(~static_cast<int>(__a
)); }
152 _S_failbit
= 1L << 2,
153 _S_ios_iostate_end
= 1L << 16
157 operator&(_Ios_Iostate __a
, _Ios_Iostate __b
)
158 { return _Ios_Iostate(static_cast<int>(__a
) & static_cast<int>(__b
)); }
161 operator|(_Ios_Iostate __a
, _Ios_Iostate __b
)
162 { return _Ios_Iostate(static_cast<int>(__a
) | static_cast<int>(__b
)); }
165 operator^(_Ios_Iostate __a
, _Ios_Iostate __b
)
166 { return _Ios_Iostate(static_cast<int>(__a
) ^ static_cast<int>(__b
)); }
169 operator|=(_Ios_Iostate
& __a
, _Ios_Iostate __b
)
170 { return __a
= __a
| __b
; }
173 operator&=(_Ios_Iostate
& __a
, _Ios_Iostate __b
)
174 { return __a
= __a
& __b
; }
177 operator^=(_Ios_Iostate
& __a
, _Ios_Iostate __b
)
178 { return __a
= __a
^ __b
; }
181 operator~(_Ios_Iostate __a
)
182 { return _Ios_Iostate(~static_cast<int>(__a
)); }
189 _S_ios_seekdir_end
= 1L << 16
192 // 27.4.2 Class ios_base
194 * @brief The base of the I/O class hierarchy.
196 * This class defines everything that can be defined about I/O that does
197 * not depend on the type of characters being input or output. Most
198 * people will only see @c ios_base when they need to specify the full
199 * name of the various I/O flags (e.g., the openmodes).
205 // 27.4.2.1.1 Class ios_base::failure
206 /// These are thrown to indicate problems. Doc me.
207 class failure
: public exception
210 // _GLIBCXX_RESOLVE_LIB_DEFECTS
211 // 48. Use of non-existent exception constructor
213 failure(const string
& __str
) throw();
215 // This declaration is not useless:
216 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
221 what() const throw();
227 // 27.4.2.1.2 Type ios_base::fmtflags
229 * @brief This is a bitmask type.
231 * @c "_Ios_Fmtflags" is implementation-defined, but it is valid to
232 * perform bitwise operations on these values and expect the Right
233 * Thing to happen. Defined objects of type fmtflags are:
253 typedef _Ios_Fmtflags fmtflags
;
255 /// Insert/extract @c bool in alphabetic rather than numeric format.
256 static const fmtflags boolalpha
= _S_boolalpha
;
258 /// Converts integer input or generates integer output in decimal base.
259 static const fmtflags dec
= _S_dec
;
261 /// Generate floating-point output in fixed-point notation.
262 static const fmtflags fixed
= _S_fixed
;
264 /// Converts integer input or generates integer output in hexadecimal base.
265 static const fmtflags hex
= _S_hex
;
267 /// Adds fill characters at a designated internal point in certain
268 /// generated output, or identical to @c right if no such point is
270 static const fmtflags internal
= _S_internal
;
272 /// Adds fill characters on the right (final positions) of certain
273 /// generated output. (I.e., the thing you print is flush left.)
274 static const fmtflags left
= _S_left
;
276 /// Converts integer input or generates integer output in octal base.
277 static const fmtflags oct
= _S_oct
;
279 /// Adds fill characters on the left (initial positions) of certain
280 /// generated output. (I.e., the thing you print is flush right.)
281 static const fmtflags right
= _S_right
;
283 /// Generates floating-point output in scientific notation.
284 static const fmtflags scientific
= _S_scientific
;
286 /// Generates a prefix indicating the numeric base of generated integer
288 static const fmtflags showbase
= _S_showbase
;
290 /// Generates a decimal-point character unconditionally in generated
291 /// floating-point output.
292 static const fmtflags showpoint
= _S_showpoint
;
294 /// Generates a + sign in non-negative generated numeric output.
295 static const fmtflags showpos
= _S_showpos
;
297 /// Skips leading white space before certain input operations.
298 static const fmtflags skipws
= _S_skipws
;
300 /// Flushes output after each output operation.
301 static const fmtflags unitbuf
= _S_unitbuf
;
303 /// Replaces certain lowercase letters with their uppercase equivalents
304 /// in generated output.
305 static const fmtflags uppercase
= _S_uppercase
;
307 /// A mask of left|right|internal. Useful for the 2-arg form of @c setf.
308 static const fmtflags adjustfield
= _S_adjustfield
;
310 /// A mask of dec|oct|hex. Useful for the 2-arg form of @c setf.
311 static const fmtflags basefield
= _S_basefield
;
313 /// A mask of scientific|fixed. Useful for the 2-arg form of @c setf.
314 static const fmtflags floatfield
= _S_floatfield
;
316 // 27.4.2.1.3 Type ios_base::iostate
318 * @brief This is a bitmask type.
320 * @c "_Ios_Iostate" is implementation-defined, but it is valid to
321 * perform bitwise operations on these values and expect the Right
322 * Thing to happen. Defined objects of type iostate are:
328 typedef _Ios_Iostate iostate
;
330 /// Indicates a loss of integrity in an input or output sequence (such
331 /// as an irrecoverable read error from a file).
332 static const iostate badbit
= _S_badbit
;
334 /// Indicates that an input operation reached the end of an input sequence.
335 static const iostate eofbit
= _S_eofbit
;
337 /// Indicates that an input operation failed to read the expected
338 /// characters, or that an output operation failed to generate the
339 /// desired characters.
340 static const iostate failbit
= _S_failbit
;
342 /// Indicates all is well.
343 static const iostate goodbit
= _S_goodbit
;
345 // 27.4.2.1.4 Type ios_base::openmode
347 * @brief This is a bitmask type.
349 * @c "_Ios_Openmode" is implementation-defined, but it is valid to
350 * perform bitwise operations on these values and expect the Right
351 * Thing to happen. Defined objects of type openmode are:
359 typedef _Ios_Openmode openmode
;
361 /// Seek to end before each write.
362 static const openmode app
= _S_app
;
364 /// Open and seek to end immediately after opening.
365 static const openmode ate
= _S_ate
;
367 /// Perform input and output in binary mode (as opposed to text mode).
368 /// This is probably not what you think it is; see
369 /// http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#3 and
370 /// http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#7 for more.
371 static const openmode binary
= _S_bin
;
373 /// Open for input. Default for @c ifstream and fstream.
374 static const openmode in
= _S_in
;
376 /// Open for output. Default for @c ofstream and fstream.
377 static const openmode out
= _S_out
;
379 /// Open for input. Default for @c ofstream.
380 static const openmode trunc
= _S_trunc
;
382 // 27.4.2.1.5 Type ios_base::seekdir
384 * @brief This is an enumerated type.
386 * @c "_Ios_Seekdir" is implementation-defined. Defined values
387 * of type seekdir are:
389 * - cur, equivalent to @c SEEK_CUR in the C standard library.
390 * - end, equivalent to @c SEEK_END in the C standard library.
392 typedef _Ios_Seekdir seekdir
;
394 /// Request a seek relative to the beginning of the stream.
395 static const seekdir beg
= _S_beg
;
397 /// Request a seek relative to the current position within the sequence.
398 static const seekdir cur
= _S_cur
;
400 /// Request a seek relative to the current end of the sequence.
401 static const seekdir end
= _S_end
;
404 typedef int io_state
;
405 typedef int open_mode
;
406 typedef int seek_dir
;
408 typedef std::streampos streampos
;
409 typedef std::streamoff streamoff
;
413 * @brief The set of events that may be passed to an event callback.
415 * erase_event is used during ~ios() and copyfmt(). imbue_event is used
416 * during imbue(). copyfmt_event is used during copyfmt().
426 * @brief The type of an event callback function.
427 * @param event One of the members of the event enum.
428 * @param ios_base Reference to the ios_base object.
429 * @param int The integer provided when the callback was registered.
431 * Event callbacks are user defined functions that get called during
432 * several ios_base and basic_ios functions, specifically imbue(),
433 * copyfmt(), and ~ios().
435 typedef void (*event_callback
) (event
, ios_base
&, int);
438 * @brief Add the callback __fn with parameter __index.
439 * @param __fn The function to add.
440 * @param __index The integer to pass to the function when invoked.
442 * Registers a function as an event callback with an integer parameter to
443 * be passed to the function when invoked. Multiple copies of the
444 * function are allowed. If there are multiple callbacks, they are
445 * invoked in the order they were registered.
448 register_callback(event_callback __fn
, int __index
);
454 * ios_base data members (doc me)
457 streamsize _M_precision
;
460 iostate _M_exception
;
461 iostate _M_streambuf_state
;
464 // 27.4.2.6 Members for callbacks
465 // 27.4.2.6 ios_base callbacks
466 struct _Callback_list
469 _Callback_list
* _M_next
;
470 ios_base::event_callback _M_fn
;
472 _Atomic_word _M_refcount
; // 0 means one reference.
474 _Callback_list(ios_base::event_callback __fn
, int __index
,
475 _Callback_list
* __cb
)
476 : _M_next(__cb
), _M_fn(__fn
), _M_index(__index
), _M_refcount(0) { }
479 _M_add_reference() { __gnu_cxx::__atomic_add(&_M_refcount
, 1); }
481 // 0 => OK to delete.
483 _M_remove_reference()
484 { return __gnu_cxx::__exchange_and_add(&_M_refcount
, -1); }
487 _Callback_list
* _M_callbacks
;
490 _M_call_callbacks(event __ev
) throw();
493 _M_dispose_callbacks(void);
495 // 27.4.2.5 Members for iword/pword storage
500 _Words() : _M_pword(0), _M_iword(0) { }
503 // Only for failed iword/pword calls.
506 // Guaranteed storage.
507 // The first 5 iword and pword slots are reserved for internal use.
508 enum { _S_local_word_size
= 8 };
509 _Words _M_local_word
[_S_local_word_size
];
511 // Allocated storage.
516 _M_grow_words(int __index
, bool __iword
);
518 // Members for locale and locale caching.
519 locale _M_ios_locale
;
526 // 27.4.2.1.6 Class ios_base::Init
527 // Used to initialize standard streams. In theory, g++ could use
528 // -finit-priority to order this stuff correctly without going
529 // through these machinations.
532 friend class ios_base
;
538 static _Atomic_word _S_refcount
;
539 static bool _S_synced_with_stdio
;
542 // [27.4.2.2] fmtflags state functions
544 * @brief Access to format flags.
545 * @return The format control flags for both input and output.
548 flags() const { return _M_flags
; }
551 * @brief Setting new format flags all at once.
552 * @param fmtfl The new flags to set.
553 * @return The previous format control flags.
555 * This function overwrites all the format flags with @a fmtfl.
558 flags(fmtflags __fmtfl
)
560 fmtflags __old
= _M_flags
;
566 * @brief Setting new format flags.
567 * @param fmtfl Additional flags to set.
568 * @return The previous format control flags.
570 * This function sets additional flags in format control. Flags that
571 * were previously set remain set.
574 setf(fmtflags __fmtfl
)
576 fmtflags __old
= _M_flags
;
582 * @brief Setting new format flags.
583 * @param fmtfl Additional flags to set.
584 * @param mask The flags mask for @a fmtfl.
585 * @return The previous format control flags.
587 * This function clears @a mask in the format flags, then sets
588 * @a fmtfl @c & @a mask. An example mask is @c ios_base::adjustfield.
591 setf(fmtflags __fmtfl
, fmtflags __mask
)
593 fmtflags __old
= _M_flags
;
595 _M_flags
|= (__fmtfl
& __mask
);
600 * @brief Clearing format flags.
601 * @param mask The flags to unset.
603 * This function clears @a mask in the format flags.
606 unsetf(fmtflags __mask
) { _M_flags
&= ~__mask
; }
609 * @brief Flags access.
610 * @return The precision to generate on certain output operations.
613 * Be careful if you try to give a definition of "precision" here; see
618 precision() const { return _M_precision
; }
621 * @brief Changing flags.
622 * @param prec The new precision value.
623 * @return The previous value of precision().
626 precision(streamsize __prec
)
628 streamsize __old
= _M_precision
;
629 _M_precision
= __prec
;
634 * @brief Flags access.
635 * @return The minimum field width to generate on output operations.
637 * "Minimum field width" refers to the number of characters.
640 width() const { return _M_width
; }
643 * @brief Changing flags.
644 * @param wide The new width value.
645 * @return The previous value of width().
648 width(streamsize __wide
)
650 streamsize __old
= _M_width
;
655 // [27.4.2.4] ios_base static members
657 * @brief Interaction with the standard C I/O objects.
658 * @param sync Whether to synchronize or not.
659 * @return True if the standard streams were previously synchronized.
661 * The synchronization referred to is @e only that between the standard
662 * C facilities (e.g., stdout) and the standard C++ objects (e.g.,
663 * cout). User-declared streams are unaffected. See
664 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#8 for more.
667 sync_with_stdio(bool __sync
= true);
669 // [27.4.2.3] ios_base locale functions
671 * @brief Setting a new locale.
672 * @param loc The new locale.
673 * @return The previous locale.
675 * Sets the new locale for this stream, and then invokes each callback
679 imbue(const locale
& __loc
);
682 * @brief Locale access
683 * @return A copy of the current locale.
685 * If @c imbue(loc) has previously been called, then this function
686 * returns @c loc. Otherwise, it returns a copy of @c std::locale(),
687 * the global C++ locale.
690 getloc() const { return _M_ios_locale
; }
693 * @brief Locale access
694 * @return A reference to the current locale.
696 * Like getloc above, but returns a reference instead of
700 _M_getloc() const { return _M_ios_locale
; }
702 // [27.4.2.5] ios_base storage functions
704 * @brief Access to unique indices.
705 * @return An integer different from all previous calls.
707 * This function returns a unique integer every time it is called. It
708 * can be used for any purpose, but is primarily intended to be a unique
709 * index for the iword and pword functions. The expectation is that an
710 * application calls xalloc in order to obtain an index in the iword and
711 * pword arrays that can be used without fear of conflict.
713 * The implementation maintains a static variable that is incremented and
714 * returned on each invocation. xalloc is guaranteed to return an index
715 * that is safe to use in the iword and pword arrays.
721 * @brief Access to integer array.
722 * @param __ix Index into the array.
723 * @return A reference to an integer associated with the index.
725 * The iword function provides access to an array of integers that can be
726 * used for any purpose. The array grows as required to hold the
727 * supplied index. All integers in the array are initialized to 0.
729 * The implementation reserves several indices. You should use xalloc to
730 * obtain an index that is safe to use. Also note that since the array
731 * can grow dynamically, it is not safe to hold onto the reference.
736 _Words
& __word
= (__ix
< _M_word_size
)
737 ? _M_word
[__ix
] : _M_grow_words(__ix
, true);
738 return __word
._M_iword
;
742 * @brief Access to void pointer array.
743 * @param __ix Index into the array.
744 * @return A reference to a void* associated with the index.
746 * The pword function provides access to an array of pointers that can be
747 * used for any purpose. The array grows as required to hold the
748 * supplied index. All pointers in the array are initialized to 0.
750 * The implementation reserves several indices. You should use xalloc to
751 * obtain an index that is safe to use. Also note that since the array
752 * can grow dynamically, it is not safe to hold onto the reference.
757 _Words
& __word
= (__ix
< _M_word_size
)
758 ? _M_word
[__ix
] : _M_grow_words(__ix
, false);
759 return __word
._M_pword
;
764 * Invokes each callback with erase_event. Destroys local storage.
766 * Note that the ios_base object for the standard streams never gets
767 * destroyed. As a result, any callbacks registered with the standard
768 * streams will not get invoked with erase_event (unless copyfmt is
776 // _GLIBCXX_RESOLVE_LIB_DEFECTS
777 // 50. Copy constructor and assignment operator of ios_base
779 ios_base(const ios_base
&);
782 operator=(const ios_base
&);
785 // [27.4.5.1] fmtflags manipulators
786 /// Calls base.setf(ios_base::boolalpha).
788 boolalpha(ios_base
& __base
)
790 __base
.setf(ios_base::boolalpha
);
794 /// Calls base.unsetf(ios_base::boolalpha).
796 noboolalpha(ios_base
& __base
)
798 __base
.unsetf(ios_base::boolalpha
);
802 /// Calls base.setf(ios_base::showbase).
804 showbase(ios_base
& __base
)
806 __base
.setf(ios_base::showbase
);
810 /// Calls base.unsetf(ios_base::showbase).
812 noshowbase(ios_base
& __base
)
814 __base
.unsetf(ios_base::showbase
);
818 /// Calls base.setf(ios_base::showpoint).
820 showpoint(ios_base
& __base
)
822 __base
.setf(ios_base::showpoint
);
826 /// Calls base.unsetf(ios_base::showpoint).
828 noshowpoint(ios_base
& __base
)
830 __base
.unsetf(ios_base::showpoint
);
834 /// Calls base.setf(ios_base::showpos).
836 showpos(ios_base
& __base
)
838 __base
.setf(ios_base::showpos
);
842 /// Calls base.unsetf(ios_base::showpos).
844 noshowpos(ios_base
& __base
)
846 __base
.unsetf(ios_base::showpos
);
850 /// Calls base.setf(ios_base::skipws).
852 skipws(ios_base
& __base
)
854 __base
.setf(ios_base::skipws
);
858 /// Calls base.unsetf(ios_base::skipws).
860 noskipws(ios_base
& __base
)
862 __base
.unsetf(ios_base::skipws
);
866 /// Calls base.setf(ios_base::uppercase).
868 uppercase(ios_base
& __base
)
870 __base
.setf(ios_base::uppercase
);
874 /// Calls base.unsetf(ios_base::uppercase).
876 nouppercase(ios_base
& __base
)
878 __base
.unsetf(ios_base::uppercase
);
882 /// Calls base.setf(ios_base::unitbuf).
884 unitbuf(ios_base
& __base
)
886 __base
.setf(ios_base::unitbuf
);
890 /// Calls base.unsetf(ios_base::unitbuf).
892 nounitbuf(ios_base
& __base
)
894 __base
.unsetf(ios_base::unitbuf
);
898 // [27.4.5.2] adjustfield anipulators
899 /// Calls base.setf(ios_base::internal, ios_base::adjustfield).
901 internal(ios_base
& __base
)
903 __base
.setf(ios_base::internal
, ios_base::adjustfield
);
907 /// Calls base.setf(ios_base::left, ios_base::adjustfield).
909 left(ios_base
& __base
)
911 __base
.setf(ios_base::left
, ios_base::adjustfield
);
915 /// Calls base.setf(ios_base::right, ios_base::adjustfield).
917 right(ios_base
& __base
)
919 __base
.setf(ios_base::right
, ios_base::adjustfield
);
923 // [27.4.5.3] basefield anipulators
924 /// Calls base.setf(ios_base::dec, ios_base::basefield).
926 dec(ios_base
& __base
)
928 __base
.setf(ios_base::dec
, ios_base::basefield
);
932 /// Calls base.setf(ios_base::hex, ios_base::basefield).
934 hex(ios_base
& __base
)
936 __base
.setf(ios_base::hex
, ios_base::basefield
);
940 /// Calls base.setf(ios_base::oct, ios_base::basefield).
942 oct(ios_base
& __base
)
944 __base
.setf(ios_base::oct
, ios_base::basefield
);
948 // [27.4.5.4] floatfield anipulators
949 /// Calls base.setf(ios_base::fixed, ios_base::floatfield).
951 fixed(ios_base
& __base
)
953 __base
.setf(ios_base::fixed
, ios_base::floatfield
);
957 /// Calls base.setf(ios_base::scientific, ios_base::floatfield).
959 scientific(ios_base
& __base
)
961 __base
.setf(ios_base::scientific
, ios_base::floatfield
);
966 #endif /* _IOS_BASE_H */