diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index f7cd975b69d14..de88d8c8ba18b 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -18,7 +18,7 @@ #include #include -#include "ostream.h" // formatbuf +#include "format.h" // formatbuf FMT_BEGIN_NAMESPACE @@ -329,6 +329,39 @@ inline auto localtime_s(...) -> null<> { return null<>(); } inline auto gmtime_r(...) -> null<> { return null<>(); } inline auto gmtime_s(...) -> null<> { return null<>(); } +// It is defined here and not in ostream.h because the latter has expensive +// includes. +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; + } +}; + inline auto get_classic_locale() -> const std::locale& { static const auto& locale = std::locale::classic(); return locale; diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 2508e58d7d548..f9ba9d7b25b64 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -18,42 +18,11 @@ # include #endif -#include "format.h" +#include "chrono.h" // formatbuf 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 {