_ios_base.h

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

Generated on Mon Jun 5 10:20:46 2006 for Intelligence.kdevelop by  doxygen 1.4.6