# 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
    00156   class ios_base
    00157   {
    00158   public:
    00159
    00160     // 27.4.2.1.1  Class ios_base::failure
    00162     class failure : public exception
    00163     {
    00164     public:
    00165 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00166       //48.  Use of non-existent exception constructor
    00167       explicit
    00168       failure(const string& __str) throw();
    00169
    00170       // This declaration is not useless:
    00171       // http://gcc.gnu.org/onlinedocs/gcc-3.0.2/gcc_6.html#SEC118
    00172       virtual
    00173       ~failure() throw();
    00174
    00175       virtual const char*
    00176       what() const throw();
    00177
    00178     private:
    00179       enum { _M_bufsize = 256 };
    00180       char _M_name[_M_bufsize];
    00181 #endif
    00182     };
    00183
    00184     // 27.4.2.1.2  Type ios_base::fmtflags
    00210     typedef _Ios_Fmtflags fmtflags;
    00212     static const fmtflags boolalpha =   fmtflags(__ios_flags::_S_boolalpha);
    00214     static const fmtflags dec =         fmtflags(__ios_flags::_S_dec);
    00216     static const fmtflags fixed =       fmtflags(__ios_flags::_S_fixed);
    00218     static const fmtflags hex =         fmtflags(__ios_flags::_S_hex);
    00222     static const fmtflags internal =    fmtflags(__ios_flags::_S_internal);
    00225     static const fmtflags left =        fmtflags(__ios_flags::_S_left);
    00227     static const fmtflags oct =         fmtflags(__ios_flags::_S_oct);
    00230     static const fmtflags right =       fmtflags(__ios_flags::_S_right);
    00232     static const fmtflags scientific =  fmtflags(__ios_flags::_S_scientific);
    00235     static const fmtflags showbase =    fmtflags(__ios_flags::_S_showbase);
    00238     static const fmtflags showpoint =   fmtflags(__ios_flags::_S_showpoint);
    00240     static const fmtflags showpos =     fmtflags(__ios_flags::_S_showpos);
    00242     static const fmtflags skipws =      fmtflags(__ios_flags::_S_skipws);
    00244     static const fmtflags unitbuf =     fmtflags(__ios_flags::_S_unitbuf);
    00247     static const fmtflags uppercase =   fmtflags(__ios_flags::_S_uppercase);
    00249     static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield);
    00251     static const fmtflags basefield =   fmtflags(__ios_flags::_S_basefield);
    00253     static const fmtflags floatfield =  fmtflags(__ios_flags::_S_floatfield);
    00254
    00255     // 27.4.2.1.3  Type ios_base::iostate
    00267     typedef _Ios_Iostate iostate;
    00270     static const iostate badbit =   iostate(__ios_flags::_S_badbit);
    00272     static const iostate eofbit =   iostate(__ios_flags::_S_eofbit);
    00276     static const iostate failbit =  iostate(__ios_flags::_S_failbit);
    00278     static const iostate goodbit =  iostate(0);
    00279
    00280     // 27.4.2.1.4  Type ios_base::openmode
    00294     typedef _Ios_Openmode openmode;
    00296     static const openmode app =     openmode(__ios_flags::_S_app);
    00298     static const openmode ate =     openmode(__ios_flags::_S_ate);
    00303     static const openmode binary =  openmode(__ios_flags::_S_bin);
    00305     static const openmode in =      openmode(__ios_flags::_S_in);
    00307     static const openmode out =     openmode(__ios_flags::_S_out);
    00309     static const openmode trunc =   openmode(__ios_flags::_S_trunc);
    00310
    00311     // 27.4.2.1.5  Type ios_base::seekdir
    00321     typedef _Ios_Seekdir seekdir;
    00323     static const seekdir beg =      seekdir(0);
    00325     static const seekdir cur =      seekdir(SEEK_CUR);
    00327     static const seekdir end =      seekdir(SEEK_END);
    00328
    00329 #ifdef _GLIBCPP_DEPRECATED
    00330     // Annex D.6
    00331     typedef int io_state;
    00332     typedef int open_mode;
    00333     typedef int seek_dir;
    00334
    00335     typedef std::streampos streampos;
    00336     typedef std::streamoff streamoff;
    00337 #endif
    00338
    00339     // Callbacks;
    00343     enum event
    00344     {
    00345       erase_event,
    00346       imbue_event,
    00347       copyfmt_event
    00348     };
    00349
    00353     typedef void (*event_callback) (event, ios_base&, int);
    00354
    00358     void
    00359     register_callback(event_callback __fn, int __index);
    00360
    00361   protected:
    00363
    00368     streamsize      _M_precision;
    00369     streamsize      _M_width;
    00370     fmtflags        _M_flags;
    00371     iostate         _M_exception;
    00372     iostate             _M_streambuf_state;
    00374
    00375     // 27.4.2.6  Members for callbacks
    00376     // 27.4.2.6  ios_base callbacks
    00377     struct _Callback_list
    00378     {
    00379       // Data Members
    00380       _Callback_list*       _M_next;
    00381       ios_base::event_callback  _M_fn;
    00382       int           _M_index;
    00383       _Atomic_word      _M_refcount;  // 0 means one reference.
    00384
    00385       _Callback_list(ios_base::event_callback __fn, int __index,
    00386              _Callback_list* __cb)
    00387       : _M_next(__cb), _M_fn(__fn), _M_index(__index), _M_refcount(0) { }
    00388
    00389       void
    00390       _M_add_reference() { __atomic_add(&_M_refcount, 1); }
    00391
    00392       // 0 => OK to delete.
    00393       int
    00394       _M_remove_reference() { return __exchange_and_add(&_M_refcount, -1); }
    00395     };
    00396
    00397      _Callback_list*    _M_callbacks;
    00398
    00399     void
    00400     _M_call_callbacks(event __ev) throw();
    00401
    00402     void
    00403     _M_dispose_callbacks(void);
    00404
    00405     // 27.4.2.5  Members for iword/pword storage
    00406     struct _Words
    00407     {
    00408       void*     _M_pword;
    00409       long  _M_iword;
    00410       _Words() : _M_pword(0), _M_iword(0) { }
    00411     };
    00412
    00413     // Only for failed iword/pword calls.
    00414     _Words          _M_word_zero;
    00415
    00416     // Guaranteed storage.
    00417     static const int    _S_local_word_size = 8;
    00418     _Words          _M_local_word[_S_local_word_size];
    00419
    00420     // Allocated storage.
    00421     int             _M_word_size;
    00422     _Words*         _M_word;
    00423
    00424     _Words&
    00425     _M_grow_words(int __index);
    00426
    00427     // Members for locale and locale caching.
    00428     locale      _M_ios_locale;
    00429
    00430     void
    00431     _M_init();
    00432
    00433   public:
    00434
    00435     // 27.4.2.1.6  Class ios_base::Init
    00436     // Used to initialize standard streams. In theory, g++ could use
    00437     // -finit-priority to order this stuff correctly without going
    00438     // through these machinations.
    00439     class Init
    00440     {
    00441       friend class ios_base;
    00442     public:
    00443       Init();
    00444       ~Init();
    00445
    00446       static void
    00447       _S_ios_create(bool __sync);
    00448
    00449       static void
    00450       _S_ios_destroy();
    00451
    00452     private:
    00453       static int    _S_ios_base_init;
    00454       static bool   _S_synced_with_stdio;
    00455     };
    00456
    00457     // [27.4.2.2] fmtflags state functions
    00462     inline fmtflags
    00463     flags() const { return _M_flags; }
    00464
    00472     inline fmtflags
    00473     flags(fmtflags __fmtfl)
    00474     {
    00475       fmtflags __old = _M_flags;
    00476       _M_flags = __fmtfl;
    00477       return __old;
    00478     }
    00479
    00488     inline fmtflags
    00489     setf(fmtflags __fmtfl)
    00490     {
    00491       fmtflags __old = _M_flags;
    00492       _M_flags |= __fmtfl;
    00493       return __old;
    00494     }
    00495
    00505     inline fmtflags
    00506     setf(fmtflags __fmtfl, fmtflags __mask)
    00507     {
    00508       fmtflags __old = _M_flags;
    00509       _M_flags &= ~__mask;
    00510       _M_flags |= (__fmtfl & __mask);
    00511       return __old;
    00512     }
    00513
    00520     inline void
    00521     unsetf(fmtflags __mask) { _M_flags &= ~__mask; }
    00522
    00532     inline streamsize
    00533     precision() const { return _M_precision; }
    00534
    00540     inline streamsize
    00541     precision(streamsize __prec)
    00542     {
    00543       streamsize __old = _M_precision;
    00544       _M_precision = __prec;
    00545       return __old;
    00546     }
    00547
    00554     inline streamsize
    00555     width() const { return _M_width; }
    00556
    00562     inline streamsize
    00563     width(streamsize __wide)
    00564     {
    00565       streamsize __old = _M_width;
    00566       _M_width = __wide;
    00567       return __old;
    00568     }
    00569
    00570     // [27.4.2.4] ios_base static members
    00581     static bool
    00582     sync_with_stdio(bool __sync = true);
    00583
    00584     // [27.4.2.3] ios_base locale functions
    00593     locale
    00594     imbue(const locale& __loc);
    00595
    00604     inline locale
    00605     getloc() const { return _M_ios_locale; }
    00606
    00607     // [27.4.2.5] ios_base storage functions
    00611     static int
    00612     xalloc() throw();
    00613
    00617     inline long&
    00618     iword(int __ix)
    00619     {
    00620       _Words& __word = (__ix < _M_word_size)
    00621             ? _M_word[__ix] : _M_grow_words(__ix);
    00622       return __word._M_iword;
    00623     }
    00624
    00628     inline void*&
    00629     pword(int __ix)
    00630     {
    00631       _Words& __word = (__ix < _M_word_size)
    00632             ? _M_word[__ix] : _M_grow_words(__ix);
    00633       return __word._M_pword;
    00634     }
    00635
    00636     // Destructor
    00641     virtual ~ios_base();
    00642
    00643   protected:
    00644     ios_base();
    00645
    00646 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
    00647   //50.  Copy constructor and assignment operator of ios_base
    00648   private:
    00649     ios_base(const ios_base&);
    00650
    00651     ios_base&
    00652     operator=(const ios_base&);
    00653 #endif
    00654   };
    00655
    00656   // [27.4.5.1] fmtflags manipulators
    00658   inline ios_base&
    00659   boolalpha(ios_base& __base)
    00660   {
    00661     __base.setf(ios_base::boolalpha);
    00662     return __base;
    00663   }
    00664
    00666   inline ios_base&
    00667   noboolalpha(ios_base& __base)
    00668   {
    00669     __base.unsetf(ios_base::boolalpha);
    00670     return __base;
    00671   }
    00672
    00674   inline ios_base&
    00675   showbase(ios_base& __base)
    00676   {
    00677     __base.setf(ios_base::showbase);
    00678     return __base;
    00679   }
    00680
    00682   inline ios_base&
    00683   noshowbase(ios_base& __base)
    00684   {
    00685     __base.unsetf(ios_base::showbase);
    00686     return __base;
    00687   }
    00688
    00690   inline ios_base&
    00691   showpoint(ios_base& __base)
    00692   {
    00693     __base.setf(ios_base::showpoint);
    00694     return __base;
    00695   }
    00696
    00698   inline ios_base&
    00699   noshowpoint(ios_base& __base)
    00700   {
    00701     __base.unsetf(ios_base::showpoint);
    00702     return __base;
    00703   }
    00704
    00706   inline ios_base&
    00707   showpos(ios_base& __base)
    00708   {
    00709     __base.setf(ios_base::showpos);
    00710     return __base;
    00711   }
    00712
    00714   inline ios_base&
    00715   noshowpos(ios_base& __base)
    00716   {
    00717     __base.unsetf(ios_base::showpos);
    00718     return __base;
    00719   }
    00720
    00722   inline ios_base&
    00723   skipws(ios_base& __base)
    00724   {
    00725     __base.setf(ios_base::skipws);
    00726     return __base;
    00727   }
    00728
    00730   inline ios_base&
    00731   noskipws(ios_base& __base)
    00732   {
    00733     __base.unsetf(ios_base::skipws);
    00734     return __base;
    00735   }
    00736
    00738   inline ios_base&
    00739   uppercase(ios_base& __base)
    00740   {
    00741     __base.setf(ios_base::uppercase);
    00742     return __base;
    00743   }
    00744
    00746   inline ios_base&
    00747   nouppercase(ios_base& __base)
    00748   {
    00749     __base.unsetf(ios_base::uppercase);
    00750     return __base;
    00751   }
    00752
    00754   inline ios_base&
    00755   unitbuf(ios_base& __base)
    00756   {
    00757      __base.setf(ios_base::unitbuf);
    00758      return __base;
    00759   }
    00760
    00762   inline ios_base&
    00763   nounitbuf(ios_base& __base)
    00764   {
    00765      __base.unsetf(ios_base::unitbuf);
    00766      return __base;
    00767   }
    00768
    00769   // [27.4.5.2] adjustfield anipulators
    00771   inline ios_base&
    00772   internal(ios_base& __base)
    00773   {
    00774      __base.setf(ios_base::internal, ios_base::adjustfield);
    00775      return __base;
    00776   }
    00777
    00779   inline ios_base&
    00780   left(ios_base& __base)
    00781   {
    00782     __base.setf(ios_base::left, ios_base::adjustfield);
    00783     return __base;
    00784   }
    00785
    00787   inline ios_base&
    00788   right(ios_base& __base)
    00789   {
    00790     __base.setf(ios_base::right, ios_base::adjustfield);
    00791     return __base;
    00792   }
    00793
    00794   // [27.4.5.3] basefield anipulators
    00796   inline ios_base&
    00797   dec(ios_base& __base)
    00798   {
    00799     __base.setf(ios_base::dec, ios_base::basefield);
    00800     return __base;
    00801   }
    00802
    00804   inline ios_base&
    00805   hex(ios_base& __base)
    00806   {
    00807     __base.setf(ios_base::hex, ios_base::basefield);
    00808     return __base;
    00809   }
    00810
    00812   inline ios_base&
    00813   oct(ios_base& __base)
    00814   {
    00815     __base.setf(ios_base::oct, ios_base::basefield);
    00816     return __base;
    00817   }
    00818
    00819   // [27.4.5.4] floatfield anipulators
    00821   inline ios_base&
    00822   fixed(ios_base& __base)
    00823   {
    00824     __base.setf(ios_base::fixed, ios_base::floatfield);
    00825     return __base;
    00826   }
    00827
    00829   inline ios_base&
    00830   scientific(ios_base& __base)
    00831   {
    00832     __base.setf(ios_base::scientific, ios_base::floatfield);
    00833     return __base;
    00834   }
    00835
    00836 } // namespace std
    00837
    00838 #endif /* _CPP_BITS_IOSBASE_H */
    00839