3 * Silicon Graphics Computer Systems, Inc.
8 * This material is provided "as is", with absolutely no warranty expressed
9 * or implied. Any use is at your own risk.
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.
18 #ifndef _STLP_IOS_BASE_H
19 #define _STLP_IOS_BASE_H
21 #ifndef _STLP_INTERNAL_STDEXCEPT_BASE
22 # include <stl/_stdexcept_base.h>
29 #ifndef _STLP_INTERNAL_LOCALE_H
30 # include <stl/_locale.h>
33 #ifndef _STLP_INTERNAL_STRING_H
34 # include <stl/_string.h>
39 // ----------------------------------------------------------------------
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.
48 class _STLP_CLASS_DECLSPEC ios_base {
51 class _STLP_CLASS_DECLSPEC failure : public __Named_exception {
53 explicit failure(const string&);
54 virtual ~failure() _STLP_NOTHROW_INHERENTLY;
62 # ifndef _STLP_NO_ANACHRONISMS
63 typedef fmtflags fmt_flags;
67 #if defined (_STLP_STATIC_CONST_INIT_BUG)
70 // boris : type for all those constants is int
88 adjustfield = left | right | internal,
89 basefield = dec | hex | oct,
90 floatfield = scientific | fixed,
99 __default_mode = 0x0, /* implementation detail */
112 # ifdef _STLP_STATIC_CONST_INIT_BUG
117 public: // Flag-manipulation functions.
118 fmtflags flags() const { return _M_fmtflags; }
119 fmtflags flags(fmtflags __flags) {
120 fmtflags __tmp = _M_fmtflags;
121 _M_fmtflags = __flags;
125 fmtflags setf(fmtflags __flag) {
126 fmtflags __tmp = _M_fmtflags;
127 _M_fmtflags |= __flag;
130 fmtflags setf(fmtflags __flag, fmtflags __mask) {
131 fmtflags __tmp = _M_fmtflags;
132 _M_fmtflags &= ~__mask;
133 _M_fmtflags |= __flag & __mask;
136 void unsetf(fmtflags __mask) { _M_fmtflags &= ~__mask; }
138 streamsize precision() const { return _M_precision; }
139 streamsize precision(streamsize __newprecision) {
140 streamsize __tmp = _M_precision;
141 _M_precision = __newprecision;
145 streamsize width() const { return _M_width; }
146 streamsize width(streamsize __newwidth) {
147 streamsize __tmp = _M_width;
148 _M_width = __newwidth;
153 locale imbue(const locale&);
154 locale getloc() const { return _M_locale; }
156 public: // Auxiliary storage.
157 static int _STLP_CALL xalloc();
158 long& iword(int __index);
159 void*& pword(int __index);
161 public: // Destructor.
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);
169 public: // This member function affects only
170 // the eight predefined ios objects:
172 static bool _STLP_CALL sync_with_stdio(bool __sync = true);
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(); }
181 iostate rdstate() const { return _M_iostate; }
183 bool good() const { return _M_iostate == 0; }
184 bool eof() const { return (_M_iostate & eofbit) != 0; }
185 bool fail() const { return (_M_iostate & (failbit | badbit)) != 0; }
186 bool bad() const { return (_M_iostate & badbit) != 0; }
188 protected: // The functional protected interface.
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
194 void _M_copy_state(const ios_base& __x);
196 void _M_setstate_nothrow(iostate __state) { _M_iostate |= __state; }
197 void _M_clear_nothrow(iostate __state) { _M_iostate = __state; }
198 iostate _M_get_exception_mask() const { return _M_exception_mask; }
199 void _M_set_exception_mask(iostate __mask) { _M_exception_mask = __mask; }
200 void _M_check_exception_mask() {
201 if (_M_iostate & _M_exception_mask)
205 void _M_invoke_callbacks(event);
206 void _STLP_FUNCTION_THROWS _M_throw_failure();
208 ios_base(); // Default constructor.
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;
215 private: // Invalidate the copy constructor and
216 // assignment operator.
217 ios_base(const ios_base&);
218 void operator=(const ios_base&);
220 private: // Data members.
222 fmtflags _M_fmtflags; // Flags
224 openmode _M_openmode;
226 iostate _M_exception_mask;
228 streamsize _M_precision;
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;
238 long* _M_iwords; // Auxiliary storage. The count is zero
239 size_t _M_num_iwords; // if and only if the pointer is null.
242 size_t _M_num_pwords;
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;
250 // Equivalent to &use_facet< Facet >(getloc()), but faster.
251 const locale::facet* _M_ctype_facet() const { return _M_cached_ctype; }
252 const locale::facet* _M_numpunct_facet() const { return _M_cached_numpunct; }
253 const string& _M_grouping() const { return _M_cached_grouping; }
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
262 class _STLP_CLASS_DECLSPEC Init
268 static long _S_count;
269 friend class ios_base;
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;
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.
289 // fmtflag manipulators, section 27.4.5.1
290 inline ios_base& _STLP_CALL boolalpha(ios_base& __s)
291 { __s.setf(ios_base::boolalpha); return __s;}
293 inline ios_base& _STLP_CALL noboolalpha(ios_base& __s)
294 { __s.unsetf(ios_base::boolalpha); return __s;}
296 inline ios_base& _STLP_CALL showbase(ios_base& __s)
297 { __s.setf(ios_base::showbase); return __s;}
299 inline ios_base& _STLP_CALL noshowbase(ios_base& __s)
300 { __s.unsetf(ios_base::showbase); return __s;}
302 inline ios_base& _STLP_CALL showpoint(ios_base& __s)
303 { __s.setf(ios_base::showpoint); return __s;}
305 inline ios_base& _STLP_CALL noshowpoint(ios_base& __s)
306 { __s.unsetf(ios_base::showpoint); return __s;}
308 inline ios_base& _STLP_CALL showpos(ios_base& __s)
309 { __s.setf(ios_base::showpos); return __s;}
311 inline ios_base& _STLP_CALL noshowpos(ios_base& __s)
312 { __s.unsetf(ios_base::showpos); return __s;}
314 inline ios_base& _STLP_CALL skipws(ios_base& __s)
315 { __s.setf(ios_base::skipws); return __s;}
317 inline ios_base& _STLP_CALL noskipws(ios_base& __s)
318 { __s.unsetf(ios_base::skipws); return __s;}
320 inline ios_base& _STLP_CALL uppercase(ios_base& __s)
321 { __s.setf(ios_base::uppercase); return __s;}
323 inline ios_base& _STLP_CALL nouppercase(ios_base& __s)
324 { __s.unsetf(ios_base::uppercase); return __s;}
326 inline ios_base& _STLP_CALL unitbuf(ios_base& __s)
327 { __s.setf(ios_base::unitbuf); return __s;}
329 inline ios_base& _STLP_CALL nounitbuf(ios_base& __s)
330 { __s.unsetf(ios_base::unitbuf); return __s;}
333 // adjustfield manipulators, section 27.4.5.2
334 inline ios_base& _STLP_CALL internal(ios_base& __s)
335 { __s.setf(ios_base::internal, ios_base::adjustfield); return __s; }
337 inline ios_base& _STLP_CALL left(ios_base& __s)
338 { __s.setf(ios_base::left, ios_base::adjustfield); return __s; }
340 inline ios_base& _STLP_CALL right(ios_base& __s)
341 { __s.setf(ios_base::right, ios_base::adjustfield); return __s; }
343 // basefield manipulators, section 27.4.5.3
344 inline ios_base& _STLP_CALL dec(ios_base& __s)
345 { __s.setf(ios_base::dec, ios_base::basefield); return __s; }
347 inline ios_base& _STLP_CALL hex(ios_base& __s)
348 { __s.setf(ios_base::hex, ios_base::basefield); return __s; }
350 inline ios_base& _STLP_CALL oct(ios_base& __s)
351 { __s.setf(ios_base::oct, ios_base::basefield); return __s; }
354 // floatfield manipulators, section 27.4.5.3
355 inline ios_base& _STLP_CALL fixed(ios_base& __s)
356 { __s.setf(ios_base::fixed, ios_base::floatfield); return __s; }
358 inline ios_base& _STLP_CALL scientific(ios_base& __s)
359 { __s.setf(ios_base::scientific, ios_base::floatfield); return __s; }
363 #endif /* _STLP_IOS_BASE */