Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

ios_base.h

Go to the documentation of this file.
00001 // Iostreams base classes -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 //
00031 // ISO C++ 14882: 27.8  File-based streams
00032 //
00033 
00034 #ifndef _CPP_BITS_IOSBASE_H
00035 #define _CPP_BITS_IOSBASE_H 1
00036 
00037 #pragma GCC system_header
00038 
00039 namespace std
00040 {
00041 
00042   // The following definitions of bitmask types are enums, not ints,
00043   // as permitted (but not required) in the standard, in order to provide
00044   // better type safety in iostream calls.  A side effect is that
00045   // expressions involving them are no longer compile-time constants.
00046   enum _Ios_Fmtflags { _M_ios_fmtflags_end = 1L << 16 };
00047 
00048   inline _Ios_Fmtflags 
00049   operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
00050   { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); }
00051 
00052   inline _Ios_Fmtflags 
00053   operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
00054   { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); }
00055 
00056   inline _Ios_Fmtflags 
00057   operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
00058   { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); }
00059 
00060   inline _Ios_Fmtflags 
00061   operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
00062   { return __a = __a | __b; }
00063 
00064   inline _Ios_Fmtflags 
00065   operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
00066   { return __a = __a & __b; }
00067 
00068   inline _Ios_Fmtflags 
00069   operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
00070   { return __a = __a ^ __b; }
00071 
00072   inline _Ios_Fmtflags 
00073   operator~(_Ios_Fmtflags __a)
00074   { return _Ios_Fmtflags(~static_cast<int>(__a)); }
00075 
00076 
00077   enum _Ios_Openmode { _M_ios_openmode_end = 1L << 16 };
00078 
00079   inline _Ios_Openmode 
00080   operator&(_Ios_Openmode __a, _Ios_Openmode __b)
00081   { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); }
00082 
00083   inline _Ios_Openmode 
00084   operator|(_Ios_Openmode __a, _Ios_Openmode __b)
00085   { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); }
00086 
00087   inline _Ios_Openmode 
00088   operator^(_Ios_Openmode __a, _Ios_Openmode __b)
00089   { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); }
00090 
00091   inline _Ios_Openmode 
00092   operator|=(_Ios_Openmode& __a, _Ios_Openmode __b)
00093   { return __a = __a | __b; }
00094 
00095   inline _Ios_Openmode 
00096   operator&=(_Ios_Openmode& __a, _Ios_Openmode __b)
00097   { return __a = __a & __b; }
00098 
00099   inline _Ios_Openmode 
00100   operator^=(_Ios_Openmode& __a, _Ios_Openmode __b)
00101   { return __a = __a ^ __b; }
00102 
00103   inline _Ios_Openmode 
00104   operator~(_Ios_Openmode __a)
00105   { return _Ios_Openmode(~static_cast<int>(__a)); }
00106 
00107 
00108   enum _Ios_Iostate { _M_ios_iostate_end = 1L << 16 };
00109 
00110   inline _Ios_Iostate 
00111   operator&(_Ios_Iostate __a, _Ios_Iostate __b)
00112   { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); }
00113 
00114   inline _Ios_Iostate 
00115   operator|(_Ios_Iostate __a, _Ios_Iostate __b)
00116   { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); }
00117 
00118   inline _Ios_Iostate 
00119   operator^(_Ios_Iostate __a, _Ios_Iostate __b)
00120   { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); }
00121 
00122   inline _Ios_Iostate 
00123   operator|=(_Ios_Iostate& __a, _Ios_Iostate __b)
00124   { return __a = __a | __b; }
00125 
00126   inline _Ios_Iostate 
00127   operator&=(_Ios_Iostate& __a, _Ios_Iostate __b)
00128   { return __a = __a & __b; }
00129 
00130   inline _Ios_Iostate 
00131   operator^=(_Ios_Iostate& __a, _Ios_Iostate __b)
00132   { return __a = __a ^ __b; }
00133 
00134   inline _Ios_Iostate 
00135   operator~(_Ios_Iostate __a)
00136   { return _Ios_Iostate(~static_cast<int>(__a)); }
00137 
00138   enum _Ios_Seekdir { _M_ios_seekdir_end = 1L << 16 };
00139 
00140   // 27.4.2  Class ios_base
00141   class ios_base
00142   {
00143   public:
00144     
00145     // 27.4.2.1.1  Class ios_base::failure
00146     class failure : public exception
00147     {
00148     public:
00149 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00150       // Can't do exception(_msg) as defined in 27.4.2.1.1
00151       explicit 
00152       failure(const string& __str) throw();
00153 
00154       virtual 
00155       ~failure() throw();
00156 
00157       virtual const char*
00158       what() const throw();
00159       
00160     private:
00161       enum { _M_bufsize = 256 };
00162       char _M_name[_M_bufsize];
00163 #endif
00164     };
00165 
00166     // 27.4.2.1.2  Type ios_base::fmtflags
00167     typedef _Ios_Fmtflags fmtflags;
00168     // 27.4.2.1.2  Type fmtflags
00169     static const fmtflags boolalpha =   fmtflags(__ios_flags::_S_boolalpha);
00170     static const fmtflags dec =         fmtflags(__ios_flags::_S_dec);
00171     static const fmtflags fixed =       fmtflags(__ios_flags::_S_fixed);
00172     static const fmtflags hex =         fmtflags(__ios_flags::_S_hex);
00173     static const fmtflags internal =    fmtflags(__ios_flags::_S_internal);
00174     static const fmtflags left =        fmtflags(__ios_flags::_S_left);
00175     static const fmtflags oct =         fmtflags(__ios_flags::_S_oct);
00176     static const fmtflags right =       fmtflags(__ios_flags::_S_right);
00177     static const fmtflags scientific =  fmtflags(__ios_flags::_S_scientific);
00178     static const fmtflags showbase =    fmtflags(__ios_flags::_S_showbase);
00179     static const fmtflags showpoint =   fmtflags(__ios_flags::_S_showpoint);
00180     static const fmtflags showpos =     fmtflags(__ios_flags::_S_showpos);
00181     static const fmtflags skipws =      fmtflags(__ios_flags::_S_skipws);
00182     static const fmtflags unitbuf =     fmtflags(__ios_flags::_S_unitbuf);
00183     static const fmtflags uppercase =   fmtflags(__ios_flags::_S_uppercase);
00184     static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield);
00185     static const fmtflags basefield =   fmtflags(__ios_flags::_S_basefield);
00186     static const fmtflags floatfield =  fmtflags(__ios_flags::_S_floatfield);
00187 
00188     // 27.4.2.1.3  Type ios_base::iostate
00189     typedef _Ios_Iostate iostate;
00190     static const iostate badbit =   iostate(__ios_flags::_S_badbit);
00191     static const iostate eofbit =   iostate(__ios_flags::_S_eofbit);
00192     static const iostate failbit =  iostate(__ios_flags::_S_failbit);
00193     static const iostate goodbit =  iostate(0);
00194 
00195     // 27.4.2.1.4  Type openmode
00196     typedef _Ios_Openmode openmode;
00197     static const openmode app =     openmode(__ios_flags::_S_app);
00198     static const openmode ate =     openmode(__ios_flags::_S_ate);
00199     static const openmode binary =  openmode(__ios_flags::_S_bin);
00200     static const openmode in =      openmode(__ios_flags::_S_in);
00201     static const openmode out =     openmode(__ios_flags::_S_out);
00202     static const openmode trunc =   openmode(__ios_flags::_S_trunc);
00203 
00204     // 27.4.2.1.5  Type seekdir
00205     typedef _Ios_Seekdir seekdir;
00206     static const seekdir beg =      seekdir(0);
00207     static const seekdir cur =      seekdir(SEEK_CUR);
00208     static const seekdir end =      seekdir(SEEK_END);
00209 
00210 #ifdef _GLIBCPP_DEPRECATED
00211     typedef int io_state;
00212     typedef int open_mode;
00213     typedef int seek_dir;
00214 #endif
00215 
00216     // Callbacks;
00217     enum event
00218     {
00219       erase_event,
00220       imbue_event,
00221       copyfmt_event
00222     };
00223 
00224     typedef void (*event_callback) (event, ios_base&, int);
00225 
00226     void 
00227     register_callback(event_callback __fn, int __index);
00228 
00229   protected:
00230     // Data Members
00231     streamsize      _M_precision;
00232     streamsize      _M_width;
00233     fmtflags        _M_flags;
00234 
00235     // 27.4.2.6  Members for callbacks
00236     // 27.4.2.6  ios_base callbacks
00237     struct _Callback_list
00238     {
00239       // Data Members
00240       _Callback_list*       _M_next;
00241       ios_base::event_callback  _M_fn;
00242       int           _M_index;
00243       int           _M_refcount;  // 0 means one reference.
00244     
00245       _Callback_list(ios_base::event_callback __fn, int __index, 
00246              _Callback_list* __cb)
00247       : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
00248       
00249       void 
00250       _M_add_reference() { ++_M_refcount; } // XXX MT
00251       
00252       int 
00253       _M_remove_reference() { return _M_refcount--; }  // 0 => OK to delete
00254     };
00255 
00256      _Callback_list*    _M_callbacks;
00257 
00258     void 
00259     _M_call_callbacks(event __ev) throw();
00260 
00261     void 
00262     _M_dispose_callbacks(void);
00263 
00264     // 27.4.2.5  Members for iword/pword storage
00265     struct _Words 
00266     { 
00267       void*     _M_pword; 
00268       long  _M_iword; 
00269     };
00270 
00271     static const int    _S_local_words = 8;
00272     _Words          _M_word_array[_S_local_words];  // Guaranteed storage
00273     _Words          _M_dummy;    // Only for failed iword/pword calls.
00274     _Words*         _M_words;
00275     int             _M_word_limit;
00276  
00277     _Words& 
00278     _M_grow_words(int __index);
00279 
00280     // Members for locale and locale caching.
00281     locale      _M_ios_locale;
00282 
00283     void 
00284     _M_init();
00285 
00286   public:
00287 
00288     // 27.4.2.1.6  Class ios_base::Init
00289     // Used to initialize standard streams. In theory, g++ could use
00290     // -finit-priority to order this stuff correctly without going
00291     // through these machinations. 
00292     class Init 
00293     {
00294       friend class ios_base;
00295     public:
00296       Init();
00297       ~Init();
00298       
00299       static void
00300       _S_ios_create(bool __sync);
00301       
00302       static void
00303       _S_ios_destroy();
00304 
00305     private:
00306       static int    _S_ios_base_init;
00307       static bool   _S_synced_with_stdio;
00308     };
00309 
00310     // Fmtflags state:
00311     inline fmtflags 
00312     flags() const { return _M_flags; }
00313 
00314     inline fmtflags 
00315     flags(fmtflags __fmtfl)
00316     { 
00317       fmtflags __old = _M_flags; 
00318       _M_flags = __fmtfl; 
00319       return __old; 
00320     }
00321 
00322     inline fmtflags 
00323     setf(fmtflags __fmtfl)
00324     { 
00325       fmtflags __old = _M_flags; 
00326       _M_flags |= __fmtfl; 
00327       return __old; 
00328     }
00329 
00330     inline fmtflags 
00331     setf(fmtflags __fmtfl, fmtflags __mask)
00332     {
00333       fmtflags __old = _M_flags;
00334       _M_flags &= ~__mask;
00335       _M_flags |= (__fmtfl & __mask);
00336       return __old;
00337     }
00338 
00339     inline void 
00340     unsetf(fmtflags __mask) { _M_flags &= ~__mask; }
00341 
00342     inline streamsize 
00343     precision() const { return _M_precision; }
00344 
00345     inline streamsize 
00346     precision(streamsize __prec)
00347     { 
00348       streamsize __old = _M_precision; 
00349       _M_precision = __prec; 
00350       return __old; 
00351     }
00352 
00353     inline streamsize 
00354     width() const { return _M_width; }
00355 
00356     inline streamsize 
00357     width(streamsize __wide)
00358     { 
00359       streamsize __old = _M_width; 
00360       _M_width = __wide; 
00361       return __old; 
00362     }
00363 
00364     static bool 
00365     sync_with_stdio(bool __sync = true);
00366 
00367     // Locales:
00368     locale 
00369     imbue(const locale& __loc);
00370 
00371     inline locale 
00372     getloc() const { return _M_ios_locale; }
00373 
00374     // Storage:
00375     static int 
00376     xalloc() throw();
00377 
00378     inline long& 
00379     iword(int __ix)
00380     {
00381       _Words& __word = (__ix < _M_word_limit) 
00382             ? _M_words[__ix] : _M_grow_words(__ix);
00383       return __word._M_iword;
00384     }
00385 
00386     inline void*& 
00387     pword(int __ix)
00388     {
00389       _Words& __word = (__ix < _M_word_limit) 
00390             ? _M_words[__ix] : _M_grow_words(__ix);
00391       return __word._M_pword;
00392     }
00393 
00394     // Destructor
00395     ~ios_base();
00396 
00397   protected:
00398     ios_base();
00399 
00400 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00401   private:
00402     ios_base(const ios_base&);
00403 
00404     ios_base& 
00405     operator=(const ios_base&);
00406 #endif
00407   };
00408  
00409   // 27.4.5.1 fmtflags manipulators:
00410   inline ios_base& 
00411   boolalpha(ios_base& __base)
00412   {
00413     __base.setf(ios_base::boolalpha);
00414     return __base;
00415   }
00416 
00417   inline ios_base& 
00418   noboolalpha(ios_base& __base)
00419   {
00420     __base.unsetf(ios_base::boolalpha);
00421     return __base;
00422   }
00423 
00424   inline ios_base& 
00425   showbase(ios_base& __base)
00426   {
00427     __base.setf(ios_base::showbase);
00428     return __base;
00429   }
00430 
00431   inline ios_base& 
00432   noshowbase(ios_base& __base)
00433   {
00434     __base.unsetf(ios_base::showbase);
00435     return __base;
00436   }
00437 
00438   inline ios_base& 
00439   showpoint(ios_base& __base)
00440   {
00441     __base.setf(ios_base::showpoint);
00442     return __base;
00443   }
00444 
00445   inline ios_base& 
00446   noshowpoint(ios_base& __base)
00447   {
00448     __base.unsetf(ios_base::showpoint);
00449     return __base;
00450   }
00451 
00452   inline ios_base& 
00453   showpos(ios_base& __base)
00454   {
00455     __base.setf(ios_base::showpos);
00456     return __base;
00457   }
00458 
00459   inline ios_base& 
00460   noshowpos(ios_base& __base)
00461   {
00462     __base.unsetf(ios_base::showpos);
00463     return __base;
00464   }
00465 
00466   inline ios_base& 
00467   skipws(ios_base& __base)
00468   {
00469     __base.setf(ios_base::skipws);
00470     return __base;
00471   }
00472   
00473   inline ios_base& 
00474   noskipws(ios_base& __base)
00475   {
00476     __base.unsetf(ios_base::skipws);
00477     return __base;
00478   }
00479 
00480   inline ios_base& 
00481   uppercase(ios_base& __base)
00482   {
00483     __base.setf(ios_base::uppercase);
00484     return __base;
00485   }
00486 
00487   inline ios_base& 
00488   nouppercase(ios_base& __base)
00489   {
00490     __base.unsetf(ios_base::uppercase);
00491     return __base;
00492   }
00493 
00494   inline ios_base& 
00495   unitbuf(ios_base& __base)
00496   {
00497      __base.setf(ios_base::unitbuf);      
00498      return __base;
00499   }
00500 
00501   inline ios_base& 
00502   nounitbuf(ios_base& __base)
00503   {
00504      __base.unsetf(ios_base::unitbuf);
00505      return __base;    
00506   }
00507 
00508   // 27.4.5.2 adjustfield anipulators:
00509   inline ios_base& 
00510   internal(ios_base& __base)
00511   {
00512      __base.setf(ios_base::internal, ios_base::adjustfield);
00513      return __base;    
00514   }
00515 
00516   inline ios_base& 
00517   left(ios_base& __base)
00518   {
00519     __base.setf(ios_base::left, ios_base::adjustfield);
00520     return __base;
00521   }
00522   
00523   inline ios_base& 
00524   right(ios_base& __base)
00525   {
00526     __base.setf(ios_base::right, ios_base::adjustfield);
00527     return __base;
00528   }
00529   
00530   // 27.4.5.3 basefield anipulators:
00531   inline ios_base& 
00532   dec(ios_base& __base)
00533   {
00534     __base.setf(ios_base::dec, ios_base::basefield);
00535     return __base;
00536   }
00537   
00538   inline ios_base& 
00539   hex(ios_base& __base)
00540   {
00541     __base.setf(ios_base::hex, ios_base::basefield);
00542     return __base;
00543   }
00544 
00545   inline ios_base& 
00546   oct(ios_base& __base)
00547   {
00548     __base.setf(ios_base::oct, ios_base::basefield);
00549     return __base;
00550   }
00551   
00552   // 27.4.5.4 floatfield anipulators:
00553   inline ios_base& 
00554   fixed(ios_base& __base)
00555   {
00556     __base.setf(ios_base::fixed, ios_base::floatfield);
00557     return __base;
00558   }
00559 
00560   inline ios_base& 
00561   scientific(ios_base& __base)
00562   {
00563     __base.setf(ios_base::scientific, ios_base::floatfield);
00564     return __base;
00565   }
00566 
00567 } // namespace std
00568 
00569 #endif /* _CPP_BITS_IOSBASE_H */
00570 

Generated at Tue May 1 16:28:38 2001 for libstdc++-v3 by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001