# ios_base.h

Go to the documentation of this file.

    00001 // Iostreams base classes -*- C++ -*-
    00002
    00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
    00004 // Free Software Foundation, Inc.
    00005 //
    00006 // This file is part of the GNU ISO C++ Library.  This library is free
    00007 // software; you can redistribute it and/or modify it under the
    00008 // terms of the GNU General Public License as published by the
    00009 // Free Software Foundation; either version 2, or (at your option)
    00010 // any later version.
    00011
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00015 // GNU General Public License for more details.
    00016
    00017 // You should have received a copy of the GNU General Public License along
    00018 // with this library; see the file COPYING.  If not, write to the Free
    00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00020 // USA.
    00021
    00022 // As a special exception, you may use this file as part of a free software
    00023 // library without restriction.  Specifically, if other files instantiate
    00024 // templates or use macros or inline functions from this file, or you compile
    00025 // this file and link it with other files to produce an executable, this
    00026 // file does not by itself cause the resulting executable to be covered by
    00027 // the GNU General Public License.  This exception does not however
    00028 // invalidate any other reasons why the executable file might be covered by
    00029 // the GNU General Public License.
    00030
    00031 //
    00032 // ISO C++ 14882: 27.8  File-based streams
    00033 //
    00034
    00040 #ifndef _CPP_BITS_IOSBASE_H
    00041 #define _CPP_BITS_IOSBASE_H 1
    00042
    00043 #pragma GCC system_header
    00044
    00045 #include <bits/atomicity.h>
    00046 #include <bits/localefwd.h>
    00047 #include <bits/locale_classes.h>
    00048
    00049 namespace std
    00050 {
    00051   // The following definitions of bitmask types are enums, not ints,
    00052   // as permitted (but not required) in the standard, in order to provide
    00053   // better type safety in iostream calls.  A side effect is that
    00054   // expressions involving them are no longer compile-time constants.
    00055   enum _Ios_Fmtflags { _M_ios_fmtflags_end = 1L << 16 };
    00056
    00057   inline _Ios_Fmtflags
    00058   operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
    00059   { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); }
    00060
    00061   inline _Ios_Fmtflags
    00062   operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
    00063   { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); }
    00064
    00065   inline _Ios_Fmtflags
    00066   operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
    00067   { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); }
    00068
    00069   inline _Ios_Fmtflags
    00070   operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
    00071   { return __a = __a | __b; }
    00072
    00073   inline _Ios_Fmtflags
    00074   operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
    00075   { return __a = __a & __b; }
    00076
    00077   inline _Ios_Fmtflags
    00078   operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
    00079   { return __a = __a ^ __b; }
    00080
    00081   inline _Ios_Fmtflags
    00082   operator~(_Ios_Fmtflags __a)
    00083   { return _Ios_Fmtflags(~static_cast<int>(__a)); }
    00084
    00085
    00086   enum _Ios_Openmode { _M_ios_openmode_end = 1L << 16 };
    00087
    00088   inline _Ios_Openmode
    00089   operator&(_Ios_Openmode __a, _Ios_Openmode __b)
    00090   { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); }
    00091
    00092   inline _Ios_Openmode
    00093   operator|(_Ios_Openmode __a, _Ios_Openmode __b)
    00094   { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); }
    00095
    00096   inline _Ios_Openmode
    00097   operator^(_Ios_Openmode __a, _Ios_Openmode __b)
    00098   { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); }
    00099
    00100   inline _Ios_Openmode
    00101   operator|=(_Ios_Openmode& __a, _Ios_Openmode __b)
    00102   { return __a = __a | __b; }
    00103
    00104   inline _Ios_Openmode
    00105   operator&=(_Ios_Openmode& __a, _Ios_Openmode __b)
    00106   { return __a = __a & __b; }
    00107
    00108   inline _Ios_Openmode
    00109   operator^=(_Ios_Openmode& __a, _Ios_Openmode __b)
    00110   { return __a = __a ^ __b; }
    00111
    00112   inline _Ios_Openmode
    00113   operator~(_Ios_Openmode __a)
    00114   { return _Ios_Openmode(~static_cast<int>(__a)); }
    00115
    00116
    00117   enum _Ios_Iostate { _M_ios_iostate_end = 1L << 16 };
    00118
    00119   inline _Ios_Iostate
    00120   operator&(_Ios_Iostate __a, _Ios_Iostate __b)
    00121   { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); }
    00122
    00123   inline _Ios_Iostate
    00124   operator|(_Ios_Iostate __a, _Ios_Iostate __b)
    00125   { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); }
    00126
    00127   inline _Ios_Iostate
    00128   operator^(_Ios_Iostate __a, _Ios_Iostate __b)
    00129   { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); }
    00130
    00131   inline _Ios_Iostate
    00132   operator|=(_Ios_Iostate& __a, _Ios_Iostate __b)
    00133   { return __a = __a | __b; }
    00134
    00135   inline _Ios_Iostate
    00136   operator&=(_Ios_Iostate& __a, _Ios_Iostate __b)
    00137   { return __a = __a & __b; }
    00138
    00139   inline _Ios_Iostate
    00140   operator^=(_Ios_Iostate& __a, _Ios_Iostate __b)
    00141   { return __a = __a ^ __b; }
    00142
    00143   inline _Ios_Iostate
    00144   operator~(_Ios_Iostate __a)
    00145   { return _Ios_Iostate(~static_cast<int>(__a)); }
    00146
    00147   enum _Ios_Seekdir { _M_ios_seekdir_end = 1L << 16 };
    00148
    00149   class __locale_cache_base;
    00150
    00151   // 27.4.2  Class ios_base
    00160   class ios_base
    00161   {
    00162   public:
    00163
    00164     // 27.4.2.1.1  Class ios_base::failure
    00166     class failure : public exception
    00167     {
    00168     public:
    00169 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00170       //48.  Use of non-existent exception constructor
    00171       explicit
    00172       failure(const string& __str) throw();
    00173
    00174       // This declaration is not useless:
    00175       // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
    00176       virtual
    00177       ~failure() throw();
    00178
    00179       virtual const char*
    00180       what() const throw();
    00181
    00182     private:
    00183       enum { _M_bufsize = 256 };
    00184       char _M_name[_M_bufsize];
    00185 #endif
    00186     };
    00187
    00188     // 27.4.2.1.2  Type ios_base::fmtflags
    00214     typedef _Ios_Fmtflags fmtflags;
    00216     static const fmtflags boolalpha =   fmtflags(__ios_flags::_S_boolalpha);
    00218     static const fmtflags dec =         fmtflags(__ios_flags::_S_dec);
    00220     static const fmtflags fixed =       fmtflags(__ios_flags::_S_fixed);
    00222     static const fmtflags hex =         fmtflags(__ios_flags::_S_hex);
    00226     static const fmtflags internal =    fmtflags(__ios_flags::_S_internal);
    00229     static const fmtflags left =        fmtflags(__ios_flags::_S_left);
    00231     static const fmtflags oct =         fmtflags(__ios_flags::_S_oct);
    00234     static const fmtflags right =       fmtflags(__ios_flags::_S_right);
    00236     static const fmtflags scientific =  fmtflags(__ios_flags::_S_scientific);
    00239     static const fmtflags showbase =    fmtflags(__ios_flags::_S_showbase);
    00242     static const fmtflags showpoint =   fmtflags(__ios_flags::_S_showpoint);
    00244     static const fmtflags showpos =     fmtflags(__ios_flags::_S_showpos);
    00246     static const fmtflags skipws =      fmtflags(__ios_flags::_S_skipws);
    00248     static const fmtflags unitbuf =     fmtflags(__ios_flags::_S_unitbuf);
    00251     static const fmtflags uppercase =   fmtflags(__ios_flags::_S_uppercase);
    00253     static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield);
    00255     static const fmtflags basefield =   fmtflags(__ios_flags::_S_basefield);
    00257     static const fmtflags floatfield =  fmtflags(__ios_flags::_S_floatfield);
    00258
    00259     // 27.4.2.1.3  Type ios_base::iostate
    00271     typedef _Ios_Iostate iostate;
    00274     static const iostate badbit =   iostate(__ios_flags::_S_badbit);
    00276     static const iostate eofbit =   iostate(__ios_flags::_S_eofbit);
    00280     static const iostate failbit =  iostate(__ios_flags::_S_failbit);
    00282     static const iostate goodbit =  iostate(0);
    00283
    00284     // 27.4.2.1.4  Type ios_base::openmode
    00298     typedef _Ios_Openmode openmode;
    00300     static const openmode app =     openmode(__ios_flags::_S_app);
    00302     static const openmode ate =     openmode(__ios_flags::_S_ate);
    00307     static const openmode binary =  openmode(__ios_flags::_S_bin);
    00309     static const openmode in =      openmode(__ios_flags::_S_in);
    00311     static const openmode out =     openmode(__ios_flags::_S_out);
    00313     static const openmode trunc =   openmode(__ios_flags::_S_trunc);
    00314
    00315     // 27.4.2.1.5  Type ios_base::seekdir
    00325     typedef _Ios_Seekdir seekdir;
    00327     static const seekdir beg =      seekdir(0);
    00329     static const seekdir cur =      seekdir(SEEK_CUR);
    00331     static const seekdir end =      seekdir(SEEK_END);
    00332
    00333 #ifdef _GLIBCPP_DEPRECATED
    00334     // Annex D.6
    00335     typedef int io_state;
    00336     typedef int open_mode;
    00337     typedef int seek_dir;
    00338
    00339     typedef std::streampos streampos;
    00340     typedef std::streamoff streamoff;
    00341 #endif
    00342
    00343     // Callbacks;
    00347     enum event
    00348     {
    00349       erase_event,
    00350       imbue_event,
    00351       copyfmt_event
    00352     };
    00353
    00357     typedef void (*event_callback) (event, ios_base&, int);
    00358
    00362     void
    00363     register_callback(event_callback __fn, int __index);
    00364
    00365   protected:
    00367
    00372     streamsize      _M_precision;
    00373     streamsize      _M_width;
    00374     fmtflags        _M_flags;
    00375     iostate         _M_exception;
    00376     iostate             _M_streambuf_state;
    00378
    00379     // 27.4.2.6  Members for callbacks
    00380     // 27.4.2.6  ios_base callbacks
    00381     struct _Callback_list
    00382     {
    00383       // Data Members
    00384       _Callback_list*       _M_next;
    00385       ios_base::event_callback  _M_fn;
    00386       int           _M_index;
    00387       _Atomic_word      _M_refcount;  // 0 means one reference.
    00388
    00389       _Callback_list(ios_base::event_callback __fn, int __index,
    00390              _Callback_list* __cb)
    00391       : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
    00392
    00393       void
    00394       _M_add_reference() { __atomic_add(&_M_refcount, 1); }
    00395
    00396       // 0 => OK to delete.
    00397       int
    00398       _M_remove_reference() { return __exchange_and_add(&_M_refcount, -1); }
    00399     };
    00400
    00401      _Callback_list*    _M_callbacks;
    00402
    00403     void
    00404     _M_call_callbacks(event __ev) throw();
    00405
    00406     void
    00407     _M_dispose_callbacks(void);
    00408
    00409     // 27.4.2.5  Members for iword/pword storage
    00410     struct _Words
    00411     {
    00412       void*     _M_pword;
    00413       long  _M_iword;
    00414       _Words() : _M_pword(0), _M_iword(0) { }
    00415     };
    00416
    00417     // Only for failed iword/pword calls.
    00418     _Words          _M_word_zero;
    00419
    00420     // Guaranteed storage.
    00421     // The first 5 iword and pword slots are reserved for internal use.
    00422     static const int    _S_local_word_size = 8;
    00423     _Words          _M_local_word[_S_local_word_size];
    00424
    00425     // Allocated storage.
    00426     int             _M_word_size;
    00427     _Words*         _M_word;
    00428
    00429     _Words&
    00430     _M_grow_words(int __index);
    00431
    00432     // Members for locale and locale caching.
    00433     locale      _M_ios_locale;
    00434
    00435     void
    00436     _M_init();
    00437
    00438   public:
    00439
    00440     // 27.4.2.1.6  Class ios_base::Init
    00441     // Used to initialize standard streams. In theory, g++ could use
    00442     // -finit-priority to order this stuff correctly without going
    00443     // through these machinations.
    00444     class Init
    00445     {
    00446       friend class ios_base;
    00447     public:
    00448       Init();
    00449       ~Init();
    00450
    00451       static void
    00452       _S_ios_create(bool __sync);
    00453
    00454       static void
    00455       _S_ios_destroy();
    00456
    00457       // NB: Allows debugger applications use of the standard streams
    00458       // from operator new. _S_ios_base_init must be incremented in
    00459       // _S_ios_create _after_ initialization is completed.
    00460       static bool
    00461       _S_initialized() { return _S_ios_base_init; }
    00462
    00463     private:
    00464       static int    _S_ios_base_init;
    00465       static bool   _S_synced_with_stdio;
    00466     };
    00467
    00468     // [27.4.2.2] fmtflags state functions
    00473     inline fmtflags
    00474     flags() const { return _M_flags; }
    00475
    00483     inline fmtflags
    00484     flags(fmtflags __fmtfl)
    00485     {
    00486       fmtflags __old = _M_flags;
    00487       _M_flags = __fmtfl;
    00488       return __old;
    00489     }
    00490
    00499     inline fmtflags
    00500     setf(fmtflags __fmtfl)
    00501     {
    00502       fmtflags __old = _M_flags;
    00503       _M_flags |= __fmtfl;
    00504       return __old;
    00505     }
    00506
    00516     inline fmtflags
    00517     setf(fmtflags __fmtfl, fmtflags __mask)
    00518     {
    00519       fmtflags __old = _M_flags;
    00520       _M_flags &= ~__mask;
    00521       _M_flags |= (__fmtfl & __mask);
    00522       return __old;
    00523     }
    00524
    00531     inline void
    00532     unsetf(fmtflags __mask) { _M_flags &= ~__mask; }
    00533
    00543     inline streamsize
    00544     precision() const { return _M_precision; }
    00545
    00551     inline streamsize
    00552     precision(streamsize __prec)
    00553     {
    00554       streamsize __old = _M_precision;
    00555       _M_precision = __prec;
    00556       return __old;
    00557     }
    00558
    00565     inline streamsize
    00566     width() const { return _M_width; }
    00567
    00573     inline streamsize
    00574     width(streamsize __wide)
    00575     {
    00576       streamsize __old = _M_width;
    00577       _M_width = __wide;
    00578       return __old;
    00579     }
    00580
    00581     // [27.4.2.4] ios_base static members
    00592     static bool
    00593     sync_with_stdio(bool __sync = true);
    00594
    00595     // [27.4.2.3] ios_base locale functions
    00604     locale
    00605     imbue(const locale& __loc);
    00606
    00615     inline locale
    00616     getloc() const { return _M_ios_locale; }
    00617
    00618     // [27.4.2.5] ios_base storage functions
    00622     static int
    00623     xalloc() throw();
    00624
    00628     inline long&
    00629     iword(int __ix)
    00630     {
    00631       _Words& __word = (__ix < _M_word_size)
    00632             ? _M_word[__ix] : _M_grow_words(__ix);
    00633       return __word._M_iword;
    00634     }
    00635
    00639     inline void*&
    00640     pword(int __ix)
    00641     {
    00642       _Words& __word = (__ix < _M_word_size)
    00643             ? _M_word[__ix] : _M_grow_words(__ix);
    00644       return __word._M_pword;
    00645     }
    00646
    00647     // Access to the cache.  Not safe to call until basic_ios::_M_init() has
    00648     // happened.
    00649     __locale_cache_base&
    00650     _M_cache() { return *static_cast<__locale_cache_base*>(pword(0)); }
    00651
    00652     // Destructor
    00657     ~ios_base();
    00658
    00659   protected:
    00660     ios_base();
    00661
    00662 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00663   //50.  Copy constructor and assignment operator of ios_base
    00664   private:
    00665     ios_base(const ios_base&);
    00666
    00667     ios_base&
    00668     operator=(const ios_base&);
    00669 #endif
    00670   };
    00671
    00672   // [27.4.5.1] fmtflags manipulators
    00674   inline ios_base&
    00675   boolalpha(ios_base& __base)
    00676   {
    00677     __base.setf(ios_base::boolalpha);
    00678     return __base;
    00679   }
    00680
    00682   inline ios_base&
    00683   noboolalpha(ios_base& __base)
    00684   {
    00685     __base.unsetf(ios_base::boolalpha);
    00686     return __base;
    00687   }
    00688
    00690   inline ios_base&
    00691   showbase(ios_base& __base)
    00692   {
    00693     __base.setf(ios_base::showbase);
    00694     return __base;
    00695   }
    00696
    00698   inline ios_base&
    00699   noshowbase(ios_base& __base)
    00700   {
    00701     __base.unsetf(ios_base::showbase);
    00702     return __base;
    00703   }
    00704
    00706   inline ios_base&
    00707   showpoint(ios_base& __base)
    00708   {
    00709     __base.setf(ios_base::showpoint);
    00710     return __base;
    00711   }
    00712
    00714   inline ios_base&
    00715   noshowpoint(ios_base& __base)
    00716   {
    00717     __base.unsetf(ios_base::showpoint);
    00718     return __base;
    00719   }
    00720
    00722   inline ios_base&
    00723   showpos(ios_base& __base)
    00724   {
    00725     __base.setf(ios_base::showpos);
    00726     return __base;
    00727   }
    00728
    00730   inline ios_base&
    00731   noshowpos(ios_base& __base)
    00732   {
    00733     __base.unsetf(ios_base::showpos);
    00734     return __base;
    00735   }
    00736
    00738   inline ios_base&
    00739   skipws(ios_base& __base)
    00740   {
    00741     __base.setf(ios_base::skipws);
    00742     return __base;
    00743   }
    00744
    00746   inline ios_base&
    00747   noskipws(ios_base& __base)
    00748   {
    00749     __base.unsetf(ios_base::skipws);
    00750     return __base;
    00751   }
    00752
    00754   inline ios_base&
    00755   uppercase(ios_base& __base)
    00756   {
    00757     __base.setf(ios_base::uppercase);
    00758     return __base;
    00759   }
    00760
    00762   inline ios_base&
    00763   nouppercase(ios_base& __base)
    00764   {
    00765     __base.unsetf(ios_base::uppercase);
    00766     return __base;
    00767   }
    00768
    00770   inline ios_base&
    00771   unitbuf(ios_base& __base)
    00772   {
    00773      __base.setf(ios_base::unitbuf);
    00774      return __base;
    00775   }
    00776
    00778   inline ios_base&
    00779   nounitbuf(ios_base& __base)
    00780   {
    00781      __base.unsetf(ios_base::unitbuf);
    00782      return __base;
    00783   }
    00784
    00785   // [27.4.5.2] adjustfield anipulators
    00787   inline ios_base&
    00788   internal(ios_base& __base)
    00789   {
    00790      __base.setf(ios_base::internal, ios_base::adjustfield);
    00791      return __base;
    00792   }
    00793
    00795   inline ios_base&
    00796   left(ios_base& __base)
    00797   {
    00798     __base.setf(ios_base::left, ios_base::adjustfield);
    00799     return __base;
    00800   }
    00801
    00803   inline ios_base&
    00804   right(ios_base& __base)
    00805   {
    00806     __base.setf(ios_base::right, ios_base::adjustfield);
    00807     return __base;
    00808   }
    00809
    00810   // [27.4.5.3] basefield anipulators
    00812   inline ios_base&
    00813   dec(ios_base& __base)
    00814   {
    00815     __base.setf(ios_base::dec, ios_base::basefield);
    00816     return __base;
    00817   }
    00818
    00820   inline ios_base&
    00821   hex(ios_base& __base)
    00822   {
    00823     __base.setf(ios_base::hex, ios_base::basefield);
    00824     return __base;
    00825   }
    00826
    00828   inline ios_base&
    00829   oct(ios_base& __base)
    00830   {
    00831     __base.setf(ios_base::oct, ios_base::basefield);
    00832     return __base;
    00833   }
    00834
    00835   // [27.4.5.4] floatfield anipulators
    00837   inline ios_base&
    00838   fixed(ios_base& __base)
    00839   {
    00840     __base.setf(ios_base::fixed, ios_base::floatfield);
    00841     return __base;
    00842   }
    00843
    00845   inline ios_base&
    00846   scientific(ios_base& __base)
    00847   {
    00848     __base.setf(ios_base::scientific, ios_base::floatfield);
    00849     return __base;
    00850   }
    00851
    00852 } // namespace std
    00853
    00854 #endif /* _CPP_BITS_IOSBASE_H */
    00855