[libcxx-commits] [libcxx] [libc++][print] Adds ostream overloads. (PR #73262)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Nov 28 09:00:12 PST 2023
================
@@ -1195,6 +1209,140 @@ extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>;
extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>;
#endif
+#if _LIBCPP_STD_VER >= 23
+
+template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
+_LIBCPP_HIDE_FROM_ABI inline void
+__vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
+ ostream::sentry __s(__os);
+ if (__s) {
+ string __o = vformat(__os.getloc(), __fmt, __args);
+ if (__write_nl)
+ __o += '\n';
+
+ const char* __str = __o.data();
+ size_t __len = __o.size();
+
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ try {
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ typedef ostreambuf_iterator<char> _Ip;
+ if (std::__pad_and_output(
+ _Ip(__os),
+ __str,
+ (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
+ __str + __len,
+ __os,
+ __os.fill())
+ .failed())
+ __os.setstate(ios_base::badbit | ios_base::failbit);
+
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ } catch (...) {
+ __os.__set_badbit_and_consider_rethrow();
+ }
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ }
+}
+
+template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
+_LIBCPP_HIDE_FROM_ABI inline void vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args) {
+ std::__vprint_nonunicode(__os, __fmt, __args, false);
+}
+
+// Returns the FILE* associated with the __os.
+// Returns a nullptr when no FILE* is associated with __os.
+// This function is in the dylib since the type of the buffer associated
+// with std::cout, std::cerr, and std::clog is only known in the dylib.
+//
+// This function implements part of the implementation-defined behavior
+// of [ostream.formatted.print]/3
+// If the function is vprint_unicode and os is a stream that refers to
+// a terminal capable of displaying Unicode which is determined in an
+// implementation-defined manner, writes out to the terminal using the
+// native Unicode API;
+// Whether the returned FILE* is "a terminal capable of displaying Unicode"
+// is determined in the same way as the print(FILE*, ...) overloads.
+_LIBCPP_AVAILABILITY_PRINT _LIBCPP_EXPORTED_FROM_ABI FILE* __get_ostream_file(ostream& __os);
+
+# ifndef _LIBCPP_HAS_NO_UNICODE
+template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
+_LIBCPP_AVAILABILITY_PRINT _LIBCPP_HIDE_FROM_ABI void
+__vprint_unicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) {
+ FILE* __file = std::__get_ostream_file(__os);
+ if (!__file || !__print::__is_terminal(__file))
+ return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl);
+
+ // [ostream.formatted.print]/3
+ // If the function is vprint_unicode and os is a stream that refers to a
+ // terminal capable of displaying Unicode which is determined in an
+ // implementation-defined manner, writes out to the terminal using the
+ // native Unicode API; if out contains invalid code units, the behavior is
+ // undefined and implementations are encouraged to diagnose it. If the
+ // native Unicode API is used, the function flushes os before writing out.
+ //
+ // This is the path for the native API, start with flushing.
+ __os.flush();
+
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ try {
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+ ostream::sentry __s(__os);
+ if (__s) {
+# ifndef _WIN32
+ __print::__vprint_unicode_posix(__file, __fmt, __args, __write_nl, true);
+# elif !defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS)
+ __print::__vprint_unicode_windows(__file, __fmt, __args, __write_nl, true);
+# else
+# error "Windows builds with wchar_t disabled are not supported."
+# endif
+ }
+
+# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
+ } catch (...) {
+ __os.__set_badbit_and_consider_rethrow();
+ }
+# endif // _LIBCPP_HAS_NO_EXCEPTIONS
+}
+
+template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563).
+_LIBCPP_AVAILABILITY_PRINT _LIBCPP_HIDE_FROM_ABI inline void
+vprint_unicode(ostream& __os, string_view __fmt, format_args __args) {
+ std::__vprint_unicode(__os, __fmt, __args, false);
+}
+# endif // _LIBCPP_HAS_NO_UNICODE
+
+template <class... _Args>
+_LIBCPP_AVAILABILITY_PRINT _LIBCPP_HIDE_FROM_ABI void
+print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) {
+# ifndef _LIBCPP_HAS_NO_UNICODE
----------------
ldionne wrote:
Is there a reason why we have both `_LIBCPP_HAS_NO_UNICODE` and `__print::__use_unicode`?
https://github.com/llvm/llvm-project/pull/73262
More information about the libcxx-commits
mailing list