ios_base.h

Go to the documentation of this file.
00001 // Iostreams base classes -*- C++ -*- 00002 00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 00004 // Free Software Foundation, Inc. 00005 // 00006 // This file is part of the GNU ISO C++ Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the GNU General Public License as published by the 00009 // Free Software Foundation; either version 2, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 00017 // You should have received a copy of the GNU General Public License along 00018 // with this library; see the file COPYING. If not, write to the Free 00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, 00020 // USA. 00021 00022 // As a special exception, you may use this file as part of a free software 00023 // library without restriction. Specifically, if other files instantiate 00024 // templates or use macros or inline functions from this file, or you compile 00025 // this file and link it with other files to produce an executable, this 00026 // file does not by itself cause the resulting executable to be covered by 00027 // the GNU General Public License. This exception does not however 00028 // invalidate any other reasons why the executable file might be covered by 00029 // the GNU General Public License. 00030 00031 // 00032 // ISO C++ 14882: 27.8 File-based streams 00033 // 00034 00040 #ifndef _CPP_BITS_IOSBASE_H 00041 #define _CPP_BITS_IOSBASE_H 1 00042 00043 #pragma GCC system_header 00044 00045 #include <bits/atomicity.h> 00046 00047 namespace std 00048 { 00049 // The following definitions of bitmask types are enums, not ints, 00050 // as permitted (but not required) in the standard, in order to provide 00051 // better type safety in iostream calls. A side effect is that 00052 // expressions involving them are no longer compile-time constants. 00053 enum _Ios_Fmtflags { _M_ios_fmtflags_end = 1L << 16 }; 00054 00055 inline _Ios_Fmtflags 00056 operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b) 00057 { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); } 00058 00059 inline _Ios_Fmtflags 00060 operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b) 00061 { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); } 00062 00063 inline _Ios_Fmtflags 00064 operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b) 00065 { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); } 00066 00067 inline _Ios_Fmtflags 00068 operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) 00069 { return __a = __a | __b; } 00070 00071 inline _Ios_Fmtflags 00072 operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) 00073 { return __a = __a & __b; } 00074 00075 inline _Ios_Fmtflags 00076 operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b) 00077 { return __a = __a ^ __b; } 00078 00079 inline _Ios_Fmtflags 00080 operator~(_Ios_Fmtflags __a) 00081 { return _Ios_Fmtflags(~static_cast<int>(__a)); } 00082 00083 00084 enum _Ios_Openmode { _M_ios_openmode_end = 1L << 16 }; 00085 00086 inline _Ios_Openmode 00087 operator&(_Ios_Openmode __a, _Ios_Openmode __b) 00088 { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); } 00089 00090 inline _Ios_Openmode 00091 operator|(_Ios_Openmode __a, _Ios_Openmode __b) 00092 { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); } 00093 00094 inline _Ios_Openmode 00095 operator^(_Ios_Openmode __a, _Ios_Openmode __b) 00096 { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); } 00097 00098 inline _Ios_Openmode 00099 operator|=(_Ios_Openmode& __a, _Ios_Openmode __b) 00100 { return __a = __a | __b; } 00101 00102 inline _Ios_Openmode 00103 operator&=(_Ios_Openmode& __a, _Ios_Openmode __b) 00104 { return __a = __a & __b; } 00105 00106 inline _Ios_Openmode 00107 operator^=(_Ios_Openmode& __a, _Ios_Openmode __b) 00108 { return __a = __a ^ __b; } 00109 00110 inline _Ios_Openmode 00111 operator~(_Ios_Openmode __a) 00112 { return _Ios_Openmode(~static_cast<int>(__a)); } 00113 00114 00115 enum _Ios_Iostate { _M_ios_iostate_end = 1L << 16 }; 00116 00117 inline _Ios_Iostate 00118 operator&(_Ios_Iostate __a, _Ios_Iostate __b) 00119 { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); } 00120 00121 inline _Ios_Iostate 00122 operator|(_Ios_Iostate __a, _Ios_Iostate __b) 00123 { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); } 00124 00125 inline _Ios_Iostate 00126 operator^(_Ios_Iostate __a, _Ios_Iostate __b) 00127 { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); } 00128 00129 inline _Ios_Iostate 00130 operator|=(_Ios_Iostate& __a, _Ios_Iostate __b) 00131 { return __a = __a | __b; } 00132 00133 inline _Ios_Iostate 00134 operator&=(_Ios_Iostate& __a, _Ios_Iostate __b) 00135 { return __a = __a & __b; } 00136 00137 inline _Ios_Iostate 00138 operator^=(_Ios_Iostate& __a, _Ios_Iostate __b) 00139 { return __a = __a ^ __b; } 00140 00141 inline _Ios_Iostate 00142 operator~(_Ios_Iostate __a) 00143 { return _Ios_Iostate(~static_cast<int>(__a)); } 00144 00145 enum _Ios_Seekdir { _M_ios_seekdir_end = 1L << 16 }; 00146 00147 // 27.4.2 Class ios_base 00148 class ios_base 00149 { 00150 public: 00151 00152 // 27.4.2.1.1 Class ios_base::failure 00153 class failure : public exception 00154 { 00155 public: 00156 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 00157 //48. Use of non-existent exception constructor 00158 explicit 00159 failure(const string& __str) throw(); 00160 00161 // This declaration is not useless: 00162 // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118 00163 virtual 00164 ~failure() throw(); 00165 00166 virtual const char* 00167 what() const throw(); 00168 00169 private: 00170 enum { _M_bufsize = 256 }; 00171 char _M_name[_M_bufsize]; 00172 #endif 00173 }; 00174 00175 // 27.4.2.1.2 Type ios_base::fmtflags 00176 typedef _Ios_Fmtflags fmtflags; 00177 // 27.4.2.1.2 Type fmtflags 00178 static const fmtflags boolalpha = fmtflags(__ios_flags::_S_boolalpha); 00179 static const fmtflags dec = fmtflags(__ios_flags::_S_dec); 00180 static const fmtflags fixed = fmtflags(__ios_flags::_S_fixed); 00181 static const fmtflags hex = fmtflags(__ios_flags::_S_hex); 00182 static const fmtflags internal = fmtflags(__ios_flags::_S_internal); 00183 static const fmtflags left = fmtflags(__ios_flags::_S_left); 00184 static const fmtflags oct = fmtflags(__ios_flags::_S_oct); 00185 static const fmtflags right = fmtflags(__ios_flags::_S_right); 00186 static const fmtflags scientific = fmtflags(__ios_flags::_S_scientific); 00187 static const fmtflags showbase = fmtflags(__ios_flags::_S_showbase); 00188 static const fmtflags showpoint = fmtflags(__ios_flags::_S_showpoint); 00189 static const fmtflags showpos = fmtflags(__ios_flags::_S_showpos); 00190 static const fmtflags skipws = fmtflags(__ios_flags::_S_skipws); 00191 static const fmtflags unitbuf = fmtflags(__ios_flags::_S_unitbuf); 00192 static const fmtflags uppercase = fmtflags(__ios_flags::_S_uppercase); 00193 static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield); 00194 static const fmtflags basefield = fmtflags(__ios_flags::_S_basefield); 00195 static const fmtflags floatfield = fmtflags(__ios_flags::_S_floatfield); 00196 00197 // 27.4.2.1.3 Type ios_base::iostate 00198 typedef _Ios_Iostate iostate; 00199 static const iostate badbit = iostate(__ios_flags::_S_badbit); 00200 static const iostate eofbit = iostate(__ios_flags::_S_eofbit); 00201 static const iostate failbit = iostate(__ios_flags::_S_failbit); 00202 static const iostate goodbit = iostate(0); 00203 00204 // 27.4.2.1.4 Type openmode 00205 typedef _Ios_Openmode openmode; 00206 static const openmode app = openmode(__ios_flags::_S_app); 00207 static const openmode ate = openmode(__ios_flags::_S_ate); 00208 static const openmode binary = openmode(__ios_flags::_S_bin); 00209 static const openmode in = openmode(__ios_flags::_S_in); 00210 static const openmode out = openmode(__ios_flags::_S_out); 00211 static const openmode trunc = openmode(__ios_flags::_S_trunc); 00212 00213 // 27.4.2.1.5 Type seekdir 00214 typedef _Ios_Seekdir seekdir; 00215 static const seekdir beg = seekdir(0); 00216 static const seekdir cur = seekdir(SEEK_CUR); 00217 static const seekdir end = seekdir(SEEK_END); 00218 00219 #ifdef _GLIBCPP_DEPRECATED 00220 typedef int io_state; 00221 typedef int open_mode; 00222 typedef int seek_dir; 00223 00224 typedef std::streampos streampos; 00225 typedef std::streamoff streamoff; 00226 #endif 00227 00228 // Callbacks; 00229 enum event 00230 { 00231 erase_event, 00232 imbue_event, 00233 copyfmt_event 00234 }; 00235 00236 typedef void (*event_callback) (event, ios_base&, int); 00237 00238 void 00239 register_callback(event_callback __fn, int __index); 00240 00241 protected: 00242 // Data Members 00243 streamsize _M_precision; 00244 streamsize _M_width; 00245 fmtflags _M_flags; 00246 iostate _M_exception; 00247 iostate _M_streambuf_state; 00248 00249 // 27.4.2.6 Members for callbacks 00250 // 27.4.2.6 ios_base callbacks 00251 struct _Callback_list 00252 { 00253 // Data Members 00254 _Callback_list* _M_next; 00255 ios_base::event_callback _M_fn; 00256 int _M_index; 00257 _Atomic_word _M_refcount; // 0 means one reference. 00258 00259 _Callback_list(ios_base::event_callback __fn, int __index, 00260 _Callback_list* __cb) 00261 : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { } 00262 00263 void 00264 _M_add_reference() { __atomic_add(&_M_refcount, 1); } 00265 00266 // 0 => OK to delete. 00267 int 00268 _M_remove_reference() { return __exchange_and_add(&_M_refcount, -1); } 00269 }; 00270 00271 _Callback_list* _M_callbacks; 00272 00273 void 00274 _M_call_callbacks(event __ev) throw(); 00275 00276 void 00277 _M_dispose_callbacks(void); 00278 00279 // 27.4.2.5 Members for iword/pword storage 00280 struct _Words 00281 { 00282 void* _M_pword; 00283 long _M_iword; 00284 _Words() : _M_pword(0), _M_iword(0) { } 00285 }; 00286 00287 // Only for failed iword/pword calls. 00288 _Words _M_word_zero; 00289 00290 // Guaranteed storage. 00291 static const int _S_local_word_size = 8; 00292 _Words _M_local_word[_S_local_word_size]; 00293 00294 // Allocated storage. 00295 int _M_word_size; 00296 _Words* _M_word; 00297 00298 _Words& 00299 _M_grow_words(int __index); 00300 00301 // Members for locale and locale caching. 00302 locale _M_ios_locale; 00303 00304 void 00305 _M_init(); 00306 00307 public: 00308 00309 // 27.4.2.1.6 Class ios_base::Init 00310 // Used to initialize standard streams. In theory, g++ could use 00311 // -finit-priority to order this stuff correctly without going 00312 // through these machinations. 00313 class Init 00314 { 00315 friend class ios_base; 00316 public: 00317 Init(); 00318 ~Init(); 00319 00320 static void 00321 _S_ios_create(bool __sync); 00322 00323 static void 00324 _S_ios_destroy(); 00325 00326 private: 00327 static int _S_ios_base_init; 00328 static bool _S_synced_with_stdio; 00329 }; 00330 00331 // Fmtflags state: 00332 inline fmtflags 00333 flags() const { return _M_flags; } 00334 00335 inline fmtflags 00336 flags(fmtflags __fmtfl) 00337 { 00338 fmtflags __old = _M_flags; 00339 _M_flags = __fmtfl; 00340 return __old; 00341 } 00342 00343 inline fmtflags 00344 setf(fmtflags __fmtfl) 00345 { 00346 fmtflags __old = _M_flags; 00347 _M_flags |= __fmtfl; 00348 return __old; 00349 } 00350 00351 inline fmtflags 00352 setf(fmtflags __fmtfl, fmtflags __mask) 00353 { 00354 fmtflags __old = _M_flags; 00355 _M_flags &= ~__mask; 00356 _M_flags |= (__fmtfl & __mask); 00357 return __old; 00358 } 00359 00360 inline void 00361 unsetf(fmtflags __mask) { _M_flags &= ~__mask; } 00362 00363 inline streamsize 00364 precision() const { return _M_precision; } 00365 00366 inline streamsize 00367 precision(streamsize __prec) 00368 { 00369 streamsize __old = _M_precision; 00370 _M_precision = __prec; 00371 return __old; 00372 } 00373 00374 inline streamsize 00375 width() const { return _M_width; } 00376 00377 inline streamsize 00378 width(streamsize __wide) 00379 { 00380 streamsize __old = _M_width; 00381 _M_width = __wide; 00382 return __old; 00383 } 00384 00385 static bool 00386 sync_with_stdio(bool __sync = true); 00387 00388 // Locales: 00389 locale 00390 imbue(const locale& __loc); 00391 00392 inline locale 00393 getloc() const { return _M_ios_locale; } 00394 00395 // Storage: 00396 static int 00397 xalloc() throw(); 00398 00399 inline long& 00400 iword(int __ix) 00401 { 00402 _Words& __word = (__ix < _M_word_size) 00403 ? _M_word[__ix] : _M_grow_words(__ix); 00404 return __word._M_iword; 00405 } 00406 00407 inline void*& 00408 pword(int __ix) 00409 { 00410 _Words& __word = (__ix < _M_word_size) 00411 ? _M_word[__ix] : _M_grow_words(__ix); 00412 return __word._M_pword; 00413 } 00414 00415 // Destructor 00416 ~ios_base(); 00417 00418 protected: 00419 ios_base(); 00420 00421 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS 00422 //50. Copy constructor and assignment operator of ios_base 00423 private: 00424 ios_base(const ios_base&); 00425 00426 ios_base& 00427 operator=(const ios_base&); 00428 #endif 00429 }; 00430 00431 // 27.4.5.1 fmtflags manipulators: 00432 inline ios_base& 00433 boolalpha(ios_base& __base) 00434 { 00435 __base.setf(ios_base::boolalpha); 00436 return __base; 00437 } 00438 00439 inline ios_base& 00440 noboolalpha(ios_base& __base) 00441 { 00442 __base.unsetf(ios_base::boolalpha); 00443 return __base; 00444 } 00445 00446 inline ios_base& 00447 showbase(ios_base& __base) 00448 { 00449 __base.setf(ios_base::showbase); 00450 return __base; 00451 } 00452 00453 inline ios_base& 00454 noshowbase(ios_base& __base) 00455 { 00456 __base.unsetf(ios_base::showbase); 00457 return __base; 00458 } 00459 00460 inline ios_base& 00461 showpoint(ios_base& __base) 00462 { 00463 __base.setf(ios_base::showpoint); 00464 return __base; 00465 } 00466 00467 inline ios_base& 00468 noshowpoint(ios_base& __base) 00469 { 00470 __base.unsetf(ios_base::showpoint); 00471 return __base; 00472 } 00473 00474 inline ios_base& 00475 showpos(ios_base& __base) 00476 { 00477 __base.setf(ios_base::showpos); 00478 return __base; 00479 } 00480 00481 inline ios_base& 00482 noshowpos(ios_base& __base) 00483 { 00484 __base.unsetf(ios_base::showpos); 00485 return __base; 00486 } 00487 00488 inline ios_base& 00489 skipws(ios_base& __base) 00490 { 00491 __base.setf(ios_base::skipws); 00492 return __base; 00493 } 00494 00495 inline ios_base& 00496 noskipws(ios_base& __base) 00497 { 00498 __base.unsetf(ios_base::skipws); 00499 return __base; 00500 } 00501 00502 inline ios_base& 00503 uppercase(ios_base& __base) 00504 { 00505 __base.setf(ios_base::uppercase); 00506 return __base; 00507 } 00508 00509 inline ios_base& 00510 nouppercase(ios_base& __base) 00511 { 00512 __base.unsetf(ios_base::uppercase); 00513 return __base; 00514 } 00515 00516 inline ios_base& 00517 unitbuf(ios_base& __base) 00518 { 00519 __base.setf(ios_base::unitbuf); 00520 return __base; 00521 } 00522 00523 inline ios_base& 00524 nounitbuf(ios_base& __base) 00525 { 00526 __base.unsetf(ios_base::unitbuf); 00527 return __base; 00528 } 00529 00530 // 27.4.5.2 adjustfield anipulators: 00531 inline ios_base& 00532 internal(ios_base& __base) 00533 { 00534 __base.setf(ios_base::internal, ios_base::adjustfield); 00535 return __base; 00536 } 00537 00538 inline ios_base& 00539 left(ios_base& __base) 00540 { 00541 __base.setf(ios_base::left, ios_base::adjustfield); 00542 return __base; 00543 } 00544 00545 inline ios_base& 00546 right(ios_base& __base) 00547 { 00548 __base.setf(ios_base::right, ios_base::adjustfield); 00549 return __base; 00550 } 00551 00552 // 27.4.5.3 basefield anipulators: 00553 inline ios_base& 00554 dec(ios_base& __base) 00555 { 00556 __base.setf(ios_base::dec, ios_base::basefield); 00557 return __base; 00558 } 00559 00560 inline ios_base& 00561 hex(ios_base& __base) 00562 { 00563 __base.setf(ios_base::hex, ios_base::basefield); 00564 return __base; 00565 } 00566 00567 inline ios_base& 00568 oct(ios_base& __base) 00569 { 00570 __base.setf(ios_base::oct, ios_base::basefield); 00571 return __base; 00572 } 00573 00574 // 27.4.5.4 floatfield anipulators: 00575 inline ios_base& 00576 fixed(ios_base& __base) 00577 { 00578 __base.setf(ios_base::fixed, ios_base::floatfield); 00579 return __base; 00580 } 00581 00582 inline ios_base& 00583 scientific(ios_base& __base) 00584 { 00585 __base.setf(ios_base::scientific, ios_base::floatfield); 00586 return __base; 00587 } 00588 00589 } // namespace std 00590 00591 #endif /* _CPP_BITS_IOSBASE_H */ 00592

Generated on Wed Sep 29 13:54:49 2004 for libstdc++-v3 Source by doxygen 1.3.7