Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to stream user defined types in a MemoryWriter #456

Merged
merged 8 commits into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class FormatBuf : public std::basic_streambuf<Char> {
this->setp(start_, start_ + buffer_.capacity());
}

FormatBuf(Buffer<Char> &buffer, Char *start) : buffer_(buffer) , start_(start) {
this->setp(start_, start_ + buffer_.capacity());
}

int_type overflow(int_type ch = traits_type::eof()) {
if (!traits_type::eq_int_type(ch, traits_type::eof())) {
size_t buf_size = size();
Expand Down Expand Up @@ -69,6 +73,19 @@ struct ConvertToIntImpl<T, true> {

// Write the content of w to os.
void write(std::ostream &os, Writer &w);

template<typename T>
class is_streamable
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please follow Google C++ Style ({ on the previous line).

template<typename U>
static auto test(int) -> decltype(std::declval<std::ostream &>() << std::declval<U>(), std::true_type());

template<typename>
static auto test(...) -> std::false_type;

public:
static constexpr bool value = decltype(test<T>(0))::value;
};
} // namespace internal

// Formats a value.
Expand Down Expand Up @@ -97,6 +114,29 @@ void format_arg(BasicFormatter<Char, ArgFormatter> &f,
*/
FMT_API void print(std::ostream &os, CStringRef format_str, ArgList args);
FMT_VARIADIC(void, print, std::ostream &, CStringRef)

template<typename T, typename Char>
typename std::enable_if<
!std::is_same<
typename std::remove_cv<typename std::decay<T>::type>::type,
char *
>::value,
BasicWriter<Char>&
>::type
operator<<(BasicWriter<Char> &writer, const T &value)
{
static_assert(internal::is_streamable<T>::value, "T must be Streamable");

auto &buffer = writer.buffer();
Char *start = &buffer[0] + buffer.size();

internal::FormatBuf<Char> format_buf(buffer, start);
std::basic_ostream<Char> output(&format_buf);
output << value;

buffer.resize(buffer.size() + format_buf.size());
return writer;
}
} // namespace fmt

#ifdef FMT_HEADER_ONLY
Expand Down
14 changes: 14 additions & 0 deletions test/ostream-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ std::ostream &operator<<(std::ostream &os, EmptyTest) {
return os << "";
}

struct UserDefinedTest { int i = 42; };
std::ostream &operator<<(std::ostream &os, const UserDefinedTest &u) {
return os << u.i;
}

TEST(OStreamTest, EmptyCustomOutput) {
EXPECT_EQ("", fmt::format("{}", EmptyTest()));
}
Expand All @@ -129,6 +134,15 @@ TEST(OStreamTest, WriteToOStream) {
EXPECT_EQ("foo", os.str());
}

TEST(OStreamTest, WriteUserDefinedTypeToOStream) {
std::ostringstream os;
fmt::MemoryWriter w;
UserDefinedTest u;
w << "The answer is " << u;
fmt::internal::write(os, w);
EXPECT_EQ("The answer is 42", os.str());
}

TEST(OStreamTest, WriteToOStreamMaxSize) {
std::size_t max_size = std::numeric_limits<std::size_t>::max();
std::streamsize max_streamsize = std::numeric_limits<std::streamsize>::max();
Expand Down