From fc0f84d29003bf4d13dfcc86c87f198d88509ed9 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 23 Dec 2023 15:00:28 -0800 Subject: [PATCH] Move formatbuf to ostream.h --- include/fmt/chrono.h | 2 +- include/fmt/format.h | 31 ------------------------------- include/fmt/ostream.h | 32 +++++++++++++++++++++++++++++++- 3 files changed, 32 insertions(+), 33 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 1a4b7d5a..6a3d38ff 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -18,7 +18,7 @@ #include #include -#include "format.h" +#include "ostream.h" // formatbuf FMT_BEGIN_NAMESPACE diff --git a/include/fmt/format.h b/include/fmt/format.h index d7193146..ded5d513 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -298,37 +298,6 @@ template constexpr CharT string_literal::value[sizeof...(C)]; #endif -template class formatbuf : public Streambuf { - private: - using char_type = typename Streambuf::char_type; - using streamsize = decltype(std::declval().sputn(nullptr, 0)); - using int_type = typename Streambuf::int_type; - using traits_type = typename Streambuf::traits_type; - - buffer& buffer_; - - public: - explicit formatbuf(buffer& buf) : buffer_(buf) {} - - protected: - // The put area is always empty. This makes the implementation simpler and has - // the advantage that the streambuf and the buffer are always in sync and - // sputc never writes into uninitialized memory. A disadvantage is that each - // 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) -> int_type override { - if (!traits_type::eq_int_type(ch, traits_type::eof())) - buffer_.push_back(static_cast(ch)); - return ch; - } - - auto xsputn(const char_type* s, streamsize count) -> streamsize override { - buffer_.append(s, s + count); - return count; - } -}; - // Implementation of std::bit_cast for pre-C++20. template FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To { diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 3eb62143..26fb3b5a 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -21,9 +21,39 @@ #include "format.h" FMT_BEGIN_NAMESPACE - namespace detail { +template class formatbuf : public Streambuf { + private: + using char_type = typename Streambuf::char_type; + using streamsize = decltype(std::declval().sputn(nullptr, 0)); + using int_type = typename Streambuf::int_type; + using traits_type = typename Streambuf::traits_type; + + buffer& buffer_; + + public: + explicit formatbuf(buffer& buf) : buffer_(buf) {} + + protected: + // The put area is always empty. This makes the implementation simpler and has + // the advantage that the streambuf and the buffer are always in sync and + // sputc never writes into uninitialized memory. A disadvantage is that each + // 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) -> int_type override { + if (!traits_type::eq_int_type(ch, traits_type::eof())) + buffer_.push_back(static_cast(ch)); + return ch; + } + + auto xsputn(const char_type* s, streamsize count) -> streamsize override { + buffer_.append(s, s + count); + return count; + } +}; + // Generate a unique explicit instantion in every translation unit using a tag // type in an anonymous namespace. namespace {