/home/ntakagi/work/STLport-5.1.5/stlport/stl/_ios_base.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 #ifndef _STLP_IOS_BASE_H
00019 #define _STLP_IOS_BASE_H
00020 
00021 #ifndef _STLP_INTERNAL_STDEXCEPT_BASE
00022 #  include <stl/_stdexcept_base.h>
00023 #endif
00024 
00025 #ifndef _STLP_UTILITY
00026 #  include <utility>
00027 #endif
00028 
00029 #ifndef _STLP_INTERNAL_LOCALE_H
00030 #  include <stl/_locale.h>
00031 #endif
00032 
00033 #ifndef _STLP_INTERNAL_STRING_H
00034 #  include <stl/_string.h>
00035 #endif
00036 
00037 _STLP_BEGIN_NAMESPACE
00038 
00039 // ----------------------------------------------------------------------
00040 
00041 // Class ios_base.  This is the base class of the ios hierarchy, which
00042 // includes basic_istream and basic_ostream.  Classes in the ios
00043 // hierarchy are actually quite simple: they are just glorified
00044 // wrapper classes.  They delegate buffering and physical character
00045 // manipulation to the streambuf classes, and they delegate most
00046 // formatting tasks to a locale.
00047 
00048 class _STLP_CLASS_DECLSPEC ios_base {
00049 public:
00050 
00051   class _STLP_CLASS_DECLSPEC failure : public __Named_exception {
00052   public:
00053     explicit failure(const string&);
00054     virtual ~failure() _STLP_NOTHROW_INHERENTLY;
00055   };
00056 
00057   typedef int fmtflags;
00058   typedef int iostate;
00059   typedef int openmode;
00060   typedef int seekdir;
00061 
00062 # ifndef _STLP_NO_ANACHRONISMS
00063   typedef fmtflags fmt_flags;
00064 # endif
00065 
00066   // Formatting flags.
00067 #if defined (_STLP_STATIC_CONST_INIT_BUG)
00068   enum  {
00069 #else
00070   // boris : type for all those constants is int
00071   static const int
00072 #endif
00073     left       = 0x0001,
00074     right      = 0x0002,
00075     internal   = 0x0004,
00076     dec        = 0x0008,
00077     hex        = 0x0010,
00078     oct        = 0x0020,
00079     fixed      = 0x0040,
00080     scientific = 0x0080,
00081     boolalpha  = 0x0100,
00082     showbase   = 0x0200,
00083     showpoint  = 0x0400,
00084     showpos    = 0x0800,
00085     skipws     = 0x1000,
00086     unitbuf    = 0x2000,
00087     uppercase  = 0x4000,
00088     adjustfield = left | right | internal,
00089     basefield   = dec | hex | oct,
00090     floatfield  = scientific | fixed,
00091 
00092     // State flags.
00093     goodbit = 0x00,
00094     badbit  = 0x01,
00095     eofbit  = 0x02,
00096     failbit = 0x04,
00097 
00098     // Openmode flags.
00099     __default_mode = 0x0, /* implementation detail */
00100     app    = 0x01,
00101     ate    = 0x02,
00102     binary = 0x04,
00103     in     = 0x08,
00104     out    = 0x10,
00105     trunc  = 0x20,
00106 
00107     // Seekdir flags
00108 
00109     beg = 0x01,
00110     cur = 0x02,
00111     end = 0x04
00112 # ifdef _STLP_STATIC_CONST_INIT_BUG
00113   }
00114 # endif
00115   ;
00116 
00117 public:                         // Flag-manipulation functions.
00118   fmtflags flags() const { return _M_fmtflags; }
00119   fmtflags flags(fmtflags __flags) {
00120     fmtflags __tmp = _M_fmtflags;
00121     _M_fmtflags = __flags;
00122     return __tmp;
00123   }
00124 
00125   fmtflags setf(fmtflags __flag) {
00126     fmtflags __tmp = _M_fmtflags;
00127     _M_fmtflags |= __flag;
00128     return __tmp;
00129   }
00130   fmtflags setf(fmtflags __flag, fmtflags __mask) {
00131     fmtflags __tmp = _M_fmtflags;
00132     _M_fmtflags &= ~__mask;
00133     _M_fmtflags |= __flag & __mask;
00134     return __tmp;
00135   }
00136   void unsetf(fmtflags __mask) { _M_fmtflags &= ~__mask; }
00137 
00138   streamsize precision() const { return _M_precision; }
00139   streamsize precision(streamsize __newprecision) {
00140     streamsize __tmp = _M_precision;
00141     _M_precision = __newprecision;
00142     return __tmp;
00143   }
00144 
00145   streamsize width() const { return _M_width; }
00146   streamsize width(streamsize __newwidth) {
00147     streamsize __tmp = _M_width;
00148     _M_width = __newwidth;
00149     return __tmp;
00150   }
00151 
00152 public:                         // Locales
00153   locale imbue(const locale&);
00154   locale getloc() const { return _M_locale; }
00155 
00156 public:                         // Auxiliary storage.
00157   static int _STLP_CALL xalloc();
00158   long&  iword(int __index);
00159   void*& pword(int __index);
00160 
00161 public:                         // Destructor.
00162   virtual ~ios_base();
00163 
00164 public:                         // Callbacks.
00165   enum event { erase_event, imbue_event, copyfmt_event };
00166   typedef void (*event_callback)(event, ios_base&, int __index);
00167   void register_callback(event_callback __fn, int __index);
00168 
00169 public:                         // This member function affects only
00170                                 // the eight predefined ios objects:
00171                                 // cin, cout, etc.
00172   static bool _STLP_CALL sync_with_stdio(bool __sync = true);
00173 
00174 public:                         // The C++ standard requires only that these
00175                                 // member functions be defined in basic_ios.
00176                                 // We define them in the non-template
00177                                 // base class to avoid code duplication.
00178   operator void*() const { return !fail() ? (void*) __CONST_CAST(ios_base*,this) : (void*) 0; }
00179   bool operator!() const { return fail(); }
00180 
00181   iostate rdstate() const { return _M_iostate; }
00182 
00183   bool good() const { return _M_iostate == 0; }
00184   bool eof() const { return (_M_iostate & eofbit) != 0; }
00185   bool fail() const { return (_M_iostate & (failbit | badbit)) != 0; }
00186   bool bad() const { return (_M_iostate & badbit) != 0; }
00187 
00188 protected:                      // The functional protected interface.
00189 
00190   // Copies the state of __x to *this.  This member function makes it
00191   // possible to implement basic_ios::copyfmt without having to expose
00192   // ios_base's private data members.  Does not copy _M_exception_mask
00193   // or _M_iostate.
00194   void _M_copy_state(const ios_base& __x);
00195 
00196   void _M_setstate_nothrow(iostate __state) { _M_iostate |= __state; }
00197   void _M_clear_nothrow(iostate __state) { _M_iostate = __state; }
00198   iostate _M_get_exception_mask() const { return _M_exception_mask; }
00199   void _M_set_exception_mask(iostate __mask) { _M_exception_mask = __mask; }
00200   void _M_check_exception_mask() {
00201     if (_M_iostate & _M_exception_mask)
00202       _M_throw_failure();
00203   }
00204 
00205   void _M_invoke_callbacks(event);
00206   void _STLP_FUNCTION_THROWS _M_throw_failure();
00207 
00208   ios_base();                   // Default constructor.
00209 
00210 protected:                        // Initialization of the I/O system
00211   static void _STLP_CALL _S_initialize();
00212   static void _STLP_CALL _S_uninitialize();
00213   static bool _S_was_synced;
00214 
00215 private:                        // Invalidate the copy constructor and
00216                                 // assignment operator.
00217   ios_base(const ios_base&);
00218   void operator=(const ios_base&);
00219 
00220 private:                        // Data members.
00221 
00222   fmtflags _M_fmtflags;         // Flags
00223   iostate _M_iostate;
00224   openmode _M_openmode;
00225   seekdir _M_seekdir;
00226   iostate _M_exception_mask;
00227 
00228   streamsize _M_precision;
00229   streamsize _M_width;
00230 
00231   locale _M_locale;
00232 
00233   pair<event_callback, int>* _M_callbacks;
00234   size_t _M_num_callbacks;      // Size of the callback array.
00235   size_t _M_callback_index;     // Index of the next available callback;
00236                                 // initially zero.
00237 
00238   long* _M_iwords;              // Auxiliary storage.  The count is zero
00239   size_t _M_num_iwords;         // if and only if the pointer is null.
00240 
00241   void** _M_pwords;
00242   size_t _M_num_pwords;
00243 
00244 protected:
00245   // Cached copies of the curent locale's facets.  Set by init() and imbue().
00246   locale::facet* _M_cached_ctype;
00247   locale::facet* _M_cached_numpunct;
00248   string         _M_cached_grouping;
00249 public:
00250   // Equivalent to &use_facet< Facet >(getloc()), but faster.
00251   const locale::facet* _M_ctype_facet() const { return _M_cached_ctype; }
00252   const locale::facet* _M_numpunct_facet() const { return _M_cached_numpunct; }
00253   const string&  _M_grouping() const { return _M_cached_grouping; }
00254 public:
00255 
00256   // ----------------------------------------------------------------------
00257   // Nested initializer class.  This is an implementation detail, but it's
00258   // prescribed by the standard.  The static initializer object (on
00259   // implementations where such a thing is required) is declared in
00260   // <iostream>
00261 
00262   class _STLP_CLASS_DECLSPEC Init
00263   {
00264     public:
00265       Init();
00266       ~Init();
00267     private:
00268       static long _S_count;
00269       friend class ios_base;
00270   };
00271 
00272   friend class Init;
00273 
00274 public:
00275 # ifndef _STLP_NO_ANACHRONISMS
00276   //  31.6  Old iostreams members                         [depr.ios.members]
00277   typedef iostate  io_state;
00278   typedef openmode open_mode;
00279   typedef seekdir  seek_dir;
00280   typedef _STLP_STD::streamoff  streamoff;
00281   typedef _STLP_STD::streampos  streampos;
00282 # endif
00283 };
00284 
00285 // ----------------------------------------------------------------------
00286 // ios_base manipulator functions, from section 27.4.5 of the C++ standard.
00287 // All of them are trivial one-line wrapper functions.
00288 
00289 // fmtflag manipulators, section 27.4.5.1
00290 inline ios_base& _STLP_CALL boolalpha(ios_base& __s)
00291   { __s.setf(ios_base::boolalpha); return __s;}
00292 
00293 inline ios_base& _STLP_CALL noboolalpha(ios_base& __s)
00294   { __s.unsetf(ios_base::boolalpha); return __s;}
00295 
00296 inline ios_base& _STLP_CALL showbase(ios_base& __s)
00297   { __s.setf(ios_base::showbase); return __s;}
00298 
00299 inline ios_base& _STLP_CALL noshowbase(ios_base& __s)
00300   { __s.unsetf(ios_base::showbase); return __s;}
00301 
00302 inline ios_base& _STLP_CALL showpoint(ios_base& __s)
00303   { __s.setf(ios_base::showpoint); return __s;}
00304 
00305 inline ios_base& _STLP_CALL noshowpoint(ios_base& __s)
00306   { __s.unsetf(ios_base::showpoint); return __s;}
00307 
00308 inline ios_base& _STLP_CALL showpos(ios_base& __s)
00309   { __s.setf(ios_base::showpos); return __s;}
00310 
00311 inline ios_base& _STLP_CALL noshowpos(ios_base& __s)
00312   { __s.unsetf(ios_base::showpos); return __s;}
00313 
00314 inline ios_base& _STLP_CALL skipws(ios_base& __s)
00315   { __s.setf(ios_base::skipws); return __s;}
00316 
00317 inline ios_base& _STLP_CALL noskipws(ios_base& __s)
00318   { __s.unsetf(ios_base::skipws); return __s;}
00319 
00320 inline ios_base& _STLP_CALL uppercase(ios_base& __s)
00321   { __s.setf(ios_base::uppercase); return __s;}
00322 
00323 inline ios_base& _STLP_CALL nouppercase(ios_base& __s)
00324   { __s.unsetf(ios_base::uppercase); return __s;}
00325 
00326 inline ios_base& _STLP_CALL unitbuf(ios_base& __s)
00327   { __s.setf(ios_base::unitbuf); return __s;}
00328 
00329 inline ios_base& _STLP_CALL nounitbuf(ios_base& __s)
00330   { __s.unsetf(ios_base::unitbuf); return __s;}
00331 
00332 
00333 // adjustfield manipulators, section 27.4.5.2
00334 inline ios_base& _STLP_CALL internal(ios_base& __s)
00335   { __s.setf(ios_base::internal, ios_base::adjustfield); return __s; }
00336 
00337 inline ios_base& _STLP_CALL left(ios_base& __s)
00338   { __s.setf(ios_base::left, ios_base::adjustfield); return __s; }
00339 
00340 inline ios_base& _STLP_CALL right(ios_base& __s)
00341   { __s.setf(ios_base::right, ios_base::adjustfield); return __s; }
00342 
00343 // basefield manipulators, section 27.4.5.3
00344 inline ios_base& _STLP_CALL dec(ios_base& __s)
00345   { __s.setf(ios_base::dec, ios_base::basefield); return __s; }
00346 
00347 inline ios_base& _STLP_CALL hex(ios_base& __s)
00348   { __s.setf(ios_base::hex, ios_base::basefield); return __s; }
00349 
00350 inline ios_base& _STLP_CALL oct(ios_base& __s)
00351   { __s.setf(ios_base::oct, ios_base::basefield); return __s; }
00352 
00353 
00354 // floatfield manipulators, section 27.4.5.3
00355 inline ios_base& _STLP_CALL fixed(ios_base& __s)
00356   { __s.setf(ios_base::fixed, ios_base::floatfield); return __s; }
00357 
00358 inline ios_base& _STLP_CALL scientific(ios_base& __s)
00359   { __s.setf(ios_base::scientific, ios_base::floatfield); return __s; }
00360 
00361 _STLP_END_NAMESPACE
00362 
00363 #endif /* _STLP_IOS_BASE */
00364 
00365 // Local Variables:
00366 // mode:C++
00367 // End:
00368 



Generated on Mon Mar 10 15:32:25 2008 by  doxygen 1.5.1