]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/ios_base.h
algo.h: Use std not __STD.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / ios_base.h
1 // Iostreams base classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 //
31 // ISO C++ 14882: 27.8 File-based streams
32 //
33
34 #ifndef _CPP_BITS_IOSBASE_H
35 #define _CPP_BITS_IOSBASE_H 1
36
37 #pragma GCC system_header
38
39 namespace std
40 {
41
42 // The following definitions of bitmask types are enums, not ints,
43 // as permitted (but not required) in the standard, in order to provide
44 // better type safety in iostream calls. A side effect is that
45 // expressions involving them are no longer compile-time constants.
46 enum _Ios_Fmtflags { _M_ios_fmtflags_end = 1L << 16 };
47
48 inline _Ios_Fmtflags
49 operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
50 { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); }
51
52 inline _Ios_Fmtflags
53 operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
54 { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); }
55
56 inline _Ios_Fmtflags
57 operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
58 { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); }
59
60 inline _Ios_Fmtflags
61 operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
62 { return __a = __a | __b; }
63
64 inline _Ios_Fmtflags
65 operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
66 { return __a = __a & __b; }
67
68 inline _Ios_Fmtflags
69 operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
70 { return __a = __a ^ __b; }
71
72 inline _Ios_Fmtflags
73 operator~(_Ios_Fmtflags __a)
74 { return _Ios_Fmtflags(~static_cast<int>(__a)); }
75
76
77 enum _Ios_Openmode { _M_ios_openmode_end = 1L << 16 };
78
79 inline _Ios_Openmode
80 operator&(_Ios_Openmode __a, _Ios_Openmode __b)
81 { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); }
82
83 inline _Ios_Openmode
84 operator|(_Ios_Openmode __a, _Ios_Openmode __b)
85 { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); }
86
87 inline _Ios_Openmode
88 operator^(_Ios_Openmode __a, _Ios_Openmode __b)
89 { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); }
90
91 inline _Ios_Openmode
92 operator|=(_Ios_Openmode& __a, _Ios_Openmode __b)
93 { return __a = __a | __b; }
94
95 inline _Ios_Openmode
96 operator&=(_Ios_Openmode& __a, _Ios_Openmode __b)
97 { return __a = __a & __b; }
98
99 inline _Ios_Openmode
100 operator^=(_Ios_Openmode& __a, _Ios_Openmode __b)
101 { return __a = __a ^ __b; }
102
103 inline _Ios_Openmode
104 operator~(_Ios_Openmode __a)
105 { return _Ios_Openmode(~static_cast<int>(__a)); }
106
107
108 enum _Ios_Iostate { _M_ios_iostate_end = 1L << 16 };
109
110 inline _Ios_Iostate
111 operator&(_Ios_Iostate __a, _Ios_Iostate __b)
112 { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); }
113
114 inline _Ios_Iostate
115 operator|(_Ios_Iostate __a, _Ios_Iostate __b)
116 { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); }
117
118 inline _Ios_Iostate
119 operator^(_Ios_Iostate __a, _Ios_Iostate __b)
120 { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); }
121
122 inline _Ios_Iostate
123 operator|=(_Ios_Iostate& __a, _Ios_Iostate __b)
124 { return __a = __a | __b; }
125
126 inline _Ios_Iostate
127 operator&=(_Ios_Iostate& __a, _Ios_Iostate __b)
128 { return __a = __a & __b; }
129
130 inline _Ios_Iostate
131 operator^=(_Ios_Iostate& __a, _Ios_Iostate __b)
132 { return __a = __a ^ __b; }
133
134 inline _Ios_Iostate
135 operator~(_Ios_Iostate __a)
136 { return _Ios_Iostate(~static_cast<int>(__a)); }
137
138 enum _Ios_Seekdir { _M_ios_seekdir_end = 1L << 16 };
139
140 // 27.4.2 Class ios_base
141 class ios_base
142 {
143 public:
144
145 // 27.4.2.1.1 Class ios_base::failure
146 class failure : public exception
147 {
148 public:
149 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
150 // Can't do exception(_msg) as defined in 27.4.2.1.1
151 explicit
152 failure(const string& __str) throw();
153
154 virtual
155 ~failure() throw();
156
157 virtual const char*
158 what() const throw();
159
160 private:
161 enum { _M_bufsize = 256 };
162 char _M_name[_M_bufsize];
163 #endif
164 };
165
166 // 27.4.2.1.2 Type ios_base::fmtflags
167 typedef _Ios_Fmtflags fmtflags;
168 // 27.4.2.1.2 Type fmtflags
169 static const fmtflags boolalpha = fmtflags(__ios_flags::_S_boolalpha);
170 static const fmtflags dec = fmtflags(__ios_flags::_S_dec);
171 static const fmtflags fixed = fmtflags(__ios_flags::_S_fixed);
172 static const fmtflags hex = fmtflags(__ios_flags::_S_hex);
173 static const fmtflags internal = fmtflags(__ios_flags::_S_internal);
174 static const fmtflags left = fmtflags(__ios_flags::_S_left);
175 static const fmtflags oct = fmtflags(__ios_flags::_S_oct);
176 static const fmtflags right = fmtflags(__ios_flags::_S_right);
177 static const fmtflags scientific = fmtflags(__ios_flags::_S_scientific);
178 static const fmtflags showbase = fmtflags(__ios_flags::_S_showbase);
179 static const fmtflags showpoint = fmtflags(__ios_flags::_S_showpoint);
180 static const fmtflags showpos = fmtflags(__ios_flags::_S_showpos);
181 static const fmtflags skipws = fmtflags(__ios_flags::_S_skipws);
182 static const fmtflags unitbuf = fmtflags(__ios_flags::_S_unitbuf);
183 static const fmtflags uppercase = fmtflags(__ios_flags::_S_uppercase);
184 static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield);
185 static const fmtflags basefield = fmtflags(__ios_flags::_S_basefield);
186 static const fmtflags floatfield = fmtflags(__ios_flags::_S_floatfield);
187
188 // 27.4.2.1.3 Type ios_base::iostate
189 typedef _Ios_Iostate iostate;
190 static const iostate badbit = iostate(__ios_flags::_S_badbit);
191 static const iostate eofbit = iostate(__ios_flags::_S_eofbit);
192 static const iostate failbit = iostate(__ios_flags::_S_failbit);
193 static const iostate goodbit = iostate(0);
194
195 // 27.4.2.1.4 Type openmode
196 typedef _Ios_Openmode openmode;
197 static const openmode app = openmode(__ios_flags::_S_app);
198 static const openmode ate = openmode(__ios_flags::_S_ate);
199 static const openmode binary = openmode(__ios_flags::_S_bin);
200 static const openmode in = openmode(__ios_flags::_S_in);
201 static const openmode out = openmode(__ios_flags::_S_out);
202 static const openmode trunc = openmode(__ios_flags::_S_trunc);
203
204 // 27.4.2.1.5 Type seekdir
205 typedef _Ios_Seekdir seekdir;
206 static const seekdir beg = seekdir(0);
207 static const seekdir cur = seekdir(SEEK_CUR);
208 static const seekdir end = seekdir(SEEK_END);
209
210 #ifdef _GLIBCPP_DEPRECATED
211 typedef int io_state;
212 typedef int open_mode;
213 typedef int seek_dir;
214 #endif
215
216 // Callbacks;
217 enum event
218 {
219 erase_event,
220 imbue_event,
221 copyfmt_event
222 };
223
224 typedef void (*event_callback) (event, ios_base&, int);
225
226 void
227 register_callback(event_callback __fn, int __index);
228
229 protected:
230 // Data Members
231 streamsize _M_precision;
232 streamsize _M_width;
233 fmtflags _M_flags;
234
235 // 27.4.2.6 Members for callbacks
236 // 27.4.2.6 ios_base callbacks
237 struct _Callback_list
238 {
239 // Data Members
240 _Callback_list* _M_next;
241 ios_base::event_callback _M_fn;
242 int _M_index;
243 int _M_refcount; // 0 means one reference.
244
245 _Callback_list(ios_base::event_callback __fn, int __index,
246 _Callback_list* __cb)
247 : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
248
249 void
250 _M_add_reference() { ++_M_refcount; } // XXX MT
251
252 int
253 _M_remove_reference() { return _M_refcount--; } // 0 => OK to delete
254 };
255
256 _Callback_list* _M_callbacks;
257
258 void
259 _M_call_callbacks(event __ev) throw();
260
261 void
262 _M_dispose_callbacks(void);
263
264 // 27.4.2.5 Members for iword/pword storage
265 struct _Words
266 {
267 void* _M_pword;
268 long _M_iword;
269 };
270
271 static const int _S_local_words = 8;
272 _Words _M_word_array[_S_local_words]; // Guaranteed storage
273 _Words _M_dummy; // Only for failed iword/pword calls.
274 _Words* _M_words;
275 int _M_word_limit;
276
277 _Words&
278 _M_grow_words(int __index);
279
280 // Members for locale and locale caching.
281 locale _M_ios_locale;
282
283 void
284 _M_init();
285
286 public:
287
288 // 27.4.2.1.6 Class ios_base::Init
289 // Used to initialize standard streams. In theory, g++ could use
290 // -finit-priority to order this stuff correctly without going
291 // through these machinations.
292 class Init
293 {
294 friend class ios_base;
295 public:
296 Init();
297 ~Init();
298 private:
299 static int _S_ios_base_init;
300 static bool _S_synced_with_stdio;
301 filebuf* _M_cout;
302 filebuf* _M_cin;
303 filebuf* _M_cerr;
304 #ifdef _GLIBCPP_USE_WCHAR_T
305 wfilebuf* _M_wcout;
306 wfilebuf* _M_wcin;
307 wfilebuf* _M_wcerr;
308 #endif
309 };
310
311 // Fmtflags state:
312 inline fmtflags
313 flags() const { return _M_flags; }
314
315 inline fmtflags
316 flags(fmtflags __fmtfl)
317 {
318 fmtflags __old = _M_flags;
319 _M_flags = __fmtfl;
320 return __old;
321 }
322
323 inline fmtflags
324 setf(fmtflags __fmtfl)
325 {
326 fmtflags __old = _M_flags;
327 _M_flags |= __fmtfl;
328 return __old;
329 }
330
331 inline fmtflags
332 setf(fmtflags __fmtfl, fmtflags __mask)
333 {
334 fmtflags __old = _M_flags;
335 _M_flags &= ~__mask;
336 _M_flags |= (__fmtfl & __mask);
337 return __old;
338 }
339
340 inline void
341 unsetf(fmtflags __mask) { _M_flags &= ~__mask; }
342
343 inline streamsize
344 precision() const { return _M_precision; }
345
346 inline streamsize
347 precision(streamsize __prec)
348 {
349 streamsize __old = _M_precision;
350 _M_precision = __prec;
351 return __old;
352 }
353
354 inline streamsize
355 width() const { return _M_width; }
356
357 inline streamsize
358 width(streamsize __wide)
359 {
360 streamsize __old = _M_width;
361 _M_width = __wide;
362 return __old;
363 }
364
365 static bool
366 sync_with_stdio(bool __sync = true);
367
368 // Locales:
369 locale
370 imbue(const locale& __loc);
371
372 inline locale
373 getloc() const { return _M_ios_locale; }
374
375 // Storage:
376 static int
377 xalloc() throw();
378
379 inline long&
380 iword(int __ix)
381 {
382 _Words& __word = (__ix < _M_word_limit)
383 ? _M_words[__ix] : _M_grow_words(__ix);
384 return __word._M_iword;
385 }
386
387 inline void*&
388 pword(int __ix)
389 {
390 _Words& __word = (__ix < _M_word_limit)
391 ? _M_words[__ix] : _M_grow_words(__ix);
392 return __word._M_pword;
393 }
394
395 // Destructor
396 ~ios_base();
397
398 protected:
399 ios_base();
400
401 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
402 private:
403 ios_base(const ios_base&);
404
405 ios_base&
406 operator=(const ios_base&);
407 #endif
408 };
409
410 // 27.4.5.1 fmtflags manipulators:
411 inline ios_base&
412 boolalpha(ios_base& __base)
413 {
414 __base.setf(ios_base::boolalpha);
415 return __base;
416 }
417
418 inline ios_base&
419 noboolalpha(ios_base& __base)
420 {
421 __base.unsetf(ios_base::boolalpha);
422 return __base;
423 }
424
425 inline ios_base&
426 showbase(ios_base& __base)
427 {
428 __base.setf(ios_base::showbase);
429 return __base;
430 }
431
432 inline ios_base&
433 noshowbase(ios_base& __base)
434 {
435 __base.unsetf(ios_base::showbase);
436 return __base;
437 }
438
439 inline ios_base&
440 showpoint(ios_base& __base)
441 {
442 __base.setf(ios_base::showpoint);
443 return __base;
444 }
445
446 inline ios_base&
447 noshowpoint(ios_base& __base)
448 {
449 __base.unsetf(ios_base::showpoint);
450 return __base;
451 }
452
453 inline ios_base&
454 showpos(ios_base& __base)
455 {
456 __base.setf(ios_base::showpos);
457 return __base;
458 }
459
460 inline ios_base&
461 noshowpos(ios_base& __base)
462 {
463 __base.unsetf(ios_base::showpos);
464 return __base;
465 }
466
467 inline ios_base&
468 skipws(ios_base& __base)
469 {
470 __base.setf(ios_base::skipws);
471 return __base;
472 }
473
474 inline ios_base&
475 noskipws(ios_base& __base)
476 {
477 __base.unsetf(ios_base::skipws);
478 return __base;
479 }
480
481 inline ios_base&
482 uppercase(ios_base& __base)
483 {
484 __base.setf(ios_base::uppercase);
485 return __base;
486 }
487
488 inline ios_base&
489 nouppercase(ios_base& __base)
490 {
491 __base.unsetf(ios_base::uppercase);
492 return __base;
493 }
494
495 inline ios_base&
496 unitbuf(ios_base& __base)
497 {
498 __base.setf(ios_base::unitbuf);
499 return __base;
500 }
501
502 inline ios_base&
503 nounitbuf(ios_base& __base)
504 {
505 __base.unsetf(ios_base::unitbuf);
506 return __base;
507 }
508
509 // 27.4.5.2 adjustfield anipulators:
510 inline ios_base&
511 internal(ios_base& __base)
512 {
513 __base.setf(ios_base::internal, ios_base::adjustfield);
514 return __base;
515 }
516
517 inline ios_base&
518 left(ios_base& __base)
519 {
520 __base.setf(ios_base::left, ios_base::adjustfield);
521 return __base;
522 }
523
524 inline ios_base&
525 right(ios_base& __base)
526 {
527 __base.setf(ios_base::right, ios_base::adjustfield);
528 return __base;
529 }
530
531 // 27.4.5.3 basefield anipulators:
532 inline ios_base&
533 dec(ios_base& __base)
534 {
535 __base.setf(ios_base::dec, ios_base::basefield);
536 return __base;
537 }
538
539 inline ios_base&
540 hex(ios_base& __base)
541 {
542 __base.setf(ios_base::hex, ios_base::basefield);
543 return __base;
544 }
545
546 inline ios_base&
547 oct(ios_base& __base)
548 {
549 __base.setf(ios_base::oct, ios_base::basefield);
550 return __base;
551 }
552
553 // 27.4.5.4 floatfield anipulators:
554 inline ios_base&
555 fixed(ios_base& __base)
556 {
557 __base.setf(ios_base::fixed, ios_base::floatfield);
558 return __base;
559 }
560
561 inline ios_base&
562 scientific(ios_base& __base)
563 {
564 __base.setf(ios_base::scientific, ios_base::floatfield);
565 return __base;
566 }
567
568 } // namespace std
569
570 #endif /* _CPP_BITS_IOSBASE_H */
571