1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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.8 File-based streams
36 * This is an internal header file, included by other library headers.
37 * You should not attempt to use it directly.
40 #ifndef _CPP_BITS_IOSBASE_H
41 #define _CPP_BITS_IOSBASE_H 1
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.
55 enum _Ios_Fmtflags { _M_ios_fmtflags_end = 1L << 16 };
58 operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
59 { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); }
62 operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
63 { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); }
66 operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
67 { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); }
70 operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
71 { return __a = __a | __b; }
74 operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
75 { return __a = __a & __b; }
78 operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
79 { return __a = __a ^ __b; }
82 operator~(_Ios_Fmtflags __a)
83 { return _Ios_Fmtflags(~static_cast<int>(__a)); }
86 enum _Ios_Openmode { _M_ios_openmode_end = 1L << 16 };
89 operator&(_Ios_Openmode __a, _Ios_Openmode __b)
90 { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); }
93 operator|(_Ios_Openmode __a, _Ios_Openmode __b)
94 { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); }
97 operator^(_Ios_Openmode __a, _Ios_Openmode __b)
98 { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); }
101 operator|=(_Ios_Openmode& __a, _Ios_Openmode __b)
102 { return __a = __a | __b; }
105 operator&=(_Ios_Openmode& __a, _Ios_Openmode __b)
106 { return __a = __a & __b; }
109 operator^=(_Ios_Openmode& __a, _Ios_Openmode __b)
110 { return __a = __a ^ __b; }
113 operator~(_Ios_Openmode __a)
114 { return _Ios_Openmode(~static_cast<int>(__a)); }
117 enum _Ios_Iostate { _M_ios_iostate_end = 1L << 16 };
120 operator&(_Ios_Iostate __a, _Ios_Iostate __b)
121 { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); }
124 operator|(_Ios_Iostate __a, _Ios_Iostate __b)
125 { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); }
128 operator^(_Ios_Iostate __a, _Ios_Iostate __b)
129 { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); }
132 operator|=(_Ios_Iostate& __a, _Ios_Iostate __b)
133 { return __a = __a | __b; }
136 operator&=(_Ios_Iostate& __a, _Ios_Iostate __b)
137 { return __a = __a & __b; }
140 operator^=(_Ios_Iostate& __a, _Ios_Iostate __b)
141 { return __a = __a ^ __b; }
144 operator~(_Ios_Iostate __a)
145 { return _Ios_Iostate(~static_cast<int>(__a)); }
147 enum _Ios_Seekdir { _M_ios_seekdir_end = 1L << 16 };
149 // 27.4.2 Class ios_base
151 * @brief The very top of the I/O class hierarchy.
153 * This class defines everything that can be defined about I/O that does
154 * not depend on the type of characters being input or output. Most
155 * people will only see @c ios_base when they need to specify the full
156 * name of the various I/O flags (e.g., the openmodes).
162 // 27.4.2.1.1 Class ios_base::failure
163 /// These are thrown to indicate problems. Doc me.
164 class failure : public exception
167 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
168 //48. Use of non-existent exception constructor
170 failure(const string& __str) throw();
172 // This declaration is not useless:
173 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
178 what() const throw();
181 enum { _M_bufsize = 256 };
182 char _M_name[_M_bufsize];
186 // 27.4.2.1.2 Type ios_base::fmtflags
188 * @brief This is a bitmask type.
190 * @c "_Ios_Fmtflags" is implementation-defined, but it is valid to
191 * perform bitwise operations on these values and expect the Right
192 * Thing to happen. Defined objects of type fmtflags are:
212 typedef _Ios_Fmtflags fmtflags;
213 /// Insert/extract @c bool in alphabetic rather than numeric format.
214 static const fmtflags boolalpha = fmtflags(__ios_flags::_S_boolalpha);
215 /// Converts integer input or generates integer output in decimal base.
216 static const fmtflags dec = fmtflags(__ios_flags::_S_dec);
217 /// Generate floating-point output in fixed-point notation.
218 static const fmtflags fixed = fmtflags(__ios_flags::_S_fixed);
219 /// Converts integer input or generates integer output in hexadecimal base.
220 static const fmtflags hex = fmtflags(__ios_flags::_S_hex);
221 /// Adds fill characters at a designated internal point in certain
222 /// generated output, or identical to @c right if no such point is
224 static const fmtflags internal = fmtflags(__ios_flags::_S_internal);
225 /// Adds fill characters on the right (final positions) of certain
226 /// generated output. (I.e., the thing you print is flush left.)
227 static const fmtflags left = fmtflags(__ios_flags::_S_left);
228 /// Converts integer input or generates integer output in octal base.
229 static const fmtflags oct = fmtflags(__ios_flags::_S_oct);
230 /// Adds fill characters on the left (initial positions) of certain
231 /// generated output. (I.e., the thing you print is flush right.)
232 static const fmtflags right = fmtflags(__ios_flags::_S_right);
233 /// Generates floating-point output in scientific notation.
234 static const fmtflags scientific = fmtflags(__ios_flags::_S_scientific);
235 /// Generates a prefix indicating the numeric base of generated integer
237 static const fmtflags showbase = fmtflags(__ios_flags::_S_showbase);
238 /// Generates a decimal-point character unconditionally in generated
239 /// floating-point output.
240 static const fmtflags showpoint = fmtflags(__ios_flags::_S_showpoint);
241 /// Generates a + sign in non-negative generated numeric output.
242 static const fmtflags showpos = fmtflags(__ios_flags::_S_showpos);
243 /// Skips leading white space before certain input operations.
244 static const fmtflags skipws = fmtflags(__ios_flags::_S_skipws);
245 /// Flushes output after each output operation.
246 static const fmtflags unitbuf = fmtflags(__ios_flags::_S_unitbuf);
247 /// Replaces certain lowercase letters with their uppercase equivalents
248 /// in generated output.
249 static const fmtflags uppercase = fmtflags(__ios_flags::_S_uppercase);
250 /// A mask of left|right|internal. Useful for the 2-arg form of @c setf.
251 static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield);
252 /// A mask of dec|oct|hex. Useful for the 2-arg form of @c setf.
253 static const fmtflags basefield = fmtflags(__ios_flags::_S_basefield);
254 /// A mask of scientific|fixed. Useful for the 2-arg form of @c setf.
255 static const fmtflags floatfield = fmtflags(__ios_flags::_S_floatfield);
257 // 27.4.2.1.3 Type ios_base::iostate
259 * @brief This is a bitmask type.
261 * @c "_Ios_Iostate" is implementation-defined, but it is valid to
262 * perform bitwise operations on these values and expect the Right
263 * Thing to happen. Defined objects of type iostate are:
269 typedef _Ios_Iostate iostate;
270 /// Indicates a loss of integrity in an input or output sequence (such
271 /// as an irrecoverable read error from a file).
272 static const iostate badbit = iostate(__ios_flags::_S_badbit);
273 /// Indicates that an input operation reached the end of an input sequence.
274 static const iostate eofbit = iostate(__ios_flags::_S_eofbit);
275 /// Indicates that an input operation failed to read the expected
276 /// characters, or that an output operation failed to generate the
277 /// desired characters.
278 static const iostate failbit = iostate(__ios_flags::_S_failbit);
279 /// Indicates all is well.
280 static const iostate goodbit = iostate(0);
282 // 27.4.2.1.4 Type ios_base::openmode
284 * @brief This is a bitmask type.
286 * @c "_Ios_Openmode" is implementation-defined, but it is valid to
287 * perform bitwise operations on these values and expect the Right
288 * Thing to happen. Defined objects of type openmode are:
296 typedef _Ios_Openmode openmode;
297 /// Seek to end before each write.
298 static const openmode app = openmode(__ios_flags::_S_app);
299 /// Open and seek to end immediately after opening.
300 static const openmode ate = openmode(__ios_flags::_S_ate);
301 /// Perform input and output in binary mode (as opposed to text mode).
302 /// This is probably not what you think it is; see
303 /// http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#3 and
304 /// http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#7 for more.
305 static const openmode binary = openmode(__ios_flags::_S_bin);
306 /// Open for input. Default for @c ifstream and fstream.
307 static const openmode in = openmode(__ios_flags::_S_in);
308 /// Open for output. Default for @c ofstream and fstream.
309 static const openmode out = openmode(__ios_flags::_S_out);
310 /// Open for input. Default for @c ofstream.
311 static const openmode trunc = openmode(__ios_flags::_S_trunc);
313 // 27.4.2.1.5 Type ios_base::seekdir
315 * @brief This is an enumerated type.
317 * @c "_Ios_Seekdir" is implementation-defined. Defined values
318 * of type seekdir are:
320 * - cur, equivalent to @c SEEK_CUR in the C standard library.
321 * - end, equivalent to @c SEEK_END in the C standard library.
323 typedef _Ios_Seekdir seekdir;
324 /// Request a seek relative to the beginning of the stream.
325 static const seekdir beg = seekdir(0);
326 /// Request a seek relative to the current position within the sequence.
327 static const seekdir cur = seekdir(SEEK_CUR);
328 /// Request a seek relative to the current end of the sequence.
329 static const seekdir end = seekdir(SEEK_END);
331 #ifdef _GLIBCPP_DEPRECATED
333 typedef int io_state;
334 typedef int open_mode;
335 typedef int seek_dir;
337 typedef std::streampos streampos;
338 typedef std::streamoff streamoff;
355 typedef void (*event_callback) (event, ios_base&, int);
361 register_callback(event_callback __fn, int __index);
367 * ios_base data members (doc me)
370 streamsize _M_precision;
373 iostate _M_exception;
374 iostate _M_streambuf_state;
377 // 27.4.2.6 Members for callbacks
378 // 27.4.2.6 ios_base callbacks
379 struct _Callback_list
382 _Callback_list* _M_next;
383 ios_base::event_callback _M_fn;
385 _Atomic_word _M_refcount; // 0 means one reference.
387 _Callback_list(ios_base::event_callback __fn, int __index,
388 _Callback_list* __cb)
389 : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
392 _M_add_reference() { __atomic_add(&_M_refcount, 1); }
394 // 0 => OK to delete.
396 _M_remove_reference() { return __exchange_and_add(&_M_refcount, -1); }
399 _Callback_list* _M_callbacks;
402 _M_call_callbacks(event __ev) throw();
405 _M_dispose_callbacks(void);
407 // 27.4.2.5 Members for iword/pword storage
412 _Words() : _M_pword(0), _M_iword(0) { }
415 // Only for failed iword/pword calls.
418 // Guaranteed storage.
419 // The first 5 iword and pword slots are reserved for internal use.
420 static const int _S_local_word_size = 8;
421 _Words _M_local_word[_S_local_word_size];
423 // Allocated storage.
428 _M_grow_words(int __index);
430 // Members for locale and locale caching.
431 locale _M_ios_locale;
438 // 27.4.2.1.6 Class ios_base::Init
439 // Used to initialize standard streams. In theory, g++ could use
440 // -finit-priority to order this stuff correctly without going
441 // through these machinations.
444 friend class ios_base;
450 _S_ios_create(bool __sync);
456 static int _S_ios_base_init;
457 static bool _S_synced_with_stdio;
460 // [27.4.2.2] fmtflags state functions
462 * @brief Access to format flags.
463 * @return The format control flags for both input and output.
466 flags() const { return _M_flags; }
469 * @brief Setting new format flags all at once.
470 * @param fmtfl The new flags to set.
471 * @return The previous format control flags.
473 * This function overwrites all the format flags with @a fmtfl.
476 flags(fmtflags __fmtfl)
478 fmtflags __old = _M_flags;
484 * @brief Setting new format flags.
485 * @param fmtfl Additional flags to set.
486 * @return The previous format control flags.
488 * This function sets additional flags in format control. Flags that
489 * were previously set remain set.
492 setf(fmtflags __fmtfl)
494 fmtflags __old = _M_flags;
500 * @brief Setting new format flags.
501 * @param fmtfl Additional flags to set.
502 * @param mask The flags mask for @a fmtfl.
503 * @return The previous format control flags.
505 * This function clears @a mask in the format flags, then sets
506 * @a fmtfl @c & @a mask. An example mask is @c ios_base::adjustfield.
509 setf(fmtflags __fmtfl, fmtflags __mask)
511 fmtflags __old = _M_flags;
513 _M_flags |= (__fmtfl & __mask);
518 * @brief Clearing format flags.
519 * @param mask The flags to unset.
521 * This function clears @a mask in the format flags.
524 unsetf(fmtflags __mask) { _M_flags &= ~__mask; }
527 * @brief Flags access.
528 * @return The precision to generate on certain output operations.
531 * Be careful if you try to give a definition of "precision" here; see
536 precision() const { return _M_precision; }
539 * @brief Changing flags.
540 * @param prec The new precision value.
541 * @return The previous value of precision().
544 precision(streamsize __prec)
546 streamsize __old = _M_precision;
547 _M_precision = __prec;
552 * @brief Flags access.
553 * @return The minimum field width to generate on output operations.
555 * "Minimum field width" refers to the number of characters.
558 width() const { return _M_width; }
561 * @brief Changing flags.
562 * @param wide The new width value.
563 * @return The previous value of width().
566 width(streamsize __wide)
568 streamsize __old = _M_width;
573 // [27.4.2.4] ios_base static members
575 * @brief Interaction with the standard C I/O objects.
576 * @param sync Whether to synchronize or not.
577 * @return True if the standard streams were previously synchronized.
579 * The synchronization referred to is @e only that between the standard
580 * C facilities (e.g., stdout) and the standard C++ objects (e.g.,
581 * cout). User-declared streams are unaffected. See
582 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#8 for more.
585 sync_with_stdio(bool __sync = true);
587 // [27.4.2.3] ios_base locale functions
589 * @brief Setting a new locale.
590 * @param loc The new locale.
591 * @return The previous locale.
593 * Sets the new locale for this stream, and
594 * [XXX does something with callbacks].
597 imbue(const locale& __loc);
600 * @brief Locale access
601 * @return The locale currently in effect.
603 * If @c imbue(loc) has previously been called, then this function
604 * returns @c loc. Otherwise, it returns a copy of @c std::locale(),
605 * the global C++ locale.
608 getloc() const { return _M_ios_locale; }
610 // [27.4.2.5] ios_base storage functions
623 _Words& __word = (__ix < _M_word_size)
624 ? _M_word[__ix] : _M_grow_words(__ix);
625 return __word._M_iword;
634 _Words& __word = (__ix < _M_word_size)
635 ? _M_word[__ix] : _M_grow_words(__ix);
636 return __word._M_pword;
641 * Destroys local storage and
642 * [XXX does something with callbacks].
649 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
650 //50. Copy constructor and assignment operator of ios_base
652 ios_base(const ios_base&);
655 operator=(const ios_base&);
659 // [27.4.5.1] fmtflags manipulators
660 /// Calls base.setf(ios_base::boolalpha).
662 boolalpha(ios_base& __base)
664 __base.setf(ios_base::boolalpha);
668 /// Calls base.unsetf(ios_base::boolalpha).
670 noboolalpha(ios_base& __base)
672 __base.unsetf(ios_base::boolalpha);
676 /// Calls base.setf(ios_base::showbase).
678 showbase(ios_base& __base)
680 __base.setf(ios_base::showbase);
684 /// Calls base.unsetf(ios_base::showbase).
686 noshowbase(ios_base& __base)
688 __base.unsetf(ios_base::showbase);
692 /// Calls base.setf(ios_base::showpoint).
694 showpoint(ios_base& __base)
696 __base.setf(ios_base::showpoint);
700 /// Calls base.unsetf(ios_base::showpoint).
702 noshowpoint(ios_base& __base)
704 __base.unsetf(ios_base::showpoint);
708 /// Calls base.setf(ios_base::showpos).
710 showpos(ios_base& __base)
712 __base.setf(ios_base::showpos);
716 /// Calls base.unsetf(ios_base::showpos).
718 noshowpos(ios_base& __base)
720 __base.unsetf(ios_base::showpos);
724 /// Calls base.setf(ios_base::skipws).
726 skipws(ios_base& __base)
728 __base.setf(ios_base::skipws);
732 /// Calls base.unsetf(ios_base::skipws).
734 noskipws(ios_base& __base)
736 __base.unsetf(ios_base::skipws);
740 /// Calls base.setf(ios_base::uppercase).
742 uppercase(ios_base& __base)
744 __base.setf(ios_base::uppercase);
748 /// Calls base.unsetf(ios_base::uppercase).
750 nouppercase(ios_base& __base)
752 __base.unsetf(ios_base::uppercase);
756 /// Calls base.setf(ios_base::unitbuf).
758 unitbuf(ios_base& __base)
760 __base.setf(ios_base::unitbuf);
764 /// Calls base.unsetf(ios_base::unitbuf).
766 nounitbuf(ios_base& __base)
768 __base.unsetf(ios_base::unitbuf);
772 // [27.4.5.2] adjustfield anipulators
773 /// Calls base.setf(ios_base::internal, ios_base::adjustfield).
775 internal(ios_base& __base)
777 __base.setf(ios_base::internal, ios_base::adjustfield);
781 /// Calls base.setf(ios_base::left, ios_base::adjustfield).
783 left(ios_base& __base)
785 __base.setf(ios_base::left, ios_base::adjustfield);
789 /// Calls base.setf(ios_base::right, ios_base::adjustfield).
791 right(ios_base& __base)
793 __base.setf(ios_base::right, ios_base::adjustfield);
797 // [27.4.5.3] basefield anipulators
798 /// Calls base.setf(ios_base::dec, ios_base::basefield).
800 dec(ios_base& __base)
802 __base.setf(ios_base::dec, ios_base::basefield);
806 /// Calls base.setf(ios_base::hex, ios_base::basefield).
808 hex(ios_base& __base)
810 __base.setf(ios_base::hex, ios_base::basefield);
814 /// Calls base.setf(ios_base::oct, ios_base::basefield).
816 oct(ios_base& __base)
818 __base.setf(ios_base::oct, ios_base::basefield);
822 // [27.4.5.4] floatfield anipulators
823 /// Calls base.setf(ios_base::fixed, ios_base::floatfield).
825 fixed(ios_base& __base)
827 __base.setf(ios_base::fixed, ios_base::floatfield);
831 /// Calls base.setf(ios_base::scientific, ios_base::floatfield).
833 scientific(ios_base& __base)
835 __base.setf(ios_base::scientific, ios_base::floatfield);
841 #endif /* _CPP_BITS_IOSBASE_H */