From 17b362f78ce5e1322c13e7e19b30e03e88dc62c1 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 4 Feb 2022 18:33:55 -0800 Subject: [PATCH] Simplify ostream opt-in API --- include/fmt/ostream.h | 23 ++++++++++++++--------- test/ostream-test.cc | 16 +++++++--------- test/xchar-test.cc | 4 ++-- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 4634bcf9..2bb79bbb 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -78,14 +78,12 @@ void format_value(buffer& buf, const T& value, output.exceptions(std::ios_base::failbit | std::ios_base::badbit); buf.try_resize(buf.size()); } +} // namespace detail // Formats an object of type T that has an overloaded ostream operator<<. -template -struct fallback_formatter::value>> - : private formatter, Char> { - using formatter, Char>::parse; - - template +template +struct basic_ostream_formatter : formatter, Char> { + template auto format(const T& value, basic_format_context& ctx) const -> OutputIt { auto buffer = basic_memory_buffer(); @@ -93,7 +91,17 @@ struct fallback_formatter::value>> return formatter, Char>::format( {buffer.data(), buffer.size()}, ctx); } +}; +using ostream_formatter = basic_ostream_formatter; + +namespace detail { + +// Formats an object of type T that has an overloaded ostream operator<<. +template +struct fallback_formatter::value>> + : basic_ostream_formatter { + using basic_ostream_formatter::format; // DEPRECATED! template auto format(const T& value, basic_printf_context& ctx) const @@ -105,9 +113,6 @@ struct fallback_formatter::value>> }; } // namespace detail -template -using ostream_formatter = detail::fallback_formatter; - FMT_MODULE_EXPORT template void vprint(std::basic_ostream& os, basic_string_view format_str, diff --git a/test/ostream-test.cc b/test/ostream-test.cc index 2f9c4383..3834c707 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -57,11 +57,10 @@ struct empty_test {}; std::ostream& operator<<(std::ostream& os, empty_test) { return os << ""; } namespace fmt { -template <> struct formatter : ostream_formatter {}; -template <> struct formatter : ostream_formatter {}; -template <> -struct formatter : ostream_formatter {}; -template <> struct formatter : ostream_formatter {}; +template <> struct formatter : ostream_formatter {}; +template <> struct formatter : ostream_formatter {}; +template <> struct formatter : ostream_formatter {}; +template <> struct formatter : ostream_formatter {}; } // namespace fmt TEST(ostream_test, enum) { @@ -193,8 +192,7 @@ template struct formatter> : formatter { } }; -template <> -struct formatter : ostream_formatter {}; +template <> struct formatter : ostream_formatter {}; } // namespace fmt TEST(ostream_test, template) { @@ -262,7 +260,7 @@ std::ostream& operator<<(std::ostream& os, copyfmt_test) { } namespace fmt { -template <> struct formatter : ostream_formatter {}; +template <> struct formatter : ostream_formatter {}; } // namespace fmt TEST(ostream_test, copyfmt) { @@ -287,7 +285,7 @@ struct abstract { }; namespace fmt { -template <> struct formatter : ostream_formatter {}; +template <> struct formatter : ostream_formatter {}; } // namespace fmt void format_abstract_compiles(const abstract& a) { diff --git a/test/xchar-test.cc b/test/xchar-test.cc index 2d7dfa85..40d7fca1 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -222,8 +222,8 @@ std::wostream& operator<<(std::wostream& os, streamable_enum) { namespace fmt { template <> -struct formatter - : ostream_formatter {}; +struct formatter : basic_ostream_formatter { +}; } // namespace fmt enum unstreamable_enum {};