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