1 // Iostreams base classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2001, 2002, 2003
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
36 #ifndef _CPP_BITS_BASICIOS_H
37 #define _CPP_BITS_BASICIOS_H 1
39 #pragma GCC system_header
41 #include <bits/streambuf_iterator.h>
42 #include <bits/localefwd.h>
43 #include <bits/locale_classes.h>
44 #include <bits/locale_facets.h>
48 // 27.4.5 Template class basic_ios
50 * @brief Virtual base class for all stream classes.
52 * Most of the member functions called dispatched on stream objects
53 * (e.g., @c std::cout.foo(bar);) are consolidated in this class.
55 template<typename _CharT, typename _Traits>
56 class basic_ios : public ios_base
61 * These are standard types. They permit a standardized way of
62 * referring to names of (or names dependant on) the template
63 * parameters, which are specific to the implementation.
65 typedef _CharT char_type;
66 typedef typename _Traits::int_type int_type;
67 typedef typename _Traits::pos_type pos_type;
68 typedef typename _Traits::off_type off_type;
69 typedef _Traits traits_type;
75 * These are non-standard types.
78 typedef ctype<_CharT> __ctype_type;
79 typedef ostreambuf_iterator<_CharT, _Traits> __ostreambuf_iter;
80 typedef num_put<_CharT, __ostreambuf_iter> __numput_type;
81 typedef istreambuf_iterator<_CharT, _Traits> __istreambuf_iter;
82 typedef num_get<_CharT, __istreambuf_iter> __numget_type;
85 friend void ios_base::Init::_S_ios_create(bool);
89 basic_ostream<_CharT, _Traits>* _M_tie;
90 mutable char_type _M_fill;
91 mutable bool _M_fill_init;
92 basic_streambuf<_CharT, _Traits>* _M_streambuf;
94 // Cached use_facet<ctype>, which is based on the current locale info.
95 const __ctype_type* _M_fctype;
97 const __numput_type* _M_fnumput;
99 const __numget_type* _M_fnumget;
104 * @brief The quick-and-easy status check.
106 * This allows you to write constructs such as
107 * "if (!a_stream) ..." and "while (a_stream) ..."
109 operator void*() const
110 { return this->fail() ? 0 : const_cast<basic_ios*>(this); }
114 { return this->fail(); }
118 * @brief Returns the error state of the stream buffer.
119 * @return A bit pattern (well, isn't everything?)
121 * See std::ios_base::iostate for the possible bit values. Most
122 * users will call one of the interpreting wrappers, e.g., good().
126 { return _M_streambuf_state; }
129 * @brief [Re]sets the error state.
130 * @param state The new state flag(s) to set.
132 * See std::ios_base::iostate for the possible bit values. Most
133 * users will not need to pass an argument.
136 clear(iostate __state = goodbit);
139 * @brief Sets additional flags in the error state.
140 * @param state The additional state flag(s) to set.
142 * See std::ios_base::iostate for the possible bit values.
145 setstate(iostate __state)
146 { this->clear(this->rdstate() | __state); }
149 * @brief Fast error checking.
150 * @return True if no error flags are set.
152 * A wrapper around rdstate.
156 { return this->rdstate() == 0; }
159 * @brief Fast error checking.
160 * @return True if the eofbit is set.
162 * Note that other iostate flags may also be set.
166 { return (this->rdstate() & eofbit) != 0; }
169 * @brief Fast error checking.
170 * @return True if either the badbit or the failbit is set.
172 * Checking the badbit in fail() is historical practice.
173 * Note that other iostate flags may also be set.
177 { return (this->rdstate() & (badbit | failbit)) != 0; }
180 * @brief Fast error checking.
181 * @return True if the badbit is set.
183 * Note that other iostate flags may also be set.
187 { return (this->rdstate() & badbit) != 0; }
190 * @brief Throwing exceptions on errors.
191 * @return The current exceptions mask.
193 * This changes nothing in the stream. See the one-argument version
194 * of exceptions(iostate) for the meaning of the return value.
198 { return _M_exception; }
201 * @brief Throwing exceptions on errors.
202 * @param except The new exceptions mask.
204 * By default, error flags are set silently. You can set an
205 * exceptions mask for each stream; if a bit in the mask becomes set
206 * in the error flags, then an exception of type
207 * std::ios_base::failure is thrown.
209 * If the error flage is already set when the exceptions mask is
210 * added, the exception is immediately thrown. Try running the
211 * following under GCC 3.1 or later:
213 * #include <iostream>
215 * #include <exception>
219 * std::set_terminate (__gnu_cxx::__verbose_terminate_handler);
221 * std::ifstream f ("/etc/motd");
223 * std::cerr << "Setting badbit\n";
224 * f.setstate (std::ios_base::badbit);
226 * std::cerr << "Setting exception mask\n";
227 * f.exceptions (std::ios_base::badbit);
232 exceptions(iostate __except)
234 _M_exception = __except;
235 this->clear(_M_streambuf_state);
238 // Constructor/destructor:
240 * @brief Constructor performs initialization.
242 * The parameter is passed by derived streams.
245 basic_ios(basic_streambuf<_CharT, _Traits>* __sb)
246 : ios_base(), _M_fctype(0), _M_fnumput(0), _M_fnumget(0)
247 { this->init(__sb); }
252 * The destructor does nothing. More specifically, it does not
253 * destroy the streambuf held by rdbuf().
260 * @brief Fetches the current @e tied stream.
261 * @return A pointer to the tied stream, or NULL if the stream is
264 * A stream may be @e tied (or synchronized) to a second output
265 * stream. When this stream performs any I/O, the tied stream is
266 * first flushed. For example, @c std::cin is tied to @c std::cout.
268 basic_ostream<_CharT, _Traits>*
273 * @brief Ties this stream to an output stream.
274 * @param tiestr The output stream.
275 * @return The previously tied output stream, or NULL if the stream
278 * This sets up a new tie; see tie() for more.
280 basic_ostream<_CharT, _Traits>*
281 tie(basic_ostream<_CharT, _Traits>* __tiestr)
283 basic_ostream<_CharT, _Traits>* __old = _M_tie;
289 * @brief Accessing the underlying buffer.
290 * @return The current stream buffer.
292 * This does not change the state of the stream.
294 basic_streambuf<_CharT, _Traits>*
296 { return _M_streambuf; }
299 * @brief Changing the underlying buffer.
300 * @param sb The new stream buffer.
301 * @return The previous stream buffer.
303 * Associates a new buffer with the current stream, and clears the
306 * Due to historical accidents which the LWG refuses to correct, the
307 * I/O library suffers from a design error: this function is hidden
308 * in derived classes by overrides of the zero-argument @c rdbuf(),
309 * which is non-virtual for hysterical raisins. As a result, you
310 * must use explicit qualifications to access this function via any
313 basic_streambuf<_CharT, _Traits>*
314 rdbuf(basic_streambuf<_CharT, _Traits>* __sb);
320 copyfmt(const basic_ios& __rhs);
323 * @brief Retreives the "empty" character.
324 * @return The current fill character.
326 * It defaults to a space (' ') in the current locale.
333 _M_fill = this->widen(' ');
340 * @brief Sets a new "empty" character.
341 * @param ch The new character.
342 * @return The previous fill character.
344 * The fill character is used to fill out space when P+ characters
345 * have been requested (e.g., via setw), Q characters are actually
346 * used, and Q<P. It defaults to a space (' ') in the current locale.
351 char_type __old = this->fill();
358 * @brief Moves to a new locale.
359 * @param loc The new locale.
360 * @return The previous locale.
362 * Calls @c ios_base::imbue(loc), and if a stream buffer is associated
363 * with this stream, calls that buffer's @c pubimbue(loc).
365 * Additional l10n notes are at
366 * http://gcc.gnu.org/onlinedocs/libstdc++/22_locale/howto.html
369 imbue(const locale& __loc);
372 * @brief Squeezes characters.
373 * @param c The character to narrow.
374 * @param dfault The character to narrow.
375 * @return The narrowed character.
377 * Maps a character of @c char_type to a character of @c char,
380 * Returns the result of
382 * std::use_facet< ctype<char_type> >(getloc()).narrow(c,dfault)
385 * Additional l10n notes are at
386 * http://gcc.gnu.org/onlinedocs/libstdc++/22_locale/howto.html
389 narrow(char_type __c, char __dfault) const;
392 * @brief Widens characters.
393 * @param c The character to widen.
394 * @return The widened character.
396 * Maps a character of @c char to a character of @c char_type.
398 * Returns the result of
400 * std::use_facet< ctype<char_type> >(getloc()).widen(c)
403 * Additional l10n notes are at
404 * http://gcc.gnu.org/onlinedocs/libstdc++/22_locale/howto.html
407 widen(char __c) const;
410 // 27.4.5.1 basic_ios constructors
414 * The default constructor does nothing and is not normally
415 * accessible to users.
417 basic_ios() : ios_base()
421 * @brief All setup is performed here.
423 * This is called from the public constructor. It is not virtual and
424 * cannot be redefined. The second argument, __cache, is used
425 * to initialize the standard streams without allocating
429 init(basic_streambuf<_CharT, _Traits>* __sb);
432 _M_check_facet(const locale::facet* __f) const
440 _M_cache_locale(const locale& __loc);
443 // XXX GLIBCXX_ABI Deprecated, compatibility only.
445 _M_cache_facets(const locale& __loc);
448 // Internal state setter that won't throw, only set the state bits.
449 // Used to guarantee we don't throw when setting badbit.
451 _M_setstate(iostate __state) { _M_streambuf_state |= __state; }
455 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
457 #include <bits/basic_ios.tcc>
460 #endif /* _CPP_BITS_BASICIOS_H */