_ios_base.h 11 KB

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