1 // Iostreams base classes -*- C++ -*- 2 3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004 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. 11 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. 16 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. 21 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. 30 31 // 32 // ISO C++ 14882: 27.4 Iostreams base classes 33 // 34 35 #include <ios> 36 #include <limits> 37 #include <bits/atomicity.h> 38 39 namespace std 40 { 41 // Definitions for static const data members of __ios_flags. 42 const __ios_flags::__int_type __ios_flags::_S_boolalpha; 43 const __ios_flags::__int_type __ios_flags::_S_dec; 44 const __ios_flags::__int_type __ios_flags::_S_fixed; 45 const __ios_flags::__int_type __ios_flags::_S_hex; 46 const __ios_flags::__int_type __ios_flags::_S_internal; 47 const __ios_flags::__int_type __ios_flags::_S_left; 48 const __ios_flags::__int_type __ios_flags::_S_oct; 49 const __ios_flags::__int_type __ios_flags::_S_right; 50 const __ios_flags::__int_type __ios_flags::_S_scientific; 51 const __ios_flags::__int_type __ios_flags::_S_showbase; 52 const __ios_flags::__int_type __ios_flags::_S_showpoint; 53 const __ios_flags::__int_type __ios_flags::_S_showpos; 54 const __ios_flags::__int_type __ios_flags::_S_skipws; 55 const __ios_flags::__int_type __ios_flags::_S_unitbuf; 56 const __ios_flags::__int_type __ios_flags::_S_uppercase; 57 const __ios_flags::__int_type __ios_flags::_S_adjustfield; 58 const __ios_flags::__int_type __ios_flags::_S_basefield; 59 const __ios_flags::__int_type __ios_flags::_S_floatfield; 60 61 const __ios_flags::__int_type __ios_flags::_S_badbit; 62 const __ios_flags::__int_type __ios_flags::_S_eofbit; 63 const __ios_flags::__int_type __ios_flags::_S_failbit; 64 65 const __ios_flags::__int_type __ios_flags::_S_app; 66 const __ios_flags::__int_type __ios_flags::_S_ate; 67 const __ios_flags::__int_type __ios_flags::_S_bin; 68 const __ios_flags::__int_type __ios_flags::_S_in; 69 const __ios_flags::__int_type __ios_flags::_S_out; 70 const __ios_flags::__int_type __ios_flags::_S_trunc; 71 72 // Definitions for static const members of ios_base. 73 const ios_base::fmtflags ios_base::boolalpha; 74 const ios_base::fmtflags ios_base::dec; 75 const ios_base::fmtflags ios_base::fixed; 76 const ios_base::fmtflags ios_base::hex; 77 const ios_base::fmtflags ios_base::internal; 78 const ios_base::fmtflags ios_base::left; 79 const ios_base::fmtflags ios_base::oct; 80 const ios_base::fmtflags ios_base::right; 81 const ios_base::fmtflags ios_base::scientific; 82 const ios_base::fmtflags ios_base::showbase; 83 const ios_base::fmtflags ios_base::showpoint; 84 const ios_base::fmtflags ios_base::showpos; 85 const ios_base::fmtflags ios_base::skipws; 86 const ios_base::fmtflags ios_base::unitbuf; 87 const ios_base::fmtflags ios_base::uppercase; 88 const ios_base::fmtflags ios_base::adjustfield; 89 const ios_base::fmtflags ios_base::basefield; 90 const ios_base::fmtflags ios_base::floatfield; 91 92 const ios_base::iostate ios_base::badbit; 93 const ios_base::iostate ios_base::eofbit; 94 const ios_base::iostate ios_base::failbit; 95 const ios_base::iostate ios_base::goodbit; 96 97 const ios_base::openmode ios_base::app; 98 const ios_base::openmode ios_base::ate; 99 const ios_base::openmode ios_base::binary; 100 const ios_base::openmode ios_base::in; 101 const ios_base::openmode ios_base::out; 102 const ios_base::openmode ios_base::trunc; 103 104 const ios_base::seekdir ios_base::beg; 105 const ios_base::seekdir ios_base::cur; 106 const ios_base::seekdir ios_base::end; 107 108 const int ios_base::_S_local_word_size; 109 110 _Atomic_word ios_base::Init::_S_refcount; 111 112 bool ios_base::Init::_S_synced_with_stdio = true; 113 114 ios_base::ios_base() 115 : _M_precision(), _M_width(), _M_flags(), _M_exception(), 116 _M_streambuf_state(), _M_callbacks(0), _M_word_zero(), 117 _M_word_size(_S_local_word_size), _M_word(_M_local_word), _M_ios_locale() 118 { 119 // Do nothing: basic_ios::init() does it. 120 // NB: _M_callbacks and _M_word must be zero for non-initialized 121 // ios_base to go through ~ios_base gracefully. 122 } 123 124 // 27.4.2.7 ios_base constructors/destructors 125 ios_base::~ios_base() 126 { 127 _M_call_callbacks(erase_event); 128 _M_dispose_callbacks(); 129 if (_M_word != _M_local_word) 130 { 131 delete [] _M_word; 132 _M_word = 0; 133 } 134 } 135 136 // 27.4.2.5 ios_base storage functions 137 int 138 ios_base::xalloc() throw() 139 { 140 // Implementation note: Initialize top to zero to ensure that 141 // initialization occurs before main() is started. 142 static _Atomic_word _S_top = 0; 143 return __gnu_cxx::__exchange_and_add(&_S_top, 1) + 4; 144 } 145 146 void 147 ios_base::register_callback(event_callback __fn, int __index) 148 { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); } 149 150 // 27.4.2.5 iword/pword storage 151 ios_base::_Words& 152 ios_base::_M_grow_words(int ix, bool iword) 153 { 154 // Precondition: _M_word_size <= ix 155 int newsize = _S_local_word_size; 156 _Words* words = _M_local_word; 157 if (ix > _S_local_word_size - 1) 158 { 159 if (ix < numeric_limits<int>::max()) 160 { 161 newsize = ix + 1; 162 try 163 { words = new _Words[newsize]; } 164 catch (...) 165 { 166 _M_streambuf_state |= badbit; 167 if (_M_streambuf_state & _M_exception) 168 __throw_ios_failure(__N("ios_base::_M_grow_words " 169 "allocation failed")); 170 if (iword) 171 _M_word_zero._M_iword = 0; 172 else 173 _M_word_zero._M_pword = 0; 174 return _M_word_zero; 175 } 176 for (int i = 0; i < _M_word_size; i++) 177 words[i] = _M_word[i]; 178 if (_M_word && _M_word != _M_local_word) 179 { 180 delete [] _M_word; 181 _M_word = 0; 182 } 183 } 184 else 185 { 186 _M_streambuf_state |= badbit; 187 if (_M_streambuf_state & _M_exception) 188 __throw_ios_failure(__N("ios_base::_M_grow_words is not valid")); 189 if (iword) 190 _M_word_zero._M_iword = 0; 191 else 192 _M_word_zero._M_pword = 0; 193 return _M_word_zero; 194 } 195 } 196 _M_word = words; 197 _M_word_size = newsize; 198 return _M_word[ix]; 199 } 200 201 void 202 ios_base::_M_call_callbacks(event __e) throw() 203 { 204 _Callback_list* __p = _M_callbacks; 205 while (__p) 206 { 207 try 208 { (*__p->_M_fn) (__e, *this, __p->_M_index); } 209 catch (...) 210 { } 211 __p = __p->_M_next; 212 } 213 } 214 215 void 216 ios_base::_M_dispose_callbacks(void) 217 { 218 _Callback_list* __p = _M_callbacks; 219 while (__p && __p->_M_remove_reference() == 0) 220 { 221 _Callback_list* __next = __p->_M_next; 222 delete __p; 223 __p = __next; 224 } 225 _M_callbacks = 0; 226 } 227 } // namespace std 228