1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009
5 // Free Software Foundation, Inc.
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)
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
35 #include <ext/stdio_filebuf.h>
36 #include <ext/stdio_sync_filebuf.h>
38 namespace __gnu_internal _GLIBCXX_VISIBILITY_ATTR(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;
60 } // namespace __gnu_internal
62 _GLIBCXX_BEGIN_NAMESPACE(std)
64 using namespace __gnu_internal;
71 #ifdef _GLIBCXX_USE_WCHAR_T
73 extern wostream wcout;
74 extern wostream wcerr;
75 extern wostream wclog;
78 ios_base::Init::Init()
80 if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0)
82 // Standard streams default to synced with "C" operations.
83 _S_synced_with_stdio = true;
85 new (&buf_cout_sync) stdio_sync_filebuf<char>(stdout);
86 new (&buf_cin_sync) stdio_sync_filebuf<char>(stdin);
87 new (&buf_cerr_sync) stdio_sync_filebuf<char>(stderr);
89 // The standard streams are constructed once only and never
91 new (&cout) ostream(&buf_cout_sync);
92 new (&cin) istream(&buf_cin_sync);
93 new (&cerr) ostream(&buf_cerr_sync);
94 new (&clog) ostream(&buf_cerr_sync);
96 cerr.setf(ios_base::unitbuf);
97 // _GLIBCXX_RESOLVE_LIB_DEFECTS
98 // 455. cerr::tie() and wcerr::tie() are overspecified.
101 #ifdef _GLIBCXX_USE_WCHAR_T
102 new (&buf_wcout_sync) stdio_sync_filebuf<wchar_t>(stdout);
103 new (&buf_wcin_sync) stdio_sync_filebuf<wchar_t>(stdin);
104 new (&buf_wcerr_sync) stdio_sync_filebuf<wchar_t>(stderr);
106 new (&wcout) wostream(&buf_wcout_sync);
107 new (&wcin) wistream(&buf_wcin_sync);
108 new (&wcerr) wostream(&buf_wcerr_sync);
109 new (&wclog) wostream(&buf_wcerr_sync);
111 wcerr.setf(ios_base::unitbuf);
115 // NB: Have to set refcount above one, so that standard
116 // streams are not re-initialized with uses of ios_base::Init
117 // besides <iostream> static object, ie just using <ios> with
118 // ios_base::Init objects.
119 __gnu_cxx::__atomic_add_dispatch(&_S_refcount, 1);
123 ios_base::Init::~Init()
125 if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, -1) == 2)
127 // Catch any exceptions thrown by basic_ostream::flush()
130 // Flush standard output streams as required by 27.4.2.1.6
135 #ifdef _GLIBCXX_USE_WCHAR_T
147 ios_base::sync_with_stdio(bool __sync)
149 // _GLIBCXX_RESOLVE_LIB_DEFECTS
150 // 49. Underspecification of ios_base::sync_with_stdio
151 bool __ret = ios_base::Init::_S_synced_with_stdio;
153 // Turn off sync with C FILE* for cin, cout, cerr, clog iff
154 // currently synchronized.
155 if (!__sync && __ret)
157 // Make sure the standard streams are constructed.
158 ios_base::Init __init;
160 ios_base::Init::_S_synced_with_stdio = __sync;
162 // Explicitly call dtors to free any memory that is
163 // dynamically allocated by filebuf ctor or member functions,
164 // but don't deallocate all memory by calling operator delete.
165 buf_cout_sync.~stdio_sync_filebuf<char>();
166 buf_cin_sync.~stdio_sync_filebuf<char>();
167 buf_cerr_sync.~stdio_sync_filebuf<char>();
169 #ifdef _GLIBCXX_USE_WCHAR_T
170 buf_wcout_sync.~stdio_sync_filebuf<wchar_t>();
171 buf_wcin_sync.~stdio_sync_filebuf<wchar_t>();
172 buf_wcerr_sync.~stdio_sync_filebuf<wchar_t>();
175 // Create stream buffers for the standard streams and use
176 // those buffers without destroying and recreating the
178 new (&buf_cout) stdio_filebuf<char>(stdout, ios_base::out);
179 new (&buf_cin) stdio_filebuf<char>(stdin, ios_base::in);
180 new (&buf_cerr) stdio_filebuf<char>(stderr, ios_base::out);
181 cout.rdbuf(&buf_cout);
183 cerr.rdbuf(&buf_cerr);
184 clog.rdbuf(&buf_cerr);
186 #ifdef _GLIBCXX_USE_WCHAR_T
187 new (&buf_wcout) stdio_filebuf<wchar_t>(stdout, ios_base::out);
188 new (&buf_wcin) stdio_filebuf<wchar_t>(stdin, ios_base::in);
189 new (&buf_wcerr) stdio_filebuf<wchar_t>(stderr, ios_base::out);
190 wcout.rdbuf(&buf_wcout);
191 wcin.rdbuf(&buf_wcin);
192 wcerr.rdbuf(&buf_wcerr);
193 wclog.rdbuf(&buf_wcerr);
199 _GLIBCXX_END_NAMESPACE