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