Path: blob/main/contrib/llvm-project/libcxx/include/__ostream/print.h
35233 views
//===---------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===---------------------------------------------------------------------===//78#ifndef _LIBCPP___OSTREAM_PRINT_H9#define _LIBCPP___OSTREAM_PRINT_H1011#include <__config>12#include <__fwd/ostream.h>13#include <__iterator/ostreambuf_iterator.h>14#include <__ostream/basic_ostream.h>15#include <format>16#include <ios>17#include <locale>18#include <print>1920#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)21# pragma GCC system_header22#endif2324_LIBCPP_BEGIN_NAMESPACE_STD2526#if _LIBCPP_STD_VER >= 232728template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).29_LIBCPP_HIDE_FROM_ABI inline void30__vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {31// [ostream.formatted.print]/332// Effects: Behaves as a formatted output function33// ([ostream.formatted.reqmts]) of os, except that:34// - failure to generate output is reported as specified below, and35// - any exception thrown by the call to vformat is propagated without regard36// to the value of os.exceptions() and without turning on ios_base::badbit37// in the error state of os.38// After constructing a sentry object, the function initializes an automatic39// variable via40// string out = vformat(os.getloc(), fmt, args);4142ostream::sentry __s(__os);43if (__s) {44string __o = std::vformat(__os.getloc(), __fmt, __args);45if (__write_nl)46__o += '\n';4748const char* __str = __o.data();49size_t __len = __o.size();5051# ifndef _LIBCPP_HAS_NO_EXCEPTIONS52try {53# endif // _LIBCPP_HAS_NO_EXCEPTIONS54typedef ostreambuf_iterator<char> _Ip;55if (std::__pad_and_output(56_Ip(__os),57__str,58(__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,59__str + __len,60__os,61__os.fill())62.failed())63__os.setstate(ios_base::badbit | ios_base::failbit);6465# ifndef _LIBCPP_HAS_NO_EXCEPTIONS66} catch (...) {67__os.__set_badbit_and_consider_rethrow();68}69# endif // _LIBCPP_HAS_NO_EXCEPTIONS70}71}7273template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).74_LIBCPP_HIDE_FROM_ABI inline void vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args) {75std::__vprint_nonunicode(__os, __fmt, __args, false);76}7778// Returns the FILE* associated with the __os.79// Returns a nullptr when no FILE* is associated with __os.80// This function is in the dylib since the type of the buffer associated81// with std::cout, std::cerr, and std::clog is only known in the dylib.82//83// This function implements part of the implementation-defined behavior84// of [ostream.formatted.print]/385// If the function is vprint_unicode and os is a stream that refers to86// a terminal capable of displaying Unicode which is determined in an87// implementation-defined manner, writes out to the terminal using the88// native Unicode API;89// Whether the returned FILE* is "a terminal capable of displaying Unicode"90// is determined in the same way as the print(FILE*, ...) overloads.91_LIBCPP_EXPORTED_FROM_ABI FILE* __get_ostream_file(ostream& __os);9293# ifndef _LIBCPP_HAS_NO_UNICODE94template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).95_LIBCPP_HIDE_FROM_ABI void __vprint_unicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {96# if _LIBCPP_AVAILABILITY_HAS_PRINT == 097return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);98# else99FILE* __file = std::__get_ostream_file(__os);100if (!__file || !__print::__is_terminal(__file))101return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);102103// [ostream.formatted.print]/3104// If the function is vprint_unicode and os is a stream that refers to a105// terminal capable of displaying Unicode which is determined in an106// implementation-defined manner, writes out to the terminal using the107// native Unicode API; if out contains invalid code units, the behavior is108// undefined and implementations are encouraged to diagnose it. If the109// native Unicode API is used, the function flushes os before writing out.110//111// This is the path for the native API, start with flushing.112__os.flush();113114# ifndef _LIBCPP_HAS_NO_EXCEPTIONS115try {116# endif // _LIBCPP_HAS_NO_EXCEPTIONS117ostream::sentry __s(__os);118if (__s) {119# ifndef _LIBCPP_WIN32API120__print::__vprint_unicode_posix(__file, __fmt, __args, __write_nl, true);121# elif !defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)122__print::__vprint_unicode_windows(__file, __fmt, __args, __write_nl, true);123# else124# error "Windows builds with wchar_t disabled are not supported."125# endif126}127128# ifndef _LIBCPP_HAS_NO_EXCEPTIONS129} catch (...) {130__os.__set_badbit_and_consider_rethrow();131}132# endif // _LIBCPP_HAS_NO_EXCEPTIONS133# endif // _LIBCPP_AVAILABILITY_HAS_PRINT134}135136template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).137_LIBCPP_HIDE_FROM_ABI inline void vprint_unicode(ostream& __os, string_view __fmt, format_args __args) {138std::__vprint_unicode(__os, __fmt, __args, false);139}140# endif // _LIBCPP_HAS_NO_UNICODE141142template <class... _Args>143_LIBCPP_HIDE_FROM_ABI void print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {144# ifndef _LIBCPP_HAS_NO_UNICODE145if constexpr (__print::__use_unicode_execution_charset)146std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), false);147else148std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);149# else // _LIBCPP_HAS_NO_UNICODE150std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);151# endif // _LIBCPP_HAS_NO_UNICODE152}153154template <class... _Args>155_LIBCPP_HIDE_FROM_ABI void println(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {156# ifndef _LIBCPP_HAS_NO_UNICODE157// Note the wording in the Standard is inefficient. The output of158// std::format is a std::string which is then copied. This solution159// just appends a newline at the end of the output.160if constexpr (__print::__use_unicode_execution_charset)161std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), true);162else163std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);164# else // _LIBCPP_HAS_NO_UNICODE165std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);166# endif // _LIBCPP_HAS_NO_UNICODE167}168169template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).170_LIBCPP_HIDE_FROM_ABI inline void println(ostream& __os) {171std::print(__os, "\n");172}173174#endif // _LIBCPP_STD_VER >= 23175176_LIBCPP_END_NAMESPACE_STD177178#endif // _LIBCPP___OSTREAM_PRINT_H179180181