1// Output streams -*- C++ -*-
3// Copyright (C) 1997-2025 Free Software Foundation, Inc.
5// This file is part of the GNU ISO C++ Library. This library is free
6// software; you can redistribute it and/or modify it under the
7// terms of the GNU General Public License as published by the
8// Free Software Foundation; either version 3, or (at your option)
11// This library is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
16// Under Section 7 of GPL version 3, you are granted additional
17// permissions described in the GCC Runtime Library Exception, version
18// 3.1, as published by the Free Software Foundation.
20// You should have received a copy of the GNU General Public License and
21// a copy of the GCC Runtime Library Exception along with this program;
22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23// <http://www.gnu.org/licenses/>.
25/** @file include/ostream
26 * This is a Standard C++ Library header.
30// ISO C++ 14882: 27.6.2 Output streams
33#ifndef _GLIBCXX_OSTREAM
34#define _GLIBCXX_OSTREAM 1
37#pragma GCC system_header
40#include <bits/requires_hosted.h> // iostreams
42#include <bits/ostream.h>
43#if __cplusplus > 202002L
47# define __glibcxx_want_print
48#include <bits/version.h> // __glibcxx_syncbuf
50namespace std _GLIBCXX_VISIBILITY(default)
52_GLIBCXX_BEGIN_NAMESPACE_VERSION
54 // Standard basic_ostream manipulators
57 * @brief Write a newline and flush the stream.
59 * This manipulator is often mistakenly used when a simple newline is
60 * desired, leading to poor buffering performance. See
61 * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
62 * for more on this subject.
64 template<typename _CharT, typename _Traits>
65 inline basic_ostream<_CharT, _Traits>&
66 endl(basic_ostream<_CharT, _Traits>& __os)
67 { return flush(__os.put(__os.widen('\n'))); }
70 * @brief Write a null character into the output sequence.
72 * <em>Null character</em> is @c CharT() by definition. For CharT
73 * of @c char, this correctly writes the ASCII @c NUL character
76 template<typename _CharT, typename _Traits>
77 inline basic_ostream<_CharT, _Traits>&
78 ends(basic_ostream<_CharT, _Traits>& __os)
79 { return __os.put(_CharT()); }
82 * @brief Flushes the output stream.
84 * This manipulator simply calls the stream's @c flush() member function.
86 template<typename _CharT, typename _Traits>
87 inline basic_ostream<_CharT, _Traits>&
88 flush(basic_ostream<_CharT, _Traits>& __os)
89 { return __os.flush(); }
91#ifdef __glibcxx_syncbuf // C++ >= 20 && HOSTED && CXX11ABI
92 template<typename _CharT, typename _Traits>
93 class __syncbuf_base : public basic_streambuf<_CharT, _Traits>
97 _S_get(basic_streambuf<_CharT, _Traits>* __buf [[maybe_unused]]) noexcept
100 if (auto __p = dynamic_cast<__syncbuf_base*>(__buf))
101 return &__p->_M_emit_on_sync;
107 __syncbuf_base(basic_streambuf<_CharT, _Traits>* __w = nullptr)
111 basic_streambuf<_CharT, _Traits>* _M_wrapped = nullptr;
112 bool _M_emit_on_sync = false;
113 bool _M_needs_sync = false;
116 template<typename _CharT, typename _Traits>
117 inline basic_ostream<_CharT, _Traits>&
118 emit_on_flush(basic_ostream<_CharT, _Traits>& __os)
120 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
125 template<typename _CharT, typename _Traits>
126 inline basic_ostream<_CharT, _Traits>&
127 noemit_on_flush(basic_ostream<_CharT, _Traits>& __os)
129 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
134 template<typename _CharT, typename _Traits>
135 inline basic_ostream<_CharT, _Traits>&
136 flush_emit(basic_ostream<_CharT, _Traits>& __os)
140 ~_Restore() { *_M_flag = _M_prev; }
142 bool _M_prev = false;
143 bool* _M_flag = &_M_prev;
146 if (bool* __flag = __syncbuf_base<_CharT, _Traits>::_S_get(__os.rdbuf()))
148 __restore._M_prev = *__flag;
149 __restore._M_flag = __flag;
156#endif // __glibcxx_syncbuf
158#if __cpp_lib_print // C++ >= 23
160 vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args)
162 ostream::sentry __cerb(__os);
165 __format::_Str_sink<char> __buf;
166 std::vformat_to(__buf.out(), __os.getloc(), __fmt, __args);
167 auto __out = __buf.view();
171 std::__ostream_write(__os, __out.data(), __out.size());
173 __catch(const __cxxabiv1::__forced_unwind&)
175 __os._M_setstate(ios_base::badbit);
176 __throw_exception_again;
179 { __os._M_setstate(ios_base::badbit); }
184 vprint_unicode(ostream& __os, string_view __fmt, format_args __args)
186#if !defined(_WIN32) || defined(__CYGWIN__)
187 // For most targets we don't need to do anything special to write
188 // Unicode to a terminal.
189 std::vprint_nonunicode(__os, __fmt, __args);
191 ostream::sentry __cerb(__os);
194 __format::_Str_sink<char> __buf;
195 std::vformat_to(__buf.out(), __os.getloc(), __fmt, __args);
196 auto __out = __buf._M_span();
198 void* __open_terminal(streambuf*);
199 error_code __write_to_terminal(void*, span<char>);
200 // If stream refers to a terminal, write a Unicode string to it.
201 if (auto __term = __open_terminal(__os.rdbuf()))
203#if !defined(_WIN32) || defined(__CYGWIN__)
204 // For POSIX, __open_terminal(streambuf*) uses fdopen to open a
205 // new file, so we would need to close it here. This code is not
206 // actually compiled because it's inside an #ifdef _WIN32 group,
207 // but just in case that changes in future ...
210 _Guard(void* __p) : _M_f((FILE*)__p) { }
211 ~_Guard() { std::fclose(_M_f); }
212 _Guard(_Guard&&) = delete;
213 _Guard& operator=(_Guard&&) = delete;
219 ios_base::iostate __err = ios_base::goodbit;
222 if (__os.rdbuf()->pubsync() == -1)
224 else if (auto __e = __write_to_terminal(__term, __out))
225 if (__e != std::make_error_code(errc::illegal_byte_sequence))
228 __catch(const __cxxabiv1::__forced_unwind&)
230 __os._M_setstate(ios_base::badbit);
231 __throw_exception_again;
234 { __os._M_setstate(ios_base::badbit); }
237 __os.setstate(__err);
241 // Otherwise just insert the string as vprint_nonunicode does.
244 std::__ostream_write(__os, __out.data(), __out.size());
246 __catch(const __cxxabiv1::__forced_unwind&)
248 __os._M_setstate(ios_base::badbit);
249 __throw_exception_again;
252 { __os._M_setstate(ios_base::badbit); }
257 template<typename... _Args>
259 print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args)
261 auto __fmtargs = std::make_format_args(__args...);
262 if constexpr (__unicode::__literal_encoding_is_utf8())
263 std::vprint_unicode(__os, __fmt.get(), __fmtargs);
265 std::vprint_nonunicode(__os, __fmt.get(), __fmtargs);
268 template<typename... _Args>
270 println(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args)
272 // _GLIBCXX_RESOLVE_LIB_DEFECTS
273 // 4088. println ignores the locale imbued in std::ostream
274 std::print(__os, "{}\n", std::format(__os.getloc(), __fmt,
275 std::forward<_Args>(__args)...));
278 // Defined for C++26, supported as an extension to C++23.
279 inline void println(ostream& __os)
281#if defined(_WIN32) && !defined(__CYGWIN__)
282 if constexpr (__unicode::__literal_encoding_is_utf8())
283 std::vprint_unicode(__os, "\n", std::make_format_args());
289#endif // __cpp_lib_print
291_GLIBCXX_END_NAMESPACE_VERSION
294#include <bits/ostream.tcc>
296#endif /* _GLIBCXX_OSTREAM */