1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 27.4 Iostreams base classes
39 #include <bits/atomicity.h>
40 #include <ext/stdio_filebuf.h>
41 #include <ext/stdio_sync_filebuf.h>
43 namespace __gnu_internal
45 using namespace __gnu_cxx
;
47 // Extern declarations for global objects in src/globals.cc.
48 extern stdio_sync_filebuf
<char> buf_cout_sync
;
49 extern stdio_sync_filebuf
<char> buf_cin_sync
;
50 extern stdio_sync_filebuf
<char> buf_cerr_sync
;
52 extern stdio_filebuf
<char> buf_cout
;
53 extern stdio_filebuf
<char> buf_cin
;
54 extern stdio_filebuf
<char> buf_cerr
;
56 #ifdef _GLIBCXX_USE_WCHAR_T
57 extern stdio_sync_filebuf
<wchar_t> buf_wcout_sync
;
58 extern stdio_sync_filebuf
<wchar_t> buf_wcin_sync
;
59 extern stdio_sync_filebuf
<wchar_t> buf_wcerr_sync
;
61 extern stdio_filebuf
<wchar_t> buf_wcout
;
62 extern stdio_filebuf
<wchar_t> buf_wcin
;
63 extern stdio_filebuf
<wchar_t> buf_wcerr
;
65 } // namespace __gnu_internal
69 using namespace __gnu_internal
;
76 #ifdef _GLIBCXX_USE_WCHAR_T
78 extern wostream wcout
;
79 extern wostream wcerr
;
80 extern wostream wclog
;
83 ios_base::Init::Init()
85 if (__gnu_cxx::__exchange_and_add(&_S_refcount
, 1) == 0)
87 // Standard streams default to synced with "C" operations.
88 _S_synced_with_stdio
= true;
90 new (&buf_cout_sync
) stdio_sync_filebuf
<char>(stdout
);
91 new (&buf_cin_sync
) stdio_sync_filebuf
<char>(stdin
);
92 new (&buf_cerr_sync
) stdio_sync_filebuf
<char>(stderr
);
94 // The standard streams are constructed once only and never
96 new (&cout
) ostream(&buf_cout_sync
);
97 new (&cin
) istream(&buf_cin_sync
);
98 new (&cerr
) ostream(&buf_cerr_sync
);
99 new (&clog
) ostream(&buf_cerr_sync
);
101 cerr
.flags(ios_base::unitbuf
);
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
);
113 wcerr
.flags(ios_base::unitbuf
);
116 // NB: Have to set refcount above one, so that standard
117 // streams are not re-initialized with uses of ios_base::Init
118 // besides <iostream> static object, ie just using <ios> with
119 // ios_base::Init objects.
120 __gnu_cxx::__atomic_add(&_S_refcount
, 1);
124 ios_base::Init::~Init()
126 if (__gnu_cxx::__exchange_and_add(&_S_refcount
, -1) == 2)
128 // Catch any exceptions thrown by basic_ostream::flush()
131 // Flush standard output streams as required by 27.4.2.1.6
136 #ifdef _GLIBCXX_USE_WCHAR_T
148 ios_base::sync_with_stdio(bool __sync
)
150 // _GLIBCXX_RESOLVE_LIB_DEFECTS
151 // 49. Underspecification of ios_base::sync_with_stdio
152 bool __ret
= ios_base::Init::_S_synced_with_stdio
;
154 // Turn off sync with C FILE* for cin, cout, cerr, clog iff
155 // currently synchronized.
156 if (!__sync
&& __ret
)
158 // Make sure the standard streams are constructed.
159 ios_base::Init __init
;
161 ios_base::Init::_S_synced_with_stdio
= __sync
;
163 // Explicitly call dtors to free any memory that is
164 // dynamically allocated by filebuf ctor or member functions,
165 // but don't deallocate all memory by calling operator delete.
166 buf_cout_sync
.~stdio_sync_filebuf
<char>();
167 buf_cin_sync
.~stdio_sync_filebuf
<char>();
168 buf_cerr_sync
.~stdio_sync_filebuf
<char>();
170 #ifdef _GLIBCXX_USE_WCHAR_T
171 buf_wcout_sync
.~stdio_sync_filebuf
<wchar_t>();
172 buf_wcin_sync
.~stdio_sync_filebuf
<wchar_t>();
173 buf_wcerr_sync
.~stdio_sync_filebuf
<wchar_t>();
176 // Create stream buffers for the standard streams and use
177 // those buffers without destroying and recreating the
179 new (&buf_cout
) stdio_filebuf
<char>(stdout
, ios_base::out
);
180 new (&buf_cin
) stdio_filebuf
<char>(stdin
, ios_base::in
);
181 new (&buf_cerr
) stdio_filebuf
<char>(stderr
, ios_base::out
);
182 cout
.rdbuf(&buf_cout
);
184 cerr
.rdbuf(&buf_cerr
);
185 clog
.rdbuf(&buf_cerr
);
187 #ifdef _GLIBCXX_USE_WCHAR_T
188 new (&buf_wcout
) stdio_filebuf
<wchar_t>(stdout
, ios_base::out
);
189 new (&buf_wcin
) stdio_filebuf
<wchar_t>(stdin
, ios_base::in
);
190 new (&buf_wcerr
) stdio_filebuf
<wchar_t>(stderr
, ios_base::out
);
191 wcout
.rdbuf(&buf_wcout
);
192 wcin
.rdbuf(&buf_wcin
);
193 wcerr
.rdbuf(&buf_wcerr
);
194 wclog
.rdbuf(&buf_wcerr
);