| // <print> Print functions -*- C++ -*- | |
| // Copyright The GNU Toolchain Authors. | |
| // | |
| // This file is part of the GNU ISO C++ Library. This library is free | |
| // software; you can redistribute it and/or modify it under the | |
| // terms of the GNU General Public License as published by the | |
| // Free Software Foundation; either version 3, or (at your option) | |
| // any later version. | |
| // This library is distributed in the hope that it will be useful, | |
| // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| // GNU General Public License for more details. | |
| // Under Section 7 of GPL version 3, you are granted additional | |
| // permissions described in the GCC Runtime Library Exception, version | |
| // 3.1, as published by the Free Software Foundation. | |
| // You should have received a copy of the GNU General Public License and | |
| // a copy of the GCC Runtime Library Exception along with this program; | |
| // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
| // <http://www.gnu.org/licenses/>. | |
| /** @file include/print | |
| * This is a Standard C++ Library header. | |
| */ | |
| #ifndef _GLIBCXX_PRINT | |
| #define _GLIBCXX_PRINT 1 | |
| #ifdef _GLIBCXX_SYSHDR | |
| #pragma GCC system_header | |
| #endif | |
| #include <bits/requires_hosted.h> // for std::format | |
| #define __glibcxx_want_print | |
| #include <bits/version.h> | |
| #ifdef __cpp_lib_print // C++ >= 23 | |
| #include <format> // format_args (TODO: move to bits/formatfwd.h?) | |
| #include <cstdio> // FILE, EOF, putc | |
| #include <cerrno> // EACCES, EIO | |
| #include <bits/functexcept.h> // __throw_system_error | |
| #ifdef _WIN32 | |
| # include <system_error> | |
| #endif | |
| #ifndef _GLIBCXX_NO_INLINE_PRINT | |
| # include <bits/print.h> | |
| #endif | |
| namespace std _GLIBCXX_VISIBILITY(default) | |
| { | |
| _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
| void | |
| vprint_nonunicode(FILE* __stream, string_view __fmt, format_args __args); | |
| void | |
| vprint_nonunicode_buffered(FILE* __stream, string_view __fmt, | |
| format_args __args); | |
| void | |
| vprint_unicode(FILE* __stream, string_view __fmt, format_args __args); | |
| void | |
| vprint_unicode_buffered(FILE* __stream, string_view __fmt, | |
| format_args __args); | |
| void | |
| vprint_unicode_buffered(string_view __fmt, format_args __args); | |
| void | |
| vprint_nonunicode_buffered(string_view __fmt, format_args __args); | |
| template<typename... _Args> | |
| inline void | |
| print(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args) | |
| { | |
| constexpr bool __locksafe = | |
| (enable_nonlocking_formatter_optimization<remove_cvref_t<_Args>> && ...); | |
| auto __fmtargs = std::make_format_args(__args...); | |
| #if defined(_WIN32) && !defined(__CYGWIN__) | |
| if constexpr (__unicode::__literal_encoding_is_utf8()) | |
| std::vprint_unicode_buffered(__stream, __fmt.get(), __fmtargs); | |
| else | |
| #endif | |
| if constexpr (__locksafe) | |
| std::vprint_nonunicode(__stream, __fmt.get(), __fmtargs); | |
| else | |
| std::vprint_nonunicode_buffered(__stream, __fmt.get(), __fmtargs); | |
| } | |
| template<typename... _Args> | |
| inline void | |
| print(format_string<_Args...> __fmt, _Args&&... __args) | |
| { std::print(stdout, __fmt, std::forward<_Args>(__args)...); } | |
| template<typename... _Args> | |
| inline void | |
| println(FILE* __stream, format_string<_Args...> __fmt, _Args&&... __args) | |
| { | |
| constexpr bool __locksafe = | |
| (enable_nonlocking_formatter_optimization<remove_cvref_t<_Args>> && ...); | |
| // The standard wants us to call | |
| // print(stream, dynamic_format(string(fmt.get()) + '\n'), args...) | |
| // here, but we can avoid that string concatenation in most cases, | |
| // and we know what that would call, so we can call that directly. | |
| auto __fmtargs = std::make_format_args(__args...); | |
| #if defined(_WIN32) && !defined(__CYGWIN__) | |
| if constexpr (__unicode::__literal_encoding_is_utf8()) | |
| { | |
| // We can't avoid the string concatenation here, but we can call | |
| // vprint_unicode_buffered directly, since that's what print would do. | |
| string __fmtn; | |
| __fmtn.reserve(__fmt.get().size() + 1); | |
| __fmtn = __fmt.get(); | |
| __fmtn += '\n'; | |
| std::vprint_unicode_buffered(__stream, __fmtn, __fmtargs); | |
| } | |
| else | |
| #endif | |
| #ifdef _GLIBCXX_NO_INLINE_PRINT | |
| { | |
| string __fmtn; | |
| __fmtn.reserve(__fmt.get().size() + 1); | |
| __fmtn = __fmt.get(); | |
| __fmtn += '\n'; | |
| std::vprint_nonunicode(__stream, __fmtn, __fmtargs); | |
| } | |
| #else | |
| // For non-Windows and for non-Unicode on Windows, we know that print | |
| // would call vprint_nonunicode or vprint_nonunicode_buffered with a | |
| // newline appended to the format-string. Use a _File_sink that adds | |
| // the newline automatically and write to it directly. | |
| if constexpr (__locksafe) | |
| std::vformat_to(__format::_File_sink(__stream, true).out(), | |
| __fmt.get(), __fmtargs); | |
| else | |
| { | |
| // Format to a string buffer first, then write the result to a | |
| // _File_sink that adds a newline. | |
| __format::_Str_sink<char> __buf; | |
| std::vformat_to(__buf.out(), __fmt.get(), __fmtargs); | |
| string_view __s(__buf.view()); | |
| __format::_File_sink(__stream, true).out() = __s; | |
| } | |
| #endif | |
| } | |
| template<typename... _Args> | |
| inline void | |
| println(format_string<_Args...> __fmt, _Args&&... __args) | |
| { std::println(stdout, __fmt, std::forward<_Args>(__args)...); } | |
| inline void | |
| vprint_unicode_buffered(string_view __fmt, format_args __args) | |
| { std::vprint_unicode_buffered(stdout, __fmt, __args); } | |
| inline void | |
| vprint_nonunicode_buffered(string_view __fmt, format_args __args) | |
| { std::vprint_nonunicode_buffered(stdout, __fmt, __args); } | |
| // Defined for C++26, supported as an extension to C++23. | |
| inline void println(FILE* __stream) | |
| { | |
| #if defined(_WIN32) && !defined(__CYGWIN__) | |
| if constexpr (__unicode::__literal_encoding_is_utf8()) | |
| std::vprint_unicode_buffered(__stream, "\n", std::make_format_args()); | |
| else | |
| #endif | |
| if (std::putc('\n', __stream) == EOF) | |
| __throw_system_error(EIO); | |
| } | |
| inline void println() { std::println(stdout); } | |
| _GLIBCXX_END_NAMESPACE_VERSION | |
| } // namespace std | |
| #endif // __cpp_lib_print | |
| #endif // _GLIBCXX_PRINT |