STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
basic_ios.h
Go to the documentation of this file.
1 // Iostreams base classes -*- C++ -*-
2 
3 // Copyright (C) 1997-2013 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 
16 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
30 #ifndef _BASIC_IOS_H
31 #define _BASIC_IOS_H 1
32 
33 #pragma GCC system_header
34 
35 #include <bits/localefwd.h>
36 #include <bits/locale_classes.h>
37 #include <bits/locale_facets.h>
39 
40 namespace std _GLIBCXX_VISIBILITY(default)
41 {
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 
44  template<typename _Facet>
45  inline const _Facet&
46  __check_facet(const _Facet* __f)
47  {
48  if (!__f)
49  __throw_bad_cast();
50  return *__f;
51  }
52 
65  template<typename _CharT, typename _Traits>
66  class basic_ios : public ios_base
67  {
68  public:
70 
75  typedef _CharT char_type;
76  typedef typename _Traits::int_type int_type;
77  typedef typename _Traits::pos_type pos_type;
78  typedef typename _Traits::off_type off_type;
79  typedef _Traits traits_type;
81 
83 
86  typedef ctype<_CharT> __ctype_type;
87  typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
88  __num_put_type;
89  typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
90  __num_get_type;
92 
93  // Data members:
94  protected:
95  basic_ostream<_CharT, _Traits>* _M_tie;
96  mutable char_type _M_fill;
97  mutable bool _M_fill_init;
98  basic_streambuf<_CharT, _Traits>* _M_streambuf;
99 
100  // Cached use_facet<ctype>, which is based on the current locale info.
101  const __ctype_type* _M_ctype;
102  // For ostream.
103  const __num_put_type* _M_num_put;
104  // For istream.
105  const __num_get_type* _M_num_get;
106 
107  public:
109 
115  operator void*() const
116  { return this->fail() ? 0 : const_cast<basic_ios*>(this); }
117 
118  bool
119  operator!() const
120  { return this->fail(); }
122 
130  iostate
131  rdstate() const
132  { return _M_streambuf_state; }
133 
141  void
142  clear(iostate __state = goodbit);
143 
150  void
151  setstate(iostate __state)
152  { this->clear(this->rdstate() | __state); }
153 
154  // Flip the internal state on for the proper state bits, then re
155  // throws the propagated exception if bit also set in
156  // exceptions().
157  void
158  _M_setstate(iostate __state)
159  {
160  // 27.6.1.2.1 Common requirements.
161  // Turn this on without causing an ios::failure to be thrown.
162  _M_streambuf_state |= __state;
163  if (this->exceptions() & __state)
165  }
166 
173  bool
174  good() const
175  { return this->rdstate() == 0; }
176 
183  bool
184  eof() const
185  { return (this->rdstate() & eofbit) != 0; }
186 
194  bool
195  fail() const
196  { return (this->rdstate() & (badbit | failbit)) != 0; }
197 
204  bool
205  bad() const
206  { return (this->rdstate() & badbit) != 0; }
207 
215  iostate
216  exceptions() const
217  { return _M_exception; }
218 
250  void
251  exceptions(iostate __except)
252  {
253  _M_exception = __except;
254  this->clear(_M_streambuf_state);
255  }
256 
257  // Constructor/destructor:
263  explicit
264  basic_ios(basic_streambuf<_CharT, _Traits>* __sb)
265  : ios_base(), _M_tie(0), _M_fill(), _M_fill_init(false), _M_streambuf(0),
266  _M_ctype(0), _M_num_put(0), _M_num_get(0)
267  { this->init(__sb); }
268 
275  virtual
276  ~basic_ios() { }
277 
278  // Members:
288  basic_ostream<_CharT, _Traits>*
289  tie() const
290  { return _M_tie; }
291 
300  basic_ostream<_CharT, _Traits>*
301  tie(basic_ostream<_CharT, _Traits>* __tiestr)
302  {
303  basic_ostream<_CharT, _Traits>* __old = _M_tie;
304  _M_tie = __tiestr;
305  return __old;
306  }
307 
314  basic_streambuf<_CharT, _Traits>*
315  rdbuf() const
316  { return _M_streambuf; }
317 
340  basic_streambuf<_CharT, _Traits>*
341  rdbuf(basic_streambuf<_CharT, _Traits>* __sb);
342 
354  basic_ios&
355  copyfmt(const basic_ios& __rhs);
356 
363  char_type
364  fill() const
365  {
366  if (!_M_fill_init)
367  {
368  _M_fill = this->widen(' ');
369  _M_fill_init = true;
370  }
371  return _M_fill;
372  }
373 
383  char_type
384  fill(char_type __ch)
385  {
386  char_type __old = this->fill();
387  _M_fill = __ch;
388  return __old;
389  }
390 
391  // Locales:
403  locale
404  imbue(const locale& __loc);
405 
423  char
424  narrow(char_type __c, char __dfault) const
425  { return __check_facet(_M_ctype).narrow(__c, __dfault); }
426 
442  char_type
443  widen(char __c) const
444  { return __check_facet(_M_ctype).widen(__c); }
445 
446  protected:
447  // 27.4.5.1 basic_ios constructors
454  basic_ios()
455  : ios_base(), _M_tie(0), _M_fill(char_type()), _M_fill_init(false),
456  _M_streambuf(0), _M_ctype(0), _M_num_put(0), _M_num_get(0)
457  { }
458 
465  void
466  init(basic_streambuf<_CharT, _Traits>* __sb);
467 
468  void
469  _M_cache_locale(const locale& __loc);
470  };
471 
472 _GLIBCXX_END_NAMESPACE_VERSION
473 } // namespace
474 
475 #include <bits/basic_ios.tcc>
476 
477 #endif /* _BASIC_IOS_H */
#define __throw_exception_again
Definition: exception_defines.h:37
namespace std _GLIBCXX_VISIBILITY(default)
Definition: basic_ios.h:40