2012-01-13 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / src / ios_init.cc
bloba811daf00cc2d4002a9305e7cc9a3ce0ce5b22a8
1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010, 2011
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
10 // Free Software Foundation; either version 3, or (at your option)
11 // any later version.
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.
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.
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/>.
28 // ISO C++ 14882: 27.4 Iostreams base classes
31 #include <ios>
32 #include <ostream>
33 #include <istream>
34 #include <fstream>
35 #include <ext/stdio_filebuf.h>
36 #include <ext/stdio_sync_filebuf.h>
38 namespace __gnu_internal _GLIBCXX_VISIBILITY(hidden)
40 using namespace __gnu_cxx;
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;
47 extern stdio_filebuf<char> buf_cout;
48 extern stdio_filebuf<char> buf_cin;
49 extern stdio_filebuf<char> buf_cerr;
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;
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
60 } // namespace __gnu_internal
62 namespace std _GLIBCXX_VISIBILITY(default)
64 _GLIBCXX_BEGIN_NAMESPACE_VERSION
66 using namespace __gnu_internal;
68 extern istream cin;
69 extern ostream cout;
70 extern ostream cerr;
71 extern ostream clog;
73 #ifdef _GLIBCXX_USE_WCHAR_T
74 extern wistream wcin;
75 extern wostream wcout;
76 extern wostream wcerr;
77 extern wostream wclog;
78 #endif
80 ios_base::Init::Init()
82 if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0)
84 // Standard streams default to synced with "C" operations.
85 _S_synced_with_stdio = true;
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);
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);
98 cerr.setf(ios_base::unitbuf);
99 // _GLIBCXX_RESOLVE_LIB_DEFECTS
100 // 455. cerr::tie() and wcerr::tie() are overspecified.
101 cerr.tie(&cout);
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);
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);
113 wcerr.setf(ios_base::unitbuf);
114 wcerr.tie(&wcout);
115 #endif
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.
121 __gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1);
125 ios_base::Init::~Init()
127 // Be race-detector-friendly. For more info see bits/c++config.
128 _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_S_refcount);
129 if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, -1) == 2)
131 _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_S_refcount);
132 // Catch any exceptions thrown by basic_ostream::flush()
133 __try
135 // Flush standard output streams as required by 27.4.2.1.6
136 cout.flush();
137 cerr.flush();
138 clog.flush();
140 #ifdef _GLIBCXX_USE_WCHAR_T
141 wcout.flush();
142 wcerr.flush();
143 wclog.flush();
144 #endif
146 __catch(...)
151 bool
152 ios_base::sync_with_stdio(bool __sync)
154 // _GLIBCXX_RESOLVE_LIB_DEFECTS
155 // 49. Underspecification of ios_base::sync_with_stdio
156 bool __ret = ios_base::Init::_S_synced_with_stdio;
158 // Turn off sync with C FILE* for cin, cout, cerr, clog iff
159 // currently synchronized.
160 if (!__sync && __ret)
162 // Make sure the standard streams are constructed.
163 ios_base::Init __init;
165 ios_base::Init::_S_synced_with_stdio = __sync;
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>();
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
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);
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
201 return __ret;
204 _GLIBCXX_END_NAMESPACE_VERSION
205 } // namespace