Skip to content

Commit

Permalink
Move formatbuf to ostream.h
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Dec 23, 2023
1 parent 86f2ec5 commit fc0f84d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 33 deletions.
2 changes: 1 addition & 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 "format.h"
#include "ostream.h" // formatbuf

FMT_BEGIN_NAMESPACE

Expand Down
31 changes: 0 additions & 31 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,37 +298,6 @@ template <typename CharT, CharT... C>
constexpr CharT string_literal<CharT, C...>::value[sizeof...(C)];
#endif

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;
}
};

// Implementation of std::bit_cast for pre-C++20.
template <typename To, typename From, FMT_ENABLE_IF(sizeof(To) == sizeof(From))>
FMT_CONSTEXPR20 auto bit_cast(const From& from) -> To {
Expand Down
32 changes: 31 additions & 1 deletion include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,39 @@
#include "format.h"

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 fc0f84d

Please sign in to comment.