Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-03 08:13:37

0001 //===---------------------------------------------------------------------===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===---------------------------------------------------------------------===//
0008 
0009 #ifndef _LIBCPP___CXX03___OSTREAM_PRINT_H
0010 #define _LIBCPP___CXX03___OSTREAM_PRINT_H
0011 
0012 #include <__cxx03/__config>
0013 #include <__cxx03/__fwd/ostream.h>
0014 #include <__cxx03/__iterator/ostreambuf_iterator.h>
0015 #include <__cxx03/__ostream/basic_ostream.h>
0016 #include <__cxx03/format>
0017 #include <__cxx03/ios>
0018 #include <__cxx03/locale>
0019 #include <__cxx03/print>
0020 
0021 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0022 #  pragma GCC system_header
0023 #endif
0024 
0025 _LIBCPP_BEGIN_NAMESPACE_STD
0026 
0027 #if _LIBCPP_STD_VER >= 23
0028 
0029 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
0030 _LIBCPP_HIDE_FROM_ABI inline void
0031 __vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
0032   // [ostream.formatted.print]/3
0033   // Effects: Behaves as a formatted output function
0034   // ([ostream.formatted.reqmts]) of os, except that:
0035   // - failure to generate output is reported as specified below, and
0036   // - any exception thrown by the call to vformat is propagated without regard
0037   //   to the value of os.exceptions() and without turning on ios_base::badbit
0038   //   in the error state of os.
0039   // After constructing a sentry object, the function initializes an automatic
0040   // variable via
0041   //   string out = vformat(os.getloc(), fmt, args);
0042 
0043   ostream::sentry __s(__os);
0044   if (__s) {
0045     string __o = std::vformat(__os.getloc(), __fmt, __args);
0046     if (__write_nl)
0047       __o += '\n';
0048 
0049     const char* __str = __o.data();
0050     size_t __len      = __o.size();
0051 
0052 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0053     try {
0054 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
0055       typedef ostreambuf_iterator<char> _Ip;
0056       if (std::__pad_and_output(
0057               _Ip(__os),
0058               __str,
0059               (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
0060               __str + __len,
0061               __os,
0062               __os.fill())
0063               .failed())
0064         __os.setstate(ios_base::badbit | ios_base::failbit);
0065 
0066 #  ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0067     } catch (...) {
0068       __os.__set_badbit_and_consider_rethrow();
0069     }
0070 #  endif // _LIBCPP_HAS_NO_EXCEPTIONS
0071   }
0072 }
0073 
0074 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
0075 _LIBCPP_HIDE_FROM_ABI inline void vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args) {
0076   std::__vprint_nonunicode(__os, __fmt, __args, false);
0077 }
0078 
0079 // Returns the FILE* associated with the __os.
0080 // Returns a nullptr when no FILE* is associated with __os.
0081 // This function is in the dylib since the type of the buffer associated
0082 // with std::cout, std::cerr, and std::clog is only known in the dylib.
0083 //
0084 // This function implements part of the implementation-defined behavior
0085 // of [ostream.formatted.print]/3
0086 //   If the function is vprint_unicode and os is a stream that refers to
0087 //   a terminal capable of displaying Unicode which is determined in an
0088 //   implementation-defined manner, writes out to the terminal using the
0089 //   native Unicode API;
0090 // Whether the returned FILE* is "a terminal capable of displaying Unicode"
0091 // is determined in the same way as the print(FILE*, ...) overloads.
0092 _LIBCPP_EXPORTED_FROM_ABI FILE* __get_ostream_file(ostream& __os);
0093 
0094 #  ifndef _LIBCPP_HAS_NO_UNICODE
0095 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
0096 _LIBCPP_HIDE_FROM_ABI void __vprint_unicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
0097 #    if _LIBCPP_AVAILABILITY_HAS_PRINT == 0
0098   return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);
0099 #    else
0100   FILE* __file = std::__get_ostream_file(__os);
0101   if (!__file || !__print::__is_terminal(__file))
0102     return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);
0103 
0104   // [ostream.formatted.print]/3
0105   //    If the function is vprint_unicode and os is a stream that refers to a
0106   //    terminal capable of displaying Unicode which is determined in an
0107   //    implementation-defined manner, writes out to the terminal using the
0108   //    native Unicode API; if out contains invalid code units, the behavior is
0109   //    undefined and implementations are encouraged to diagnose it. If the
0110   //    native Unicode API is used, the function flushes os before writing out.
0111   //
0112   // This is the path for the native API, start with flushing.
0113   __os.flush();
0114 
0115 #      ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0116   try {
0117 #      endif // _LIBCPP_HAS_NO_EXCEPTIONS
0118     ostream::sentry __s(__os);
0119     if (__s) {
0120 #      ifndef _LIBCPP_WIN32API
0121       __print::__vprint_unicode_posix(__file, __fmt, __args, __write_nl, true);
0122 #      elif !defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)
0123     __print::__vprint_unicode_windows(__file, __fmt, __args, __write_nl, true);
0124 #      else
0125 #        error "Windows builds with wchar_t disabled are not supported."
0126 #      endif
0127     }
0128 
0129 #      ifndef _LIBCPP_HAS_NO_EXCEPTIONS
0130   } catch (...) {
0131     __os.__set_badbit_and_consider_rethrow();
0132   }
0133 #      endif // _LIBCPP_HAS_NO_EXCEPTIONS
0134 #    endif   // _LIBCPP_AVAILABILITY_HAS_PRINT
0135 }
0136 
0137 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
0138 _LIBCPP_HIDE_FROM_ABI inline void vprint_unicode(ostream& __os, string_view __fmt, format_args __args) {
0139   std::__vprint_unicode(__os, __fmt, __args, false);
0140 }
0141 #  endif // _LIBCPP_HAS_NO_UNICODE
0142 
0143 template <class... _Args>
0144 _LIBCPP_HIDE_FROM_ABI void print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {
0145 #  ifndef _LIBCPP_HAS_NO_UNICODE
0146   if constexpr (__print::__use_unicode_execution_charset)
0147     std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), false);
0148   else
0149     std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);
0150 #  else  // _LIBCPP_HAS_NO_UNICODE
0151   std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false);
0152 #  endif // _LIBCPP_HAS_NO_UNICODE
0153 }
0154 
0155 template <class... _Args>
0156 _LIBCPP_HIDE_FROM_ABI void println(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {
0157 #  ifndef _LIBCPP_HAS_NO_UNICODE
0158   // Note the wording in the Standard is inefficient. The output of
0159   // std::format is a std::string which is then copied. This solution
0160   // just appends a newline at the end of the output.
0161   if constexpr (__print::__use_unicode_execution_charset)
0162     std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), true);
0163   else
0164     std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);
0165 #  else  // _LIBCPP_HAS_NO_UNICODE
0166   std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true);
0167 #  endif // _LIBCPP_HAS_NO_UNICODE
0168 }
0169 
0170 template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
0171 _LIBCPP_HIDE_FROM_ABI inline void println(ostream& __os) {
0172   std::print(__os, "\n");
0173 }
0174 
0175 #endif // _LIBCPP_STD_VER >= 23
0176 
0177 _LIBCPP_END_NAMESPACE_STD
0178 
0179 #endif // _LIBCPP___CXX03___OSTREAM_PRINT_H