1*404b540aSrobert // Iostreams base classes -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert // Copyright (C) 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005
4*404b540aSrobert // Free Software Foundation, Inc.
5*404b540aSrobert //
6*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free
7*404b540aSrobert // software; you can redistribute it and/or modify it under the
8*404b540aSrobert // terms of the GNU General Public License as published by the
9*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
10*404b540aSrobert // any later version.
11*404b540aSrobert
12*404b540aSrobert // This library is distributed in the hope that it will be useful,
13*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
14*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15*404b540aSrobert // GNU General Public License for more details.
16*404b540aSrobert
17*404b540aSrobert // You should have received a copy of the GNU General Public License along
18*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free
19*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
20*404b540aSrobert // USA.
21*404b540aSrobert
22*404b540aSrobert // As a special exception, you may use this file as part of a free software
23*404b540aSrobert // library without restriction. Specifically, if other files instantiate
24*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
25*404b540aSrobert // this file and link it with other files to produce an executable, this
26*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
27*404b540aSrobert // the GNU General Public License. This exception does not however
28*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
29*404b540aSrobert // the GNU General Public License.
30*404b540aSrobert
31*404b540aSrobert /** @file basic_ios.h
32*404b540aSrobert * This is an internal header file, included by other library headers.
33*404b540aSrobert * You should not attempt to use it directly.
34*404b540aSrobert */
35*404b540aSrobert
36*404b540aSrobert #ifndef _BASIC_IOS_H
37*404b540aSrobert #define _BASIC_IOS_H 1
38*404b540aSrobert
39*404b540aSrobert #pragma GCC system_header
40*404b540aSrobert
41*404b540aSrobert #include <bits/streambuf_iterator.h>
42*404b540aSrobert #include <bits/localefwd.h>
43*404b540aSrobert #include <bits/locale_classes.h>
44*404b540aSrobert #include <bits/locale_facets.h>
45*404b540aSrobert
_GLIBCXX_BEGIN_NAMESPACE(std)46*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
47*404b540aSrobert
48*404b540aSrobert // 27.4.5 Template class basic_ios
49*404b540aSrobert /**
50*404b540aSrobert * @brief Virtual base class for all stream classes.
51*404b540aSrobert *
52*404b540aSrobert * Most of the member functions called dispatched on stream objects
53*404b540aSrobert * (e.g., @c std::cout.foo(bar);) are consolidated in this class.
54*404b540aSrobert */
55*404b540aSrobert template<typename _CharT, typename _Traits>
56*404b540aSrobert class basic_ios : public ios_base
57*404b540aSrobert {
58*404b540aSrobert public:
59*404b540aSrobert //@{
60*404b540aSrobert /**
61*404b540aSrobert * These are standard types. They permit a standardized way of
62*404b540aSrobert * referring to names of (or names dependant on) the template
63*404b540aSrobert * parameters, which are specific to the implementation.
64*404b540aSrobert */
65*404b540aSrobert typedef _CharT char_type;
66*404b540aSrobert typedef typename _Traits::int_type int_type;
67*404b540aSrobert typedef typename _Traits::pos_type pos_type;
68*404b540aSrobert typedef typename _Traits::off_type off_type;
69*404b540aSrobert typedef _Traits traits_type;
70*404b540aSrobert //@}
71*404b540aSrobert
72*404b540aSrobert //@{
73*404b540aSrobert /**
74*404b540aSrobert * @if maint
75*404b540aSrobert * These are non-standard types.
76*404b540aSrobert * @endif
77*404b540aSrobert */
78*404b540aSrobert typedef ctype<_CharT> __ctype_type;
79*404b540aSrobert typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
80*404b540aSrobert __num_put_type;
81*404b540aSrobert typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
82*404b540aSrobert __num_get_type;
83*404b540aSrobert //@}
84*404b540aSrobert
85*404b540aSrobert // Data members:
86*404b540aSrobert protected:
87*404b540aSrobert basic_ostream<_CharT, _Traits>* _M_tie;
88*404b540aSrobert mutable char_type _M_fill;
89*404b540aSrobert mutable bool _M_fill_init;
90*404b540aSrobert basic_streambuf<_CharT, _Traits>* _M_streambuf;
91*404b540aSrobert
92*404b540aSrobert // Cached use_facet<ctype>, which is based on the current locale info.
93*404b540aSrobert const __ctype_type* _M_ctype;
94*404b540aSrobert // For ostream.
95*404b540aSrobert const __num_put_type* _M_num_put;
96*404b540aSrobert // For istream.
97*404b540aSrobert const __num_get_type* _M_num_get;
98*404b540aSrobert
99*404b540aSrobert public:
100*404b540aSrobert //@{
101*404b540aSrobert /**
102*404b540aSrobert * @brief The quick-and-easy status check.
103*404b540aSrobert *
104*404b540aSrobert * This allows you to write constructs such as
105*404b540aSrobert * "if (!a_stream) ..." and "while (a_stream) ..."
106*404b540aSrobert */
107*404b540aSrobert operator void*() const
108*404b540aSrobert { return this->fail() ? 0 : const_cast<basic_ios*>(this); }
109*404b540aSrobert
110*404b540aSrobert bool
111*404b540aSrobert operator!() const
112*404b540aSrobert { return this->fail(); }
113*404b540aSrobert //@}
114*404b540aSrobert
115*404b540aSrobert /**
116*404b540aSrobert * @brief Returns the error state of the stream buffer.
117*404b540aSrobert * @return A bit pattern (well, isn't everything?)
118*404b540aSrobert *
119*404b540aSrobert * See std::ios_base::iostate for the possible bit values. Most
120*404b540aSrobert * users will call one of the interpreting wrappers, e.g., good().
121*404b540aSrobert */
122*404b540aSrobert iostate
123*404b540aSrobert rdstate() const
124*404b540aSrobert { return _M_streambuf_state; }
125*404b540aSrobert
126*404b540aSrobert /**
127*404b540aSrobert * @brief [Re]sets the error state.
128*404b540aSrobert * @param state The new state flag(s) to set.
129*404b540aSrobert *
130*404b540aSrobert * See std::ios_base::iostate for the possible bit values. Most
131*404b540aSrobert * users will not need to pass an argument.
132*404b540aSrobert */
133*404b540aSrobert void
134*404b540aSrobert clear(iostate __state = goodbit);
135*404b540aSrobert
136*404b540aSrobert /**
137*404b540aSrobert * @brief Sets additional flags in the error state.
138*404b540aSrobert * @param state The additional state flag(s) to set.
139*404b540aSrobert *
140*404b540aSrobert * See std::ios_base::iostate for the possible bit values.
141*404b540aSrobert */
142*404b540aSrobert void
143*404b540aSrobert setstate(iostate __state)
144*404b540aSrobert { this->clear(this->rdstate() | __state); }
145*404b540aSrobert
146*404b540aSrobert // Flip the internal state on for the proper state bits, then re
147*404b540aSrobert // throws the propagated exception if bit also set in
148*404b540aSrobert // exceptions().
149*404b540aSrobert void
150*404b540aSrobert _M_setstate(iostate __state)
151*404b540aSrobert {
152*404b540aSrobert // 27.6.1.2.1 Common requirements.
153*404b540aSrobert // Turn this on without causing an ios::failure to be thrown.
154*404b540aSrobert _M_streambuf_state |= __state;
155*404b540aSrobert if (this->exceptions() & __state)
156*404b540aSrobert __throw_exception_again;
157*404b540aSrobert }
158*404b540aSrobert
159*404b540aSrobert /**
160*404b540aSrobert * @brief Fast error checking.
161*404b540aSrobert * @return True if no error flags are set.
162*404b540aSrobert *
163*404b540aSrobert * A wrapper around rdstate.
164*404b540aSrobert */
165*404b540aSrobert bool
166*404b540aSrobert good() const
167*404b540aSrobert { return this->rdstate() == 0; }
168*404b540aSrobert
169*404b540aSrobert /**
170*404b540aSrobert * @brief Fast error checking.
171*404b540aSrobert * @return True if the eofbit is set.
172*404b540aSrobert *
173*404b540aSrobert * Note that other iostate flags may also be set.
174*404b540aSrobert */
175*404b540aSrobert bool
176*404b540aSrobert eof() const
177*404b540aSrobert { return (this->rdstate() & eofbit) != 0; }
178*404b540aSrobert
179*404b540aSrobert /**
180*404b540aSrobert * @brief Fast error checking.
181*404b540aSrobert * @return True if either the badbit or the failbit is set.
182*404b540aSrobert *
183*404b540aSrobert * Checking the badbit in fail() is historical practice.
184*404b540aSrobert * Note that other iostate flags may also be set.
185*404b540aSrobert */
186*404b540aSrobert bool
187*404b540aSrobert fail() const
188*404b540aSrobert { return (this->rdstate() & (badbit | failbit)) != 0; }
189*404b540aSrobert
190*404b540aSrobert /**
191*404b540aSrobert * @brief Fast error checking.
192*404b540aSrobert * @return True if the badbit is set.
193*404b540aSrobert *
194*404b540aSrobert * Note that other iostate flags may also be set.
195*404b540aSrobert */
196*404b540aSrobert bool
197*404b540aSrobert bad() const
198*404b540aSrobert { return (this->rdstate() & badbit) != 0; }
199*404b540aSrobert
200*404b540aSrobert /**
201*404b540aSrobert * @brief Throwing exceptions on errors.
202*404b540aSrobert * @return The current exceptions mask.
203*404b540aSrobert *
204*404b540aSrobert * This changes nothing in the stream. See the one-argument version
205*404b540aSrobert * of exceptions(iostate) for the meaning of the return value.
206*404b540aSrobert */
207*404b540aSrobert iostate
208*404b540aSrobert exceptions() const
209*404b540aSrobert { return _M_exception; }
210*404b540aSrobert
211*404b540aSrobert /**
212*404b540aSrobert * @brief Throwing exceptions on errors.
213*404b540aSrobert * @param except The new exceptions mask.
214*404b540aSrobert *
215*404b540aSrobert * By default, error flags are set silently. You can set an
216*404b540aSrobert * exceptions mask for each stream; if a bit in the mask becomes set
217*404b540aSrobert * in the error flags, then an exception of type
218*404b540aSrobert * std::ios_base::failure is thrown.
219*404b540aSrobert *
220*404b540aSrobert * If the error flage is already set when the exceptions mask is
221*404b540aSrobert * added, the exception is immediately thrown. Try running the
222*404b540aSrobert * following under GCC 3.1 or later:
223*404b540aSrobert * @code
224*404b540aSrobert * #include <iostream>
225*404b540aSrobert * #include <fstream>
226*404b540aSrobert * #include <exception>
227*404b540aSrobert *
228*404b540aSrobert * int main()
229*404b540aSrobert * {
230*404b540aSrobert * std::set_terminate (__gnu_cxx::__verbose_terminate_handler);
231*404b540aSrobert *
232*404b540aSrobert * std::ifstream f ("/etc/motd");
233*404b540aSrobert *
234*404b540aSrobert * std::cerr << "Setting badbit\n";
235*404b540aSrobert * f.setstate (std::ios_base::badbit);
236*404b540aSrobert *
237*404b540aSrobert * std::cerr << "Setting exception mask\n";
238*404b540aSrobert * f.exceptions (std::ios_base::badbit);
239*404b540aSrobert * }
240*404b540aSrobert * @endcode
241*404b540aSrobert */
242*404b540aSrobert void
243*404b540aSrobert exceptions(iostate __except)
244*404b540aSrobert {
245*404b540aSrobert _M_exception = __except;
246*404b540aSrobert this->clear(_M_streambuf_state);
247*404b540aSrobert }
248*404b540aSrobert
249*404b540aSrobert // Constructor/destructor:
250*404b540aSrobert /**
251*404b540aSrobert * @brief Constructor performs initialization.
252*404b540aSrobert *
253*404b540aSrobert * The parameter is passed by derived streams.
254*404b540aSrobert */
255*404b540aSrobert explicit
256*404b540aSrobert basic_ios(basic_streambuf<_CharT, _Traits>* __sb)
257*404b540aSrobert : ios_base(), _M_tie(0), _M_fill(), _M_fill_init(false), _M_streambuf(0),
258*404b540aSrobert _M_ctype(0), _M_num_put(0), _M_num_get(0)
259*404b540aSrobert { this->init(__sb); }
260*404b540aSrobert
261*404b540aSrobert /**
262*404b540aSrobert * @brief Empty.
263*404b540aSrobert *
264*404b540aSrobert * The destructor does nothing. More specifically, it does not
265*404b540aSrobert * destroy the streambuf held by rdbuf().
266*404b540aSrobert */
267*404b540aSrobert virtual
268*404b540aSrobert ~basic_ios() { }
269*404b540aSrobert
270*404b540aSrobert // Members:
271*404b540aSrobert /**
272*404b540aSrobert * @brief Fetches the current @e tied stream.
273*404b540aSrobert * @return A pointer to the tied stream, or NULL if the stream is
274*404b540aSrobert * not tied.
275*404b540aSrobert *
276*404b540aSrobert * A stream may be @e tied (or synchronized) to a second output
277*404b540aSrobert * stream. When this stream performs any I/O, the tied stream is
278*404b540aSrobert * first flushed. For example, @c std::cin is tied to @c std::cout.
279*404b540aSrobert */
280*404b540aSrobert basic_ostream<_CharT, _Traits>*
281*404b540aSrobert tie() const
282*404b540aSrobert { return _M_tie; }
283*404b540aSrobert
284*404b540aSrobert /**
285*404b540aSrobert * @brief Ties this stream to an output stream.
286*404b540aSrobert * @param tiestr The output stream.
287*404b540aSrobert * @return The previously tied output stream, or NULL if the stream
288*404b540aSrobert * was not tied.
289*404b540aSrobert *
290*404b540aSrobert * This sets up a new tie; see tie() for more.
291*404b540aSrobert */
292*404b540aSrobert basic_ostream<_CharT, _Traits>*
293*404b540aSrobert tie(basic_ostream<_CharT, _Traits>* __tiestr)
294*404b540aSrobert {
295*404b540aSrobert basic_ostream<_CharT, _Traits>* __old = _M_tie;
296*404b540aSrobert _M_tie = __tiestr;
297*404b540aSrobert return __old;
298*404b540aSrobert }
299*404b540aSrobert
300*404b540aSrobert /**
301*404b540aSrobert * @brief Accessing the underlying buffer.
302*404b540aSrobert * @return The current stream buffer.
303*404b540aSrobert *
304*404b540aSrobert * This does not change the state of the stream.
305*404b540aSrobert */
306*404b540aSrobert basic_streambuf<_CharT, _Traits>*
307*404b540aSrobert rdbuf() const
308*404b540aSrobert { return _M_streambuf; }
309*404b540aSrobert
310*404b540aSrobert /**
311*404b540aSrobert * @brief Changing the underlying buffer.
312*404b540aSrobert * @param sb The new stream buffer.
313*404b540aSrobert * @return The previous stream buffer.
314*404b540aSrobert *
315*404b540aSrobert * Associates a new buffer with the current stream, and clears the
316*404b540aSrobert * error state.
317*404b540aSrobert *
318*404b540aSrobert * Due to historical accidents which the LWG refuses to correct, the
319*404b540aSrobert * I/O library suffers from a design error: this function is hidden
320*404b540aSrobert * in derived classes by overrides of the zero-argument @c rdbuf(),
321*404b540aSrobert * which is non-virtual for hysterical raisins. As a result, you
322*404b540aSrobert * must use explicit qualifications to access this function via any
323*404b540aSrobert * derived class. For example:
324*404b540aSrobert *
325*404b540aSrobert * @code
326*404b540aSrobert * std::fstream foo; // or some other derived type
327*404b540aSrobert * std::streambuf* p = .....;
328*404b540aSrobert *
329*404b540aSrobert * foo.ios::rdbuf(p); // ios == basic_ios<char>
330*404b540aSrobert * @endcode
331*404b540aSrobert */
332*404b540aSrobert basic_streambuf<_CharT, _Traits>*
333*404b540aSrobert rdbuf(basic_streambuf<_CharT, _Traits>* __sb);
334*404b540aSrobert
335*404b540aSrobert /**
336*404b540aSrobert * @brief Copies fields of __rhs into this.
337*404b540aSrobert * @param __rhs The source values for the copies.
338*404b540aSrobert * @return Reference to this object.
339*404b540aSrobert *
340*404b540aSrobert * All fields of __rhs are copied into this object except that rdbuf()
341*404b540aSrobert * and rdstate() remain unchanged. All values in the pword and iword
342*404b540aSrobert * arrays are copied. Before copying, each callback is invoked with
343*404b540aSrobert * erase_event. After copying, each (new) callback is invoked with
344*404b540aSrobert * copyfmt_event. The final step is to copy exceptions().
345*404b540aSrobert */
346*404b540aSrobert basic_ios&
347*404b540aSrobert copyfmt(const basic_ios& __rhs);
348*404b540aSrobert
349*404b540aSrobert /**
350*404b540aSrobert * @brief Retreives the "empty" character.
351*404b540aSrobert * @return The current fill character.
352*404b540aSrobert *
353*404b540aSrobert * It defaults to a space (' ') in the current locale.
354*404b540aSrobert */
355*404b540aSrobert char_type
356*404b540aSrobert fill() const
357*404b540aSrobert {
358*404b540aSrobert if (!_M_fill_init)
359*404b540aSrobert {
360*404b540aSrobert _M_fill = this->widen(' ');
361*404b540aSrobert _M_fill_init = true;
362*404b540aSrobert }
363*404b540aSrobert return _M_fill;
364*404b540aSrobert }
365*404b540aSrobert
366*404b540aSrobert /**
367*404b540aSrobert * @brief Sets a new "empty" character.
368*404b540aSrobert * @param ch The new character.
369*404b540aSrobert * @return The previous fill character.
370*404b540aSrobert *
371*404b540aSrobert * The fill character is used to fill out space when P+ characters
372*404b540aSrobert * have been requested (e.g., via setw), Q characters are actually
373*404b540aSrobert * used, and Q<P. It defaults to a space (' ') in the current locale.
374*404b540aSrobert */
375*404b540aSrobert char_type
376*404b540aSrobert fill(char_type __ch)
377*404b540aSrobert {
378*404b540aSrobert char_type __old = this->fill();
379*404b540aSrobert _M_fill = __ch;
380*404b540aSrobert return __old;
381*404b540aSrobert }
382*404b540aSrobert
383*404b540aSrobert // Locales:
384*404b540aSrobert /**
385*404b540aSrobert * @brief Moves to a new locale.
386*404b540aSrobert * @param loc The new locale.
387*404b540aSrobert * @return The previous locale.
388*404b540aSrobert *
389*404b540aSrobert * Calls @c ios_base::imbue(loc), and if a stream buffer is associated
390*404b540aSrobert * with this stream, calls that buffer's @c pubimbue(loc).
391*404b540aSrobert *
392*404b540aSrobert * Additional l10n notes are at
393*404b540aSrobert * http://gcc.gnu.org/onlinedocs/libstdc++/22_locale/howto.html
394*404b540aSrobert */
395*404b540aSrobert locale
396*404b540aSrobert imbue(const locale& __loc);
397*404b540aSrobert
398*404b540aSrobert /**
399*404b540aSrobert * @brief Squeezes characters.
400*404b540aSrobert * @param c The character to narrow.
401*404b540aSrobert * @param dfault The character to narrow.
402*404b540aSrobert * @return The narrowed character.
403*404b540aSrobert *
404*404b540aSrobert * Maps a character of @c char_type to a character of @c char,
405*404b540aSrobert * if possible.
406*404b540aSrobert *
407*404b540aSrobert * Returns the result of
408*404b540aSrobert * @code
409*404b540aSrobert * std::use_facet<ctype<char_type> >(getloc()).narrow(c,dfault)
410*404b540aSrobert * @endcode
411*404b540aSrobert *
412*404b540aSrobert * Additional l10n notes are at
413*404b540aSrobert * http://gcc.gnu.org/onlinedocs/libstdc++/22_locale/howto.html
414*404b540aSrobert */
415*404b540aSrobert char
416*404b540aSrobert narrow(char_type __c, char __dfault) const;
417*404b540aSrobert
418*404b540aSrobert /**
419*404b540aSrobert * @brief Widens characters.
420*404b540aSrobert * @param c The character to widen.
421*404b540aSrobert * @return The widened character.
422*404b540aSrobert *
423*404b540aSrobert * Maps a character of @c char to a character of @c char_type.
424*404b540aSrobert *
425*404b540aSrobert * Returns the result of
426*404b540aSrobert * @code
427*404b540aSrobert * std::use_facet<ctype<char_type> >(getloc()).widen(c)
428*404b540aSrobert * @endcode
429*404b540aSrobert *
430*404b540aSrobert * Additional l10n notes are at
431*404b540aSrobert * http://gcc.gnu.org/onlinedocs/libstdc++/22_locale/howto.html
432*404b540aSrobert */
433*404b540aSrobert char_type
434*404b540aSrobert widen(char __c) const;
435*404b540aSrobert
436*404b540aSrobert protected:
437*404b540aSrobert // 27.4.5.1 basic_ios constructors
438*404b540aSrobert /**
439*404b540aSrobert * @brief Empty.
440*404b540aSrobert *
441*404b540aSrobert * The default constructor does nothing and is not normally
442*404b540aSrobert * accessible to users.
443*404b540aSrobert */
444*404b540aSrobert basic_ios()
445*404b540aSrobert : ios_base(), _M_tie(0), _M_fill(char_type()), _M_fill_init(false),
446*404b540aSrobert _M_streambuf(0), _M_ctype(0), _M_num_put(0), _M_num_get(0)
447*404b540aSrobert { }
448*404b540aSrobert
449*404b540aSrobert /**
450*404b540aSrobert * @brief All setup is performed here.
451*404b540aSrobert *
452*404b540aSrobert * This is called from the public constructor. It is not virtual and
453*404b540aSrobert * cannot be redefined.
454*404b540aSrobert */
455*404b540aSrobert void
456*404b540aSrobert init(basic_streambuf<_CharT, _Traits>* __sb);
457*404b540aSrobert
458*404b540aSrobert void
459*404b540aSrobert _M_cache_locale(const locale& __loc);
460*404b540aSrobert };
461*404b540aSrobert
462*404b540aSrobert _GLIBCXX_END_NAMESPACE
463*404b540aSrobert
464*404b540aSrobert #ifndef _GLIBCXX_EXPORT_TEMPLATE
465*404b540aSrobert #include <bits/basic_ios.tcc>
466*404b540aSrobert #endif
467*404b540aSrobert
468*404b540aSrobert #endif /* _BASIC_IOS_H */
469