Home | History | Annotate | Download | only in stl
      1 /*
      2  * Copyright (c) 1999
      3  * Silicon Graphics Computer Systems, Inc.
      4  *
      5  * Copyright (c) 1999
      6  * Boris Fomitchev
      7  *
      8  * This material is provided "as is", with absolutely no warranty expressed
      9  * or implied. Any use is at your own risk.
     10  *
     11  * Permission to use or copy this software for any purpose is hereby granted
     12  * without fee, provided the above notices are retained on all copies.
     13  * Permission to modify the code and to distribute modified code is granted,
     14  * provided the above notices are retained, and a notice that the code was
     15  * modified is included with the above copyright notice.
     16  *
     17  */
     18 #ifndef _STLP_IOS_BASE_H
     19 #define _STLP_IOS_BASE_H
     20 
     21 #ifndef _STLP_INTERNAL_STDEXCEPT_BASE
     22 #  include <stl/_stdexcept_base.h>
     23 #endif
     24 
     25 #ifndef _STLP_INTERNAL_PAIR_H
     26 #  include <stl/_pair.h>
     27 #endif
     28 
     29 #ifndef _STLP_INTERNAL_LOCALE_H
     30 #  include <stl/_locale.h>
     31 #endif
     32 
     33 #ifndef _STLP_INTERNAL_STRING_H
     34 #  include <stl/_string.h>
     35 #endif
     36 
     37 _STLP_BEGIN_NAMESPACE
     38 
     39 // ----------------------------------------------------------------------
     40 
     41 // Class ios_base.  This is the base class of the ios hierarchy, which
     42 // includes basic_istream and basic_ostream.  Classes in the ios
     43 // hierarchy are actually quite simple: they are just glorified
     44 // wrapper classes.  They delegate buffering and physical character
     45 // manipulation to the streambuf classes, and they delegate most
     46 // formatting tasks to a locale.
     47 
     48 class _STLP_CLASS_DECLSPEC ios_base {
     49 public:
     50 
     51 # ifdef _STLP_USE_EXCEPTIONS
     52   class _STLP_CLASS_DECLSPEC failure : public __Named_exception {
     53   public:
     54     explicit failure(const string&);
     55     virtual ~failure() _STLP_NOTHROW_INHERENTLY;
     56   };
     57 #endif
     58 
     59   typedef int fmtflags;
     60   typedef int iostate;
     61   typedef int openmode;
     62   typedef int seekdir;
     63 
     64 # ifndef _STLP_NO_ANACHRONISMS
     65   typedef fmtflags fmt_flags;
     66 # endif
     67 
     68   // Formatting flags.
     69   _STLP_STATIC_CONSTANT(int, left = 0x0001);
     70   _STLP_STATIC_CONSTANT(int, right = 0x0002);
     71   _STLP_STATIC_CONSTANT(int, internal   = 0x0004);
     72   _STLP_STATIC_CONSTANT(int, dec        = 0x0008);
     73   _STLP_STATIC_CONSTANT(int, hex        = 0x0010);
     74   _STLP_STATIC_CONSTANT(int, oct        = 0x0020);
     75   _STLP_STATIC_CONSTANT(int, fixed      = 0x0040);
     76   _STLP_STATIC_CONSTANT(int, scientific = 0x0080);
     77   _STLP_STATIC_CONSTANT(int, boolalpha  = 0x0100);
     78   _STLP_STATIC_CONSTANT(int, showbase   = 0x0200);
     79   _STLP_STATIC_CONSTANT(int, showpoint  = 0x0400);
     80   _STLP_STATIC_CONSTANT(int, showpos    = 0x0800);
     81   _STLP_STATIC_CONSTANT(int, skipws     = 0x1000);
     82   _STLP_STATIC_CONSTANT(int, unitbuf    = 0x2000);
     83   _STLP_STATIC_CONSTANT(int, uppercase  = 0x4000);
     84   _STLP_STATIC_CONSTANT(int, adjustfield = left | right | internal);
     85   _STLP_STATIC_CONSTANT(int, basefield   = dec | hex | oct);
     86   _STLP_STATIC_CONSTANT(int, floatfield  = scientific | fixed);
     87 
     88   // State flags.
     89   _STLP_STATIC_CONSTANT(int, goodbit = 0x00);
     90   _STLP_STATIC_CONSTANT(int, badbit  = 0x01);
     91   _STLP_STATIC_CONSTANT(int, eofbit  = 0x02);
     92   _STLP_STATIC_CONSTANT(int, failbit = 0x04);
     93 
     94   // Openmode flags.
     95   _STLP_STATIC_CONSTANT(int, __default_mode = 0x0); /* implementation detail */
     96   _STLP_STATIC_CONSTANT(int, app    = 0x01);
     97   _STLP_STATIC_CONSTANT(int, ate    = 0x02);
     98   _STLP_STATIC_CONSTANT(int, binary = 0x04);
     99   _STLP_STATIC_CONSTANT(int, in     = 0x08);
    100   _STLP_STATIC_CONSTANT(int, out    = 0x10);
    101   _STLP_STATIC_CONSTANT(int, trunc  = 0x20);
    102 
    103   // Seekdir flags
    104   _STLP_STATIC_CONSTANT(int, beg = 0x01);
    105   _STLP_STATIC_CONSTANT(int, cur = 0x02);
    106   _STLP_STATIC_CONSTANT(int, end = 0x04);
    107 
    108 public:                         // Flag-manipulation functions.
    109   fmtflags flags() const { return _M_fmtflags; }
    110   fmtflags flags(fmtflags __flags) {
    111     fmtflags __tmp = _M_fmtflags;
    112     _M_fmtflags = __flags;
    113     return __tmp;
    114   }
    115 
    116   fmtflags setf(fmtflags __flag) {
    117     fmtflags __tmp = _M_fmtflags;
    118     _M_fmtflags |= __flag;
    119     return __tmp;
    120   }
    121   fmtflags setf(fmtflags __flag, fmtflags __mask) {
    122     fmtflags __tmp = _M_fmtflags;
    123     _M_fmtflags &= ~__mask;
    124     _M_fmtflags |= __flag & __mask;
    125     return __tmp;
    126   }
    127   void unsetf(fmtflags __mask) { _M_fmtflags &= ~__mask; }
    128 
    129   streamsize precision() const { return _M_precision; }
    130   streamsize precision(streamsize __newprecision) {
    131     streamsize __tmp = _M_precision;
    132     _M_precision = __newprecision;
    133     return __tmp;
    134   }
    135 
    136   streamsize width() const { return _M_width; }
    137   streamsize width(streamsize __newwidth) {
    138     streamsize __tmp = _M_width;
    139     _M_width = __newwidth;
    140     return __tmp;
    141   }
    142 
    143 public:                         // Locales
    144   locale imbue(const locale&);
    145   locale getloc() const { return _M_locale; }
    146 
    147 public:                         // Auxiliary storage.
    148   static int _STLP_CALL xalloc();
    149   long&  iword(int __index);
    150   void*& pword(int __index);
    151 
    152 public:                         // Destructor.
    153   virtual ~ios_base();
    154 
    155 public:                         // Callbacks.
    156   enum event { erase_event, imbue_event, copyfmt_event };
    157   typedef void (*event_callback)(event, ios_base&, int __index);
    158   void register_callback(event_callback __fn, int __index);
    159 
    160 public:                         // This member function affects only
    161                                 // the eight predefined ios objects:
    162                                 // cin, cout, etc.
    163   static bool _STLP_CALL sync_with_stdio(bool __sync = true);
    164 
    165 public:                         // The C++ standard requires only that these
    166                                 // member functions be defined in basic_ios.
    167                                 // We define them in the non-template
    168                                 // base class to avoid code duplication.
    169   operator void*() const { return !fail() ? (void*) __CONST_CAST(ios_base*,this) : (void*) 0; }
    170   bool operator!() const { return fail(); }
    171 
    172   iostate rdstate() const { return _M_iostate; }
    173 
    174   bool good() const { return _M_iostate == 0; }
    175   bool eof() const { return (_M_iostate & eofbit) != 0; }
    176   bool fail() const { return (_M_iostate & (failbit | badbit)) != 0; }
    177   bool bad() const { return (_M_iostate & badbit) != 0; }
    178 
    179 protected:                      // The functional protected interface.
    180 
    181   // Copies the state of __x to *this.  This member function makes it
    182   // possible to implement basic_ios::copyfmt without having to expose
    183   // ios_base's private data members.  Does not copy _M_exception_mask
    184   // or _M_iostate.
    185   void _M_copy_state(const ios_base& __x);
    186 
    187   void _M_setstate_nothrow(iostate __state) { _M_iostate |= __state; }
    188   void _M_clear_nothrow(iostate __state) { _M_iostate = __state; }
    189   iostate _M_get_exception_mask() const { return _M_exception_mask; }
    190   void _M_set_exception_mask(iostate __mask) { _M_exception_mask = __mask; }
    191   void _M_check_exception_mask() {
    192     if (_M_iostate & _M_exception_mask)
    193       _M_throw_failure();
    194   }
    195 
    196   void _M_invoke_callbacks(event);
    197   void _STLP_FUNCTION_THROWS _M_throw_failure();
    198 
    199   ios_base();                   // Default constructor.
    200 
    201 protected:                        // Initialization of the I/O system
    202   static void _STLP_CALL _S_initialize();
    203   static void _STLP_CALL _S_uninitialize();
    204   static bool _S_is_synced;
    205 
    206 private:                        // Invalidate the copy constructor and
    207                                 // assignment operator.
    208   ios_base(const ios_base&);
    209   void operator=(const ios_base&);
    210 
    211 private:                        // Data members.
    212 
    213   fmtflags _M_fmtflags;         // Flags
    214   iostate _M_iostate;
    215   openmode _M_openmode;
    216   seekdir _M_seekdir;
    217   iostate _M_exception_mask;
    218 
    219   streamsize _M_precision;
    220   streamsize _M_width;
    221 
    222   locale _M_locale;
    223 
    224   pair<event_callback, int>* _M_callbacks;
    225   size_t _M_num_callbacks;      // Size of the callback array.
    226   size_t _M_callback_index;     // Index of the next available callback;
    227                                 // initially zero.
    228 
    229   long* _M_iwords;              // Auxiliary storage.  The count is zero
    230   size_t _M_num_iwords;         // if and only if the pointer is null.
    231 
    232   void** _M_pwords;
    233   size_t _M_num_pwords;
    234 
    235 public:
    236   // ----------------------------------------------------------------------
    237   // Nested initializer class.  This is an implementation detail, but it's
    238   // prescribed by the standard.  The static initializer object (on
    239   // implementations where such a thing is required) is declared in
    240   // <iostream>
    241   class _STLP_CLASS_DECLSPEC Init
    242   {
    243     public:
    244       Init();
    245       ~Init();
    246     private:
    247       static long _S_count;
    248       friend class ios_base;
    249   };
    250 
    251   friend class Init;
    252 
    253 public:
    254 # ifndef _STLP_NO_ANACHRONISMS
    255   //  31.6  Old iostreams members                         [depr.ios.members]
    256   typedef iostate  io_state;
    257   typedef openmode open_mode;
    258   typedef seekdir  seek_dir;
    259   typedef _STLP_STD::streamoff  streamoff;
    260   typedef _STLP_STD::streampos  streampos;
    261 # endif
    262 };
    263 
    264 // ----------------------------------------------------------------------
    265 // ios_base manipulator functions, from section 27.4.5 of the C++ standard.
    266 // All of them are trivial one-line wrapper functions.
    267 
    268 // fmtflag manipulators, section 27.4.5.1
    269 inline ios_base& _STLP_CALL boolalpha(ios_base& __s)
    270   { __s.setf(ios_base::boolalpha); return __s;}
    271 
    272 inline ios_base& _STLP_CALL noboolalpha(ios_base& __s)
    273   { __s.unsetf(ios_base::boolalpha); return __s;}
    274 
    275 inline ios_base& _STLP_CALL showbase(ios_base& __s)
    276   { __s.setf(ios_base::showbase); return __s;}
    277 
    278 inline ios_base& _STLP_CALL noshowbase(ios_base& __s)
    279   { __s.unsetf(ios_base::showbase); return __s;}
    280 
    281 inline ios_base& _STLP_CALL showpoint(ios_base& __s)
    282   { __s.setf(ios_base::showpoint); return __s;}
    283 
    284 inline ios_base& _STLP_CALL noshowpoint(ios_base& __s)
    285   { __s.unsetf(ios_base::showpoint); return __s;}
    286 
    287 inline ios_base& _STLP_CALL showpos(ios_base& __s)
    288   { __s.setf(ios_base::showpos); return __s;}
    289 
    290 inline ios_base& _STLP_CALL noshowpos(ios_base& __s)
    291   { __s.unsetf(ios_base::showpos); return __s;}
    292 
    293 inline ios_base& _STLP_CALL skipws(ios_base& __s)
    294   { __s.setf(ios_base::skipws); return __s;}
    295 
    296 inline ios_base& _STLP_CALL noskipws(ios_base& __s)
    297   { __s.unsetf(ios_base::skipws); return __s;}
    298 
    299 inline ios_base& _STLP_CALL uppercase(ios_base& __s)
    300   { __s.setf(ios_base::uppercase); return __s;}
    301 
    302 inline ios_base& _STLP_CALL nouppercase(ios_base& __s)
    303   { __s.unsetf(ios_base::uppercase); return __s;}
    304 
    305 inline ios_base& _STLP_CALL unitbuf(ios_base& __s)
    306   { __s.setf(ios_base::unitbuf); return __s;}
    307 
    308 inline ios_base& _STLP_CALL nounitbuf(ios_base& __s)
    309   { __s.unsetf(ios_base::unitbuf); return __s;}
    310 
    311 
    312 // adjustfield manipulators, section 27.4.5.2
    313 inline ios_base& _STLP_CALL internal(ios_base& __s)
    314   { __s.setf(ios_base::internal, ios_base::adjustfield); return __s; }
    315 
    316 inline ios_base& _STLP_CALL left(ios_base& __s)
    317   { __s.setf(ios_base::left, ios_base::adjustfield); return __s; }
    318 
    319 inline ios_base& _STLP_CALL right(ios_base& __s)
    320   { __s.setf(ios_base::right, ios_base::adjustfield); return __s; }
    321 
    322 // basefield manipulators, section 27.4.5.3
    323 inline ios_base& _STLP_CALL dec(ios_base& __s)
    324   { __s.setf(ios_base::dec, ios_base::basefield); return __s; }
    325 
    326 inline ios_base& _STLP_CALL hex(ios_base& __s)
    327   { __s.setf(ios_base::hex, ios_base::basefield); return __s; }
    328 
    329 inline ios_base& _STLP_CALL oct(ios_base& __s)
    330   { __s.setf(ios_base::oct, ios_base::basefield); return __s; }
    331 
    332 
    333 // floatfield manipulators, section 27.4.5.3
    334 inline ios_base& _STLP_CALL fixed(ios_base& __s)
    335   { __s.setf(ios_base::fixed, ios_base::floatfield); return __s; }
    336 
    337 inline ios_base& _STLP_CALL scientific(ios_base& __s)
    338   { __s.setf(ios_base::scientific, ios_base::floatfield); return __s; }
    339 
    340 _STLP_END_NAMESPACE
    341 
    342 #endif /* _STLP_IOS_BASE */
    343 
    344 // Local Variables:
    345 // mode:C++
    346 // End:
    347 
    348