1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
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 <ext/stdio_filebuf.h>
40 #include <ext/stdio_sync_filebuf.h>
42 namespace __gnu_internal
_GLIBCXX_VISIBILITY_ATTR(hidden
)
44 using namespace __gnu_cxx
;
46 // Extern declarations for global objects in src/globals.cc.
47 extern stdio_sync_filebuf
<char> buf_cout_sync
;
48 extern stdio_sync_filebuf
<char> buf_cin_sync
;
49 extern stdio_sync_filebuf
<char> buf_cerr_sync
;
51 extern stdio_filebuf
<char> buf_cout
;
52 extern stdio_filebuf
<char> buf_cin
;
53 extern stdio_filebuf
<char> buf_cerr
;
55 #ifdef _GLIBCXX_USE_WCHAR_T
56 extern stdio_sync_filebuf
<wchar_t> buf_wcout_sync
;
57 extern stdio_sync_filebuf
<wchar_t> buf_wcin_sync
;
58 extern stdio_sync_filebuf
<wchar_t> buf_wcerr_sync
;
60 extern stdio_filebuf
<wchar_t> buf_wcout
;
61 extern stdio_filebuf
<wchar_t> buf_wcin
;
62 extern stdio_filebuf
<wchar_t> buf_wcerr
;
64 } // namespace __gnu_internal
66 _GLIBCXX_BEGIN_NAMESPACE(std
)
68 using namespace __gnu_internal
;
75 #ifdef _GLIBCXX_USE_WCHAR_T
77 extern wostream wcout
;
78 extern wostream wcerr
;
79 extern wostream wclog
;
82 ios_base::Init::Init()
84 if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount
, 1) == 0)
86 // Standard streams default to synced with "C" operations.
87 _S_synced_with_stdio
= true;
89 new (&buf_cout_sync
) stdio_sync_filebuf
<char>(stdout
);
90 new (&buf_cin_sync
) stdio_sync_filebuf
<char>(stdin
);
91 new (&buf_cerr_sync
) stdio_sync_filebuf
<char>(stderr
);
93 // The standard streams are constructed once only and never
95 new (&cout
) ostream(&buf_cout_sync
);
96 new (&cin
) istream(&buf_cin_sync
);
97 new (&cerr
) ostream(&buf_cerr_sync
);
98 new (&clog
) ostream(&buf_cerr_sync
);
100 cerr
.flags(ios_base::unitbuf
);
101 // _GLIBCXX_RESOLVE_LIB_DEFECTS
102 // 455. cerr::tie() and wcerr::tie() are overspecified.
105 #ifdef _GLIBCXX_USE_WCHAR_T
106 new (&buf_wcout_sync
) stdio_sync_filebuf
<wchar_t>(stdout
);
107 new (&buf_wcin_sync
) stdio_sync_filebuf
<wchar_t>(stdin
);
108 new (&buf_wcerr_sync
) stdio_sync_filebuf
<wchar_t>(stderr
);
110 new (&wcout
) wostream(&buf_wcout_sync
);
111 new (&wcin
) wistream(&buf_wcin_sync
);
112 new (&wcerr
) wostream(&buf_wcerr_sync
);
113 new (&wclog
) wostream(&buf_wcerr_sync
);
115 wcerr
.flags(ios_base::unitbuf
);
119 // NB: Have to set refcount above one, so that standard
120 // streams are not re-initialized with uses of ios_base::Init
121 // besides <iostream> static object, ie just using <ios> with
122 // ios_base::Init objects.
123 __gnu_cxx::__atomic_add_dispatch(&_S_refcount
, 1);
127 ios_base::Init::~Init()
129 if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount
, -1) == 2)
131 // Catch any exceptions thrown by basic_ostream::flush()
134 // Flush standard output streams as required by 27.4.2.1.6
139 #ifdef _GLIBCXX_USE_WCHAR_T
151 ios_base::sync_with_stdio(bool __sync
)
153 // _GLIBCXX_RESOLVE_LIB_DEFECTS
154 // 49. Underspecification of ios_base::sync_with_stdio
155 bool __ret
= ios_base::Init::_S_synced_with_stdio
;
157 // Turn off sync with C FILE* for cin, cout, cerr, clog iff
158 // currently synchronized.
159 if (!__sync
&& __ret
)
161 // Make sure the standard streams are constructed.
162 ios_base::Init __init
;
164 ios_base::Init::_S_synced_with_stdio
= __sync
;
166 // Explicitly call dtors to free any memory that is
167 // dynamically allocated by filebuf ctor or member functions,
168 // but don't deallocate all memory by calling operator delete.
169 buf_cout_sync
.~stdio_sync_filebuf
<char>();
170 buf_cin_sync
.~stdio_sync_filebuf
<char>();
171 buf_cerr_sync
.~stdio_sync_filebuf
<char>();
173 #ifdef _GLIBCXX_USE_WCHAR_T
174 buf_wcout_sync
.~stdio_sync_filebuf
<wchar_t>();
175 buf_wcin_sync
.~stdio_sync_filebuf
<wchar_t>();
176 buf_wcerr_sync
.~stdio_sync_filebuf
<wchar_t>();
179 // Create stream buffers for the standard streams and use
180 // those buffers without destroying and recreating the
182 new (&buf_cout
) stdio_filebuf
<char>(stdout
, ios_base::out
);
183 new (&buf_cin
) stdio_filebuf
<char>(stdin
, ios_base::in
);
184 new (&buf_cerr
) stdio_filebuf
<char>(stderr
, ios_base::out
);
185 cout
.rdbuf(&buf_cout
);
187 cerr
.rdbuf(&buf_cerr
);
188 clog
.rdbuf(&buf_cerr
);
190 #ifdef _GLIBCXX_USE_WCHAR_T
191 new (&buf_wcout
) stdio_filebuf
<wchar_t>(stdout
, ios_base::out
);
192 new (&buf_wcin
) stdio_filebuf
<wchar_t>(stdin
, ios_base::in
);
193 new (&buf_wcerr
) stdio_filebuf
<wchar_t>(stderr
, ios_base::out
);
194 wcout
.rdbuf(&buf_wcout
);
195 wcin
.rdbuf(&buf_wcin
);
196 wcerr
.rdbuf(&buf_wcerr
);
197 wclog
.rdbuf(&buf_wcerr
);
203 _GLIBCXX_END_NAMESPACE