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