OSDN Git Service

Backported from mainline
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / src / c++98 / ios.cc
1 // Iostreams base classes -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010
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 <limits>
33
34 namespace std _GLIBCXX_VISIBILITY(default)
35 {
36 _GLIBCXX_BEGIN_NAMESPACE_VERSION
37
38   // Definitions for static const members of ios_base.
39   const ios_base::fmtflags ios_base::boolalpha;
40   const ios_base::fmtflags ios_base::dec;
41   const ios_base::fmtflags ios_base::fixed;
42   const ios_base::fmtflags ios_base::hex;
43   const ios_base::fmtflags ios_base::internal;
44   const ios_base::fmtflags ios_base::left;
45   const ios_base::fmtflags ios_base::oct;
46   const ios_base::fmtflags ios_base::right;
47   const ios_base::fmtflags ios_base::scientific;
48   const ios_base::fmtflags ios_base::showbase;
49   const ios_base::fmtflags ios_base::showpoint;
50   const ios_base::fmtflags ios_base::showpos;
51   const ios_base::fmtflags ios_base::skipws;
52   const ios_base::fmtflags ios_base::unitbuf;
53   const ios_base::fmtflags ios_base::uppercase;
54   const ios_base::fmtflags ios_base::adjustfield;
55   const ios_base::fmtflags ios_base::basefield;
56   const ios_base::fmtflags ios_base::floatfield;
57
58   const ios_base::iostate ios_base::badbit;
59   const ios_base::iostate ios_base::eofbit;
60   const ios_base::iostate ios_base::failbit;
61   const ios_base::iostate ios_base::goodbit;
62
63   const ios_base::openmode ios_base::app;
64   const ios_base::openmode ios_base::ate;
65   const ios_base::openmode ios_base::binary;
66   const ios_base::openmode ios_base::in;
67   const ios_base::openmode ios_base::out;
68   const ios_base::openmode ios_base::trunc;
69
70   const ios_base::seekdir ios_base::beg;
71   const ios_base::seekdir ios_base::cur;
72   const ios_base::seekdir ios_base::end;
73
74   _Atomic_word ios_base::Init::_S_refcount;
75
76   bool ios_base::Init::_S_synced_with_stdio = true;
77
78   ios_base::ios_base() throw()
79   : _M_precision(), _M_width(), _M_flags(), _M_exception(), 
80   _M_streambuf_state(), _M_callbacks(0), _M_word_zero(), 
81   _M_word_size(_S_local_word_size), _M_word(_M_local_word), _M_ios_locale()
82   {
83     // Do nothing: basic_ios::init() does it.  
84     // NB: _M_callbacks and _M_word must be zero for non-initialized
85     // ios_base to go through ~ios_base gracefully.
86   }
87   
88   // 27.4.2.7  ios_base constructors/destructors
89   ios_base::~ios_base()
90   {
91     _M_call_callbacks(erase_event);
92     _M_dispose_callbacks();
93     if (_M_word != _M_local_word) 
94       {
95         delete [] _M_word;
96         _M_word = 0;
97       }
98   }
99
100   // 27.4.2.5  ios_base storage functions
101   int 
102   ios_base::xalloc() throw()
103   {
104     // Implementation note: Initialize top to zero to ensure that
105     // initialization occurs before main() is started.
106     static _Atomic_word _S_top = 0; 
107     return __gnu_cxx::__exchange_and_add_dispatch(&_S_top, 1) + 4;
108   }
109
110   void 
111   ios_base::register_callback(event_callback __fn, int __index)
112   { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
113
114   // 27.4.2.5  iword/pword storage
115   ios_base::_Words&
116   ios_base::_M_grow_words(int __ix, bool __iword)
117   {
118     // Precondition: _M_word_size <= __ix
119     int __newsize = _S_local_word_size;
120     _Words* __words = _M_local_word;
121     if (__ix > _S_local_word_size - 1)
122       {
123         if (__ix < numeric_limits<int>::max())
124           {
125             __newsize = __ix + 1;
126             __try
127               { __words = new _Words[__newsize]; }
128             __catch(const std::bad_alloc&)
129               {
130                 _M_streambuf_state |= badbit;
131                 if (_M_streambuf_state & _M_exception)
132                   __throw_ios_failure(__N("ios_base::_M_grow_words "
133                                           "allocation failed"));
134                 if (__iword)
135                   _M_word_zero._M_iword = 0;
136                 else
137                   _M_word_zero._M_pword = 0;
138                 return _M_word_zero;
139               }
140             for (int __i = 0; __i < _M_word_size; __i++) 
141               __words[__i] = _M_word[__i];
142             if (_M_word && _M_word != _M_local_word) 
143               {
144                 delete [] _M_word;
145                 _M_word = 0;
146               }
147           }
148         else
149           {
150             _M_streambuf_state |= badbit;
151             if (_M_streambuf_state & _M_exception)
152               __throw_ios_failure(__N("ios_base::_M_grow_words is not valid"));
153             if (__iword)
154               _M_word_zero._M_iword = 0;
155             else
156               _M_word_zero._M_pword = 0;
157             return _M_word_zero;
158           }
159       }
160     _M_word = __words;
161     _M_word_size = __newsize;
162     return _M_word[__ix];
163   }
164
165   void 
166   ios_base::_M_call_callbacks(event __e) throw()
167   {
168     _Callback_list* __p = _M_callbacks;
169     while (__p)
170       {
171         __try 
172           { (*__p->_M_fn) (__e, *this, __p->_M_index); } 
173         __catch(...) 
174           { }
175         __p = __p->_M_next;
176       }
177   }
178
179   void 
180   ios_base::_M_dispose_callbacks(void) throw()
181   {
182     _Callback_list* __p = _M_callbacks;
183     while (__p && __p->_M_remove_reference() == 0)
184       {
185         _Callback_list* __next = __p->_M_next;
186         delete __p;
187         __p = __next;
188       }
189     _M_callbacks = 0;
190   }
191
192 _GLIBCXX_END_NAMESPACE_VERSION
193 } // namespace