1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
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>
49 // The following definitions of bitmask types are enums, not ints,
50 // as permitted (but not required) in the standard, in order to provide
51 // better type safety in iostream calls. A side effect is that
52 // expressions involving them are no longer compile-time constants.
53 enum _Ios_Fmtflags
{ _M_ios_fmtflags_end
= 1L << 16 };
56 operator&(_Ios_Fmtflags __a
, _Ios_Fmtflags __b
)
57 { return _Ios_Fmtflags(static_cast<int>(__a
) & static_cast<int>(__b
)); }
60 operator|(_Ios_Fmtflags __a
, _Ios_Fmtflags __b
)
61 { return _Ios_Fmtflags(static_cast<int>(__a
) | static_cast<int>(__b
)); }
64 operator^(_Ios_Fmtflags __a
, _Ios_Fmtflags __b
)
65 { return _Ios_Fmtflags(static_cast<int>(__a
) ^ static_cast<int>(__b
)); }
68 operator|=(_Ios_Fmtflags
& __a
, _Ios_Fmtflags __b
)
69 { return __a
= __a
| __b
; }
72 operator&=(_Ios_Fmtflags
& __a
, _Ios_Fmtflags __b
)
73 { return __a
= __a
& __b
; }
76 operator^=(_Ios_Fmtflags
& __a
, _Ios_Fmtflags __b
)
77 { return __a
= __a
^ __b
; }
80 operator~(_Ios_Fmtflags __a
)
81 { return _Ios_Fmtflags(~static_cast<int>(__a
)); }
84 enum _Ios_Openmode
{ _M_ios_openmode_end
= 1L << 16 };
87 operator&(_Ios_Openmode __a
, _Ios_Openmode __b
)
88 { return _Ios_Openmode(static_cast<int>(__a
) & static_cast<int>(__b
)); }
91 operator|(_Ios_Openmode __a
, _Ios_Openmode __b
)
92 { return _Ios_Openmode(static_cast<int>(__a
) | static_cast<int>(__b
)); }
95 operator^(_Ios_Openmode __a
, _Ios_Openmode __b
)
96 { return _Ios_Openmode(static_cast<int>(__a
) ^ static_cast<int>(__b
)); }
99 operator|=(_Ios_Openmode
& __a
, _Ios_Openmode __b
)
100 { return __a
= __a
| __b
; }
103 operator&=(_Ios_Openmode
& __a
, _Ios_Openmode __b
)
104 { return __a
= __a
& __b
; }
107 operator^=(_Ios_Openmode
& __a
, _Ios_Openmode __b
)
108 { return __a
= __a
^ __b
; }
111 operator~(_Ios_Openmode __a
)
112 { return _Ios_Openmode(~static_cast<int>(__a
)); }
115 enum _Ios_Iostate
{ _M_ios_iostate_end
= 1L << 16 };
118 operator&(_Ios_Iostate __a
, _Ios_Iostate __b
)
119 { return _Ios_Iostate(static_cast<int>(__a
) & static_cast<int>(__b
)); }
122 operator|(_Ios_Iostate __a
, _Ios_Iostate __b
)
123 { return _Ios_Iostate(static_cast<int>(__a
) | static_cast<int>(__b
)); }
126 operator^(_Ios_Iostate __a
, _Ios_Iostate __b
)
127 { return _Ios_Iostate(static_cast<int>(__a
) ^ static_cast<int>(__b
)); }
130 operator|=(_Ios_Iostate
& __a
, _Ios_Iostate __b
)
131 { return __a
= __a
| __b
; }
134 operator&=(_Ios_Iostate
& __a
, _Ios_Iostate __b
)
135 { return __a
= __a
& __b
; }
138 operator^=(_Ios_Iostate
& __a
, _Ios_Iostate __b
)
139 { return __a
= __a
^ __b
; }
142 operator~(_Ios_Iostate __a
)
143 { return _Ios_Iostate(~static_cast<int>(__a
)); }
145 enum _Ios_Seekdir
{ _M_ios_seekdir_end
= 1L << 16 };
147 // 27.4.2 Class ios_base
149 * @brief The very top of the I/O class hierarchy.
151 * This class defines everything that can be defined about I/O that does
152 * not depend on the type of characters being input or output. Most
153 * people will only see @c ios_base when they need to specify the full
154 * name of the various I/O flags (e.g., the openmodes).
160 // 27.4.2.1.1 Class ios_base::failure
161 /// These are thrown to indicate problems. Doc me.
162 class failure
: public exception
165 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
166 //48. Use of non-existent exception constructor
168 failure(const string
& __str
) throw();
170 // This declaration is not useless:
171 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
176 what() const throw();
179 enum { _M_bufsize
= 256 };
180 char _M_name
[_M_bufsize
];
184 // 27.4.2.1.2 Type ios_base::fmtflags
186 * @brief This is a bitmask type.
188 * @c "_Ios_Fmtflags" is implementation-defined, but it is valid to
189 * perform bitwise operations on these values and expect the Right
190 * Thing to happen. Defined objects of type fmtflags are:
210 typedef _Ios_Fmtflags fmtflags
;
211 /// Insert/extract @c bool in alphabetic rather than numeric format.
212 static const fmtflags boolalpha
= fmtflags(__ios_flags::_S_boolalpha
);
213 /// Converts integer input or generates integer output in decimal base.
214 static const fmtflags dec
= fmtflags(__ios_flags::_S_dec
);
215 /// Generate floating-point output in fixed-point notation.
216 static const fmtflags fixed
= fmtflags(__ios_flags::_S_fixed
);
217 /// Converts integer input or generates integer output in hexadecimal base.
218 static const fmtflags hex
= fmtflags(__ios_flags::_S_hex
);
219 /// Adds fill characters at a designated internal point in certain
220 /// generated output, or identical to @c right if no such point is
222 static const fmtflags internal
= fmtflags(__ios_flags::_S_internal
);
223 /// Adds fill characters on the right (final positions) of certain
224 /// generated output. (I.e., the thing you print is flush left.)
225 static const fmtflags left
= fmtflags(__ios_flags::_S_left
);
226 /// Converts integer input or generates integer output in octal base.
227 static const fmtflags oct
= fmtflags(__ios_flags::_S_oct
);
228 /// Adds fill characters on the left (initial positions) of certain
229 /// generated output. (I.e., the thing you print is flush right.)
230 static const fmtflags right
= fmtflags(__ios_flags::_S_right
);
231 /// Generates floating-point output in scientific notation.
232 static const fmtflags scientific
= fmtflags(__ios_flags::_S_scientific
);
233 /// Generates a prefix indicating the numeric base of generated integer
235 static const fmtflags showbase
= fmtflags(__ios_flags::_S_showbase
);
236 /// Generates a decimal-point character unconditionally in generated
237 /// floating-point output.
238 static const fmtflags showpoint
= fmtflags(__ios_flags::_S_showpoint
);
239 /// Generates a + sign in non-negative generated numeric output.
240 static const fmtflags showpos
= fmtflags(__ios_flags::_S_showpos
);
241 /// Skips leading white space before certain input operations.
242 static const fmtflags skipws
= fmtflags(__ios_flags::_S_skipws
);
243 /// Flushes output after each output operation.
244 static const fmtflags unitbuf
= fmtflags(__ios_flags::_S_unitbuf
);
245 /// Replaces certain lowercase letters with their uppercase equivalents
246 /// in generated output.
247 static const fmtflags uppercase
= fmtflags(__ios_flags::_S_uppercase
);
248 /// A mask of left|right|internal. Useful for the 2-arg form of @c setf.
249 static const fmtflags adjustfield
= fmtflags(__ios_flags::_S_adjustfield
);
250 /// A mask of dec|oct|hex. Useful for the 2-arg form of @c setf.
251 static const fmtflags basefield
= fmtflags(__ios_flags::_S_basefield
);
252 /// A mask of scientific|fixed. Useful for the 2-arg form of @c setf.
253 static const fmtflags floatfield
= fmtflags(__ios_flags::_S_floatfield
);
255 // 27.4.2.1.3 Type ios_base::iostate
257 * @brief This is a bitmask type.
259 * @c "_Ios_Iostate" is implementation-defined, but it is valid to
260 * perform bitwise operations on these values and expect the Right
261 * Thing to happen. Defined objects of type iostate are:
267 typedef _Ios_Iostate iostate
;
268 /// Indicates a loss of integrity in an input or output sequence (such
269 /// as an irrecoverable read error from a file).
270 static const iostate badbit
= iostate(__ios_flags::_S_badbit
);
271 /// Indicates that an input operation reached the end of an input sequence.
272 static const iostate eofbit
= iostate(__ios_flags::_S_eofbit
);
273 /// Indicates that an input operation failed to read the expected
274 /// characters, or that an output operation failed to generate the
275 /// desired characters.
276 static const iostate failbit
= iostate(__ios_flags::_S_failbit
);
277 /// Indicates all is well.
278 static const iostate goodbit
= iostate(0);
280 // 27.4.2.1.4 Type ios_base::openmode
282 * @brief This is a bitmask type.
284 * @c "_Ios_Openmode" is implementation-defined, but it is valid to
285 * perform bitwise operations on these values and expect the Right
286 * Thing to happen. Defined objects of type openmode are:
294 typedef _Ios_Openmode openmode
;
295 /// Seek to end before each write.
296 static const openmode app
= openmode(__ios_flags::_S_app
);
297 /// Open and seek to end immediately after opening.
298 static const openmode ate
= openmode(__ios_flags::_S_ate
);
299 /// Perform input and output in binary mode (as opposed to text mode).
300 /// This is probably not what you think it is; see
301 /// http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#3 and
302 /// http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#7 for more.
303 static const openmode binary
= openmode(__ios_flags::_S_bin
);
304 /// Open for input. Default for @c ifstream and fstream.
305 static const openmode in
= openmode(__ios_flags::_S_in
);
306 /// Open for output. Default for @c ofstream and fstream.
307 static const openmode out
= openmode(__ios_flags::_S_out
);
308 /// Open for input. Default for @c ofstream.
309 static const openmode trunc
= openmode(__ios_flags::_S_trunc
);
311 // 27.4.2.1.5 Type ios_base::seekdir
313 * @brief This is an enumerated type.
315 * @c "_Ios_Seekdir" is implementation-defined. Defined values
316 * of type seekdir are:
318 * - cur, equivalent to @c SEEK_CUR in the C standard library.
319 * - end, equivalent to @c SEEK_END in the C standard library.
321 typedef _Ios_Seekdir seekdir
;
322 /// Request a seek relative to the beginning of the stream.
323 static const seekdir beg
= seekdir(0);
324 /// Request a seek relative to the current position within the sequence.
325 static const seekdir cur
= seekdir(SEEK_CUR
);
326 /// Request a seek relative to the current end of the sequence.
327 static const seekdir end
= seekdir(SEEK_END
);
329 #ifdef _GLIBCPP_DEPRECATED
331 typedef int io_state
;
332 typedef int open_mode
;
333 typedef int seek_dir
;
335 typedef std::streampos streampos
;
336 typedef std::streamoff streamoff
;
353 typedef void (*event_callback
) (event
, ios_base
&, int);
359 register_callback(event_callback __fn
, int __index
);
365 * ios_base data members (doc me)
368 streamsize _M_precision
;
371 iostate _M_exception
;
372 iostate _M_streambuf_state
;
375 // 27.4.2.6 Members for callbacks
376 // 27.4.2.6 ios_base callbacks
377 struct _Callback_list
380 _Callback_list
* _M_next
;
381 ios_base::event_callback _M_fn
;
383 _Atomic_word _M_refcount
; // 0 means one reference.
385 _Callback_list(ios_base::event_callback __fn
, int __index
,
386 _Callback_list
* __cb
)
387 : _M_next(__cb
), _M_fn(__fn
), _M_index(__index
), _M_refcount(0) { }
390 _M_add_reference() { __atomic_add(&_M_refcount
, 1); }
392 // 0 => OK to delete.
394 _M_remove_reference() { return __exchange_and_add(&_M_refcount
, -1); }
397 _Callback_list
* _M_callbacks
;
400 _M_call_callbacks(event __ev
) throw();
403 _M_dispose_callbacks(void);
405 // 27.4.2.5 Members for iword/pword storage
410 _Words() : _M_pword(0), _M_iword(0) { }
413 // Only for failed iword/pword calls.
416 // Guaranteed storage.
417 static const int _S_local_word_size
= 8;
418 _Words _M_local_word
[_S_local_word_size
];
420 // Allocated storage.
425 _M_grow_words(int __index
);
427 // Members for locale and locale caching.
428 locale _M_ios_locale
;
435 // 27.4.2.1.6 Class ios_base::Init
436 // Used to initialize standard streams. In theory, g++ could use
437 // -finit-priority to order this stuff correctly without going
438 // through these machinations.
441 friend class ios_base
;
447 _S_ios_create(bool __sync
);
453 static int _S_ios_base_init
;
454 static bool _S_synced_with_stdio
;
457 // [27.4.2.2] fmtflags state functions
459 * @brief Access to format flags.
460 * @return The format control flags for both input and output.
463 flags() const { return _M_flags
; }
466 * @brief Setting new format flags all at once.
467 * @param fmtfl The new flags to set.
468 * @return The previous format control flags.
470 * This function overwrites all the format flags with @a fmtfl.
473 flags(fmtflags __fmtfl
)
475 fmtflags __old
= _M_flags
;
481 * @brief Setting new format flags.
482 * @param fmtfl Additional flags to set.
483 * @return The previous format control flags.
485 * This function sets additional flags in format control. Flags that
486 * were previously set remain set.
489 setf(fmtflags __fmtfl
)
491 fmtflags __old
= _M_flags
;
497 * @brief Setting new format flags.
498 * @param fmtfl Additional flags to set.
499 * @param mask The flags mask for @a fmtfl.
500 * @return The previous format control flags.
502 * This function clears @a mask in the format flags, then sets
503 * @a fmtfl @c & @a mask. An example mask is @c ios_base::adjustfield.
506 setf(fmtflags __fmtfl
, fmtflags __mask
)
508 fmtflags __old
= _M_flags
;
510 _M_flags
|= (__fmtfl
& __mask
);
515 * @brief Clearing format flags.
516 * @param mask The flags to unset.
518 * This function clears @a mask in the format flags.
521 unsetf(fmtflags __mask
) { _M_flags
&= ~__mask
; }
524 * @brief Flags access.
525 * @return The precision to generate on certain output operations.
528 * Be careful if you try to give a definition of "precision" here; see
533 precision() const { return _M_precision
; }
536 * @brief Changing flags.
537 * @param prec The new precision value.
538 * @return The previous value of precision().
541 precision(streamsize __prec
)
543 streamsize __old
= _M_precision
;
544 _M_precision
= __prec
;
549 * @brief Flags access.
550 * @return The minimum field width to generate on output operations.
552 * "Minimum field width" refers to the number of characters.
555 width() const { return _M_width
; }
558 * @brief Changing flags.
559 * @param wide The new width value.
560 * @return The previous value of width().
563 width(streamsize __wide
)
565 streamsize __old
= _M_width
;
570 // [27.4.2.4] ios_base static members
572 * @brief Interaction with the standard C I/O objects.
573 * @param sync Whether to synchronize or not.
574 * @return True if the standard streams were previously synchronized.
576 * The synchronization referred to is @e only that between the standard
577 * C facilities (e.g., stdout) and the standard C++ objects (e.g.,
578 * cout). User-declared streams are unaffected. See
579 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#8 for more.
582 sync_with_stdio(bool __sync
= true);
584 // [27.4.2.3] ios_base locale functions
586 * @brief Setting a new locale.
587 * @param loc The new locale.
588 * @return The previous locale.
590 * Sets the new locale for this stream, and
591 * [XXX does something with callbacks].
594 imbue(const locale
& __loc
);
597 * @brief Locale access
598 * @return The locale currently in effect.
600 * If @c imbue(loc) has previously been called, then this function
601 * returns @c loc. Otherwise, it returns a copy of @c std::locale(),
602 * the global C++ locale.
605 getloc() const { return _M_ios_locale
; }
607 // [27.4.2.5] ios_base storage functions
620 _Words
& __word
= (__ix
< _M_word_size
)
621 ? _M_word
[__ix
] : _M_grow_words(__ix
);
622 return __word
._M_iword
;
631 _Words
& __word
= (__ix
< _M_word_size
)
632 ? _M_word
[__ix
] : _M_grow_words(__ix
);
633 return __word
._M_pword
;
638 * Destroys local storage and
639 * [XXX does something with callbacks].
646 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
647 //50. Copy constructor and assignment operator of ios_base
649 ios_base(const ios_base
&);
652 operator=(const ios_base
&);
656 // [27.4.5.1] fmtflags manipulators
657 /// Calls base.setf(ios_base::boolalpha).
659 boolalpha(ios_base
& __base
)
661 __base
.setf(ios_base::boolalpha
);
665 /// Calls base.unsetf(ios_base::boolalpha).
667 noboolalpha(ios_base
& __base
)
669 __base
.unsetf(ios_base::boolalpha
);
673 /// Calls base.setf(ios_base::showbase).
675 showbase(ios_base
& __base
)
677 __base
.setf(ios_base::showbase
);
681 /// Calls base.unsetf(ios_base::showbase).
683 noshowbase(ios_base
& __base
)
685 __base
.unsetf(ios_base::showbase
);
689 /// Calls base.setf(ios_base::showpoint).
691 showpoint(ios_base
& __base
)
693 __base
.setf(ios_base::showpoint
);
697 /// Calls base.unsetf(ios_base::showpoint).
699 noshowpoint(ios_base
& __base
)
701 __base
.unsetf(ios_base::showpoint
);
705 /// Calls base.setf(ios_base::showpos).
707 showpos(ios_base
& __base
)
709 __base
.setf(ios_base::showpos
);
713 /// Calls base.unsetf(ios_base::showpos).
715 noshowpos(ios_base
& __base
)
717 __base
.unsetf(ios_base::showpos
);
721 /// Calls base.setf(ios_base::skipws).
723 skipws(ios_base
& __base
)
725 __base
.setf(ios_base::skipws
);
729 /// Calls base.unsetf(ios_base::skipws).
731 noskipws(ios_base
& __base
)
733 __base
.unsetf(ios_base::skipws
);
737 /// Calls base.setf(ios_base::uppercase).
739 uppercase(ios_base
& __base
)
741 __base
.setf(ios_base::uppercase
);
745 /// Calls base.unsetf(ios_base::uppercase).
747 nouppercase(ios_base
& __base
)
749 __base
.unsetf(ios_base::uppercase
);
753 /// Calls base.setf(ios_base::unitbuf).
755 unitbuf(ios_base
& __base
)
757 __base
.setf(ios_base::unitbuf
);
761 /// Calls base.unsetf(ios_base::unitbuf).
763 nounitbuf(ios_base
& __base
)
765 __base
.unsetf(ios_base::unitbuf
);
769 // [27.4.5.2] adjustfield anipulators
770 /// Calls base.setf(ios_base::internal, ios_base::adjustfield).
772 internal(ios_base
& __base
)
774 __base
.setf(ios_base::internal
, ios_base::adjustfield
);
778 /// Calls base.setf(ios_base::left, ios_base::adjustfield).
780 left(ios_base
& __base
)
782 __base
.setf(ios_base::left
, ios_base::adjustfield
);
786 /// Calls base.setf(ios_base::right, ios_base::adjustfield).
788 right(ios_base
& __base
)
790 __base
.setf(ios_base::right
, ios_base::adjustfield
);
794 // [27.4.5.3] basefield anipulators
795 /// Calls base.setf(ios_base::dec, ios_base::basefield).
797 dec(ios_base
& __base
)
799 __base
.setf(ios_base::dec
, ios_base::basefield
);
803 /// Calls base.setf(ios_base::hex, ios_base::basefield).
805 hex(ios_base
& __base
)
807 __base
.setf(ios_base::hex
, ios_base::basefield
);
811 /// Calls base.setf(ios_base::oct, ios_base::basefield).
813 oct(ios_base
& __base
)
815 __base
.setf(ios_base::oct
, ios_base::basefield
);
819 // [27.4.5.4] floatfield anipulators
820 /// Calls base.setf(ios_base::fixed, ios_base::floatfield).
822 fixed(ios_base
& __base
)
824 __base
.setf(ios_base::fixed
, ios_base::floatfield
);
828 /// Calls base.setf(ios_base::scientific, ios_base::floatfield).
830 scientific(ios_base
& __base
)
832 __base
.setf(ios_base::scientific
, ios_base::floatfield
);
838 #endif /* _CPP_BITS_IOSBASE_H */