1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 // Free Software Foundation, Inc.
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)
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.
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,
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.
32 // ISO C++ 14882: 27.4 Iostreams base classes
37 #include <bits/atomicity.h>
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
;
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
;
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
;
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
;
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
;
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
;
104 const ios_base::seekdir
ios_base::beg
;
105 const ios_base::seekdir
ios_base::cur
;
106 const ios_base::seekdir
ios_base::end
;
108 const int ios_base::_S_local_word_size
;
110 _Atomic_word
ios_base::Init::_S_refcount
;
112 bool ios_base::Init::_S_synced_with_stdio
= true;
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()
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.
124 // 27.4.2.7 ios_base constructors/destructors
125 ios_base::~ios_base()
127 _M_call_callbacks(erase_event
);
128 _M_dispose_callbacks();
129 if (_M_word
!= _M_local_word
)
136 // 27.4.2.5 ios_base storage functions
138 ios_base::xalloc() throw()
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;
147 ios_base::register_callback(event_callback __fn
, int __index
)
148 { _M_callbacks
= new _Callback_list(__fn
, __index
, _M_callbacks
); }
150 // 27.4.2.5 iword/pword storage
152 ios_base::_M_grow_words(int __ix
, bool __iword
)
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)
159 if (__ix
< numeric_limits
<int>::max())
161 __newsize
= __ix
+ 1;
163 { __words
= new _Words
[__newsize
]; }
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"));
171 _M_word_zero
._M_iword
= 0;
173 _M_word_zero
._M_pword
= 0;
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
)
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"));
190 _M_word_zero
._M_iword
= 0;
192 _M_word_zero
._M_pword
= 0;
197 _M_word_size
= __newsize
;
198 return _M_word
[__ix
];
202 ios_base::_M_call_callbacks(event __e
) throw()
204 _Callback_list
* __p
= _M_callbacks
;
208 { (*__p
->_M_fn
) (__e
, *this, __p
->_M_index
); }
216 ios_base::_M_dispose_callbacks(void)
218 _Callback_list
* __p
= _M_callbacks
;
219 while (__p
&& __p
->_M_remove_reference() == 0)
221 _Callback_list
* __next
= __p
->_M_next
;