Skip to content

Commit

Permalink
Invert dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jan 13, 2024
1 parent c10859f commit 4d766b1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 33 deletions.
35 changes: 34 additions & 1 deletion include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <ostream>
#include <type_traits>

#include "ostream.h" // formatbuf
#include "format.h"

FMT_BEGIN_NAMESPACE

Expand Down Expand Up @@ -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 <typename Streambuf> class formatbuf : public Streambuf {
private:
using char_type = typename Streambuf::char_type;
using streamsize = decltype(std::declval<Streambuf>().sputn(nullptr, 0));
using int_type = typename Streambuf::int_type;
using traits_type = typename Streambuf::traits_type;

buffer<char_type>& buffer_;

public:
explicit formatbuf(buffer<char_type>& 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<char_type>(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;
Expand Down
33 changes: 1 addition & 32 deletions include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,42 +18,11 @@
# include <io.h>
#endif

#include "format.h"
#include "chrono.h" // formatbuf

FMT_BEGIN_NAMESPACE
namespace detail {

template <typename Streambuf> class formatbuf : public Streambuf {
private:
using char_type = typename Streambuf::char_type;
using streamsize = decltype(std::declval<Streambuf>().sputn(nullptr, 0));
using int_type = typename Streambuf::int_type;
using traits_type = typename Streambuf::traits_type;

buffer<char_type>& buffer_;

public:
explicit formatbuf(buffer<char_type>& 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<char_type>(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 {
Expand Down

0 comments on commit 4d766b1

Please sign in to comment.