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 class __locale_cache_base;
151 // 27.4.2 Class ios_base
153 * @brief The very top of the I/O class hierarchy.
155 * This class defines everything that can be defined about I/O that does
156 * not depend on the type of characters being input or output. Most
157 * people will only see @c ios_base when they need to specify the full
158 * name of the various I/O flags (e.g., the openmodes).
164 // 27.4.2.1.1 Class ios_base::failure
165 /// These are thrown to indicate problems. Doc me.
166 class failure : public exception
169 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
170 //48. Use of non-existent exception constructor
172 failure(const string& __str) throw();
174 // This declaration is not useless:
175 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
180 what() const throw();
183 enum { _M_bufsize = 256 };
184 char _M_name[_M_bufsize];
188 // 27.4.2.1.2 Type ios_base::fmtflags
190 * @brief This is a bitmask type.
192 * @c "_Ios_Fmtflags" is implementation-defined, but it is valid to
193 * perform bitwise operations on these values and expect the Right
194 * Thing to happen. Defined objects of type fmtflags are:
214 typedef _Ios_Fmtflags fmtflags;
215 /// Insert/extract @c bool in alphabetic rather than numeric format.
216 static const fmtflags boolalpha = fmtflags(__ios_flags::_S_boolalpha);
217 /// Converts integer input or generates integer output in decimal base.
218 static const fmtflags dec = fmtflags(__ios_flags::_S_dec);
219 /// Generate floating-point output in fixed-point notation.
220 static const fmtflags fixed = fmtflags(__ios_flags::_S_fixed);
221 /// Converts integer input or generates integer output in hexadecimal base.
222 static const fmtflags hex = fmtflags(__ios_flags::_S_hex);
223 /// Adds fill characters at a designated internal point in certain
224 /// generated output, or identical to @c right if no such point is
226 static const fmtflags internal = fmtflags(__ios_flags::_S_internal);
227 /// Adds fill characters on the right (final positions) of certain
228 /// generated output. (I.e., the thing you print is flush left.)
229 static const fmtflags left = fmtflags(__ios_flags::_S_left);
230 /// Converts integer input or generates integer output in octal base.
231 static const fmtflags oct = fmtflags(__ios_flags::_S_oct);
232 /// Adds fill characters on the left (initial positions) of certain
233 /// generated output. (I.e., the thing you print is flush right.)
234 static const fmtflags right = fmtflags(__ios_flags::_S_right);
235 /// Generates floating-point output in scientific notation.
236 static const fmtflags scientific = fmtflags(__ios_flags::_S_scientific);
237 /// Generates a prefix indicating the numeric base of generated integer
239 static const fmtflags showbase = fmtflags(__ios_flags::_S_showbase);
240 /// Generates a decimal-point character unconditionally in generated
241 /// floating-point output.
242 static const fmtflags showpoint = fmtflags(__ios_flags::_S_showpoint);
243 /// Generates a + sign in non-negative generated numeric output.
244 static const fmtflags showpos = fmtflags(__ios_flags::_S_showpos);
245 /// Skips leading white space before certain input operations.
246 static const fmtflags skipws = fmtflags(__ios_flags::_S_skipws);
247 /// Flushes output after each output operation.
248 static const fmtflags unitbuf = fmtflags(__ios_flags::_S_unitbuf);
249 /// Replaces certain lowercase letters with their uppercase equivalents
250 /// in generated output.
251 static const fmtflags uppercase = fmtflags(__ios_flags::_S_uppercase);
252 /// A mask of left|right|internal. Useful for the 2-arg form of @c setf.
253 static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield);
254 /// A mask of dec|oct|hex. Useful for the 2-arg form of @c setf.
255 static const fmtflags basefield = fmtflags(__ios_flags::_S_basefield);
256 /// A mask of scientific|fixed. Useful for the 2-arg form of @c setf.
257 static const fmtflags floatfield = fmtflags(__ios_flags::_S_floatfield);
259 // 27.4.2.1.3 Type ios_base::iostate
261 * @brief This is a bitmask type.
263 * @c "_Ios_Iostate" is implementation-defined, but it is valid to
264 * perform bitwise operations on these values and expect the Right
265 * Thing to happen. Defined objects of type iostate are:
271 typedef _Ios_Iostate iostate;
272 /// Indicates a loss of integrity in an input or output sequence (such
273 /// as an irrecoverable read error from a file).
274 static const iostate badbit = iostate(__ios_flags::_S_badbit);
275 /// Indicates that an input operation reached the end of an input sequence.
276 static const iostate eofbit = iostate(__ios_flags::_S_eofbit);
277 /// Indicates that an input operation failed to read the expected
278 /// characters, or that an output operation failed to generate the
279 /// desired characters.
280 static const iostate failbit = iostate(__ios_flags::_S_failbit);
281 /// Indicates all is well.
282 static const iostate goodbit = iostate(0);
284 // 27.4.2.1.4 Type ios_base::openmode
286 * @brief This is a bitmask type.
288 * @c "_Ios_Openmode" is implementation-defined, but it is valid to
289 * perform bitwise operations on these values and expect the Right
290 * Thing to happen. Defined objects of type openmode are:
298 typedef _Ios_Openmode openmode;
299 /// Seek to end before each write.
300 static const openmode app = openmode(__ios_flags::_S_app);
301 /// Open and seek to end immediately after opening.
302 static const openmode ate = openmode(__ios_flags::_S_ate);
303 /// Perform input and output in binary mode (as opposed to text mode).
304 /// This is probably not what you think it is; see
305 /// http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#3 and
306 /// http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#7 for more.
307 static const openmode binary = openmode(__ios_flags::_S_bin);
308 /// Open for input. Default for @c ifstream and fstream.
309 static const openmode in = openmode(__ios_flags::_S_in);
310 /// Open for output. Default for @c ofstream and fstream.
311 static const openmode out = openmode(__ios_flags::_S_out);
312 /// Open for input. Default for @c ofstream.
313 static const openmode trunc = openmode(__ios_flags::_S_trunc);
315 // 27.4.2.1.5 Type ios_base::seekdir
317 * @brief This is an enumerated type.
319 * @c "_Ios_Seekdir" is implementation-defined. Defined values
320 * of type seekdir are:
322 * - cur, equivalent to @c SEEK_CUR in the C standard library.
323 * - end, equivalent to @c SEEK_END in the C standard library.
325 typedef _Ios_Seekdir seekdir;
326 /// Request a seek relative to the beginning of the stream.
327 static const seekdir beg = seekdir(0);
328 /// Request a seek relative to the current position within the sequence.
329 static const seekdir cur = seekdir(SEEK_CUR);
330 /// Request a seek relative to the current end of the sequence.
331 static const seekdir end = seekdir(SEEK_END);
333 #ifdef _GLIBCPP_DEPRECATED
335 typedef int io_state;
336 typedef int open_mode;
337 typedef int seek_dir;
339 typedef std::streampos streampos;
340 typedef std::streamoff streamoff;
357 typedef void (*event_callback) (event, ios_base&, int);
363 register_callback(event_callback __fn, int __index);
369 * ios_base data members (doc me)
372 streamsize _M_precision;
375 iostate _M_exception;
376 iostate _M_streambuf_state;
379 // 27.4.2.6 Members for callbacks
380 // 27.4.2.6 ios_base callbacks
381 struct _Callback_list
384 _Callback_list* _M_next;
385 ios_base::event_callback _M_fn;
387 _Atomic_word _M_refcount; // 0 means one reference.
389 _Callback_list(ios_base::event_callback __fn, int __index,
390 _Callback_list* __cb)
391 : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
394 _M_add_reference() { __atomic_add(&_M_refcount, 1); }
396 // 0 => OK to delete.
398 _M_remove_reference() { return __exchange_and_add(&_M_refcount, -1); }
401 _Callback_list* _M_callbacks;
404 _M_call_callbacks(event __ev) throw();
407 _M_dispose_callbacks(void);
409 // 27.4.2.5 Members for iword/pword storage
414 _Words() : _M_pword(0), _M_iword(0) { }
417 // Only for failed iword/pword calls.
420 // Guaranteed storage.
421 // The first 5 iword and pword slots are reserved for internal use.
422 static const int _S_local_word_size = 8;
423 _Words _M_local_word[_S_local_word_size];
425 // Allocated storage.
430 _M_grow_words(int __index);
432 // Members for locale and locale caching.
433 locale _M_ios_locale;
435 // Cache of locale and facet data.
436 // Cast this to __locale_cache<_CharT>*
437 auto_ptr<__locale_cache_base> _M_locale_cache;
444 // 27.4.2.1.6 Class ios_base::Init
445 // Used to initialize standard streams. In theory, g++ could use
446 // -finit-priority to order this stuff correctly without going
447 // through these machinations.
450 friend class ios_base;
456 _S_create_buffers(bool __sync);
459 _S_destroy_buffers();
461 // NB: Allows debugger applications use of the standard streams
462 // from operator new. _S_ios_base_init must be incremented in
463 // _S_ios_create _after_ initialization is completed.
465 _S_initialized() { return _S_ios_base_init; }
468 static int _S_ios_base_init;
469 static bool _S_synced_with_stdio;
472 // [27.4.2.2] fmtflags state functions
474 * @brief Access to format flags.
475 * @return The format control flags for both input and output.
478 flags() const { return _M_flags; }
481 * @brief Setting new format flags all at once.
482 * @param fmtfl The new flags to set.
483 * @return The previous format control flags.
485 * This function overwrites all the format flags with @a fmtfl.
488 flags(fmtflags __fmtfl)
490 fmtflags __old = _M_flags;
496 * @brief Setting new format flags.
497 * @param fmtfl Additional flags to set.
498 * @return The previous format control flags.
500 * This function sets additional flags in format control. Flags that
501 * were previously set remain set.
504 setf(fmtflags __fmtfl)
506 fmtflags __old = _M_flags;
512 * @brief Setting new format flags.
513 * @param fmtfl Additional flags to set.
514 * @param mask The flags mask for @a fmtfl.
515 * @return The previous format control flags.
517 * This function clears @a mask in the format flags, then sets
518 * @a fmtfl @c & @a mask. An example mask is @c ios_base::adjustfield.
521 setf(fmtflags __fmtfl, fmtflags __mask)
523 fmtflags __old = _M_flags;
525 _M_flags |= (__fmtfl & __mask);
530 * @brief Clearing format flags.
531 * @param mask The flags to unset.
533 * This function clears @a mask in the format flags.
536 unsetf(fmtflags __mask) { _M_flags &= ~__mask; }
539 * @brief Flags access.
540 * @return The precision to generate on certain output operations.
543 * Be careful if you try to give a definition of "precision" here; see
548 precision() const { return _M_precision; }
551 * @brief Changing flags.
552 * @param prec The new precision value.
553 * @return The previous value of precision().
556 precision(streamsize __prec)
558 streamsize __old = _M_precision;
559 _M_precision = __prec;
564 * @brief Flags access.
565 * @return The minimum field width to generate on output operations.
567 * "Minimum field width" refers to the number of characters.
570 width() const { return _M_width; }
573 * @brief Changing flags.
574 * @param wide The new width value.
575 * @return The previous value of width().
578 width(streamsize __wide)
580 streamsize __old = _M_width;
585 // [27.4.2.4] ios_base static members
587 * @brief Interaction with the standard C I/O objects.
588 * @param sync Whether to synchronize or not.
589 * @return True if the standard streams were previously synchronized.
591 * The synchronization referred to is @e only that between the standard
592 * C facilities (e.g., stdout) and the standard C++ objects (e.g.,
593 * cout). User-declared streams are unaffected. See
594 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#8 for more.
597 sync_with_stdio(bool __sync = true);
599 // [27.4.2.3] ios_base locale functions
601 * @brief Setting a new locale.
602 * @param loc The new locale.
603 * @return The previous locale.
605 * Sets the new locale for this stream, and
606 * [XXX does something with callbacks].
609 imbue(const locale& __loc);
612 * @brief Locale access
613 * @return The locale currently in effect.
615 * If @c imbue(loc) has previously been called, then this function
616 * returns @c loc. Otherwise, it returns a copy of @c std::locale(),
617 * the global C++ locale.
620 getloc() const { return _M_ios_locale; }
622 // [27.4.2.5] ios_base storage functions
635 _Words& __word = (__ix < _M_word_size)
636 ? _M_word[__ix] : _M_grow_words(__ix);
637 return __word._M_iword;
646 _Words& __word = (__ix < _M_word_size)
647 ? _M_word[__ix] : _M_grow_words(__ix);
648 return __word._M_pword;
651 // Access to the cache. Not safe to call until basic_ios::_M_init() has
654 _M_cache() { return *_M_locale_cache; }
658 * Destroys local storage and
659 * [XXX does something with callbacks].
666 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
667 //50. Copy constructor and assignment operator of ios_base
669 ios_base(const ios_base&);
672 operator=(const ios_base&);
676 // [27.4.5.1] fmtflags manipulators
677 /// Calls base.setf(ios_base::boolalpha).
679 boolalpha(ios_base& __base)
681 __base.setf(ios_base::boolalpha);
685 /// Calls base.unsetf(ios_base::boolalpha).
687 noboolalpha(ios_base& __base)
689 __base.unsetf(ios_base::boolalpha);
693 /// Calls base.setf(ios_base::showbase).
695 showbase(ios_base& __base)
697 __base.setf(ios_base::showbase);
701 /// Calls base.unsetf(ios_base::showbase).
703 noshowbase(ios_base& __base)
705 __base.unsetf(ios_base::showbase);
709 /// Calls base.setf(ios_base::showpoint).
711 showpoint(ios_base& __base)
713 __base.setf(ios_base::showpoint);
717 /// Calls base.unsetf(ios_base::showpoint).
719 noshowpoint(ios_base& __base)
721 __base.unsetf(ios_base::showpoint);
725 /// Calls base.setf(ios_base::showpos).
727 showpos(ios_base& __base)
729 __base.setf(ios_base::showpos);
733 /// Calls base.unsetf(ios_base::showpos).
735 noshowpos(ios_base& __base)
737 __base.unsetf(ios_base::showpos);
741 /// Calls base.setf(ios_base::skipws).
743 skipws(ios_base& __base)
745 __base.setf(ios_base::skipws);
749 /// Calls base.unsetf(ios_base::skipws).
751 noskipws(ios_base& __base)
753 __base.unsetf(ios_base::skipws);
757 /// Calls base.setf(ios_base::uppercase).
759 uppercase(ios_base& __base)
761 __base.setf(ios_base::uppercase);
765 /// Calls base.unsetf(ios_base::uppercase).
767 nouppercase(ios_base& __base)
769 __base.unsetf(ios_base::uppercase);
773 /// Calls base.setf(ios_base::unitbuf).
775 unitbuf(ios_base& __base)
777 __base.setf(ios_base::unitbuf);
781 /// Calls base.unsetf(ios_base::unitbuf).
783 nounitbuf(ios_base& __base)
785 __base.unsetf(ios_base::unitbuf);
789 // [27.4.5.2] adjustfield anipulators
790 /// Calls base.setf(ios_base::internal, ios_base::adjustfield).
792 internal(ios_base& __base)
794 __base.setf(ios_base::internal, ios_base::adjustfield);
798 /// Calls base.setf(ios_base::left, ios_base::adjustfield).
800 left(ios_base& __base)
802 __base.setf(ios_base::left, ios_base::adjustfield);
806 /// Calls base.setf(ios_base::right, ios_base::adjustfield).
808 right(ios_base& __base)
810 __base.setf(ios_base::right, ios_base::adjustfield);
814 // [27.4.5.3] basefield anipulators
815 /// Calls base.setf(ios_base::dec, ios_base::basefield).
817 dec(ios_base& __base)
819 __base.setf(ios_base::dec, ios_base::basefield);
823 /// Calls base.setf(ios_base::hex, ios_base::basefield).
825 hex(ios_base& __base)
827 __base.setf(ios_base::hex, ios_base::basefield);
831 /// Calls base.setf(ios_base::oct, ios_base::basefield).
833 oct(ios_base& __base)
835 __base.setf(ios_base::oct, ios_base::basefield);
839 // [27.4.5.4] floatfield anipulators
840 /// Calls base.setf(ios_base::fixed, ios_base::floatfield).
842 fixed(ios_base& __base)
844 __base.setf(ios_base::fixed, ios_base::floatfield);
848 /// Calls base.setf(ios_base::scientific, ios_base::floatfield).
850 scientific(ios_base& __base)
852 __base.setf(ios_base::scientific, ios_base::floatfield);
858 #endif /* _CPP_BITS_IOSBASE_H */