OSDN Git Service

fix broken checkin, test should be link not assemble
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / ios_init.cc
1 // Iostreams base classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009
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.
12
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.
17
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.
21
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/>.
26
27 //
28 // ISO C++ 14882: 27.4  Iostreams base classes
29 //
30
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>
37
38 namespace __gnu_internal _GLIBCXX_VISIBILITY_ATTR(hidden)
39 {
40   using namespace __gnu_cxx;
41
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;
46
47   extern stdio_filebuf<char> buf_cout;
48   extern stdio_filebuf<char> buf_cin;
49   extern stdio_filebuf<char> buf_cerr;
50
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;
55
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
61
62 _GLIBCXX_BEGIN_NAMESPACE(std)
63
64   using namespace __gnu_internal;
65   
66   extern istream cin;
67   extern ostream cout;
68   extern ostream cerr;
69   extern ostream clog;
70
71 #ifdef _GLIBCXX_USE_WCHAR_T
72   extern wistream wcin;
73   extern wostream wcout;
74   extern wostream wcerr;
75   extern wostream wclog;
76 #endif
77
78   ios_base::Init::Init()
79   {
80     if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0)
81       {
82         // Standard streams default to synced with "C" operations.
83         _S_synced_with_stdio = true;
84
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);
88
89         // The standard streams are constructed once only and never
90         // destroyed.
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);
95         cin.tie(&cout);
96         cerr.setf(ios_base::unitbuf);
97         // _GLIBCXX_RESOLVE_LIB_DEFECTS
98         // 455. cerr::tie() and wcerr::tie() are overspecified.
99         cerr.tie(&cout);
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.setf(ios_base::unitbuf);
112         wcerr.tie(&wcout);      
113 #endif
114         
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);
120       }
121   }
122
123   ios_base::Init::~Init()
124   {
125     if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, -1) == 2)
126       {
127         // Catch any exceptions thrown by basic_ostream::flush()
128         __try
129           { 
130             // Flush standard output streams as required by 27.4.2.1.6
131             cout.flush();
132             cerr.flush();
133             clog.flush();
134     
135 #ifdef _GLIBCXX_USE_WCHAR_T
136             wcout.flush();
137             wcerr.flush();
138             wclog.flush();    
139 #endif
140           }
141         __catch(...)
142           { }
143       }
144   } 
145
146   bool 
147   ios_base::sync_with_stdio(bool __sync)
148   { 
149     // _GLIBCXX_RESOLVE_LIB_DEFECTS
150     // 49.  Underspecification of ios_base::sync_with_stdio
151     bool __ret = ios_base::Init::_S_synced_with_stdio;
152
153     // Turn off sync with C FILE* for cin, cout, cerr, clog iff
154     // currently synchronized.
155     if (!__sync && __ret)
156       {
157         // Make sure the standard streams are constructed.
158         ios_base::Init __init;
159
160         ios_base::Init::_S_synced_with_stdio = __sync;
161
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>();
168
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>();
173 #endif
174
175         // Create stream buffers for the standard streams and use
176         // those buffers without destroying and recreating the
177         // streams.
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);
182         cin.rdbuf(&buf_cin);
183         cerr.rdbuf(&buf_cerr);
184         clog.rdbuf(&buf_cerr);
185     
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);
194 #endif
195       }
196     return __ret; 
197   }
198
199 _GLIBCXX_END_NAMESPACE