From 4fd9a00f35a8a8ab3755babf7ddfae819c22d487 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Fri, 3 Sep 2021 12:47:33 -0700 Subject: [PATCH] Simplify ostream interface --- include/fmt/ostream.h | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index d6769880..c04655cc 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -14,7 +14,6 @@ FMT_BEGIN_NAMESPACE -template class basic_printf_parse_context; template class basic_printf_context; namespace detail { @@ -27,7 +26,7 @@ template class formatbuf : public std::basic_streambuf { buffer& buffer_; public: - formatbuf(buffer& buf) : buffer_(buf) {} + explicit formatbuf(buffer& buf) : buffer_(buf) {} protected: // The put area is always empty. This makes the implementation simpler and has @@ -36,7 +35,7 @@ template class formatbuf : public std::basic_streambuf { // call to sputc always results in a (virtual) call to overflow. There is no // disadvantage here for sputn since this always results in a call to xsputn. - auto overflow(int_type ch = traits_type::eof()) -> int_type FMT_OVERRIDE { + auto overflow(int_type ch = traits_type::eof()) -> int_type override { if (!traits_type::eq_int_type(ch, traits_type::eof())) buffer_.push_back(static_cast(ch)); return ch; @@ -79,6 +78,7 @@ struct is_streamable< !std::is_enum::value)>> : std::false_type {}; // Write the content of buf to os. +// It is a separate function rather than a part of vprint to simplify testing. template void write_buffer(std::basic_ostream& os, buffer& buf) { const Char* buf_data = buf.data(); @@ -96,8 +96,8 @@ void write_buffer(std::basic_ostream& os, buffer& buf) { template void format_value(buffer& buf, const T& value, locale_ref loc = locale_ref()) { - formatbuf format_buf(buf); - std::basic_ostream output(&format_buf); + auto&& format_buf = formatbuf(buf); + auto&& output = std::basic_ostream(&format_buf); #if !defined(FMT_STATIC_THOUSANDS_SEPARATOR) if (loc) output.imbue(loc.get()); #endif @@ -110,33 +110,22 @@ void format_value(buffer& buf, const T& value, template struct fallback_formatter::value>> : private formatter, Char> { - FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) - -> decltype(ctx.begin()) { - return formatter, Char>::parse(ctx); - } - - // DEPRECATED! - template >::value)> - auto parse(ParseCtx& ctx) -> decltype(ctx.begin()) { - return ctx.begin(); - } + using formatter, Char>::parse; template auto format(const T& value, basic_format_context& ctx) -> OutputIt { - basic_memory_buffer buffer; + auto buffer = basic_memory_buffer(); format_value(buffer, value, ctx.locale()); - basic_string_view str(buffer.data(), buffer.size()); - return formatter, Char>::format(str, ctx); + return formatter, Char>::format( + {buffer.data(), buffer.size()}, ctx); } // DEPRECATED! template auto format(const T& value, basic_printf_context& ctx) -> OutputIt { - basic_memory_buffer buffer; + auto buffer = basic_memory_buffer(); format_value(buffer, value, ctx.locale()); return std::copy(buffer.begin(), buffer.end(), ctx.out()); }