home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / stlpt453.zip / STLport-4.5.3 / stlport / stl / _ios_base.h < prev    next >
C/C++ Source or Header  |  2002-01-18  |  12KB  |  409 lines

  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_STDEXCEPT
  22. #include <stdexcept>
  23. #endif
  24. #ifndef _STLP_UTILITY
  25. #include <utility>
  26. #endif
  27. #ifndef _STLP_INTERNAL_LOCALE_H
  28. #include <stl/_locale.h>
  29. #endif
  30. #ifndef _STLP_STRING_H
  31. # include <stl/_string.h>
  32. #endif
  33.  
  34. _STLP_BEGIN_NAMESPACE
  35.  
  36. // ----------------------------------------------------------------------
  37.  
  38. // Class ios_base.  This is the base class of the ios hierarchy, which
  39. // includes basic_istream and basic_ostream.  Classes in the ios
  40. // hierarchy are actually quite simple: they are just glorified
  41. // wrapper classes.  They delegate buffering and physical character
  42. // manipulation to the streambuf classes, and they delegate most
  43. // formatting tasks to a locale.
  44.  
  45. class _STLP_CLASS_DECLSPEC ios_base {
  46. public:
  47.   
  48.   class _STLP_CLASS_DECLSPEC failure : public __Named_exception {
  49.   public:
  50.     explicit failure(const string&);
  51.     virtual ~failure() _STLP_NOTHROW_INHERENTLY;
  52.   };
  53.  
  54.   typedef int fmtflags;
  55.   typedef int iostate;
  56.   typedef int openmode;
  57.   typedef int seekdir;
  58.  
  59. # ifndef _STLP_NO_ANACHRONISMS
  60.   typedef fmtflags fmt_flags;
  61. # endif
  62.  
  63.   // Formatting flags.
  64. # ifdef _STLP_STATIC_CONST_INIT_BUG
  65.   enum  {
  66. # else
  67.   // boris : type for all those constants is int   
  68.   static const int
  69. # endif
  70.     left       = 0x0001,
  71.     right      = 0x0002,
  72.     internal   = 0x0004,
  73.     dec        = 0x0008,
  74.     hex        = 0x0010,
  75.     oct        = 0x0020,
  76.     fixed      = 0x0040,
  77.     scientific = 0x0080,
  78.     boolalpha  = 0x0100,
  79.     showbase   = 0x0200,
  80.     showpoint  = 0x0400,
  81.     showpos    = 0x0800,
  82.     skipws     = 0x1000,
  83.     unitbuf    = 0x2000,
  84.     uppercase  = 0x4000,
  85.     adjustfield = left | right | internal,
  86.     basefield   = dec | hex | oct,
  87.     floatfield  = scientific | fixed,
  88.     
  89.     // State flags.
  90.     goodbit = 0x00,
  91.     badbit  = 0x01,
  92.     eofbit  = 0x02,
  93.     failbit = 0x04,
  94.     
  95.     // Openmode flags.
  96.     __default_mode = 0x0, /* implementation detail */
  97.     app    = 0x01,
  98.     ate    = 0x02,
  99.     binary = 0x04,
  100.     in     = 0x08,
  101.     out    = 0x10,
  102.     trunc  = 0x20,
  103.     
  104.     // Seekdir flags
  105.     
  106.     beg = 0x01,
  107.     cur = 0x02,
  108.     end = 0x04
  109. # ifdef _STLP_STATIC_CONST_INIT_BUG
  110.   }
  111. # endif
  112.   ;
  113.  
  114. public:                         // Flag-manipulation functions.
  115.   fmtflags flags() const { return _M_fmtflags; }
  116.   fmtflags flags(fmtflags __flags) {
  117.     fmtflags __tmp = _M_fmtflags;
  118.     _M_fmtflags = __flags;
  119.     return __tmp;
  120.   }
  121.  
  122.   fmtflags setf(fmtflags __flag) {
  123.     fmtflags __tmp = _M_fmtflags;
  124.     _M_fmtflags |= __flag;
  125.     return __tmp;
  126.   }
  127.   fmtflags setf(fmtflags __flag, fmtflags __mask) {
  128.     fmtflags __tmp = _M_fmtflags;
  129.     _M_fmtflags &= ~__mask;
  130.     _M_fmtflags |= __flag & __mask;
  131.     return __tmp;
  132.   }
  133.   void unsetf(fmtflags __mask) { _M_fmtflags &= ~__mask; }
  134.  
  135.   streamsize precision() const { return _M_precision; }
  136.   streamsize precision(streamsize __newprecision) {
  137.     streamsize __tmp = _M_precision;
  138.     _M_precision = __newprecision;
  139.     return __tmp;
  140.   }
  141.  
  142.   streamsize width() const { return _M_width; }
  143.   streamsize width(streamsize __newwidth) {
  144.     streamsize __tmp = _M_width;
  145.     _M_width = __newwidth;
  146.     return __tmp;
  147.   }
  148.  
  149. public:                         // Locales
  150.   locale imbue(const locale&);
  151.   locale getloc() const { return _M_locale; }
  152.  
  153. public:                         // Auxiliary storage.
  154.   static int _STLP_CALL xalloc();
  155.   long&  iword(int __index);
  156.   void*& pword(int __index);
  157.  
  158. public:                         // Destructor.
  159.   virtual ~ios_base();
  160.  
  161. public:                         // Callbacks.
  162.   enum event { erase_event, imbue_event, copyfmt_event };
  163.   typedef void (*event_callback)(event, ios_base&, int __index);
  164.   void register_callback(event_callback __fn, int __index);
  165.  
  166. public:                         // This member function affects only
  167.                                 // the eight predefined ios objects:
  168.                                 // cin, cout, etc.
  169.   static bool _STLP_CALL sync_with_stdio(bool __sync = true);
  170.  
  171. public:                         // The C++ standard requires only that these
  172.                                 // member functions be defined in basic_ios.
  173.                                 // We define them in the non-template
  174.                                 // base class to avoid code duplication.
  175.   operator void*() const { return !fail() ? (void*) __CONST_CAST(ios_base*,this) : (void*) 0; }
  176.   bool operator!() const { return fail(); }
  177.  
  178.   iostate rdstate() const { return _M_iostate; }
  179.  
  180.   bool good() const { return _M_iostate == 0; }
  181.   bool eof() const { return (_M_iostate & eofbit) != 0; }
  182.   bool fail() const { return (_M_iostate & (failbit | badbit)) != 0; }
  183.   bool bad() const { return (_M_iostate & badbit) != 0; }
  184.  
  185. protected:                      // The functional protected interface.
  186.  
  187.   // Copies the state of __x to *this.  This member function makes it
  188.   // possible to implement basic_ios::copyfmt without having to expose
  189.   // ios_base's private data members.  Does not copy _M_exception_mask
  190.   // or _M_iostate.
  191.   void _M_copy_state(const ios_base& __x);
  192.  
  193.   void _M_setstate_nothrow(iostate __state) { _M_iostate |= __state; }
  194.   void _M_clear_nothrow(iostate __state) { _M_iostate = __state; }
  195.   iostate _M_get_exception_mask() const { return _M_exception_mask; }
  196.   void _M_set_exception_mask(iostate __mask) { _M_exception_mask = __mask; }
  197.   void _M_check_exception_mask() { 
  198.     if (_M_iostate & _M_exception_mask)
  199.       _M_throw_failure(); 
  200.   }
  201.  
  202.   void _M_invoke_callbacks(event);
  203.   void _M_throw_failure();
  204.  
  205.   ios_base();                   // Default constructor.
  206.  
  207. protected:                        // Initialization of the I/O system
  208.   static void _STLP_CALL _S_initialize();
  209.   static void _STLP_CALL _S_uninitialize();
  210.   static bool _S_was_synced;
  211.   
  212. private:                        // Invalidate the copy constructor and
  213.                                 // assignment operator.
  214.   ios_base(const ios_base&);
  215.   void operator=(const ios_base&);
  216.  
  217. private:                        // Data members.
  218.  
  219.   fmtflags _M_fmtflags;         // Flags
  220.   iostate _M_iostate;
  221.   openmode _M_openmode;
  222.   seekdir _M_seekdir;
  223.   iostate _M_exception_mask;
  224.  
  225.   streamsize _M_precision;
  226.   streamsize _M_width;
  227.  
  228.   locale _M_locale;
  229.  
  230.   pair<event_callback, int>* _M_callbacks;
  231.   size_t _M_num_callbacks;      // Size of the callback array.
  232.   size_t _M_callback_index;     // Index of the next available callback;
  233.                                 // initially zero.
  234.  
  235.   long* _M_iwords;              // Auxiliary storage.  The count is zero
  236.   size_t _M_num_iwords;         // if and only if the pointer is null.
  237.  
  238.   void** _M_pwords;
  239.   size_t _M_num_pwords;
  240.  
  241.   static int _S_index;
  242.  
  243. protected:
  244.   // Cached copies of the curent locale's facets.  Set by init() and imbue().
  245.   locale::facet* _M_cached_ctype;
  246.   locale::facet* _M_cached_numpunct;
  247.   string         _M_cached_grouping;
  248. public:
  249.   // Equivalent to &use_facet< Facet >(getloc()), but faster.
  250.   const locale::facet* _M_ctype_facet() const { return _M_cached_ctype; }
  251.   const locale::facet* _M_numpunct_facet() const { return _M_cached_numpunct; }
  252.   const string&  _M_grouping() const { return _M_cached_grouping; }
  253. public:
  254.  
  255.   // ----------------------------------------------------------------------
  256.   // Nested initializer class.  This is an implementation detail, but it's
  257.   // prescribed by the standard.  The static initializer object (on 
  258.   // implementations where such a thing is required) is declared in
  259.   // <iostream>
  260.   
  261.   class _STLP_CLASS_DECLSPEC Init {
  262.   public:
  263.     Init();
  264.     ~Init();
  265.   private:
  266.     static long _S_count;
  267.     friend class ios_base;
  268.   };
  269.  
  270.   // this class is needed to ensure locale initialization w/o <iostream> inclusion
  271.   class _STLP_CLASS_DECLSPEC _Loc_init {
  272.   public:
  273.     _Loc_init();
  274.     ~_Loc_init();
  275.   private:
  276.     static long _S_count;
  277.     friend class ios_base;
  278.   };
  279.  
  280.   friend class Init;
  281.  
  282. public:
  283. # ifndef _STLP_NO_ANACHRONISMS
  284.   //  31.6  Old iostreams members                         [depr.ios.members]
  285.   typedef iostate  io_state;
  286.   typedef openmode open_mode;
  287.   typedef seekdir  seek_dir;
  288.   typedef _STLP_STD::streamoff  streamoff;
  289.   typedef _STLP_STD::streampos  streampos;
  290. # endif  
  291. };
  292.  
  293. template <class Facet>
  294. locale::facet* _M_get_facet(ios_base& __i, Facet*)
  295. {
  296.  
  297. }
  298.  
  299. // ----------------------------------------------------------------------
  300. // ios_base manipulator functions, from section 27.4.5 of the C++ standard.
  301. // All of them are trivial one-line wrapper functions.
  302.  
  303. // fmtflag manipulators, section 27.4.5.1
  304. inline ios_base& _STLP_CALL boolalpha(ios_base& __s)
  305.   { __s.setf(ios_base::boolalpha); return __s;}
  306.  
  307. inline ios_base& _STLP_CALL noboolalpha(ios_base& __s)
  308.   { __s.unsetf(ios_base::boolalpha); return __s;}
  309.  
  310. inline ios_base& _STLP_CALL showbase(ios_base& __s)
  311.   { __s.setf(ios_base::showbase); return __s;}
  312.  
  313. inline ios_base& _STLP_CALL noshowbase(ios_base& __s)
  314.   { __s.unsetf(ios_base::showbase); return __s;}
  315.  
  316. inline ios_base& _STLP_CALL showpoint(ios_base& __s)
  317.   { __s.setf(ios_base::showpoint); return __s;}
  318.  
  319. inline ios_base& _STLP_CALL noshowpoint(ios_base& __s)
  320.   { __s.unsetf(ios_base::showpoint); return __s;}
  321.  
  322. inline ios_base& _STLP_CALL showpos(ios_base& __s)
  323.   { __s.setf(ios_base::showpos); return __s;}
  324.  
  325. inline ios_base& _STLP_CALL noshowpos(ios_base& __s) 
  326.   { __s.unsetf(ios_base::showpos); return __s;}
  327.  
  328. inline ios_base& _STLP_CALL skipws(ios_base& __s)
  329.   { __s.setf(ios_base::skipws); return __s;}
  330.  
  331. inline ios_base& _STLP_CALL noskipws(ios_base& __s)
  332.   { __s.unsetf(ios_base::skipws); return __s;}
  333.  
  334. inline ios_base& _STLP_CALL uppercase(ios_base& __s)
  335.   { __s.setf(ios_base::uppercase); return __s;}
  336.  
  337. inline ios_base& _STLP_CALL nouppercase(ios_base& __s)
  338.   { __s.unsetf(ios_base::uppercase); return __s;}
  339.  
  340. inline ios_base& _STLP_CALL unitbuf(ios_base& __s)
  341.   { __s.setf(ios_base::unitbuf); return __s;}
  342.  
  343. inline ios_base& _STLP_CALL nounitbuf(ios_base& __s)
  344.   { __s.unsetf(ios_base::unitbuf); return __s;}
  345.  
  346.  
  347. // adjustfield manipulators, section 27.4.5.2
  348. inline ios_base& _STLP_CALL internal(ios_base& __s)
  349.   { __s.setf(ios_base::internal, ios_base::adjustfield); return __s; }
  350.  
  351. inline ios_base& _STLP_CALL left(ios_base& __s)
  352.   { __s.setf(ios_base::left, ios_base::adjustfield); return __s; }
  353.  
  354. inline ios_base& _STLP_CALL right(ios_base& __s)
  355.   { __s.setf(ios_base::right, ios_base::adjustfield); return __s; }
  356.  
  357. // basefield manipulators, section 27.4.5.3
  358. inline ios_base& _STLP_CALL dec(ios_base& __s)
  359.   { __s.setf(ios_base::dec, ios_base::basefield); return __s; }
  360.  
  361. inline ios_base& _STLP_CALL hex(ios_base& __s) 
  362.   { __s.setf(ios_base::hex, ios_base::basefield); return __s; }
  363.  
  364. inline ios_base& _STLP_CALL oct(ios_base& __s)
  365.   { __s.setf(ios_base::oct, ios_base::basefield); return __s; }
  366.  
  367.  
  368. // floatfield manipulators, section 27.4.5.3
  369. inline ios_base& _STLP_CALL fixed(ios_base& __s)
  370.   { __s.setf(ios_base::fixed, ios_base::floatfield); return __s; }
  371.  
  372. inline ios_base& _STLP_CALL scientific(ios_base& __s)
  373.   { __s.setf(ios_base::scientific, ios_base::floatfield); return __s; }
  374.  
  375. #if defined(__BORLANDC__) && defined(_RTLDLL)
  376.  
  377. long ios_base::_Loc_init::_S_count = 0;
  378.  
  379. void _STLP_CALL _Stl_loc_init_num_put();
  380. void _STLP_CALL _Stl_loc_init_num_get();
  381. void _STLP_CALL _Stl_loc_init_monetary();
  382. void _STLP_CALL _Stl_loc_init_time_facets();
  383.  
  384. inline ios_base::_Loc_init::_Loc_init() {
  385.   if (_S_count++ == 0) {
  386.       _Stl_loc_init_num_put();
  387.       _Stl_loc_init_num_get();
  388.       _Stl_loc_init_monetary();
  389.       _Stl_loc_init_time_facets();
  390.       locale::_S_initialize();
  391.   }
  392. }
  393.  
  394. inline ios_base::_Loc_init::~_Loc_init() {
  395.     if (--_S_count == 0)
  396.       locale::_S_uninitialize();
  397. }
  398.  
  399. #endif /* __BORLANDC__ */
  400.  
  401. _STLP_END_NAMESPACE
  402.  
  403. #endif /* _STLP_IOS_BASE */
  404.  
  405. // Local Variables:
  406. // mode:C++
  407. // End:
  408.  
  409.