]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/src/ios_init.cc
2004-01-28 Michael Koch <konqueror@gmx.de>
[thirdparty/gcc.git] / libstdc++-v3 / src / ios_init.cc
CommitLineData
c755e77d
BK
1// Iostreams base classes -*- C++ -*-
2
3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
4// Free Software Foundation, Inc.
5//
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)
10// any later version.
11
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.
16
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20// USA.
21
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.
30
31//
32// ISO C++ 14882: 27.4 Iostreams base classes
33//
34
35#include <ios>
36#include <ostream>
37#include <istream>
38#include <fstream>
39#include <bits/atomicity.h>
40#include <ext/stdio_filebuf.h>
41#include <ext/stdio_sync_filebuf.h>
42
43namespace __gnu_cxx
44{
45 // Extern declarations for global objects in src/globals.cc.
46 extern stdio_sync_filebuf<char> buf_cout_sync;
47 extern stdio_sync_filebuf<char> buf_cin_sync;
48 extern stdio_sync_filebuf<char> buf_cerr_sync;
49
50 extern stdio_filebuf<char> buf_cout;
51 extern stdio_filebuf<char> buf_cin;
52 extern stdio_filebuf<char> buf_cerr;
53
54#ifdef _GLIBCXX_USE_WCHAR_T
55 extern stdio_sync_filebuf<wchar_t> buf_wcout_sync;
56 extern stdio_sync_filebuf<wchar_t> buf_wcin_sync;
57 extern stdio_sync_filebuf<wchar_t> buf_wcerr_sync;
58
59 extern stdio_filebuf<wchar_t> buf_wcout;
60 extern stdio_filebuf<wchar_t> buf_wcin;
61 extern stdio_filebuf<wchar_t> buf_wcerr;
62#endif
63} // namespace __gnu_cxx
64
65namespace std
66{
67 using namespace __gnu_cxx;
68
69 extern istream cin;
70 extern ostream cout;
71 extern ostream cerr;
72 extern ostream clog;
73
74#ifdef _GLIBCXX_USE_WCHAR_T
75 extern wistream wcin;
76 extern wostream wcout;
77 extern wostream wcerr;
78 extern wostream wclog;
79#endif
80
81 ios_base::Init::Init()
82 {
fa972243 83 if (__exchange_and_add(&_S_refcount, 1) == 0)
c755e77d
BK
84 {
85 // Standard streams default to synced with "C" operations.
86 _S_synced_with_stdio = true;
87
88 new (&buf_cout_sync) stdio_sync_filebuf<char>(stdout);
89 new (&buf_cin_sync) stdio_sync_filebuf<char>(stdin);
90 new (&buf_cerr_sync) stdio_sync_filebuf<char>(stderr);
91
92 // The standard streams are constructed once only and never
93 // destroyed.
94 new (&cout) ostream(&buf_cout_sync);
95 new (&cin) istream(&buf_cin_sync);
96 new (&cerr) ostream(&buf_cerr_sync);
97 new (&clog) ostream(&buf_cerr_sync);
98 cin.tie(&cout);
99 cerr.flags(ios_base::unitbuf);
100
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);
105
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);
110 wcin.tie(&wcout);
111 wcerr.flags(ios_base::unitbuf);
112#endif
fa972243
BK
113
114 // NB: Have to set refcount above one, so that standard
115 // streams are not re-initialized with uses of ios_base::Init
116 // besides <iostream> static object, ie just using <ios> with
117 // ios_base::Init objects.
118 __atomic_add(&_S_refcount, 1);
c755e77d 119 }
c755e77d
BK
120 }
121
122 ios_base::Init::~Init()
123 {
fa972243 124 if (__exchange_and_add(&_S_refcount, -1) == 2)
c755e77d
BK
125 {
126 // Catch any exceptions thrown by basic_ostream::flush()
127 try
128 {
129 // Flush standard output streams as required by 27.4.2.1.6
130 cout.flush();
131 cerr.flush();
132 clog.flush();
133
134#ifdef _GLIBCXX_USE_WCHAR_T
135 wcout.flush();
136 wcerr.flush();
137 wclog.flush();
138#endif
139 }
140 catch (...)
141 { }
142 }
143 }
144
145 bool
146 ios_base::sync_with_stdio(bool __sync)
147 {
148 // _GLIBCXX_RESOLVE_LIB_DEFECTS
149 // 49. Underspecification of ios_base::sync_with_stdio
150 bool __ret = ios_base::Init::_S_synced_with_stdio;
151
152 // Turn off sync with C FILE* for cin, cout, cerr, clog iff
153 // currently synchronized.
154 if (!__sync && __ret)
155 {
156 ios_base::Init::_S_synced_with_stdio = __sync;
157
158 // Explicitly call dtors to free any memory that is
159 // dynamically allocated by filebuf ctor or member functions,
160 // but don't deallocate all memory by calling operator delete.
161 buf_cout_sync.~stdio_sync_filebuf<char>();
162 buf_cin_sync.~stdio_sync_filebuf<char>();
163 buf_cerr_sync.~stdio_sync_filebuf<char>();
164
165#ifdef _GLIBCXX_USE_WCHAR_T
166 buf_wcout_sync.~stdio_sync_filebuf<wchar_t>();
167 buf_wcin_sync.~stdio_sync_filebuf<wchar_t>();
168 buf_wcerr_sync.~stdio_sync_filebuf<wchar_t>();
169#endif
170
171 // Create stream buffers for the standard streams and use
172 // those buffers without destroying and recreating the
173 // streams.
174 new (&buf_cout) stdio_filebuf<char>(stdout, ios_base::out);
175 new (&buf_cin) stdio_filebuf<char>(stdin, ios_base::in);
176 new (&buf_cerr) stdio_filebuf<char>(stderr, ios_base::out);
177 cout.rdbuf(&buf_cout);
178 cin.rdbuf(&buf_cin);
179 cerr.rdbuf(&buf_cerr);
180 clog.rdbuf(&buf_cerr);
181
182#ifdef _GLIBCXX_USE_WCHAR_T
183 new (&buf_wcout) stdio_filebuf<wchar_t>(stdout, ios_base::out);
184 new (&buf_wcin) stdio_filebuf<wchar_t>(stdin, ios_base::in);
185 new (&buf_wcerr) stdio_filebuf<wchar_t>(stderr, ios_base::out);
186 wcout.rdbuf(&buf_wcout);
187 wcin.rdbuf(&buf_wcin);
188 wcerr.rdbuf(&buf_wcerr);
189 wclog.rdbuf(&buf_wcerr);
190#endif
191 }
192 return __ret;
193 }
194} // namespace std