]>
Commit | Line | Data |
---|---|---|
c755e77d BK |
1 | // Iostreams base classes -*- C++ -*- |
2 | ||
bc2631e0 | 3 | // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, |
12ffa228 | 4 | // 2006, 2007, 2008, 2009, 2010, 2011 |
c755e77d BK |
5 | // Free Software Foundation, Inc. |
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 | |
748086b7 | 10 | // Free Software Foundation; either version 3, or (at your option) |
c755e77d BK |
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 | ||
748086b7 JJ |
18 | // Under Section 7 of GPL version 3, you are granted additional |
19 | // permissions described in the GCC Runtime Library Exception, version | |
20 | // 3.1, as published by the Free Software Foundation. | |
21 | ||
22 | // You should have received a copy of the GNU General Public License and | |
23 | // a copy of the GCC Runtime Library Exception along with this program; | |
24 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
25 | // <http://www.gnu.org/licenses/>. | |
c755e77d BK |
26 | |
27 | // | |
28 | // ISO C++ 14882: 27.4 Iostreams base classes | |
29 | // | |
30 | ||
31 | #include <ios> | |
32 | #include <ostream> | |
33 | #include <istream> | |
34 | #include <fstream> | |
c755e77d BK |
35 | #include <ext/stdio_filebuf.h> |
36 | #include <ext/stdio_sync_filebuf.h> | |
37 | ||
12ffa228 | 38 | namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden) |
c755e77d | 39 | { |
ff4cf05b BK |
40 | using namespace __gnu_cxx; |
41 | ||
c755e77d BK |
42 | // Extern declarations for global objects in src/globals.cc. |
43 | extern stdio_sync_filebuf<char> buf_cout_sync; | |
44 | extern stdio_sync_filebuf<char> buf_cin_sync; | |
45 | extern stdio_sync_filebuf<char> buf_cerr_sync; | |
46 | ||
47 | extern stdio_filebuf<char> buf_cout; | |
48 | extern stdio_filebuf<char> buf_cin; | |
49 | extern stdio_filebuf<char> buf_cerr; | |
50 | ||
51 | #ifdef _GLIBCXX_USE_WCHAR_T | |
52 | extern stdio_sync_filebuf<wchar_t> buf_wcout_sync; | |
53 | extern stdio_sync_filebuf<wchar_t> buf_wcin_sync; | |
54 | extern stdio_sync_filebuf<wchar_t> buf_wcerr_sync; | |
55 | ||
56 | extern stdio_filebuf<wchar_t> buf_wcout; | |
57 | extern stdio_filebuf<wchar_t> buf_wcin; | |
58 | extern stdio_filebuf<wchar_t> buf_wcerr; | |
59 | #endif | |
ff4cf05b | 60 | } // namespace __gnu_internal |
c755e77d | 61 | |
12ffa228 BK |
62 | namespace std _GLIBCXX_VISIBILITY(default) |
63 | { | |
64 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 65 | |
ff4cf05b | 66 | using namespace __gnu_internal; |
c755e77d BK |
67 | |
68 | extern istream cin; | |
69 | extern ostream cout; | |
70 | extern ostream cerr; | |
71 | extern ostream clog; | |
72 | ||
73 | #ifdef _GLIBCXX_USE_WCHAR_T | |
74 | extern wistream wcin; | |
75 | extern wostream wcout; | |
76 | extern wostream wcerr; | |
77 | extern wostream wclog; | |
78 | #endif | |
79 | ||
80 | ios_base::Init::Init() | |
81 | { | |
b7ee72de | 82 | if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0) |
c755e77d BK |
83 | { |
84 | // Standard streams default to synced with "C" operations. | |
85 | _S_synced_with_stdio = true; | |
86 | ||
87 | new (&buf_cout_sync) stdio_sync_filebuf<char>(stdout); | |
88 | new (&buf_cin_sync) stdio_sync_filebuf<char>(stdin); | |
89 | new (&buf_cerr_sync) stdio_sync_filebuf<char>(stderr); | |
90 | ||
91 | // The standard streams are constructed once only and never | |
92 | // destroyed. | |
93 | new (&cout) ostream(&buf_cout_sync); | |
94 | new (&cin) istream(&buf_cin_sync); | |
95 | new (&cerr) ostream(&buf_cerr_sync); | |
96 | new (&clog) ostream(&buf_cerr_sync); | |
97 | cin.tie(&cout); | |
af0b8d73 | 98 | cerr.setf(ios_base::unitbuf); |
634a11f7 PC |
99 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
100 | // 455. cerr::tie() and wcerr::tie() are overspecified. | |
101 | cerr.tie(&cout); | |
102 | ||
c755e77d BK |
103 | #ifdef _GLIBCXX_USE_WCHAR_T |
104 | new (&buf_wcout_sync) stdio_sync_filebuf<wchar_t>(stdout); | |
105 | new (&buf_wcin_sync) stdio_sync_filebuf<wchar_t>(stdin); | |
106 | new (&buf_wcerr_sync) stdio_sync_filebuf<wchar_t>(stderr); | |
107 | ||
108 | new (&wcout) wostream(&buf_wcout_sync); | |
109 | new (&wcin) wistream(&buf_wcin_sync); | |
110 | new (&wcerr) wostream(&buf_wcerr_sync); | |
111 | new (&wclog) wostream(&buf_wcerr_sync); | |
112 | wcin.tie(&wcout); | |
af0b8d73 | 113 | wcerr.setf(ios_base::unitbuf); |
634a11f7 | 114 | wcerr.tie(&wcout); |
c755e77d | 115 | #endif |
fa972243 BK |
116 | |
117 | // NB: Have to set refcount above one, so that standard | |
118 | // streams are not re-initialized with uses of ios_base::Init | |
119 | // besides <iostream> static object, ie just using <ios> with | |
120 | // ios_base::Init objects. | |
b7ee72de | 121 | __gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1); |
c755e77d | 122 | } |
c755e77d BK |
123 | } |
124 | ||
125 | ios_base::Init::~Init() | |
126 | { | |
be335b18 | 127 | // Be race-detector-friendly. For more info see bits/c++config. |
8c61f400 | 128 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_S_refcount); |
b7ee72de | 129 | if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, -1) == 2) |
c755e77d | 130 | { |
8c61f400 | 131 | _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_S_refcount); |
c755e77d | 132 | // Catch any exceptions thrown by basic_ostream::flush() |
bc2631e0 | 133 | __try |
c755e77d BK |
134 | { |
135 | // Flush standard output streams as required by 27.4.2.1.6 | |
136 | cout.flush(); | |
137 | cerr.flush(); | |
138 | clog.flush(); | |
139 | ||
140 | #ifdef _GLIBCXX_USE_WCHAR_T | |
141 | wcout.flush(); | |
142 | wcerr.flush(); | |
143 | wclog.flush(); | |
144 | #endif | |
145 | } | |
bc2631e0 | 146 | __catch(...) |
c755e77d BK |
147 | { } |
148 | } | |
149 | } | |
150 | ||
151 | bool | |
152 | ios_base::sync_with_stdio(bool __sync) | |
153 | { | |
154 | // _GLIBCXX_RESOLVE_LIB_DEFECTS | |
155 | // 49. Underspecification of ios_base::sync_with_stdio | |
156 | bool __ret = ios_base::Init::_S_synced_with_stdio; | |
157 | ||
158 | // Turn off sync with C FILE* for cin, cout, cerr, clog iff | |
159 | // currently synchronized. | |
160 | if (!__sync && __ret) | |
161 | { | |
48dd95f2 PC |
162 | // Make sure the standard streams are constructed. |
163 | ios_base::Init __init; | |
164 | ||
c755e77d BK |
165 | ios_base::Init::_S_synced_with_stdio = __sync; |
166 | ||
167 | // Explicitly call dtors to free any memory that is | |
168 | // dynamically allocated by filebuf ctor or member functions, | |
169 | // but don't deallocate all memory by calling operator delete. | |
170 | buf_cout_sync.~stdio_sync_filebuf<char>(); | |
171 | buf_cin_sync.~stdio_sync_filebuf<char>(); | |
172 | buf_cerr_sync.~stdio_sync_filebuf<char>(); | |
173 | ||
174 | #ifdef _GLIBCXX_USE_WCHAR_T | |
175 | buf_wcout_sync.~stdio_sync_filebuf<wchar_t>(); | |
176 | buf_wcin_sync.~stdio_sync_filebuf<wchar_t>(); | |
177 | buf_wcerr_sync.~stdio_sync_filebuf<wchar_t>(); | |
178 | #endif | |
179 | ||
180 | // Create stream buffers for the standard streams and use | |
181 | // those buffers without destroying and recreating the | |
182 | // streams. | |
183 | new (&buf_cout) stdio_filebuf<char>(stdout, ios_base::out); | |
184 | new (&buf_cin) stdio_filebuf<char>(stdin, ios_base::in); | |
185 | new (&buf_cerr) stdio_filebuf<char>(stderr, ios_base::out); | |
186 | cout.rdbuf(&buf_cout); | |
187 | cin.rdbuf(&buf_cin); | |
188 | cerr.rdbuf(&buf_cerr); | |
189 | clog.rdbuf(&buf_cerr); | |
190 | ||
191 | #ifdef _GLIBCXX_USE_WCHAR_T | |
192 | new (&buf_wcout) stdio_filebuf<wchar_t>(stdout, ios_base::out); | |
193 | new (&buf_wcin) stdio_filebuf<wchar_t>(stdin, ios_base::in); | |
194 | new (&buf_wcerr) stdio_filebuf<wchar_t>(stderr, ios_base::out); | |
195 | wcout.rdbuf(&buf_wcout); | |
196 | wcin.rdbuf(&buf_wcin); | |
197 | wcerr.rdbuf(&buf_wcerr); | |
198 | wclog.rdbuf(&buf_wcerr); | |
199 | #endif | |
200 | } | |
201 | return __ret; | |
202 | } | |
3cbc7af0 | 203 | |
12ffa228 BK |
204 | _GLIBCXX_END_NAMESPACE_VERSION |
205 | } // namespace |