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, 2004, 2005, 2006
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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.4  Iostreams base classes
00033 //
00034 
00040 #ifndef _IOS_BASE_H
00041 #define _IOS_BASE_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 
00056     { 
00057       _S_boolalpha  = 1L << 0,
00058       _S_dec        = 1L << 1,
00059       _S_fixed      = 1L << 2,
00060       _S_hex        = 1L << 3,
00061       _S_internal   = 1L << 4,
00062       _S_left       = 1L << 5,
00063       _S_oct        = 1L << 6,
00064       _S_right      = 1L << 7,
00065       _S_scientific     = 1L << 8,
00066       _S_showbase   = 1L << 9,
00067       _S_showpoint  = 1L << 10,
00068       _S_showpos    = 1L << 11,
00069       _S_skipws     = 1L << 12,
00070       _S_unitbuf    = 1L << 13,
00071       _S_uppercase  = 1L << 14,
00072       _S_adjustfield    = _S_left | _S_right | _S_internal,
00073       _S_basefield  = _S_dec | _S_oct | _S_hex,
00074       _S_floatfield     = _S_scientific | _S_fixed,
00075       _S_ios_fmtflags_end = 1L << 16 
00076     };
00077 
00078   inline _Ios_Fmtflags
00079   operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
00080   { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); }
00081 
00082   inline _Ios_Fmtflags
00083   operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
00084   { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); }
00085 
00086   inline _Ios_Fmtflags
00087   operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
00088   { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); }
00089 
00090   inline _Ios_Fmtflags&
00091   operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
00092   { return __a = __a | __b; }
00093 
00094   inline _Ios_Fmtflags&
00095   operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
00096   { return __a = __a & __b; }
00097 
00098   inline _Ios_Fmtflags&
00099   operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
00100   { return __a = __a ^ __b; }
00101 
00102   inline _Ios_Fmtflags
00103   operator~(_Ios_Fmtflags __a)
00104   { return _Ios_Fmtflags(~static_cast<int>(__a)); }
00105 
00106 
00107   enum _Ios_Openmode 
00108     { 
00109       _S_app        = 1L << 0,
00110       _S_ate        = 1L << 1,
00111       _S_bin        = 1L << 2,
00112       _S_in         = 1L << 3,
00113       _S_out        = 1L << 4,
00114       _S_trunc      = 1L << 5,
00115       _S_ios_openmode_end = 1L << 16 
00116     };
00117 
00118   inline _Ios_Openmode
00119   operator&(_Ios_Openmode __a, _Ios_Openmode __b)
00120   { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); }
00121 
00122   inline _Ios_Openmode
00123   operator|(_Ios_Openmode __a, _Ios_Openmode __b)
00124   { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); }
00125 
00126   inline _Ios_Openmode
00127   operator^(_Ios_Openmode __a, _Ios_Openmode __b)
00128   { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); }
00129 
00130   inline _Ios_Openmode&
00131   operator|=(_Ios_Openmode& __a, _Ios_Openmode __b)
00132   { return __a = __a | __b; }
00133 
00134   inline _Ios_Openmode&
00135   operator&=(_Ios_Openmode& __a, _Ios_Openmode __b)
00136   { return __a = __a & __b; }
00137 
00138   inline _Ios_Openmode&
00139   operator^=(_Ios_Openmode& __a, _Ios_Openmode __b)
00140   { return __a = __a ^ __b; }
00141 
00142   inline _Ios_Openmode
00143   operator~(_Ios_Openmode __a)
00144   { return _Ios_Openmode(~static_cast<int>(__a)); }
00145 
00146 
00147   enum _Ios_Iostate
00148     { 
00149       _S_goodbit        = 0,
00150       _S_badbit         = 1L << 0,
00151       _S_eofbit         = 1L << 1,
00152       _S_failbit        = 1L << 2,
00153       _S_ios_iostate_end = 1L << 16 
00154     };
00155 
00156   inline _Ios_Iostate
00157   operator&(_Ios_Iostate __a, _Ios_Iostate __b)
00158   { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); }
00159 
00160   inline _Ios_Iostate
00161   operator|(_Ios_Iostate __a, _Ios_Iostate __b)
00162   { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); }
00163 
00164   inline _Ios_Iostate
00165   operator^(_Ios_Iostate __a, _Ios_Iostate __b)
00166   { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); }
00167 
00168   inline _Ios_Iostate&
00169   operator|=(_Ios_Iostate& __a, _Ios_Iostate __b)
00170   { return __a = __a | __b; }
00171 
00172   inline _Ios_Iostate&
00173   operator&=(_Ios_Iostate& __a, _Ios_Iostate __b)
00174   { return __a = __a & __b; }
00175 
00176   inline _Ios_Iostate&
00177   operator^=(_Ios_Iostate& __a, _Ios_Iostate __b)
00178   { return __a = __a ^ __b; }
00179 
00180   inline _Ios_Iostate
00181   operator~(_Ios_Iostate __a)
00182   { return _Ios_Iostate(~static_cast<int>(__a)); }
00183 
00184   enum _Ios_Seekdir 
00185     { 
00186       _S_beg = 0,
00187       _S_cur = SEEK_CUR,
00188       _S_end = SEEK_END,
00189       _S_ios_seekdir_end = 1L << 16 
00190     };
00191 
00192   // 27.4.2  Class ios_base
00201   class ios_base
00202   {
00203   public:
00204 
00205     // 27.4.2.1.1  Class ios_base::failure
00207     class failure : public exception
00208     {
00209     public:
00210       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00211       // 48.  Use of non-existent exception constructor
00212       explicit
00213       failure(const string& __str) throw();
00214 
00215       // This declaration is not useless:
00216       // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
00217       virtual
00218       ~failure() throw();
00219 
00220       virtual const char*
00221       what() const throw();
00222 
00223     private:
00224       string _M_msg;
00225     };
00226 
00227     // 27.4.2.1.2  Type ios_base::fmtflags
00253     typedef _Ios_Fmtflags fmtflags;
00254 
00256     static const fmtflags boolalpha =   _S_boolalpha;
00257 
00259     static const fmtflags dec =         _S_dec;
00260 
00262     static const fmtflags fixed =       _S_fixed;
00263 
00265     static const fmtflags hex =         _S_hex;
00266 
00270     static const fmtflags internal =    _S_internal;
00271 
00274     static const fmtflags left =        _S_left;
00275 
00277     static const fmtflags oct =         _S_oct;
00278 
00281     static const fmtflags right =       _S_right;
00282 
00284     static const fmtflags scientific =  _S_scientific;
00285 
00288     static const fmtflags showbase =    _S_showbase;
00289 
00292     static const fmtflags showpoint =   _S_showpoint;
00293 
00295     static const fmtflags showpos =     _S_showpos;
00296 
00298     static const fmtflags skipws =      _S_skipws;
00299 
00301     static const fmtflags unitbuf =     _S_unitbuf;
00302 
00305     static const fmtflags uppercase =   _S_uppercase;
00306 
00308     static const fmtflags adjustfield = _S_adjustfield;
00309 
00311     static const fmtflags basefield =   _S_basefield;
00312 
00314     static const fmtflags floatfield =  _S_floatfield;
00315 
00316     // 27.4.2.1.3  Type ios_base::iostate
00328     typedef _Ios_Iostate iostate;
00329 
00332     static const iostate badbit =   _S_badbit;
00333 
00335     static const iostate eofbit =   _S_eofbit;
00336 
00340     static const iostate failbit =  _S_failbit;
00341 
00343     static const iostate goodbit =  _S_goodbit;
00344 
00345     // 27.4.2.1.4  Type ios_base::openmode
00359     typedef _Ios_Openmode openmode;
00360 
00362     static const openmode app =     _S_app;
00363 
00365     static const openmode ate =     _S_ate;
00366 
00371     static const openmode binary =  _S_bin;
00372 
00374     static const openmode in =      _S_in;
00375 
00377     static const openmode out =     _S_out;
00378 
00380     static const openmode trunc =   _S_trunc;
00381 
00382     // 27.4.2.1.5  Type ios_base::seekdir
00392     typedef _Ios_Seekdir seekdir;
00393 
00395     static const seekdir beg =      _S_beg;
00396 
00398     static const seekdir cur =      _S_cur;
00399 
00401     static const seekdir end =      _S_end;
00402 
00403     // Annex D.6
00404     typedef int io_state;
00405     typedef int open_mode;
00406     typedef int seek_dir;
00407 
00408     typedef std::streampos streampos;
00409     typedef std::streamoff streamoff;
00410 
00411     // Callbacks;
00418     enum event
00419     {
00420       erase_event,
00421       imbue_event,
00422       copyfmt_event
00423     };
00424 
00435     typedef void (*event_callback) (event, ios_base&, int);
00436 
00447     void
00448     register_callback(event_callback __fn, int __index);
00449 
00450   protected:
00452 
00457     streamsize      _M_precision;
00458     streamsize      _M_width;
00459     fmtflags        _M_flags;
00460     iostate     _M_exception;
00461     iostate     _M_streambuf_state;
00463 
00464     // 27.4.2.6  Members for callbacks
00465     // 27.4.2.6  ios_base callbacks
00466     struct _Callback_list
00467     {
00468       // Data Members
00469       _Callback_list*       _M_next;
00470       ios_base::event_callback  _M_fn;
00471       int           _M_index;
00472       _Atomic_word      _M_refcount;  // 0 means one reference.
00473 
00474       _Callback_list(ios_base::event_callback __fn, int __index,
00475              _Callback_list* __cb)
00476       : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
00477 
00478       void
00479       _M_add_reference() { __gnu_cxx::__atomic_add(&_M_refcount, 1); }
00480 
00481       // 0 => OK to delete.
00482       int
00483       _M_remove_reference() 
00484       { return __gnu_cxx::__exchange_and_add(&_M_refcount, -1); }
00485     };
00486 
00487      _Callback_list*    _M_callbacks;
00488 
00489     void
00490     _M_call_callbacks(event __ev) throw();
00491 
00492     void
00493     _M_dispose_callbacks(void);
00494 
00495     // 27.4.2.5  Members for iword/pword storage
00496     struct _Words
00497     {
00498       void* _M_pword;
00499       long  _M_iword;
00500       _Words() : _M_pword(0), _M_iword(0) { }
00501     };
00502 
00503     // Only for failed iword/pword calls.
00504     _Words      _M_word_zero;
00505 
00506     // Guaranteed storage.
00507     // The first 5 iword and pword slots are reserved for internal use.
00508     enum { _S_local_word_size = 8 };
00509     _Words      _M_local_word[_S_local_word_size];
00510 
00511     // Allocated storage.
00512     int         _M_word_size;
00513     _Words*     _M_word;
00514 
00515     _Words&
00516     _M_grow_words(int __index, bool __iword);
00517 
00518     // Members for locale and locale caching.
00519     locale      _M_ios_locale;
00520 
00521     void
00522     _M_init();
00523 
00524   public:
00525 
00526     // 27.4.2.1.6  Class ios_base::Init
00527     // Used to initialize standard streams. In theory, g++ could use
00528     // -finit-priority to order this stuff correctly without going
00529     // through these machinations.
00530     class Init
00531     {
00532       friend class ios_base;
00533     public:
00534       Init();
00535       ~Init();
00536 
00537     private:
00538       static _Atomic_word   _S_refcount;
00539       static bool       _S_synced_with_stdio;
00540     };
00541 
00542     // [27.4.2.2] fmtflags state functions
00547     inline fmtflags
00548     flags() const { return _M_flags; }
00549 
00557     inline fmtflags
00558     flags(fmtflags __fmtfl)
00559     {
00560       fmtflags __old = _M_flags;
00561       _M_flags = __fmtfl;
00562       return __old;
00563     }
00564 
00573     inline fmtflags
00574     setf(fmtflags __fmtfl)
00575     {
00576       fmtflags __old = _M_flags;
00577       _M_flags |= __fmtfl;
00578       return __old;
00579     }
00580 
00590     inline fmtflags
00591     setf(fmtflags __fmtfl, fmtflags __mask)
00592     {
00593       fmtflags __old = _M_flags;
00594       _M_flags &= ~__mask;
00595       _M_flags |= (__fmtfl & __mask);
00596       return __old;
00597     }
00598 
00605     inline void
00606     unsetf(fmtflags __mask) { _M_flags &= ~__mask; }
00607 
00617     inline streamsize
00618     precision() const { return _M_precision; }
00619 
00625     inline streamsize
00626     precision(streamsize __prec)
00627     {
00628       streamsize __old = _M_precision;
00629       _M_precision = __prec;
00630       return __old;
00631     }
00632 
00639     inline streamsize
00640     width() const { return _M_width; }
00641 
00647     inline streamsize
00648     width(streamsize __wide)
00649     {
00650       streamsize __old = _M_width;
00651       _M_width = __wide;
00652       return __old;
00653     }
00654 
00655     // [27.4.2.4] ios_base static members
00666     static bool
00667     sync_with_stdio(bool __sync = true);
00668 
00669     // [27.4.2.3] ios_base locale functions
00678     locale
00679     imbue(const locale& __loc);
00680 
00689     inline locale
00690     getloc() const { return _M_ios_locale; }
00691 
00699     inline const locale&
00700     _M_getloc() const { return _M_ios_locale; }
00701 
00702     // [27.4.2.5] ios_base storage functions
00717     static int
00718     xalloc() throw();
00719 
00733     inline long&
00734     iword(int __ix)
00735     {
00736       _Words& __word = (__ix < _M_word_size)
00737             ? _M_word[__ix] : _M_grow_words(__ix, true);
00738       return __word._M_iword;
00739     }
00740 
00754     inline void*&
00755     pword(int __ix)
00756     {
00757       _Words& __word = (__ix < _M_word_size)
00758             ? _M_word[__ix] : _M_grow_words(__ix, false);
00759       return __word._M_pword;
00760     }
00761 
00762     // Destructor
00771     virtual ~ios_base();
00772 
00773   protected:
00774     ios_base();
00775 
00776   // _GLIBCXX_RESOLVE_LIB_DEFECTS
00777   // 50.  Copy constructor and assignment operator of ios_base
00778   private:
00779     ios_base(const ios_base&);
00780 
00781     ios_base&
00782     operator=(const ios_base&);
00783   };
00784 
00785   // [27.4.5.1] fmtflags manipulators
00787   inline ios_base&
00788   boolalpha(ios_base& __base)
00789   {
00790     __base.setf(ios_base::boolalpha);
00791     return __base;
00792   }
00793 
00795   inline ios_base&
00796   noboolalpha(ios_base& __base)
00797   {
00798     __base.unsetf(ios_base::boolalpha);
00799     return __base;
00800   }
00801 
00803   inline ios_base&
00804   showbase(ios_base& __base)
00805   {
00806     __base.setf(ios_base::showbase);
00807     return __base;
00808   }
00809 
00811   inline ios_base&
00812   noshowbase(ios_base& __base)
00813   {
00814     __base.unsetf(ios_base::showbase);
00815     return __base;
00816   }
00817 
00819   inline ios_base&
00820   showpoint(ios_base& __base)
00821   {
00822     __base.setf(ios_base::showpoint);
00823     return __base;
00824   }
00825 
00827   inline ios_base&
00828   noshowpoint(ios_base& __base)
00829   {
00830     __base.unsetf(ios_base::showpoint);
00831     return __base;
00832   }
00833 
00835   inline ios_base&
00836   showpos(ios_base& __base)
00837   {
00838     __base.setf(ios_base::showpos);
00839     return __base;
00840   }
00841 
00843   inline ios_base&
00844   noshowpos(ios_base& __base)
00845   {
00846     __base.unsetf(ios_base::showpos);
00847     return __base;
00848   }
00849 
00851   inline ios_base&
00852   skipws(ios_base& __base)
00853   {
00854     __base.setf(ios_base::skipws);
00855     return __base;
00856   }
00857 
00859   inline ios_base&
00860   noskipws(ios_base& __base)
00861   {
00862     __base.unsetf(ios_base::skipws);
00863     return __base;
00864   }
00865 
00867   inline ios_base&
00868   uppercase(ios_base& __base)
00869   {
00870     __base.setf(ios_base::uppercase);
00871     return __base;
00872   }
00873 
00875   inline ios_base&
00876   nouppercase(ios_base& __base)
00877   {
00878     __base.unsetf(ios_base::uppercase);
00879     return __base;
00880   }
00881 
00883   inline ios_base&
00884   unitbuf(ios_base& __base)
00885   {
00886      __base.setf(ios_base::unitbuf);
00887      return __base;
00888   }
00889 
00891   inline ios_base&
00892   nounitbuf(ios_base& __base)
00893   {
00894      __base.unsetf(ios_base::unitbuf);
00895      return __base;
00896   }
00897 
00898   // [27.4.5.2] adjustfield anipulators
00900   inline ios_base&
00901   internal(ios_base& __base)
00902   {
00903      __base.setf(ios_base::internal, ios_base::adjustfield);
00904      return __base;
00905   }
00906 
00908   inline ios_base&
00909   left(ios_base& __base)
00910   {
00911     __base.setf(ios_base::left, ios_base::adjustfield);
00912     return __base;
00913   }
00914 
00916   inline ios_base&
00917   right(ios_base& __base)
00918   {
00919     __base.setf(ios_base::right, ios_base::adjustfield);
00920     return __base;
00921   }
00922 
00923   // [27.4.5.3] basefield anipulators
00925   inline ios_base&
00926   dec(ios_base& __base)
00927   {
00928     __base.setf(ios_base::dec, ios_base::basefield);
00929     return __base;
00930   }
00931 
00933   inline ios_base&
00934   hex(ios_base& __base)
00935   {
00936     __base.setf(ios_base::hex, ios_base::basefield);
00937     return __base;
00938   }
00939 
00941   inline ios_base&
00942   oct(ios_base& __base)
00943   {
00944     __base.setf(ios_base::oct, ios_base::basefield);
00945     return __base;
00946   }
00947 
00948   // [27.4.5.4] floatfield anipulators
00950   inline ios_base&
00951   fixed(ios_base& __base)
00952   {
00953     __base.setf(ios_base::fixed, ios_base::floatfield);
00954     return __base;
00955   }
00956 
00958   inline ios_base&
00959   scientific(ios_base& __base)
00960   {
00961     __base.setf(ios_base::scientific, ios_base::floatfield);
00962     return __base;
00963   }
00964 } // namespace std
00965 
00966 #endif /* _IOS_BASE_H */
00967 

Generated on Tue Feb 2 16:56:10 2010 for GNU C++ STL by  doxygen 1.4.7