1*404b540aSrobert // Iostreams base classes -*- C++ -*- 2*404b540aSrobert 3*404b540aSrobert // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 4*404b540aSrobert // Free Software Foundation, Inc. 5*404b540aSrobert // 6*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free 7*404b540aSrobert // software; you can redistribute it and/or modify it under the 8*404b540aSrobert // terms of the GNU General Public License as published by the 9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option) 10*404b540aSrobert // any later version. 11*404b540aSrobert 12*404b540aSrobert // This library is distributed in the hope that it will be useful, 13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of 14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*404b540aSrobert // GNU General Public License for more details. 16*404b540aSrobert 17*404b540aSrobert // You should have received a copy of the GNU General Public License along 18*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free 19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 20*404b540aSrobert // USA. 21*404b540aSrobert 22*404b540aSrobert // As a special exception, you may use this file as part of a free software 23*404b540aSrobert // library without restriction. Specifically, if other files instantiate 24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile 25*404b540aSrobert // this file and link it with other files to produce an executable, this 26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by 27*404b540aSrobert // the GNU General Public License. This exception does not however 28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by 29*404b540aSrobert // the GNU General Public License. 30*404b540aSrobert 31*404b540aSrobert // 32*404b540aSrobert // ISO C++ 14882: 27.4 Iostreams base classes 33*404b540aSrobert // 34*404b540aSrobert 35*404b540aSrobert #include <ios> 36*404b540aSrobert #include <limits> 37*404b540aSrobert 38*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std) 39*404b540aSrobert 40*404b540aSrobert // Definitions for static const members of ios_base. 41*404b540aSrobert const ios_base::fmtflags ios_base::boolalpha; 42*404b540aSrobert const ios_base::fmtflags ios_base::dec; 43*404b540aSrobert const ios_base::fmtflags ios_base::fixed; 44*404b540aSrobert const ios_base::fmtflags ios_base::hex; 45*404b540aSrobert const ios_base::fmtflags ios_base::internal; 46*404b540aSrobert const ios_base::fmtflags ios_base::left; 47*404b540aSrobert const ios_base::fmtflags ios_base::oct; 48*404b540aSrobert const ios_base::fmtflags ios_base::right; 49*404b540aSrobert const ios_base::fmtflags ios_base::scientific; 50*404b540aSrobert const ios_base::fmtflags ios_base::showbase; 51*404b540aSrobert const ios_base::fmtflags ios_base::showpoint; 52*404b540aSrobert const ios_base::fmtflags ios_base::showpos; 53*404b540aSrobert const ios_base::fmtflags ios_base::skipws; 54*404b540aSrobert const ios_base::fmtflags ios_base::unitbuf; 55*404b540aSrobert const ios_base::fmtflags ios_base::uppercase; 56*404b540aSrobert const ios_base::fmtflags ios_base::adjustfield; 57*404b540aSrobert const ios_base::fmtflags ios_base::basefield; 58*404b540aSrobert const ios_base::fmtflags ios_base::floatfield; 59*404b540aSrobert 60*404b540aSrobert const ios_base::iostate ios_base::badbit; 61*404b540aSrobert const ios_base::iostate ios_base::eofbit; 62*404b540aSrobert const ios_base::iostate ios_base::failbit; 63*404b540aSrobert const ios_base::iostate ios_base::goodbit; 64*404b540aSrobert 65*404b540aSrobert const ios_base::openmode ios_base::app; 66*404b540aSrobert const ios_base::openmode ios_base::ate; 67*404b540aSrobert const ios_base::openmode ios_base::binary; 68*404b540aSrobert const ios_base::openmode ios_base::in; 69*404b540aSrobert const ios_base::openmode ios_base::out; 70*404b540aSrobert const ios_base::openmode ios_base::trunc; 71*404b540aSrobert 72*404b540aSrobert const ios_base::seekdir ios_base::beg; 73*404b540aSrobert const ios_base::seekdir ios_base::cur; 74*404b540aSrobert const ios_base::seekdir ios_base::end; 75*404b540aSrobert 76*404b540aSrobert _Atomic_word ios_base::Init::_S_refcount; 77*404b540aSrobert 78*404b540aSrobert bool ios_base::Init::_S_synced_with_stdio = true; 79*404b540aSrobert ios_base()80*404b540aSrobert ios_base::ios_base() 81*404b540aSrobert : _M_precision(), _M_width(), _M_flags(), _M_exception(), 82*404b540aSrobert _M_streambuf_state(), _M_callbacks(0), _M_word_zero(), 83*404b540aSrobert _M_word_size(_S_local_word_size), _M_word(_M_local_word), _M_ios_locale() 84*404b540aSrobert { 85*404b540aSrobert // Do nothing: basic_ios::init() does it. 86*404b540aSrobert // NB: _M_callbacks and _M_word must be zero for non-initialized 87*404b540aSrobert // ios_base to go through ~ios_base gracefully. 88*404b540aSrobert } 89*404b540aSrobert 90*404b540aSrobert // 27.4.2.7 ios_base constructors/destructors ~ios_base()91*404b540aSrobert ios_base::~ios_base() 92*404b540aSrobert { 93*404b540aSrobert _M_call_callbacks(erase_event); 94*404b540aSrobert _M_dispose_callbacks(); 95*404b540aSrobert if (_M_word != _M_local_word) 96*404b540aSrobert { 97*404b540aSrobert delete [] _M_word; 98*404b540aSrobert _M_word = 0; 99*404b540aSrobert } 100*404b540aSrobert } 101*404b540aSrobert 102*404b540aSrobert // 27.4.2.5 ios_base storage functions 103*404b540aSrobert int xalloc()104*404b540aSrobert ios_base::xalloc() throw() 105*404b540aSrobert { 106*404b540aSrobert // Implementation note: Initialize top to zero to ensure that 107*404b540aSrobert // initialization occurs before main() is started. 108*404b540aSrobert static _Atomic_word _S_top = 0; 109*404b540aSrobert return __gnu_cxx::__exchange_and_add_dispatch(&_S_top, 1) + 4; 110*404b540aSrobert } 111*404b540aSrobert 112*404b540aSrobert void register_callback(event_callback __fn,int __index)113*404b540aSrobert ios_base::register_callback(event_callback __fn, int __index) 114*404b540aSrobert { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); } 115*404b540aSrobert 116*404b540aSrobert // 27.4.2.5 iword/pword storage 117*404b540aSrobert ios_base::_Words& _M_grow_words(int __ix,bool __iword)118*404b540aSrobert ios_base::_M_grow_words(int __ix, bool __iword) 119*404b540aSrobert { 120*404b540aSrobert // Precondition: _M_word_size <= __ix 121*404b540aSrobert int __newsize = _S_local_word_size; 122*404b540aSrobert _Words* __words = _M_local_word; 123*404b540aSrobert if (__ix > _S_local_word_size - 1) 124*404b540aSrobert { 125*404b540aSrobert if (__ix < numeric_limits<int>::max()) 126*404b540aSrobert { 127*404b540aSrobert __newsize = __ix + 1; 128*404b540aSrobert try 129*404b540aSrobert { __words = new _Words[__newsize]; } 130*404b540aSrobert catch (...) 131*404b540aSrobert { 132*404b540aSrobert _M_streambuf_state |= badbit; 133*404b540aSrobert if (_M_streambuf_state & _M_exception) 134*404b540aSrobert __throw_ios_failure(__N("ios_base::_M_grow_words " 135*404b540aSrobert "allocation failed")); 136*404b540aSrobert if (__iword) 137*404b540aSrobert _M_word_zero._M_iword = 0; 138*404b540aSrobert else 139*404b540aSrobert _M_word_zero._M_pword = 0; 140*404b540aSrobert return _M_word_zero; 141*404b540aSrobert } 142*404b540aSrobert for (int __i = 0; __i < _M_word_size; __i++) 143*404b540aSrobert __words[__i] = _M_word[__i]; 144*404b540aSrobert if (_M_word && _M_word != _M_local_word) 145*404b540aSrobert { 146*404b540aSrobert delete [] _M_word; 147*404b540aSrobert _M_word = 0; 148*404b540aSrobert } 149*404b540aSrobert } 150*404b540aSrobert else 151*404b540aSrobert { 152*404b540aSrobert _M_streambuf_state |= badbit; 153*404b540aSrobert if (_M_streambuf_state & _M_exception) 154*404b540aSrobert __throw_ios_failure(__N("ios_base::_M_grow_words is not valid")); 155*404b540aSrobert if (__iword) 156*404b540aSrobert _M_word_zero._M_iword = 0; 157*404b540aSrobert else 158*404b540aSrobert _M_word_zero._M_pword = 0; 159*404b540aSrobert return _M_word_zero; 160*404b540aSrobert } 161*404b540aSrobert } 162*404b540aSrobert _M_word = __words; 163*404b540aSrobert _M_word_size = __newsize; 164*404b540aSrobert return _M_word[__ix]; 165*404b540aSrobert } 166*404b540aSrobert 167*404b540aSrobert void _M_call_callbacks(event __e)168*404b540aSrobert ios_base::_M_call_callbacks(event __e) throw() 169*404b540aSrobert { 170*404b540aSrobert _Callback_list* __p = _M_callbacks; 171*404b540aSrobert while (__p) 172*404b540aSrobert { 173*404b540aSrobert try 174*404b540aSrobert { (*__p->_M_fn) (__e, *this, __p->_M_index); } 175*404b540aSrobert catch (...) 176*404b540aSrobert { } 177*404b540aSrobert __p = __p->_M_next; 178*404b540aSrobert } 179*404b540aSrobert } 180*404b540aSrobert 181*404b540aSrobert void _M_dispose_callbacks(void)182*404b540aSrobert ios_base::_M_dispose_callbacks(void) 183*404b540aSrobert { 184*404b540aSrobert _Callback_list* __p = _M_callbacks; 185*404b540aSrobert while (__p && __p->_M_remove_reference() == 0) 186*404b540aSrobert { 187*404b540aSrobert _Callback_list* __next = __p->_M_next; 188*404b540aSrobert delete __p; 189*404b540aSrobert __p = __next; 190*404b540aSrobert } 191*404b540aSrobert _M_callbacks = 0; 192*404b540aSrobert } 193*404b540aSrobert 194*404b540aSrobert _GLIBCXX_END_NAMESPACE 195