# 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
    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
    00047 namespace std
    00048 {
    00049   // The following definitions of bitmask types are enums, not ints,
    00050   // as permitted (but not required) in the standard, in order to provide
    00051   // better type safety in iostream calls.  A side effect is that
    00052   // expressions involving them are no longer compile-time constants.
    00053   enum _Ios_Fmtflags { _M_ios_fmtflags_end = 1L << 16 };
    00054
    00055   inline _Ios_Fmtflags
    00056   operator&(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
    00057   { return _Ios_Fmtflags(static_cast<int>(__a) & static_cast<int>(__b)); }
    00058
    00059   inline _Ios_Fmtflags
    00060   operator|(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
    00061   { return _Ios_Fmtflags(static_cast<int>(__a) | static_cast<int>(__b)); }
    00062
    00063   inline _Ios_Fmtflags
    00064   operator^(_Ios_Fmtflags __a, _Ios_Fmtflags __b)
    00065   { return _Ios_Fmtflags(static_cast<int>(__a) ^ static_cast<int>(__b)); }
    00066
    00067   inline _Ios_Fmtflags
    00068   operator|=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
    00069   { return __a = __a | __b; }
    00070
    00071   inline _Ios_Fmtflags
    00072   operator&=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
    00073   { return __a = __a & __b; }
    00074
    00075   inline _Ios_Fmtflags
    00076   operator^=(_Ios_Fmtflags& __a, _Ios_Fmtflags __b)
    00077   { return __a = __a ^ __b; }
    00078
    00079   inline _Ios_Fmtflags
    00080   operator~(_Ios_Fmtflags __a)
    00081   { return _Ios_Fmtflags(~static_cast<int>(__a)); }
    00082
    00083
    00084   enum _Ios_Openmode { _M_ios_openmode_end = 1L << 16 };
    00085
    00086   inline _Ios_Openmode
    00087   operator&(_Ios_Openmode __a, _Ios_Openmode __b)
    00088   { return _Ios_Openmode(static_cast<int>(__a) & static_cast<int>(__b)); }
    00089
    00090   inline _Ios_Openmode
    00091   operator|(_Ios_Openmode __a, _Ios_Openmode __b)
    00092   { return _Ios_Openmode(static_cast<int>(__a) | static_cast<int>(__b)); }
    00093
    00094   inline _Ios_Openmode
    00095   operator^(_Ios_Openmode __a, _Ios_Openmode __b)
    00096   { return _Ios_Openmode(static_cast<int>(__a) ^ static_cast<int>(__b)); }
    00097
    00098   inline _Ios_Openmode
    00099   operator|=(_Ios_Openmode& __a, _Ios_Openmode __b)
    00100   { return __a = __a | __b; }
    00101
    00102   inline _Ios_Openmode
    00103   operator&=(_Ios_Openmode& __a, _Ios_Openmode __b)
    00104   { return __a = __a & __b; }
    00105
    00106   inline _Ios_Openmode
    00107   operator^=(_Ios_Openmode& __a, _Ios_Openmode __b)
    00108   { return __a = __a ^ __b; }
    00109
    00110   inline _Ios_Openmode
    00111   operator~(_Ios_Openmode __a)
    00112   { return _Ios_Openmode(~static_cast<int>(__a)); }
    00113
    00114
    00115   enum _Ios_Iostate { _M_ios_iostate_end = 1L << 16 };
    00116
    00117   inline _Ios_Iostate
    00118   operator&(_Ios_Iostate __a, _Ios_Iostate __b)
    00119   { return _Ios_Iostate(static_cast<int>(__a) & static_cast<int>(__b)); }
    00120
    00121   inline _Ios_Iostate
    00122   operator|(_Ios_Iostate __a, _Ios_Iostate __b)
    00123   { return _Ios_Iostate(static_cast<int>(__a) | static_cast<int>(__b)); }
    00124
    00125   inline _Ios_Iostate
    00126   operator^(_Ios_Iostate __a, _Ios_Iostate __b)
    00127   { return _Ios_Iostate(static_cast<int>(__a) ^ static_cast<int>(__b)); }
    00128
    00129   inline _Ios_Iostate
    00130   operator|=(_Ios_Iostate& __a, _Ios_Iostate __b)
    00131   { return __a = __a | __b; }
    00132
    00133   inline _Ios_Iostate
    00134   operator&=(_Ios_Iostate& __a, _Ios_Iostate __b)
    00135   { return __a = __a & __b; }
    00136
    00137   inline _Ios_Iostate
    00138   operator^=(_Ios_Iostate& __a, _Ios_Iostate __b)
    00139   { return __a = __a ^ __b; }
    00140
    00141   inline _Ios_Iostate
    00142   operator~(_Ios_Iostate __a)
    00143   { return _Ios_Iostate(~static_cast<int>(__a)); }
    00144
    00145   enum _Ios_Seekdir { _M_ios_seekdir_end = 1L << 16 };
    00146
    00147   // 27.4.2  Class ios_base
    00148   class ios_base
    00149   {
    00150   public:
    00151
    00152     // 27.4.2.1.1  Class ios_base::failure
    00153     class failure : public exception
    00154     {
    00155     public:
    00156 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00157       //48.  Use of non-existent exception constructor
    00158       explicit
    00159       failure(const string& __str) throw();
    00160
    00161       // This declaration is not useless:
    00162       // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
    00163       virtual
    00164       ~failure() throw();
    00165
    00166       virtual const char*
    00167       what() const throw();
    00168
    00169     private:
    00170       enum { _M_bufsize = 256 };
    00171       char _M_name[_M_bufsize];
    00172 #endif
    00173     };
    00174
    00175     // 27.4.2.1.2  Type ios_base::fmtflags
    00176     typedef _Ios_Fmtflags fmtflags;
    00177     // 27.4.2.1.2  Type fmtflags
    00178     static const fmtflags boolalpha =   fmtflags(__ios_flags::_S_boolalpha);
    00179     static const fmtflags dec =         fmtflags(__ios_flags::_S_dec);
    00180     static const fmtflags fixed =       fmtflags(__ios_flags::_S_fixed);
    00181     static const fmtflags hex =         fmtflags(__ios_flags::_S_hex);
    00182     static const fmtflags internal =    fmtflags(__ios_flags::_S_internal);
    00183     static const fmtflags left =        fmtflags(__ios_flags::_S_left);
    00184     static const fmtflags oct =         fmtflags(__ios_flags::_S_oct);
    00185     static const fmtflags right =       fmtflags(__ios_flags::_S_right);
    00186     static const fmtflags scientific =  fmtflags(__ios_flags::_S_scientific);
    00187     static const fmtflags showbase =    fmtflags(__ios_flags::_S_showbase);
    00188     static const fmtflags showpoint =   fmtflags(__ios_flags::_S_showpoint);
    00189     static const fmtflags showpos =     fmtflags(__ios_flags::_S_showpos);
    00190     static const fmtflags skipws =      fmtflags(__ios_flags::_S_skipws);
    00191     static const fmtflags unitbuf =     fmtflags(__ios_flags::_S_unitbuf);
    00192     static const fmtflags uppercase =   fmtflags(__ios_flags::_S_uppercase);
    00193     static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield);
    00194     static const fmtflags basefield =   fmtflags(__ios_flags::_S_basefield);
    00195     static const fmtflags floatfield =  fmtflags(__ios_flags::_S_floatfield);
    00196
    00197     // 27.4.2.1.3  Type ios_base::iostate
    00198     typedef _Ios_Iostate iostate;
    00199     static const iostate badbit =   iostate(__ios_flags::_S_badbit);
    00200     static const iostate eofbit =   iostate(__ios_flags::_S_eofbit);
    00201     static const iostate failbit =  iostate(__ios_flags::_S_failbit);
    00202     static const iostate goodbit =  iostate(0);
    00203
    00204     // 27.4.2.1.4  Type openmode
    00205     typedef _Ios_Openmode openmode;
    00206     static const openmode app =     openmode(__ios_flags::_S_app);
    00207     static const openmode ate =     openmode(__ios_flags::_S_ate);
    00208     static const openmode binary =  openmode(__ios_flags::_S_bin);
    00209     static const openmode in =      openmode(__ios_flags::_S_in);
    00210     static const openmode out =     openmode(__ios_flags::_S_out);
    00211     static const openmode trunc =   openmode(__ios_flags::_S_trunc);
    00212
    00213     // 27.4.2.1.5  Type seekdir
    00214     typedef _Ios_Seekdir seekdir;
    00215     static const seekdir beg =      seekdir(0);
    00216     static const seekdir cur =      seekdir(SEEK_CUR);
    00217     static const seekdir end =      seekdir(SEEK_END);
    00218
    00219 #ifdef _GLIBCPP_DEPRECATED
    00220     typedef int io_state;
    00221     typedef int open_mode;
    00222     typedef int seek_dir;
    00223 #endif
    00224
    00225     // Callbacks;
    00226     enum event
    00227     {
    00228       erase_event,
    00229       imbue_event,
    00230       copyfmt_event
    00231     };
    00232
    00233     typedef void (*event_callback) (event, ios_base&, int);
    00234
    00235     void
    00236     register_callback(event_callback __fn, int __index);
    00237
    00238   protected:
    00239     // Data Members
    00240     streamsize      _M_precision;
    00241     streamsize      _M_width;
    00242     fmtflags        _M_flags;
    00243     iostate         _M_exception;
    00244     iostate             _M_streambuf_state;
    00245
    00246     // 27.4.2.6  Members for callbacks
    00247     // 27.4.2.6  ios_base callbacks
    00248     struct _Callback_list
    00249     {
    00250       // Data Members
    00251       _Callback_list*       _M_next;
    00252       ios_base::event_callback  _M_fn;
    00253       int           _M_index;
    00254       _Atomic_word      _M_refcount;  // 0 means one reference.
    00255
    00256       _Callback_list(ios_base::event_callback __fn, int __index,
    00257              _Callback_list* __cb)
    00258       : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
    00259
    00260       void
    00261       _M_add_reference() { __atomic_add(&_M_refcount, 1); }
    00262
    00263       // 0 => OK to delete.
    00264       int
    00265       _M_remove_reference() { return __exchange_and_add(&_M_refcount, -1); }
    00266     };
    00267
    00268      _Callback_list*    _M_callbacks;
    00269
    00270     void
    00271     _M_call_callbacks(event __ev) throw();
    00272
    00273     void
    00274     _M_dispose_callbacks(void);
    00275
    00276     // 27.4.2.5  Members for iword/pword storage
    00277     struct _Words
    00278     {
    00279       void*     _M_pword;
    00280       long  _M_iword;
    00281       _Words() : _M_pword(0), _M_iword(0) { }
    00282     };
    00283
    00284     // Only for failed iword/pword calls.
    00285     _Words          _M_word_zero;
    00286
    00287     // Guaranteed storage.
    00288     static const int    _S_local_word_size = 8;
    00289     _Words          _M_local_word[_S_local_word_size];
    00290
    00291     // Allocated storage.
    00292     int             _M_word_size;
    00293     _Words*         _M_word;
    00294
    00295     _Words&
    00296     _M_grow_words(int __index);
    00297
    00298     // Members for locale and locale caching.
    00299     locale      _M_ios_locale;
    00300
    00301     void
    00302     _M_init();
    00303
    00304   public:
    00305
    00306     // 27.4.2.1.6  Class ios_base::Init
    00307     // Used to initialize standard streams. In theory, g++ could use
    00308     // -finit-priority to order this stuff correctly without going
    00309     // through these machinations.
    00310     class Init
    00311     {
    00312       friend class ios_base;
    00313     public:
    00314       Init();
    00315       ~Init();
    00316
    00317       static void
    00318       _S_ios_create(bool __sync);
    00319
    00320       static void
    00321       _S_ios_destroy();
    00322
    00323     private:
    00324       static int    _S_ios_base_init;
    00325       static bool   _S_synced_with_stdio;
    00326     };
    00327
    00328     // Fmtflags state:
    00329     inline fmtflags
    00330     flags() const { return _M_flags; }
    00331
    00332     inline fmtflags
    00333     flags(fmtflags __fmtfl)
    00334     {
    00335       fmtflags __old = _M_flags;
    00336       _M_flags = __fmtfl;
    00337       return __old;
    00338     }
    00339
    00340     inline fmtflags
    00341     setf(fmtflags __fmtfl)
    00342     {
    00343       fmtflags __old = _M_flags;
    00344       _M_flags |= __fmtfl;
    00345       return __old;
    00346     }
    00347
    00348     inline fmtflags
    00349     setf(fmtflags __fmtfl, fmtflags __mask)
    00350     {
    00351       fmtflags __old = _M_flags;
    00352       _M_flags &= ~__mask;
    00353       _M_flags |= (__fmtfl & __mask);
    00354       return __old;
    00355     }
    00356
    00357     inline void
    00358     unsetf(fmtflags __mask) { _M_flags &= ~__mask; }
    00359
    00360     inline streamsize
    00361     precision() const { return _M_precision; }
    00362
    00363     inline streamsize
    00364     precision(streamsize __prec)
    00365     {
    00366       streamsize __old = _M_precision;
    00367       _M_precision = __prec;
    00368       return __old;
    00369     }
    00370
    00371     inline streamsize
    00372     width() const { return _M_width; }
    00373
    00374     inline streamsize
    00375     width(streamsize __wide)
    00376     {
    00377       streamsize __old = _M_width;
    00378       _M_width = __wide;
    00379       return __old;
    00380     }
    00381
    00382     static bool
    00383     sync_with_stdio(bool __sync = true);
    00384
    00385     // Locales:
    00386     locale
    00387     imbue(const locale& __loc);
    00388
    00389     inline locale
    00390     getloc() const { return _M_ios_locale; }
    00391
    00392     // Storage:
    00393     static int
    00394     xalloc() throw();
    00395
    00396     inline long&
    00397     iword(int __ix)
    00398     {
    00399       _Words& __word = (__ix < _M_word_size)
    00400             ? _M_word[__ix] : _M_grow_words(__ix);
    00401       return __word._M_iword;
    00402     }
    00403
    00404     inline void*&
    00405     pword(int __ix)
    00406     {
    00407       _Words& __word = (__ix < _M_word_size)
    00408             ? _M_word[__ix] : _M_grow_words(__ix);
    00409       return __word._M_pword;
    00410     }
    00411
    00412     // Destructor
    00413     ~ios_base();
    00414
    00415   protected:
    00416     ios_base();
    00417
    00418 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00419   //50.  Copy constructor and assignment operator of ios_base
    00420   private:
    00421     ios_base(const ios_base&);
    00422
    00423     ios_base&
    00424     operator=(const ios_base&);
    00425 #endif
    00426   };
    00427
    00428   // 27.4.5.1 fmtflags manipulators:
    00429   inline ios_base&
    00430   boolalpha(ios_base& __base)
    00431   {
    00432     __base.setf(ios_base::boolalpha);
    00433     return __base;
    00434   }
    00435
    00436   inline ios_base&
    00437   noboolalpha(ios_base& __base)
    00438   {
    00439     __base.unsetf(ios_base::boolalpha);
    00440     return __base;
    00441   }
    00442
    00443   inline ios_base&
    00444   showbase(ios_base& __base)
    00445   {
    00446     __base.setf(ios_base::showbase);
    00447     return __base;
    00448   }
    00449
    00450   inline ios_base&
    00451   noshowbase(ios_base& __base)
    00452   {
    00453     __base.unsetf(ios_base::showbase);
    00454     return __base;
    00455   }
    00456
    00457   inline ios_base&
    00458   showpoint(ios_base& __base)
    00459   {
    00460     __base.setf(ios_base::showpoint);
    00461     return __base;
    00462   }
    00463
    00464   inline ios_base&
    00465   noshowpoint(ios_base& __base)
    00466   {
    00467     __base.unsetf(ios_base::showpoint);
    00468     return __base;
    00469   }
    00470
    00471   inline ios_base&
    00472   showpos(ios_base& __base)
    00473   {
    00474     __base.setf(ios_base::showpos);
    00475     return __base;
    00476   }
    00477
    00478   inline ios_base&
    00479   noshowpos(ios_base& __base)
    00480   {
    00481     __base.unsetf(ios_base::showpos);
    00482     return __base;
    00483   }
    00484
    00485   inline ios_base&
    00486   skipws(ios_base& __base)
    00487   {
    00488     __base.setf(ios_base::skipws);
    00489     return __base;
    00490   }
    00491
    00492   inline ios_base&
    00493   noskipws(ios_base& __base)
    00494   {
    00495     __base.unsetf(ios_base::skipws);
    00496     return __base;
    00497   }
    00498
    00499   inline ios_base&
    00500   uppercase(ios_base& __base)
    00501   {
    00502     __base.setf(ios_base::uppercase);
    00503     return __base;
    00504   }
    00505
    00506   inline ios_base&
    00507   nouppercase(ios_base& __base)
    00508   {
    00509     __base.unsetf(ios_base::uppercase);
    00510     return __base;
    00511   }
    00512
    00513   inline ios_base&
    00514   unitbuf(ios_base& __base)
    00515   {
    00516      __base.setf(ios_base::unitbuf);
    00517      return __base;
    00518   }
    00519
    00520   inline ios_base&
    00521   nounitbuf(ios_base& __base)
    00522   {
    00523      __base.unsetf(ios_base::unitbuf);
    00524      return __base;
    00525   }
    00526
    00527   // 27.4.5.2 adjustfield anipulators:
    00528   inline ios_base&
    00529   internal(ios_base& __base)
    00530   {
    00531      __base.setf(ios_base::internal, ios_base::adjustfield);
    00532      return __base;
    00533   }
    00534
    00535   inline ios_base&
    00536   left(ios_base& __base)
    00537   {
    00538     __base.setf(ios_base::left, ios_base::adjustfield);
    00539     return __base;
    00540   }
    00541
    00542   inline ios_base&
    00543   right(ios_base& __base)
    00544   {
    00545     __base.setf(ios_base::right, ios_base::adjustfield);
    00546     return __base;
    00547   }
    00548
    00549   // 27.4.5.3 basefield anipulators:
    00550   inline ios_base&
    00551   dec(ios_base& __base)
    00552   {
    00553     __base.setf(ios_base::dec, ios_base::basefield);
    00554     return __base;
    00555   }
    00556
    00557   inline ios_base&
    00558   hex(ios_base& __base)
    00559   {
    00560     __base.setf(ios_base::hex, ios_base::basefield);
    00561     return __base;
    00562   }
    00563
    00564   inline ios_base&
    00565   oct(ios_base& __base)
    00566   {
    00567     __base.setf(ios_base::oct, ios_base::basefield);
    00568     return __base;
    00569   }
    00570
    00571   // 27.4.5.4 floatfield anipulators:
    00572   inline ios_base&
    00573   fixed(ios_base& __base)
    00574   {
    00575     __base.setf(ios_base::fixed, ios_base::floatfield);
    00576     return __base;
    00577   }
    00578
    00579   inline ios_base&
    00580   scientific(ios_base& __base)
    00581   {
    00582     __base.setf(ios_base::scientific, ios_base::floatfield);
    00583     return __base;
    00584   }
    00585
    00586 } // namespace std
    00587
    00588 #endif /* _CPP_BITS_IOSBASE_H */
    00589