1*404b540aSrobert // Output streams -*- C++ -*-
2*404b540aSrobert
3*404b540aSrobert // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4*404b540aSrobert // 2006, 2007
5*404b540aSrobert // Free Software Foundation, Inc.
6*404b540aSrobert //
7*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free
8*404b540aSrobert // software; you can redistribute it and/or modify it under the
9*404b540aSrobert // terms of the GNU General Public License as published by the
10*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
11*404b540aSrobert // any later version.
12*404b540aSrobert
13*404b540aSrobert // This library is distributed in the hope that it will be useful,
14*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*404b540aSrobert // GNU General Public License for more details.
17*404b540aSrobert
18*404b540aSrobert // You should have received a copy of the GNU General Public License along
19*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free
20*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21*404b540aSrobert // USA.
22*404b540aSrobert
23*404b540aSrobert // As a special exception, you may use this file as part of a free software
24*404b540aSrobert // library without restriction. Specifically, if other files instantiate
25*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
26*404b540aSrobert // this file and link it with other files to produce an executable, this
27*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
28*404b540aSrobert // the GNU General Public License. This exception does not however
29*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
30*404b540aSrobert // the GNU General Public License.
31*404b540aSrobert
32*404b540aSrobert /** @file ostream
33*404b540aSrobert * This is a Standard C++ Library header.
34*404b540aSrobert */
35*404b540aSrobert
36*404b540aSrobert //
37*404b540aSrobert // ISO C++ 14882: 27.6.2 Output streams
38*404b540aSrobert //
39*404b540aSrobert
40*404b540aSrobert #ifndef _GLIBCXX_OSTREAM
41*404b540aSrobert #define _GLIBCXX_OSTREAM 1
42*404b540aSrobert
43*404b540aSrobert #pragma GCC system_header
44*404b540aSrobert
45*404b540aSrobert #include <ios>
46*404b540aSrobert #include <bits/ostream_insert.h>
47*404b540aSrobert
_GLIBCXX_BEGIN_NAMESPACE(std)48*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
49*404b540aSrobert
50*404b540aSrobert // [27.6.2.1] Template class basic_ostream
51*404b540aSrobert /**
52*404b540aSrobert * @brief Controlling output.
53*404b540aSrobert *
54*404b540aSrobert * This is the base class for all output streams. It provides text
55*404b540aSrobert * formatting of all builtin types, and communicates with any class
56*404b540aSrobert * derived from basic_streambuf to do the actual output.
57*404b540aSrobert */
58*404b540aSrobert template<typename _CharT, typename _Traits>
59*404b540aSrobert class basic_ostream : virtual public basic_ios<_CharT, _Traits>
60*404b540aSrobert {
61*404b540aSrobert public:
62*404b540aSrobert // Types (inherited from basic_ios (27.4.4)):
63*404b540aSrobert typedef _CharT char_type;
64*404b540aSrobert typedef typename _Traits::int_type int_type;
65*404b540aSrobert typedef typename _Traits::pos_type pos_type;
66*404b540aSrobert typedef typename _Traits::off_type off_type;
67*404b540aSrobert typedef _Traits traits_type;
68*404b540aSrobert
69*404b540aSrobert // Non-standard Types:
70*404b540aSrobert typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
71*404b540aSrobert typedef basic_ios<_CharT, _Traits> __ios_type;
72*404b540aSrobert typedef basic_ostream<_CharT, _Traits> __ostream_type;
73*404b540aSrobert typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
74*404b540aSrobert __num_put_type;
75*404b540aSrobert typedef ctype<_CharT> __ctype_type;
76*404b540aSrobert
77*404b540aSrobert // [27.6.2.2] constructor/destructor
78*404b540aSrobert /**
79*404b540aSrobert * @brief Base constructor.
80*404b540aSrobert *
81*404b540aSrobert * This ctor is almost never called by the user directly, rather from
82*404b540aSrobert * derived classes' initialization lists, which pass a pointer to
83*404b540aSrobert * their own stream buffer.
84*404b540aSrobert */
85*404b540aSrobert explicit
86*404b540aSrobert basic_ostream(__streambuf_type* __sb)
87*404b540aSrobert { this->init(__sb); }
88*404b540aSrobert
89*404b540aSrobert /**
90*404b540aSrobert * @brief Base destructor.
91*404b540aSrobert *
92*404b540aSrobert * This does very little apart from providing a virtual base dtor.
93*404b540aSrobert */
94*404b540aSrobert virtual
95*404b540aSrobert ~basic_ostream() { }
96*404b540aSrobert
97*404b540aSrobert // [27.6.2.3] prefix/suffix
98*404b540aSrobert class sentry;
99*404b540aSrobert friend class sentry;
100*404b540aSrobert
101*404b540aSrobert // [27.6.2.5] formatted output
102*404b540aSrobert // [27.6.2.5.3] basic_ostream::operator<<
103*404b540aSrobert //@{
104*404b540aSrobert /**
105*404b540aSrobert * @brief Interface for manipulators.
106*404b540aSrobert *
107*404b540aSrobert * Manuipulators such as @c std::endl and @c std::hex use these
108*404b540aSrobert * functions in constructs like "std::cout << std::endl". For more
109*404b540aSrobert * information, see the iomanip header.
110*404b540aSrobert */
111*404b540aSrobert __ostream_type&
112*404b540aSrobert operator<<(__ostream_type& (*__pf)(__ostream_type&))
113*404b540aSrobert {
114*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
115*404b540aSrobert // DR 60. What is a formatted input function?
116*404b540aSrobert // The inserters for manipulators are *not* formatted output functions.
117*404b540aSrobert return __pf(*this);
118*404b540aSrobert }
119*404b540aSrobert
120*404b540aSrobert __ostream_type&
121*404b540aSrobert operator<<(__ios_type& (*__pf)(__ios_type&))
122*404b540aSrobert {
123*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
124*404b540aSrobert // DR 60. What is a formatted input function?
125*404b540aSrobert // The inserters for manipulators are *not* formatted output functions.
126*404b540aSrobert __pf(*this);
127*404b540aSrobert return *this;
128*404b540aSrobert }
129*404b540aSrobert
130*404b540aSrobert __ostream_type&
131*404b540aSrobert operator<<(ios_base& (*__pf) (ios_base&))
132*404b540aSrobert {
133*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
134*404b540aSrobert // DR 60. What is a formatted input function?
135*404b540aSrobert // The inserters for manipulators are *not* formatted output functions.
136*404b540aSrobert __pf(*this);
137*404b540aSrobert return *this;
138*404b540aSrobert }
139*404b540aSrobert //@}
140*404b540aSrobert
141*404b540aSrobert // [27.6.2.5.2] arithmetic inserters
142*404b540aSrobert /**
143*404b540aSrobert * @name Arithmetic Inserters
144*404b540aSrobert *
145*404b540aSrobert * All the @c operator<< functions (aka <em>formatted output
146*404b540aSrobert * functions</em>) have some common behavior. Each starts by
147*404b540aSrobert * constructing a temporary object of type std::basic_ostream::sentry.
148*404b540aSrobert * This can have several effects, concluding with the setting of a
149*404b540aSrobert * status flag; see the sentry documentation for more.
150*404b540aSrobert *
151*404b540aSrobert * If the sentry status is good, the function tries to generate
152*404b540aSrobert * whatever data is appropriate for the type of the argument.
153*404b540aSrobert *
154*404b540aSrobert * If an exception is thrown during insertion, ios_base::badbit
155*404b540aSrobert * will be turned on in the stream's error state without causing an
156*404b540aSrobert * ios_base::failure to be thrown. The original exception will then
157*404b540aSrobert * be rethrown.
158*404b540aSrobert */
159*404b540aSrobert //@{
160*404b540aSrobert /**
161*404b540aSrobert * @brief Basic arithmetic inserters
162*404b540aSrobert * @param A variable of builtin type.
163*404b540aSrobert * @return @c *this if successful
164*404b540aSrobert *
165*404b540aSrobert * These functions use the stream's current locale (specifically, the
166*404b540aSrobert * @c num_get facet) to perform numeric formatting.
167*404b540aSrobert */
168*404b540aSrobert __ostream_type&
169*404b540aSrobert operator<<(long __n)
170*404b540aSrobert { return _M_insert(__n); }
171*404b540aSrobert
172*404b540aSrobert __ostream_type&
173*404b540aSrobert operator<<(unsigned long __n)
174*404b540aSrobert { return _M_insert(__n); }
175*404b540aSrobert
176*404b540aSrobert __ostream_type&
177*404b540aSrobert operator<<(bool __n)
178*404b540aSrobert { return _M_insert(__n); }
179*404b540aSrobert
180*404b540aSrobert __ostream_type&
181*404b540aSrobert operator<<(short __n);
182*404b540aSrobert
183*404b540aSrobert __ostream_type&
184*404b540aSrobert operator<<(unsigned short __n)
185*404b540aSrobert {
186*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
187*404b540aSrobert // 117. basic_ostream uses nonexistent num_put member functions.
188*404b540aSrobert return _M_insert(static_cast<unsigned long>(__n));
189*404b540aSrobert }
190*404b540aSrobert
191*404b540aSrobert __ostream_type&
192*404b540aSrobert operator<<(int __n);
193*404b540aSrobert
194*404b540aSrobert __ostream_type&
195*404b540aSrobert operator<<(unsigned int __n)
196*404b540aSrobert {
197*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
198*404b540aSrobert // 117. basic_ostream uses nonexistent num_put member functions.
199*404b540aSrobert return _M_insert(static_cast<unsigned long>(__n));
200*404b540aSrobert }
201*404b540aSrobert
202*404b540aSrobert #ifdef _GLIBCXX_USE_LONG_LONG
203*404b540aSrobert __ostream_type&
204*404b540aSrobert operator<<(long long __n)
205*404b540aSrobert { return _M_insert(__n); }
206*404b540aSrobert
207*404b540aSrobert __ostream_type&
208*404b540aSrobert operator<<(unsigned long long __n)
209*404b540aSrobert { return _M_insert(__n); }
210*404b540aSrobert #endif
211*404b540aSrobert
212*404b540aSrobert __ostream_type&
213*404b540aSrobert operator<<(double __f)
214*404b540aSrobert { return _M_insert(__f); }
215*404b540aSrobert
216*404b540aSrobert __ostream_type&
217*404b540aSrobert operator<<(float __f)
218*404b540aSrobert {
219*404b540aSrobert // _GLIBCXX_RESOLVE_LIB_DEFECTS
220*404b540aSrobert // 117. basic_ostream uses nonexistent num_put member functions.
221*404b540aSrobert return _M_insert(static_cast<double>(__f));
222*404b540aSrobert }
223*404b540aSrobert
224*404b540aSrobert __ostream_type&
225*404b540aSrobert operator<<(long double __f)
226*404b540aSrobert { return _M_insert(__f); }
227*404b540aSrobert
228*404b540aSrobert __ostream_type&
229*404b540aSrobert operator<<(const void* __p)
230*404b540aSrobert { return _M_insert(__p); }
231*404b540aSrobert
232*404b540aSrobert /**
233*404b540aSrobert * @brief Extracting from another streambuf.
234*404b540aSrobert * @param sb A pointer to a streambuf
235*404b540aSrobert *
236*404b540aSrobert * This function behaves like one of the basic arithmetic extractors,
237*404b540aSrobert * in that it also constructs a sentry object and has the same error
238*404b540aSrobert * handling behavior.
239*404b540aSrobert *
240*404b540aSrobert * If @a sb is NULL, the stream will set failbit in its error state.
241*404b540aSrobert *
242*404b540aSrobert * Characters are extracted from @a sb and inserted into @c *this
243*404b540aSrobert * until one of the following occurs:
244*404b540aSrobert *
245*404b540aSrobert * - the input stream reaches end-of-file,
246*404b540aSrobert * - insertion into the output sequence fails (in this case, the
247*404b540aSrobert * character that would have been inserted is not extracted), or
248*404b540aSrobert * - an exception occurs while getting a character from @a sb, which
249*404b540aSrobert * sets failbit in the error state
250*404b540aSrobert *
251*404b540aSrobert * If the function inserts no characters, failbit is set.
252*404b540aSrobert */
253*404b540aSrobert __ostream_type&
254*404b540aSrobert operator<<(__streambuf_type* __sb);
255*404b540aSrobert //@}
256*404b540aSrobert
257*404b540aSrobert // [27.6.2.6] unformatted output functions
258*404b540aSrobert /**
259*404b540aSrobert * @name Unformatted Output Functions
260*404b540aSrobert *
261*404b540aSrobert * All the unformatted output functions have some common behavior.
262*404b540aSrobert * Each starts by constructing a temporary object of type
263*404b540aSrobert * std::basic_ostream::sentry. This has several effects, concluding
264*404b540aSrobert * with the setting of a status flag; see the sentry documentation
265*404b540aSrobert * for more.
266*404b540aSrobert *
267*404b540aSrobert * If the sentry status is good, the function tries to generate
268*404b540aSrobert * whatever data is appropriate for the type of the argument.
269*404b540aSrobert *
270*404b540aSrobert * If an exception is thrown during insertion, ios_base::badbit
271*404b540aSrobert * will be turned on in the stream's error state. If badbit is on in
272*404b540aSrobert * the stream's exceptions mask, the exception will be rethrown
273*404b540aSrobert * without completing its actions.
274*404b540aSrobert */
275*404b540aSrobert //@{
276*404b540aSrobert /**
277*404b540aSrobert * @brief Simple insertion.
278*404b540aSrobert * @param c The character to insert.
279*404b540aSrobert * @return *this
280*404b540aSrobert *
281*404b540aSrobert * Tries to insert @a c.
282*404b540aSrobert *
283*404b540aSrobert * @note This function is not overloaded on signed char and
284*404b540aSrobert * unsigned char.
285*404b540aSrobert */
286*404b540aSrobert __ostream_type&
287*404b540aSrobert put(char_type __c);
288*404b540aSrobert
289*404b540aSrobert // Core write functionality, without sentry.
290*404b540aSrobert void
291*404b540aSrobert _M_write(const char_type* __s, streamsize __n)
292*404b540aSrobert {
293*404b540aSrobert const streamsize __put = this->rdbuf()->sputn(__s, __n);
294*404b540aSrobert if (__put != __n)
295*404b540aSrobert this->setstate(ios_base::badbit);
296*404b540aSrobert }
297*404b540aSrobert
298*404b540aSrobert /**
299*404b540aSrobert * @brief Character string insertion.
300*404b540aSrobert * @param s The array to insert.
301*404b540aSrobert * @param n Maximum number of characters to insert.
302*404b540aSrobert * @return *this
303*404b540aSrobert *
304*404b540aSrobert * Characters are copied from @a s and inserted into the stream until
305*404b540aSrobert * one of the following happens:
306*404b540aSrobert *
307*404b540aSrobert * - @a n characters are inserted
308*404b540aSrobert * - inserting into the output sequence fails (in this case, badbit
309*404b540aSrobert * will be set in the stream's error state)
310*404b540aSrobert *
311*404b540aSrobert * @note This function is not overloaded on signed char and
312*404b540aSrobert * unsigned char.
313*404b540aSrobert */
314*404b540aSrobert __ostream_type&
315*404b540aSrobert write(const char_type* __s, streamsize __n);
316*404b540aSrobert //@}
317*404b540aSrobert
318*404b540aSrobert /**
319*404b540aSrobert * @brief Synchronizing the stream buffer.
320*404b540aSrobert * @return *this
321*404b540aSrobert *
322*404b540aSrobert * If @c rdbuf() is a null pointer, changes nothing.
323*404b540aSrobert *
324*404b540aSrobert * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
325*404b540aSrobert * sets badbit.
326*404b540aSrobert */
327*404b540aSrobert __ostream_type&
328*404b540aSrobert flush();
329*404b540aSrobert
330*404b540aSrobert // [27.6.2.4] seek members
331*404b540aSrobert /**
332*404b540aSrobert * @brief Getting the current write position.
333*404b540aSrobert * @return A file position object.
334*404b540aSrobert *
335*404b540aSrobert * If @c fail() is not false, returns @c pos_type(-1) to indicate
336*404b540aSrobert * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
337*404b540aSrobert */
338*404b540aSrobert pos_type
339*404b540aSrobert tellp();
340*404b540aSrobert
341*404b540aSrobert /**
342*404b540aSrobert * @brief Changing the current write position.
343*404b540aSrobert * @param pos A file position object.
344*404b540aSrobert * @return *this
345*404b540aSrobert *
346*404b540aSrobert * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
347*404b540aSrobert * that function fails, sets failbit.
348*404b540aSrobert */
349*404b540aSrobert __ostream_type&
350*404b540aSrobert seekp(pos_type);
351*404b540aSrobert
352*404b540aSrobert /**
353*404b540aSrobert * @brief Changing the current write position.
354*404b540aSrobert * @param off A file offset object.
355*404b540aSrobert * @param dir The direction in which to seek.
356*404b540aSrobert * @return *this
357*404b540aSrobert *
358*404b540aSrobert * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
359*404b540aSrobert * If that function fails, sets failbit.
360*404b540aSrobert */
361*404b540aSrobert __ostream_type&
362*404b540aSrobert seekp(off_type, ios_base::seekdir);
363*404b540aSrobert
364*404b540aSrobert protected:
365*404b540aSrobert explicit
366*404b540aSrobert basic_ostream() { }
367*404b540aSrobert
368*404b540aSrobert template<typename _ValueT>
369*404b540aSrobert __ostream_type&
370*404b540aSrobert _M_insert(_ValueT __v);
371*404b540aSrobert };
372*404b540aSrobert
373*404b540aSrobert /**
374*404b540aSrobert * @brief Performs setup work for output streams.
375*404b540aSrobert *
376*404b540aSrobert * Objects of this class are created before all of the standard
377*404b540aSrobert * inserters are run. It is responsible for "exception-safe prefix and
378*404b540aSrobert * suffix operations." Additional actions may be added by the
379*404b540aSrobert * implementation, and we list them in
380*404b540aSrobert * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5
381*404b540aSrobert * under [27.6] notes.
382*404b540aSrobert */
383*404b540aSrobert template <typename _CharT, typename _Traits>
384*404b540aSrobert class basic_ostream<_CharT, _Traits>::sentry
385*404b540aSrobert {
386*404b540aSrobert // Data Members:
387*404b540aSrobert bool _M_ok;
388*404b540aSrobert basic_ostream<_CharT, _Traits>& _M_os;
389*404b540aSrobert
390*404b540aSrobert public:
391*404b540aSrobert /**
392*404b540aSrobert * @brief The constructor performs preparatory work.
393*404b540aSrobert * @param os The output stream to guard.
394*404b540aSrobert *
395*404b540aSrobert * If the stream state is good (@a os.good() is true), then if the
396*404b540aSrobert * stream is tied to another output stream, @c is.tie()->flush()
397*404b540aSrobert * is called to synchronize the output sequences.
398*404b540aSrobert *
399*404b540aSrobert * If the stream state is still good, then the sentry state becomes
400*404b540aSrobert * true ("okay").
401*404b540aSrobert */
402*404b540aSrobert explicit
403*404b540aSrobert sentry(basic_ostream<_CharT, _Traits>& __os);
404*404b540aSrobert
405*404b540aSrobert /**
406*404b540aSrobert * @brief Possibly flushes the stream.
407*404b540aSrobert *
408*404b540aSrobert * If @c ios_base::unitbuf is set in @c os.flags(), and
409*404b540aSrobert * @c std::uncaught_exception() is true, the sentry destructor calls
410*404b540aSrobert * @c flush() on the output stream.
411*404b540aSrobert */
~sentry()412*404b540aSrobert ~sentry()
413*404b540aSrobert {
414*404b540aSrobert // XXX MT
415*404b540aSrobert if (_M_os.flags() & ios_base::unitbuf && !uncaught_exception())
416*404b540aSrobert {
417*404b540aSrobert // Can't call flush directly or else will get into recursive lock.
418*404b540aSrobert if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
419*404b540aSrobert _M_os.setstate(ios_base::badbit);
420*404b540aSrobert }
421*404b540aSrobert }
422*404b540aSrobert
423*404b540aSrobert /**
424*404b540aSrobert * @brief Quick status checking.
425*404b540aSrobert * @return The sentry state.
426*404b540aSrobert *
427*404b540aSrobert * For ease of use, sentries may be converted to booleans. The
428*404b540aSrobert * return value is that of the sentry state (true == okay).
429*404b540aSrobert */
430*404b540aSrobert operator bool() const
431*404b540aSrobert { return _M_ok; }
432*404b540aSrobert };
433*404b540aSrobert
434*404b540aSrobert // [27.6.2.5.4] character insertion templates
435*404b540aSrobert //@{
436*404b540aSrobert /**
437*404b540aSrobert * @brief Character inserters
438*404b540aSrobert * @param out An output stream.
439*404b540aSrobert * @param c A character.
440*404b540aSrobert * @return out
441*404b540aSrobert *
442*404b540aSrobert * Behaves like one of the formatted arithmetic inserters described in
443*404b540aSrobert * std::basic_ostream. After constructing a sentry object with good
444*404b540aSrobert * status, this function inserts a single character and any required
445*404b540aSrobert * padding (as determined by [22.2.2.2.2]). @c out.width(0) is then
446*404b540aSrobert * called.
447*404b540aSrobert *
448*404b540aSrobert * If @a c is of type @c char and the character type of the stream is not
449*404b540aSrobert * @c char, the character is widened before insertion.
450*404b540aSrobert */
451*404b540aSrobert template<typename _CharT, typename _Traits>
452*404b540aSrobert inline basic_ostream<_CharT, _Traits>&
453*404b540aSrobert operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
454*404b540aSrobert { return __ostream_insert(__out, &__c, 1); }
455*404b540aSrobert
456*404b540aSrobert template<typename _CharT, typename _Traits>
457*404b540aSrobert inline basic_ostream<_CharT, _Traits>&
458*404b540aSrobert operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
459*404b540aSrobert { return (__out << __out.widen(__c)); }
460*404b540aSrobert
461*404b540aSrobert // Specialization
462*404b540aSrobert template <class _Traits>
463*404b540aSrobert inline basic_ostream<char, _Traits>&
464*404b540aSrobert operator<<(basic_ostream<char, _Traits>& __out, char __c)
465*404b540aSrobert { return __ostream_insert(__out, &__c, 1); }
466*404b540aSrobert
467*404b540aSrobert // Signed and unsigned
468*404b540aSrobert template<class _Traits>
469*404b540aSrobert inline basic_ostream<char, _Traits>&
470*404b540aSrobert operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
471*404b540aSrobert { return (__out << static_cast<char>(__c)); }
472*404b540aSrobert
473*404b540aSrobert template<class _Traits>
474*404b540aSrobert inline basic_ostream<char, _Traits>&
475*404b540aSrobert operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
476*404b540aSrobert { return (__out << static_cast<char>(__c)); }
477*404b540aSrobert //@}
478*404b540aSrobert
479*404b540aSrobert //@{
480*404b540aSrobert /**
481*404b540aSrobert * @brief String inserters
482*404b540aSrobert * @param out An output stream.
483*404b540aSrobert * @param s A character string.
484*404b540aSrobert * @return out
485*404b540aSrobert * @pre @a s must be a non-NULL pointer
486*404b540aSrobert *
487*404b540aSrobert * Behaves like one of the formatted arithmetic inserters described in
488*404b540aSrobert * std::basic_ostream. After constructing a sentry object with good
489*404b540aSrobert * status, this function inserts @c traits::length(s) characters starting
490*404b540aSrobert * at @a s, widened if necessary, followed by any required padding (as
491*404b540aSrobert * determined by [22.2.2.2.2]). @c out.width(0) is then called.
492*404b540aSrobert */
493*404b540aSrobert template<typename _CharT, typename _Traits>
494*404b540aSrobert inline basic_ostream<_CharT, _Traits>&
495*404b540aSrobert operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
496*404b540aSrobert {
497*404b540aSrobert if (!__s)
498*404b540aSrobert __out.setstate(ios_base::badbit);
499*404b540aSrobert else
500*404b540aSrobert __ostream_insert(__out, __s,
501*404b540aSrobert static_cast<streamsize>(_Traits::length(__s)));
502*404b540aSrobert return __out;
503*404b540aSrobert }
504*404b540aSrobert
505*404b540aSrobert template<typename _CharT, typename _Traits>
506*404b540aSrobert basic_ostream<_CharT, _Traits> &
507*404b540aSrobert operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
508*404b540aSrobert
509*404b540aSrobert // Partial specializationss
510*404b540aSrobert template<class _Traits>
511*404b540aSrobert inline basic_ostream<char, _Traits>&
512*404b540aSrobert operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
513*404b540aSrobert {
514*404b540aSrobert if (!__s)
515*404b540aSrobert __out.setstate(ios_base::badbit);
516*404b540aSrobert else
517*404b540aSrobert __ostream_insert(__out, __s,
518*404b540aSrobert static_cast<streamsize>(_Traits::length(__s)));
519*404b540aSrobert return __out;
520*404b540aSrobert }
521*404b540aSrobert
522*404b540aSrobert // Signed and unsigned
523*404b540aSrobert template<class _Traits>
524*404b540aSrobert inline basic_ostream<char, _Traits>&
525*404b540aSrobert operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
526*404b540aSrobert { return (__out << reinterpret_cast<const char*>(__s)); }
527*404b540aSrobert
528*404b540aSrobert template<class _Traits>
529*404b540aSrobert inline basic_ostream<char, _Traits> &
530*404b540aSrobert operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
531*404b540aSrobert { return (__out << reinterpret_cast<const char*>(__s)); }
532*404b540aSrobert //@}
533*404b540aSrobert
534*404b540aSrobert // [27.6.2.7] standard basic_ostream manipulators
535*404b540aSrobert /**
536*404b540aSrobert * @brief Write a newline and flush the stream.
537*404b540aSrobert *
538*404b540aSrobert * This manipulator is often mistakenly used when a simple newline is
539*404b540aSrobert * desired, leading to poor buffering performance. See
540*404b540aSrobert * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more
541*404b540aSrobert * on this subject.
542*404b540aSrobert */
543*404b540aSrobert template<typename _CharT, typename _Traits>
544*404b540aSrobert inline basic_ostream<_CharT, _Traits>&
endl(basic_ostream<_CharT,_Traits> & __os)545*404b540aSrobert endl(basic_ostream<_CharT, _Traits>& __os)
546*404b540aSrobert { return flush(__os.put(__os.widen('\n'))); }
547*404b540aSrobert
548*404b540aSrobert /**
549*404b540aSrobert * @brief Write a null character into the output sequence.
550*404b540aSrobert *
551*404b540aSrobert * "Null character" is @c CharT() by definition. For CharT of @c char,
552*404b540aSrobert * this correctly writes the ASCII @c NUL character string terminator.
553*404b540aSrobert */
554*404b540aSrobert template<typename _CharT, typename _Traits>
555*404b540aSrobert inline basic_ostream<_CharT, _Traits>&
ends(basic_ostream<_CharT,_Traits> & __os)556*404b540aSrobert ends(basic_ostream<_CharT, _Traits>& __os)
557*404b540aSrobert { return __os.put(_CharT()); }
558*404b540aSrobert
559*404b540aSrobert /**
560*404b540aSrobert * @brief Flushes the output stream.
561*404b540aSrobert *
562*404b540aSrobert * This manipulator simply calls the stream's @c flush() member function.
563*404b540aSrobert */
564*404b540aSrobert template<typename _CharT, typename _Traits>
565*404b540aSrobert inline basic_ostream<_CharT, _Traits>&
flush(basic_ostream<_CharT,_Traits> & __os)566*404b540aSrobert flush(basic_ostream<_CharT, _Traits>& __os)
567*404b540aSrobert { return __os.flush(); }
568*404b540aSrobert
569*404b540aSrobert _GLIBCXX_END_NAMESPACE
570*404b540aSrobert
571*404b540aSrobert #ifndef _GLIBCXX_EXPORT_TEMPLATE
572*404b540aSrobert # include <bits/ostream.tcc>
573*404b540aSrobert #endif
574*404b540aSrobert
575*404b540aSrobert #endif /* _GLIBCXX_OSTREAM */
576