]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/src/c++11/ios.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / src / c++11 / ios.cc
1 // Iostreams base classes -*- C++ -*-
2
3 // Copyright (C) 1997-2017 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24
25 //
26 // ISO C++ 14882: 27.4 Iostreams base classes
27 //
28
29 #include <ios>
30 #include <limits>
31
32 namespace std _GLIBCXX_VISIBILITY(default)
33 {
34 _GLIBCXX_BEGIN_NAMESPACE_VERSION
35
36 // Definitions for static const members of ios_base.
37 const ios_base::fmtflags ios_base::boolalpha;
38 const ios_base::fmtflags ios_base::dec;
39 const ios_base::fmtflags ios_base::fixed;
40 const ios_base::fmtflags ios_base::hex;
41 const ios_base::fmtflags ios_base::internal;
42 const ios_base::fmtflags ios_base::left;
43 const ios_base::fmtflags ios_base::oct;
44 const ios_base::fmtflags ios_base::right;
45 const ios_base::fmtflags ios_base::scientific;
46 const ios_base::fmtflags ios_base::showbase;
47 const ios_base::fmtflags ios_base::showpoint;
48 const ios_base::fmtflags ios_base::showpos;
49 const ios_base::fmtflags ios_base::skipws;
50 const ios_base::fmtflags ios_base::unitbuf;
51 const ios_base::fmtflags ios_base::uppercase;
52 const ios_base::fmtflags ios_base::adjustfield;
53 const ios_base::fmtflags ios_base::basefield;
54 const ios_base::fmtflags ios_base::floatfield;
55
56 const ios_base::iostate ios_base::badbit;
57 const ios_base::iostate ios_base::eofbit;
58 const ios_base::iostate ios_base::failbit;
59 const ios_base::iostate ios_base::goodbit;
60
61 const ios_base::openmode ios_base::app;
62 const ios_base::openmode ios_base::ate;
63 const ios_base::openmode ios_base::binary;
64 const ios_base::openmode ios_base::in;
65 const ios_base::openmode ios_base::out;
66 const ios_base::openmode ios_base::trunc;
67
68 const ios_base::seekdir ios_base::beg;
69 const ios_base::seekdir ios_base::cur;
70 const ios_base::seekdir ios_base::end;
71
72 _Atomic_word ios_base::Init::_S_refcount;
73
74 bool ios_base::Init::_S_synced_with_stdio = true;
75
76 ios_base::ios_base() throw()
77 : _M_precision(), _M_width(), _M_flags(), _M_exception(),
78 _M_streambuf_state(), _M_callbacks(0), _M_word_zero(),
79 _M_word_size(_S_local_word_size), _M_word(_M_local_word), _M_ios_locale()
80 {
81 // Do nothing: basic_ios::init() does it.
82 // NB: _M_callbacks and _M_word must be zero for non-initialized
83 // ios_base to go through ~ios_base gracefully.
84 }
85
86 // 27.4.2.7 ios_base constructors/destructors
87 ios_base::~ios_base()
88 {
89 _M_call_callbacks(erase_event);
90 _M_dispose_callbacks();
91 if (_M_word != _M_local_word)
92 {
93 delete [] _M_word;
94 _M_word = 0;
95 }
96 }
97
98 // 27.4.2.5 ios_base storage functions
99 int
100 ios_base::xalloc() throw()
101 {
102 // Implementation note: Initialize top to zero to ensure that
103 // initialization occurs before main() is started.
104 static _Atomic_word _S_top = 0;
105 return __gnu_cxx::__exchange_and_add_dispatch(&_S_top, 1) + 4;
106 }
107
108 void
109 ios_base::register_callback(event_callback __fn, int __index)
110 { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
111
112 // 27.4.2.5 iword/pword storage
113 ios_base::_Words&
114 ios_base::_M_grow_words(int __ix, bool __iword)
115 {
116 // Precondition: _M_word_size <= __ix
117 int __newsize = _S_local_word_size;
118 _Words* __words = _M_local_word;
119 if (__ix > _S_local_word_size - 1)
120 {
121 if (__ix < numeric_limits<int>::max())
122 {
123 __newsize = __ix + 1;
124 /* We still need to catch bad_alloc even though we use
125 a nothrow new, because the new-expression can throw
126 a bad_array_new_length. */
127 __try
128 { __words = new (std::nothrow) _Words[__newsize]; }
129 __catch(const std::bad_alloc&)
130 { __words = nullptr; }
131 if (!__words)
132 {
133 _M_streambuf_state |= badbit;
134 if (_M_streambuf_state & _M_exception)
135 __throw_ios_failure(__N("ios_base::_M_grow_words "
136 "allocation failed"));
137 if (__iword)
138 _M_word_zero._M_iword = 0;
139 else
140 _M_word_zero._M_pword = 0;
141 return _M_word_zero;
142 }
143 for (int __i = 0; __i < _M_word_size; __i++)
144 __words[__i] = _M_word[__i];
145 if (_M_word && _M_word != _M_local_word)
146 {
147 delete [] _M_word;
148 _M_word = 0;
149 }
150 }
151 else
152 {
153 _M_streambuf_state |= badbit;
154 if (_M_streambuf_state & _M_exception)
155 __throw_ios_failure(__N("ios_base::_M_grow_words is not valid"));
156 if (__iword)
157 _M_word_zero._M_iword = 0;
158 else
159 _M_word_zero._M_pword = 0;
160 return _M_word_zero;
161 }
162 }
163 _M_word = __words;
164 _M_word_size = __newsize;
165 return _M_word[__ix];
166 }
167
168 void
169 ios_base::_M_call_callbacks(event __e) throw()
170 {
171 _Callback_list* __p = _M_callbacks;
172 while (__p)
173 {
174 __try
175 { (*__p->_M_fn) (__e, *this, __p->_M_index); }
176 __catch(...)
177 { }
178 __p = __p->_M_next;
179 }
180 }
181
182 void
183 ios_base::_M_dispose_callbacks(void) throw()
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 }
194
195 void
196 ios_base::_M_move(ios_base& __rhs) noexcept
197 {
198 _M_precision = __rhs._M_precision;
199 _M_width = __rhs._M_width;
200 _M_flags = __rhs._M_flags;
201 _M_exception = __rhs._M_exception;
202 _M_streambuf_state = __rhs._M_streambuf_state;
203 _M_callbacks = std::__exchange(__rhs._M_callbacks, nullptr);
204 if (_M_word != _M_local_word)
205 delete[] _M_word;
206 if (__rhs._M_word == __rhs._M_local_word)
207 {
208 _M_word = _M_local_word;
209 _M_word_size = _S_local_word_size;
210 for (int __i = 0; __i < _S_local_word_size; __i++)
211 _M_word[__i] = std::__exchange(__rhs._M_word[__i], {});
212 }
213 else
214 {
215 _M_word = std::__exchange(__rhs._M_word, __rhs._M_local_word);
216 _M_word_size
217 = std::__exchange(__rhs._M_word_size, _S_local_word_size);
218 }
219 _M_ios_locale = __rhs._M_ios_locale;
220 }
221
222 void
223 ios_base::_M_swap(ios_base& __rhs) noexcept
224 {
225 std::swap(_M_precision, __rhs._M_precision);
226 std::swap(_M_width, __rhs._M_width);
227 std::swap(_M_flags, __rhs._M_flags);
228 std::swap(_M_exception, __rhs._M_exception);
229 std::swap(_M_streambuf_state, __rhs._M_streambuf_state);
230 std::swap(_M_callbacks, __rhs._M_callbacks);
231 const bool __lhs_local = _M_word == _M_local_word;
232 const bool __rhs_local = __rhs._M_word == __rhs._M_local_word;
233 if (__lhs_local && __rhs_local)
234 std::swap(_M_local_word, __rhs._M_local_word); // array swap
235 else
236 {
237 if (!__lhs_local && !__rhs_local)
238 std::swap(_M_word, __rhs._M_word);
239 else
240 {
241 ios_base* __local;
242 ios_base* __allocated;
243 if (__lhs_local)
244 {
245 __local = this;
246 __allocated = &__rhs;
247 }
248 else
249 {
250 __local = &__rhs;
251 __allocated= this;
252 }
253 for (int __i = 0; __i < _S_local_word_size; __i++)
254 __allocated->_M_local_word[__i] = __local->_M_local_word[__i];
255 __local->_M_word = __allocated->_M_word;
256 __allocated->_M_word = __allocated->_M_local_word;
257 }
258 std::swap(_M_word_size, __rhs._M_word_size);
259 }
260 std::swap(_M_ios_locale, __rhs._M_ios_locale);
261 }
262
263 _GLIBCXX_END_NAMESPACE_VERSION
264 } // namespace