STLdoc
STLdocumentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ios_base.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 //
31 // ISO C++ 14882: 27.4 Iostreams base classes
32 //
33 
34 #ifndef _IOS_BASE_H
35 #define _IOS_BASE_H 1
36 
37 #pragma GCC system_header
38 
39 #include <ext/atomicity.h>
40 #include <bits/localefwd.h>
41 #include <bits/locale_classes.h>
42 
43 namespace std _GLIBCXX_VISIBILITY(default)
44 {
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 
47  // The following definitions of bitmask types are enums, not ints,
48  // as permitted (but not required) in the standard, in order to provide
49  // better type safety in iostream calls. A side effect is that
50  // expressions involving them are no longer compile-time constants.
51  enum _Ios_Fmtflags
52  {
53  _S_boolalpha = 1L << 0,
54  _S_dec = 1L << 1,
55  _S_fixed = 1L << 2,
56  _S_hex = 1L << 3,
57  _S_internal = 1L << 4,
58  _S_left = 1L << 5,
59  _S_oct = 1L << 6,
60  _S_right = 1L << 7,
61  _S_scientific = 1L << 8,
62  _S_showbase = 1L << 9,
63  _S_showpoint = 1L << 10,
64  _S_showpos = 1L << 11,
65  _S_skipws = 1L << 12,
66  _S_unitbuf = 1L << 13,
67  _S_uppercase = 1L << 14,
68  _S_adjustfield = _S_left | _S_right | _S_internal,
69  _S_basefield = _S_dec | _S_oct | _S_hex,
70  _S_floatfield = _S_scientific | _S_fixed,
71  _S_ios_fmtflags_end = 1L << 16,
72  _S_ios_fmtflags_max = __INT_MAX__,
73  _S_ios_fmtflags_min = ~__INT_MAX__
74  };
75 
76  inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
77  operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
78  { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); }
79 
80  inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
81  operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
82  { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); }
83 
84  inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
85  operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
86  { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); }
87 
88  inline _GLIBCXX_CONSTEXPR _Ios_Fmtflags
89  operator~(_Ios_Fmtflags __a)
90  { return _Ios_Fmtflags(~static_cast<int>(__a)); }
91 
92  inline const _Ios_Fmtflags&
93  operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
94  { return __a = __a | __b; }
95 
96  inline const _Ios_Fmtflags&
97  operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
98  { return __a = __a & __b; }
99 
100  inline const _Ios_Fmtflags&
101  operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
102  { return __a = __a ^ __b; }
103 
104 
105  enum _Ios_Openmode
106  {
107  _S_app = 1L << 0,
108  _S_ate = 1L << 1,
109  _S_bin = 1L << 2,
110  _S_in = 1L << 3,
111  _S_out = 1L << 4,
112  _S_trunc = 1L << 5,
113  _S_ios_openmode_end = 1L << 16,
114  _S_ios_openmode_max = __INT_MAX__,
115  _S_ios_openmode_min = ~__INT_MAX__
116  };
117 
118  inline _GLIBCXX_CONSTEXPR _Ios_Openmode
119  operator&(_Ios_Openmode __a, _Ios_Openmode __b)
120  { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); }
121 
122  inline _GLIBCXX_CONSTEXPR _Ios_Openmode
123  operator|(_Ios_Openmode __a, _Ios_Openmode __b)
124  { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); }
125 
126  inline _GLIBCXX_CONSTEXPR _Ios_Openmode
127  operator^(_Ios_Openmode __a, _Ios_Openmode __b)
128  { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); }
129 
130  inline _GLIBCXX_CONSTEXPR _Ios_Openmode
131  operator~(_Ios_Openmode __a)
132  { return _Ios_Openmode(~static_cast<int>(__a)); }
133 
134  inline const _Ios_Openmode&
135  operator|=(_Ios_Openmode& __a, _Ios_Openmode __b)
136  { return __a = __a | __b; }
137 
138  inline const _Ios_Openmode&
139  operator&=(_Ios_Openmode& __a, _Ios_Openmode __b)
140  { return __a = __a & __b; }
141 
142  inline const _Ios_Openmode&
143  operator^=(_Ios_Openmode& __a, _Ios_Openmode __b)
144  { return __a = __a ^ __b; }
145 
146 
147  enum _Ios_Iostate
148  {
149  _S_goodbit = 0,
150  _S_badbit = 1L << 0,
151  _S_eofbit = 1L << 1,
152  _S_failbit = 1L << 2,
153  _S_ios_iostate_end = 1L << 16,
154  _S_ios_iostate_max = __INT_MAX__,
155  _S_ios_iostate_min = ~__INT_MAX__
156  };
157 
158  inline _GLIBCXX_CONSTEXPR _Ios_Iostate
159  operator&(_Ios_Iostate __a, _Ios_Iostate __b)
160  { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); }
161 
162  inline _GLIBCXX_CONSTEXPR _Ios_Iostate
163  operator|(_Ios_Iostate __a, _Ios_Iostate __b)
164  { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); }
165 
166  inline _GLIBCXX_CONSTEXPR _Ios_Iostate
167  operator^(_Ios_Iostate __a, _Ios_Iostate __b)
168  { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); }
169 
170  inline _GLIBCXX_CONSTEXPR _Ios_Iostate
171  operator~(_Ios_Iostate __a)
172  { return _Ios_Iostate(~static_cast<int>(__a)); }
173 
174  inline const _Ios_Iostate&
175  operator|=(_Ios_Iostate& __a, _Ios_Iostate __b)
176  { return __a = __a | __b; }
177 
178  inline const _Ios_Iostate&
179  operator&=(_Ios_Iostate& __a, _Ios_Iostate __b)
180  { return __a = __a & __b; }
181 
182  inline const _Ios_Iostate&
183  operator^=(_Ios_Iostate& __a, _Ios_Iostate __b)
184  { return __a = __a ^ __b; }
185 
186 
187  enum _Ios_Seekdir
188  {
189  _S_beg = 0,
190  _S_cur = _GLIBCXX_STDIO_SEEK_CUR,
191  _S_end = _GLIBCXX_STDIO_SEEK_END,
192  _S_ios_seekdir_end = 1L << 16
193  };
194 
195  // 27.4.2 Class ios_base
205  class ios_base
206  {
207  public:
208 
215  class failure : public exception
216  {
217  public:
218  // _GLIBCXX_RESOLVE_LIB_DEFECTS
219  // 48. Use of non-existent exception constructor
220  explicit
221  failure(const string& __str) throw();
222 
223  // This declaration is not useless:
224  // http://gcc.gnu.org/onlinedocs/gcc-4.3.2/gcc/Vague-Linkage.html
225  virtual
226  ~failure() throw();
227 
228  virtual const char*
229  what() const throw();
230 
231  private:
232  string _M_msg;
233  };
234 
235  // 27.4.2.1.2 Type ios_base::fmtflags
261  typedef _Ios_Fmtflags fmtflags;
262 
264  static const fmtflags boolalpha = _S_boolalpha;
265 
267  static const fmtflags dec = _S_dec;
268 
270  static const fmtflags fixed = _S_fixed;
271 
273  static const fmtflags hex = _S_hex;
274 
278  static const fmtflags internal = _S_internal;
279 
282  static const fmtflags left = _S_left;
283 
285  static const fmtflags oct = _S_oct;
286 
289  static const fmtflags right = _S_right;
290 
292  static const fmtflags scientific = _S_scientific;
293 
296  static const fmtflags showbase = _S_showbase;
297 
300  static const fmtflags showpoint = _S_showpoint;
301 
303  static const fmtflags showpos = _S_showpos;
304 
306  static const fmtflags skipws = _S_skipws;
307 
309  static const fmtflags unitbuf = _S_unitbuf;
310 
313  static const fmtflags uppercase = _S_uppercase;
314 
316  static const fmtflags adjustfield = _S_adjustfield;
317 
319  static const fmtflags basefield = _S_basefield;
320 
322  static const fmtflags floatfield = _S_floatfield;
323 
324  // 27.4.2.1.3 Type ios_base::iostate
336  typedef _Ios_Iostate iostate;
337 
340  static const iostate badbit = _S_badbit;
341 
343  static const iostate eofbit = _S_eofbit;
344 
348  static const iostate failbit = _S_failbit;
349 
351  static const iostate goodbit = _S_goodbit;
352 
353  // 27.4.2.1.4 Type ios_base::openmode
367  typedef _Ios_Openmode openmode;
368 
370  static const openmode app = _S_app;
371 
373  static const openmode ate = _S_ate;
374 
378  static const openmode binary = _S_bin;
379 
381  static const openmode in = _S_in;
382 
384  static const openmode out = _S_out;
385 
387  static const openmode trunc = _S_trunc;
388 
389  // 27.4.2.1.5 Type ios_base::seekdir
399  typedef _Ios_Seekdir seekdir;
400 
402  static const seekdir beg = _S_beg;
403 
405  static const seekdir cur = _S_cur;
406 
408  static const seekdir end = _S_end;
409 
410  // Annex D.6
411  typedef int io_state;
412  typedef int open_mode;
413  typedef int seek_dir;
414 
415  typedef std::streampos streampos;
416  typedef std::streamoff streamoff;
417 
418  // Callbacks;
425  enum event
426  {
427  erase_event,
428  imbue_event,
429  copyfmt_event
430  };
431 
442  typedef void (*event_callback) (event __e, ios_base& __b, int __i);
443 
454  void
455  register_callback(event_callback __fn, int __index);
456 
457  protected:
458  streamsize _M_precision;
459  streamsize _M_width;
460  fmtflags _M_flags;
461  iostate _M_exception;
462  iostate _M_streambuf_state;
463 
464  // 27.4.2.6 Members for callbacks
465  // 27.4.2.6 ios_base callbacks
466  struct _Callback_list
467  {
468  // Data Members
469  _Callback_list* _M_next;
470  ios_base::event_callback _M_fn;
471  int _M_index;
472  _Atomic_word _M_refcount; // 0 means one reference.
473 
474  _Callback_list(ios_base::event_callback __fn, int __index,
475  _Callback_list* __cb)
476  : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
477 
478  void
479  _M_add_reference() { __gnu_cxx::__atomic_add_dispatch(&_M_refcount, 1); }
480 
481  // 0 => OK to delete.
482  int
483  _M_remove_reference()
484  {
485  // Be race-detector-friendly. For more info see bits/c++config.
486  _GLIBCXX_SYNCHRONIZATION_HAPPENS_BEFORE(&_M_refcount);
487  int __res = __gnu_cxx::__exchange_and_add_dispatch(&_M_refcount, -1);
488  if (__res == 0)
489  {
490  _GLIBCXX_SYNCHRONIZATION_HAPPENS_AFTER(&_M_refcount);
491  }
492  return __res;
493  }
494  };
495 
496  _Callback_list* _M_callbacks;
497 
498  void
499  _M_call_callbacks(event __ev) throw();
500 
501  void
502  _M_dispose_callbacks(void) throw();
503 
504  // 27.4.2.5 Members for iword/pword storage
505  struct _Words
506  {
507  void* _M_pword;
508  long _M_iword;
509  _Words() : _M_pword(0), _M_iword(0) { }
510  };
511 
512  // Only for failed iword/pword calls.
513  _Words _M_word_zero;
514 
515  // Guaranteed storage.
516  // The first 5 iword and pword slots are reserved for internal use.
517  enum { _S_local_word_size = 8 };
518  _Words _M_local_word[_S_local_word_size];
519 
520  // Allocated storage.
521  int _M_word_size;
522  _Words* _M_word;
523 
524  _Words&
525  _M_grow_words(int __index, bool __iword);
526 
527  // Members for locale and locale caching.
528  locale _M_ios_locale;
529 
530  void
531  _M_init() throw();
532 
533  public:
534 
535  // 27.4.2.1.6 Class ios_base::Init
536  // Used to initialize standard streams. In theory, g++ could use
537  // -finit-priority to order this stuff correctly without going
538  // through these machinations.
539  class Init
540  {
541  friend class ios_base;
542  public:
543  Init();
544  ~Init();
545 
546  private:
547  static _Atomic_word _S_refcount;
548  static bool _S_synced_with_stdio;
549  };
550 
551  // [27.4.2.2] fmtflags state functions
556  fmtflags
557  flags() const
558  { return _M_flags; }
559 
567  fmtflags
568  flags(fmtflags __fmtfl)
569  {
570  fmtflags __old = _M_flags;
571  _M_flags = __fmtfl;
572  return __old;
573  }
574 
583  fmtflags
584  setf(fmtflags __fmtfl)
585  {
586  fmtflags __old = _M_flags;
587  _M_flags |= __fmtfl;
588  return __old;
589  }
590 
600  fmtflags
601  setf(fmtflags __fmtfl, fmtflags __mask)
602  {
603  fmtflags __old = _M_flags;
604  _M_flags &= ~__mask;
605  _M_flags |= (__fmtfl & __mask);
606  return __old;
607  }
608 
615  void
616  unsetf(fmtflags __mask)
617  { _M_flags &= ~__mask; }
618 
626  streamsize
627  precision() const
628  { return _M_precision; }
629 
635  streamsize
636  precision(streamsize __prec)
637  {
638  streamsize __old = _M_precision;
639  _M_precision = __prec;
640  return __old;
641  }
642 
649  streamsize
650  width() const
651  { return _M_width; }
652 
658  streamsize
659  width(streamsize __wide)
660  {
661  streamsize __old = _M_width;
662  _M_width = __wide;
663  return __old;
664  }
665 
666  // [27.4.2.4] ios_base static members
677  static bool
678  sync_with_stdio(bool __sync = true);
679 
680  // [27.4.2.3] ios_base locale functions
689  locale
690  imbue(const locale& __loc) throw();
691 
700  locale
701  getloc() const
702  { return _M_ios_locale; }
703 
711  const locale&
712  _M_getloc() const
713  { return _M_ios_locale; }
714 
715  // [27.4.2.5] ios_base storage functions
730  static int
731  xalloc() throw();
732 
746  long&
747  iword(int __ix)
748  {
749  _Words& __word = (__ix < _M_word_size)
750  ? _M_word[__ix] : _M_grow_words(__ix, true);
751  return __word._M_iword;
752  }
753 
767  void*&
768  pword(int __ix)
769  {
770  _Words& __word = (__ix < _M_word_size)
771  ? _M_word[__ix] : _M_grow_words(__ix, false);
772  return __word._M_pword;
773  }
774 
775  // Destructor
784  virtual ~ios_base();
785 
786  protected:
787  ios_base() throw ();
788 
789  // _GLIBCXX_RESOLVE_LIB_DEFECTS
790  // 50. Copy constructor and assignment operator of ios_base
791  private:
792  ios_base(const ios_base&);
793 
794  ios_base&
795  operator=(const ios_base&);
796  };
797 
798  // [27.4.5.1] fmtflags manipulators
800  inline ios_base&
801  boolalpha(ios_base& __base)
802  {
803  __base.setf(ios_base::boolalpha);
804  return __base;
805  }
806 
808  inline ios_base&
809  noboolalpha(ios_base& __base)
810  {
811  __base.unsetf(ios_base::boolalpha);
812  return __base;
813  }
814 
816  inline ios_base&
817  showbase(ios_base& __base)
818  {
819  __base.setf(ios_base::showbase);
820  return __base;
821  }
822 
824  inline ios_base&
825  noshowbase(ios_base& __base)
826  {
827  __base.unsetf(ios_base::showbase);
828  return __base;
829  }
830 
832  inline ios_base&
833  showpoint(ios_base& __base)
834  {
835  __base.setf(ios_base::showpoint);
836  return __base;
837  }
838 
840  inline ios_base&
841  noshowpoint(ios_base& __base)
842  {
843  __base.unsetf(ios_base::showpoint);
844  return __base;
845  }
846 
848  inline ios_base&
849  showpos(ios_base& __base)
850  {
851  __base.setf(ios_base::showpos);
852  return __base;
853  }
854 
856  inline ios_base&
857  noshowpos(ios_base& __base)
858  {
859  __base.unsetf(ios_base::showpos);
860  return __base;
861  }
862 
864  inline ios_base&
865  skipws(ios_base& __base)
866  {
867  __base.setf(ios_base::skipws);
868  return __base;
869  }
870 
872  inline ios_base&
873  noskipws(ios_base& __base)
874  {
875  __base.unsetf(ios_base::skipws);
876  return __base;
877  }
878 
880  inline ios_base&
881  uppercase(ios_base& __base)
882  {
883  __base.setf(ios_base::uppercase);
884  return __base;
885  }
886 
888  inline ios_base&
889  nouppercase(ios_base& __base)
890  {
891  __base.unsetf(ios_base::uppercase);
892  return __base;
893  }
894 
896  inline ios_base&
897  unitbuf(ios_base& __base)
898  {
899  __base.setf(ios_base::unitbuf);
900  return __base;
901  }
902 
904  inline ios_base&
905  nounitbuf(ios_base& __base)
906  {
907  __base.unsetf(ios_base::unitbuf);
908  return __base;
909  }
910 
911  // [27.4.5.2] adjustfield manipulators
913  inline ios_base&
914  internal(ios_base& __base)
915  {
916  __base.setf(ios_base::internal, ios_base::adjustfield);
917  return __base;
918  }
919 
921  inline ios_base&
922  left(ios_base& __base)
923  {
924  __base.setf(ios_base::left, ios_base::adjustfield);
925  return __base;
926  }
927 
929  inline ios_base&
930  right(ios_base& __base)
931  {
932  __base.setf(ios_base::right, ios_base::adjustfield);
933  return __base;
934  }
935 
936  // [27.4.5.3] basefield manipulators
938  inline ios_base&
939  dec(ios_base& __base)
940  {
941  __base.setf(ios_base::dec, ios_base::basefield);
942  return __base;
943  }
944 
946  inline ios_base&
947  hex(ios_base& __base)
948  {
949  __base.setf(ios_base::hex, ios_base::basefield);
950  return __base;
951  }
952 
954  inline ios_base&
955  oct(ios_base& __base)
956  {
957  __base.setf(ios_base::oct, ios_base::basefield);
958  return __base;
959  }
960 
961  // [27.4.5.4] floatfield manipulators
963  inline ios_base&
964  fixed(ios_base& __base)
965  {
966  __base.setf(ios_base::fixed, ios_base::floatfield);
967  return __base;
968  }
969 
971  inline ios_base&
972  scientific(ios_base& __base)
973  {
974  __base.setf(ios_base::scientific, ios_base::floatfield);
975  return __base;
976  }
977 
978 _GLIBCXX_END_NAMESPACE_VERSION
979 } // namespace
980 
981 #endif /* _IOS_BASE_H */
_Siter_base< _Iterator >::iterator_type __base(_Iterator __it)
Definition: functions.h:446
namespace std _GLIBCXX_VISIBILITY(default)
Definition: auto_ptr.h:36