Daily bump.
[official-gcc.git] / libstdc++-v3 / include / bits / ios_base.h
blobf5b026900f60636829bef8ace3332a5cbc1c29ad
1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
4 // Free Software Foundation, Inc.
5 //
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)
10 // any later version.
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,
20 // USA.
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
35 /** @file ios_base.h
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>
47 namespace std
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 };
55 inline _Ios_Fmtflags
56 operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
57 { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); }
59 inline _Ios_Fmtflags
60 operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
61 { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); }
63 inline _Ios_Fmtflags
64 operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
65 { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); }
67 inline _Ios_Fmtflags
68 operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
69 { return __a = __a | __b; }
71 inline _Ios_Fmtflags
72 operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
73 { return __a = __a & __b; }
75 inline _Ios_Fmtflags
76 operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
77 { return __a = __a ^ __b; }
79 inline _Ios_Fmtflags
80 operator~(_Ios_Fmtflags __a)
81 { return _Ios_Fmtflags(~static_cast<int>(__a)); }
84 enum _Ios_Openmode { _M_ios_openmode_end = 1L << 16 };
86 inline _Ios_Openmode
87 operator&(_Ios_Openmode __a, _Ios_Openmode __b)
88 { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); }
90 inline _Ios_Openmode
91 operator|(_Ios_Openmode __a, _Ios_Openmode __b)
92 { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); }
94 inline _Ios_Openmode
95 operator^(_Ios_Openmode __a, _Ios_Openmode __b)
96 { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); }
98 inline _Ios_Openmode
99 operator|=(_Ios_Openmode& __a, _Ios_Openmode __b)
100 { return __a = __a | __b; }
102 inline _Ios_Openmode
103 operator&=(_Ios_Openmode& __a, _Ios_Openmode __b)
104 { return __a = __a & __b; }
106 inline _Ios_Openmode
107 operator^=(_Ios_Openmode& __a, _Ios_Openmode __b)
108 { return __a = __a ^ __b; }
110 inline _Ios_Openmode
111 operator~(_Ios_Openmode __a)
112 { return _Ios_Openmode(~static_cast<int>(__a)); }
115 enum _Ios_Iostate { _M_ios_iostate_end = 1L << 16 };
117 inline _Ios_Iostate
118 operator&(_Ios_Iostate __a, _Ios_Iostate __b)
119 { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); }
121 inline _Ios_Iostate
122 operator|(_Ios_Iostate __a, _Ios_Iostate __b)
123 { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); }
125 inline _Ios_Iostate
126 operator^(_Ios_Iostate __a, _Ios_Iostate __b)
127 { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); }
129 inline _Ios_Iostate
130 operator|=(_Ios_Iostate& __a, _Ios_Iostate __b)
131 { return __a = __a | __b; }
133 inline _Ios_Iostate
134 operator&=(_Ios_Iostate& __a, _Ios_Iostate __b)
135 { return __a = __a & __b; }
137 inline _Ios_Iostate
138 operator^=(_Ios_Iostate& __a, _Ios_Iostate __b)
139 { return __a = __a ^ __b; }
141 inline _Ios_Iostate
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
148 class ios_base
150 public:
152 // 27.4.2.1.1 Class ios_base::failure
153 class failure : public exception
155 public:
156 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
157 //48. Use of non-existent exception constructor
158 explicit
159 failure(const string& __str) throw();
161 // This declaration is not useless:
162 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
163 virtual
164 ~failure() throw();
166 virtual const char*
167 what() const throw();
169 private:
170 enum { _M_bufsize = 256 };
171 char _M_name[_M_bufsize];
172 #endif
175 // 27.4.2.1.2 Type ios_base::fmtflags
176 typedef _Ios_Fmtflags fmtflags;
177 // 27.4.2.1.2 Type fmtflags
178 static const fmtflags boolalpha = fmtflags(__ios_flags::_S_boolalpha);
179 static const fmtflags dec = fmtflags(__ios_flags::_S_dec);
180 static const fmtflags fixed = fmtflags(__ios_flags::_S_fixed);
181 static const fmtflags hex = fmtflags(__ios_flags::_S_hex);
182 static const fmtflags internal = fmtflags(__ios_flags::_S_internal);
183 static const fmtflags left = fmtflags(__ios_flags::_S_left);
184 static const fmtflags oct = fmtflags(__ios_flags::_S_oct);
185 static const fmtflags right = fmtflags(__ios_flags::_S_right);
186 static const fmtflags scientific = fmtflags(__ios_flags::_S_scientific);
187 static const fmtflags showbase = fmtflags(__ios_flags::_S_showbase);
188 static const fmtflags showpoint = fmtflags(__ios_flags::_S_showpoint);
189 static const fmtflags showpos = fmtflags(__ios_flags::_S_showpos);
190 static const fmtflags skipws = fmtflags(__ios_flags::_S_skipws);
191 static const fmtflags unitbuf = fmtflags(__ios_flags::_S_unitbuf);
192 static const fmtflags uppercase = fmtflags(__ios_flags::_S_uppercase);
193 static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield);
194 static const fmtflags basefield = fmtflags(__ios_flags::_S_basefield);
195 static const fmtflags floatfield = fmtflags(__ios_flags::_S_floatfield);
197 // 27.4.2.1.3 Type ios_base::iostate
198 typedef _Ios_Iostate iostate;
199 static const iostate badbit = iostate(__ios_flags::_S_badbit);
200 static const iostate eofbit = iostate(__ios_flags::_S_eofbit);
201 static const iostate failbit = iostate(__ios_flags::_S_failbit);
202 static const iostate goodbit = iostate(0);
204 // 27.4.2.1.4 Type openmode
205 typedef _Ios_Openmode openmode;
206 static const openmode app = openmode(__ios_flags::_S_app);
207 static const openmode ate = openmode(__ios_flags::_S_ate);
208 static const openmode binary = openmode(__ios_flags::_S_bin);
209 static const openmode in = openmode(__ios_flags::_S_in);
210 static const openmode out = openmode(__ios_flags::_S_out);
211 static const openmode trunc = openmode(__ios_flags::_S_trunc);
213 // 27.4.2.1.5 Type seekdir
214 typedef _Ios_Seekdir seekdir;
215 static const seekdir beg = seekdir(0);
216 static const seekdir cur = seekdir(SEEK_CUR);
217 static const seekdir end = seekdir(SEEK_END);
219 #ifdef _GLIBCPP_DEPRECATED
220 typedef int io_state;
221 typedef int open_mode;
222 typedef int seek_dir;
223 #endif
225 // Callbacks;
226 enum event
228 erase_event,
229 imbue_event,
230 copyfmt_event
233 typedef void (*event_callback) (event, ios_base&, int);
235 void
236 register_callback(event_callback __fn, int __index);
238 protected:
239 // Data Members
240 streamsize _M_precision;
241 streamsize _M_width;
242 fmtflags _M_flags;
243 iostate _M_exception;
244 iostate _M_streambuf_state;
246 // 27.4.2.6 Members for callbacks
247 // 27.4.2.6 ios_base callbacks
248 struct _Callback_list
250 // Data Members
251 _Callback_list* _M_next;
252 ios_base::event_callback _M_fn;
253 int _M_index;
254 _Atomic_word _M_refcount; // 0 means one reference.
256 _Callback_list(ios_base::event_callback __fn, int __index,
257 _Callback_list* __cb)
258 : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
260 void
261 _M_add_reference() { __atomic_add(&_M_refcount, 1); }
263 // 0 => OK to delete.
264 int
265 _M_remove_reference() { return __exchange_and_add(&_M_refcount, -1); }
268 _Callback_list* _M_callbacks;
270 void
271 _M_call_callbacks(event __ev) throw();
273 void
274 _M_dispose_callbacks(void);
276 // 27.4.2.5 Members for iword/pword storage
277 struct _Words
279 void* _M_pword;
280 long _M_iword;
281 _Words() : _M_pword(0), _M_iword(0) { }
284 // Only for failed iword/pword calls.
285 _Words _M_word_zero;
287 // Guaranteed storage.
288 static const int _S_local_word_size = 8;
289 _Words _M_local_word[_S_local_word_size];
291 // Allocated storage.
292 int _M_word_size;
293 _Words* _M_word;
295 _Words&
296 _M_grow_words(int __index);
298 // Members for locale and locale caching.
299 locale _M_ios_locale;
301 void
302 _M_init();
304 public:
306 // 27.4.2.1.6 Class ios_base::Init
307 // Used to initialize standard streams. In theory, g++ could use
308 // -finit-priority to order this stuff correctly without going
309 // through these machinations.
310 class Init
312 friend class ios_base;
313 public:
314 Init();
315 ~Init();
317 static void
318 _S_ios_create(bool __sync);
320 static void
321 _S_ios_destroy();
323 private:
324 static int _S_ios_base_init;
325 static bool _S_synced_with_stdio;
328 // Fmtflags state:
329 inline fmtflags
330 flags() const { return _M_flags; }
332 inline fmtflags
333 flags(fmtflags __fmtfl)
335 fmtflags __old = _M_flags;
336 _M_flags = __fmtfl;
337 return __old;
340 inline fmtflags
341 setf(fmtflags __fmtfl)
343 fmtflags __old = _M_flags;
344 _M_flags |= __fmtfl;
345 return __old;
348 inline fmtflags
349 setf(fmtflags __fmtfl, fmtflags __mask)
351 fmtflags __old = _M_flags;
352 _M_flags &= ~__mask;
353 _M_flags |= (__fmtfl & __mask);
354 return __old;
357 inline void
358 unsetf(fmtflags __mask) { _M_flags &= ~__mask; }
360 inline streamsize
361 precision() const { return _M_precision; }
363 inline streamsize
364 precision(streamsize __prec)
366 streamsize __old = _M_precision;
367 _M_precision = __prec;
368 return __old;
371 inline streamsize
372 width() const { return _M_width; }
374 inline streamsize
375 width(streamsize __wide)
377 streamsize __old = _M_width;
378 _M_width = __wide;
379 return __old;
382 static bool
383 sync_with_stdio(bool __sync = true);
385 // Locales:
386 locale
387 imbue(const locale& __loc);
389 inline locale
390 getloc() const { return _M_ios_locale; }
392 // Storage:
393 static int
394 xalloc() throw();
396 inline long&
397 iword(int __ix)
399 _Words& __word = (__ix < _M_word_size)
400 ? _M_word[__ix] : _M_grow_words(__ix);
401 return __word._M_iword;
404 inline void*&
405 pword(int __ix)
407 _Words& __word = (__ix < _M_word_size)
408 ? _M_word[__ix] : _M_grow_words(__ix);
409 return __word._M_pword;
412 // Destructor
413 ~ios_base();
415 protected:
416 ios_base();
418 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
419 //50. Copy constructor and assignment operator of ios_base
420 private:
421 ios_base(const ios_base&);
423 ios_base&
424 operator=(const ios_base&);
425 #endif
428 // 27.4.5.1 fmtflags manipulators:
429 inline ios_base&
430 boolalpha(ios_base& __base)
432 __base.setf(ios_base::boolalpha);
433 return __base;
436 inline ios_base&
437 noboolalpha(ios_base& __base)
439 __base.unsetf(ios_base::boolalpha);
440 return __base;
443 inline ios_base&
444 showbase(ios_base& __base)
446 __base.setf(ios_base::showbase);
447 return __base;
450 inline ios_base&
451 noshowbase(ios_base& __base)
453 __base.unsetf(ios_base::showbase);
454 return __base;
457 inline ios_base&
458 showpoint(ios_base& __base)
460 __base.setf(ios_base::showpoint);
461 return __base;
464 inline ios_base&
465 noshowpoint(ios_base& __base)
467 __base.unsetf(ios_base::showpoint);
468 return __base;
471 inline ios_base&
472 showpos(ios_base& __base)
474 __base.setf(ios_base::showpos);
475 return __base;
478 inline ios_base&
479 noshowpos(ios_base& __base)
481 __base.unsetf(ios_base::showpos);
482 return __base;
485 inline ios_base&
486 skipws(ios_base& __base)
488 __base.setf(ios_base::skipws);
489 return __base;
492 inline ios_base&
493 noskipws(ios_base& __base)
495 __base.unsetf(ios_base::skipws);
496 return __base;
499 inline ios_base&
500 uppercase(ios_base& __base)
502 __base.setf(ios_base::uppercase);
503 return __base;
506 inline ios_base&
507 nouppercase(ios_base& __base)
509 __base.unsetf(ios_base::uppercase);
510 return __base;
513 inline ios_base&
514 unitbuf(ios_base& __base)
516 __base.setf(ios_base::unitbuf);
517 return __base;
520 inline ios_base&
521 nounitbuf(ios_base& __base)
523 __base.unsetf(ios_base::unitbuf);
524 return __base;
527 // 27.4.5.2 adjustfield anipulators:
528 inline ios_base&
529 internal(ios_base& __base)
531 __base.setf(ios_base::internal, ios_base::adjustfield);
532 return __base;
535 inline ios_base&
536 left(ios_base& __base)
538 __base.setf(ios_base::left, ios_base::adjustfield);
539 return __base;
542 inline ios_base&
543 right(ios_base& __base)
545 __base.setf(ios_base::right, ios_base::adjustfield);
546 return __base;
549 // 27.4.5.3 basefield anipulators:
550 inline ios_base&
551 dec(ios_base& __base)
553 __base.setf(ios_base::dec, ios_base::basefield);
554 return __base;
557 inline ios_base&
558 hex(ios_base& __base)
560 __base.setf(ios_base::hex, ios_base::basefield);
561 return __base;
564 inline ios_base&
565 oct(ios_base& __base)
567 __base.setf(ios_base::oct, ios_base::basefield);
568 return __base;
571 // 27.4.5.4 floatfield anipulators:
572 inline ios_base&
573 fixed(ios_base& __base)
575 __base.setf(ios_base::fixed, ios_base::floatfield);
576 return __base;
579 inline ios_base&
580 scientific(ios_base& __base)
582 __base.setf(ios_base::scientific, ios_base::floatfield);
583 return __base;
586 } // namespace std
588 #endif /* _CPP_BITS_IOSBASE_H */