1// Iostreams base classes -*- C++ -*-
2
3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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
38_GLIBCXX_BEGIN_NAMESPACE(std)
39
40  // Definitions for static const members of ios_base.
41  const ios_base::fmtflags ios_base::boolalpha;
42  const ios_base::fmtflags ios_base::dec;
43  const ios_base::fmtflags ios_base::fixed;
44  const ios_base::fmtflags ios_base::hex;
45  const ios_base::fmtflags ios_base::internal;
46  const ios_base::fmtflags ios_base::left;
47  const ios_base::fmtflags ios_base::oct;
48  const ios_base::fmtflags ios_base::right;
49  const ios_base::fmtflags ios_base::scientific;
50  const ios_base::fmtflags ios_base::showbase;
51  const ios_base::fmtflags ios_base::showpoint;
52  const ios_base::fmtflags ios_base::showpos;
53  const ios_base::fmtflags ios_base::skipws;
54  const ios_base::fmtflags ios_base::unitbuf;
55  const ios_base::fmtflags ios_base::uppercase;
56  const ios_base::fmtflags ios_base::adjustfield;
57  const ios_base::fmtflags ios_base::basefield;
58  const ios_base::fmtflags ios_base::floatfield;
59
60  const ios_base::iostate ios_base::badbit;
61  const ios_base::iostate ios_base::eofbit;
62  const ios_base::iostate ios_base::failbit;
63  const ios_base::iostate ios_base::goodbit;
64
65  const ios_base::openmode ios_base::app;
66  const ios_base::openmode ios_base::ate;
67  const ios_base::openmode ios_base::binary;
68  const ios_base::openmode ios_base::in;
69  const ios_base::openmode ios_base::out;
70  const ios_base::openmode ios_base::trunc;
71
72  const ios_base::seekdir ios_base::beg;
73  const ios_base::seekdir ios_base::cur;
74  const ios_base::seekdir ios_base::end;
75
76  _Atomic_word ios_base::Init::_S_refcount;
77
78  bool ios_base::Init::_S_synced_with_stdio = true;
79
80  ios_base::ios_base()
81  : _M_precision(), _M_width(), _M_flags(), _M_exception(),
82  _M_streambuf_state(), _M_callbacks(0), _M_word_zero(),
83  _M_word_size(_S_local_word_size), _M_word(_M_local_word), _M_ios_locale()
84  {
85    // Do nothing: basic_ios::init() does it.
86    // NB: _M_callbacks and _M_word must be zero for non-initialized
87    // ios_base to go through ~ios_base gracefully.
88  }
89
90  // 27.4.2.7  ios_base constructors/destructors
91  ios_base::~ios_base()
92  {
93    _M_call_callbacks(erase_event);
94    _M_dispose_callbacks();
95    if (_M_word != _M_local_word)
96      {
97	delete [] _M_word;
98	_M_word = 0;
99      }
100  }
101
102  // 27.4.2.5  ios_base storage functions
103  int
104  ios_base::xalloc() throw()
105  {
106    // Implementation note: Initialize top to zero to ensure that
107    // initialization occurs before main() is started.
108    static _Atomic_word _S_top = 0;
109    return __gnu_cxx::__exchange_and_add_dispatch(&_S_top, 1) + 4;
110  }
111
112  void
113  ios_base::register_callback(event_callback __fn, int __index)
114  { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
115
116  // 27.4.2.5  iword/pword storage
117  ios_base::_Words&
118  ios_base::_M_grow_words(int __ix, bool __iword)
119  {
120    // Precondition: _M_word_size <= __ix
121    int __newsize = _S_local_word_size;
122    _Words* __words = _M_local_word;
123    if (__ix > _S_local_word_size - 1)
124      {
125	if (__ix < numeric_limits<int>::max())
126	  {
127	    __newsize = __ix + 1;
128	    try
129	      { __words = new _Words[__newsize]; }
130	    catch (...)
131	      {
132		_M_streambuf_state |= badbit;
133		if (_M_streambuf_state & _M_exception)
134		  __throw_ios_failure(__N("ios_base::_M_grow_words "
135				      "allocation failed"));
136		if (__iword)
137		  _M_word_zero._M_iword = 0;
138		else
139		  _M_word_zero._M_pword = 0;
140		return _M_word_zero;
141	      }
142	    for (int __i = 0; __i < _M_word_size; __i++)
143	      __words[__i] = _M_word[__i];
144	    if (_M_word && _M_word != _M_local_word)
145	      {
146		delete [] _M_word;
147		_M_word = 0;
148	      }
149	  }
150	else
151	  {
152	    _M_streambuf_state |= badbit;
153	    if (_M_streambuf_state & _M_exception)
154	      __throw_ios_failure(__N("ios_base::_M_grow_words is not valid"));
155	    if (__iword)
156	      _M_word_zero._M_iword = 0;
157	    else
158	      _M_word_zero._M_pword = 0;
159	    return _M_word_zero;
160	  }
161      }
162    _M_word = __words;
163    _M_word_size = __newsize;
164    return _M_word[__ix];
165  }
166
167  void
168  ios_base::_M_call_callbacks(event __e) throw()
169  {
170    _Callback_list* __p = _M_callbacks;
171    while (__p)
172      {
173	try
174	  { (*__p->_M_fn) (__e, *this, __p->_M_index); }
175	catch (...)
176	  { }
177	__p = __p->_M_next;
178      }
179  }
180
181  void
182  ios_base::_M_dispose_callbacks(void)
183  {
184    _Callback_list* __p = _M_callbacks;
185    while (__p && __p->_M_remove_reference() == 0)
186      {
187	_Callback_list* __next = __p->_M_next;
188	delete __p;
189	__p = __next;
190      }
191    _M_callbacks = 0;
192  }
193
194_GLIBCXX_END_NAMESPACE
195