Pass writer to format_value

This commit is contained in:
Victor Zverovich 2016-10-25 05:55:40 -07:00
parent 64ca334a2d
commit edf98792a5
6 changed files with 36 additions and 19 deletions

View File

@ -993,7 +993,7 @@ struct Value {
}; };
typedef void (*FormatFunc)( typedef void (*FormatFunc)(
void *formatter, const void *arg, void *format_str_ptr); void *writer, void *formatter, const void *arg, void *format_str_ptr);
struct CustomValue { struct CustomValue {
const void *value; const void *value;
@ -1160,7 +1160,7 @@ inline fmt::StringRef thousands_sep(...) { return ""; }
#endif #endif
template <typename Formatter, typename Char, typename T> template <typename Formatter, typename Char, typename T>
void format_value(Formatter &, const Char *, const T &) { void format_value(BasicWriter<Char> &, Formatter &, const Char *, const T &) {
FMT_STATIC_ASSERT(False<T>::value, FMT_STATIC_ASSERT(False<T>::value,
"Cannot format argument. To enable the use of ostream " "Cannot format argument. To enable the use of ostream "
"operator<< include fmt/ostream.h. Otherwise provide " "operator<< include fmt/ostream.h. Otherwise provide "
@ -1271,8 +1271,10 @@ class MakeValue : public Arg {
// Formats an argument of a custom type, such as a user-defined class. // Formats an argument of a custom type, such as a user-defined class.
template <typename T> template <typename T>
static void format_custom_arg( static void format_custom_arg(
void *formatter, const void *arg, void *format_str_ptr) { void *writer, void *formatter, const void *arg, void *format_str_ptr) {
format_value(*static_cast<Formatter*>(formatter), typedef BasicWriter<typename Formatter::char_type> Writer;
format_value(*static_cast<Writer*>(writer),
*static_cast<Formatter*>(formatter),
*static_cast<const Char**>(format_str_ptr), *static_cast<const Char**>(format_str_ptr),
*static_cast<const T*>(arg)); *static_cast<const T*>(arg));
} }
@ -2178,7 +2180,7 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
/** Formats an argument of a custom (user-defined) type. */ /** Formats an argument of a custom (user-defined) type. */
void visit_custom(internal::Arg::CustomValue c) { void visit_custom(internal::Arg::CustomValue c) {
c.format(&formatter_, c.value, &format_); c.format(&formatter_.writer(), &formatter_, c.value, &format_);
} }
}; };
@ -2240,6 +2242,14 @@ class basic_formatter :
const Char *format(const Char *&format_str, const internal::Arg &arg); const Char *format(const Char *&format_str, const internal::Arg &arg);
}; };
template <typename ArgFormatter, typename Char = typename ArgFormatter::Char>
void vformat(BasicWriter<Char> &writer,
BasicCStringRef<Char> format_str,
basic_format_args<basic_formatter<Char, ArgFormatter>> args) {
basic_formatter<Char, ArgFormatter> formatter(args, writer);
formatter.format(format_str);
}
/** /**
An error returned by an operating system or a language runtime, An error returned by an operating system or a language runtime,
for example a file opening error. for example a file opening error.
@ -2470,7 +2480,7 @@ class BasicWriter {
void vwrite(BasicCStringRef<Char> format, void vwrite(BasicCStringRef<Char> format,
basic_format_args<basic_formatter<Char>> args) { basic_format_args<basic_formatter<Char>> args) {
basic_formatter<Char>(args, *this).format(format); vformat(*this, format, args);
} }
/** /**
@ -3459,7 +3469,7 @@ const Char *basic_formatter<Char, ArgFormatter>::format(
FormatSpec spec; FormatSpec spec;
if (*s == ':') { if (*s == ':') {
if (arg.type == Arg::CUSTOM) { if (arg.type == Arg::CUSTOM) {
arg.custom.format(this, arg.custom.value, &s); arg.custom.format(&writer(), this, arg.custom.value, &s);
return s; return s;
} }
++s; ++s;

View File

@ -83,7 +83,7 @@ BasicStringRef<Char> format_value(
// Formats a value. // Formats a value.
template <typename Char, typename ArgFormatter, typename T> template <typename Char, typename ArgFormatter, typename T>
void format_value(basic_formatter<Char, ArgFormatter> &f, void format_value(BasicWriter<Char> &w, basic_formatter<Char, ArgFormatter> &f,
const Char *&format_str, const T &value) { const Char *&format_str, const T &value) {
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer; internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
auto str = internal::format_value(buffer, value); auto str = internal::format_value(buffer, value);

View File

@ -266,7 +266,7 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
this->writer()); this->writer());
const Char format_str[] = {'}', 0}; const Char format_str[] = {'}', 0};
const Char *format = format_str; const Char *format = format_str;
c.format(&formatter, c.value, &format); c.format(&formatter.writer(), &formatter, c.value, &format);
} }
}; };
@ -497,7 +497,8 @@ void PrintfFormatter<Char, AF>::format(BasicCStringRef<Char> format_str) {
// Formats a value. // Formats a value.
template <typename Char, typename T> template <typename Char, typename T>
void format_value(PrintfFormatter<Char> &f, const Char *&, const T &value) { void format_value(BasicWriter<Char> &w, PrintfFormatter<Char> &f,
const Char *&, const T &value) {
internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer; internal::MemoryBuffer<Char, internal::INLINE_BUFFER_SIZE> buffer;
f.writer() << internal::format_value(buffer, value); f.writer() << internal::format_value(buffer, value);
} }

View File

@ -14,8 +14,9 @@
#include <ctime> #include <ctime>
namespace fmt { namespace fmt {
template <typename ArgFormatter> template <typename ArgFormatter>
void format_value(basic_formatter<char, ArgFormatter> &f, void format_value(Writer &w, basic_formatter<char, ArgFormatter> &f,
const char *&format_str, const std::tm &tm) { const char *&format_str, const std::tm &tm) {
if (*format_str == ':') if (*format_str == ':')
++format_str; ++format_str;
@ -27,7 +28,7 @@ void format_value(basic_formatter<char, ArgFormatter> &f,
internal::MemoryBuffer<char, internal::INLINE_BUFFER_SIZE> format; internal::MemoryBuffer<char, internal::INLINE_BUFFER_SIZE> format;
format.append(format_str, end + 1); format.append(format_str, end + 1);
format[format.size() - 1] = '\0'; format[format.size() - 1] = '\0';
Buffer<char> &buffer = f.writer().buffer(); Buffer<char> &buffer = w.buffer();
std::size_t start = buffer.size(); std::size_t start = buffer.size();
for (;;) { for (;;) {
std::size_t size = buffer.capacity() - start; std::size_t size = buffer.capacity() - start;

View File

@ -1355,7 +1355,8 @@ TEST(FormatterTest, FormatCStringRef) {
EXPECT_EQ("test", format("{0}", CStringRef("test"))); EXPECT_EQ("test", format("{0}", CStringRef("test")));
} }
void format_value(fmt::basic_formatter<char> &f, const char *, const Date &d) { void format_value(fmt::Writer &w, fmt::basic_formatter<char> &f,
const char *, const Date &d) {
f.writer() << d.year() << '-' << d.month() << '-' << d.day(); f.writer() << d.year() << '-' << d.month() << '-' << d.day();
} }
@ -1368,7 +1369,8 @@ TEST(FormatterTest, FormatCustom) {
class Answer {}; class Answer {};
template <typename Char> template <typename Char>
void format_value(fmt::basic_formatter<Char> &f, const Char *, Answer) { void format_value(BasicWriter<Char> &w, fmt::basic_formatter<Char> &f,
const Char *, Answer) {
f.writer() << "42"; f.writer() << "42";
} }

View File

@ -64,8 +64,9 @@ namespace {
struct Test {}; struct Test {};
template <typename Char> template <typename Char>
void format_value(fmt::basic_formatter<Char> &f, const Char *, Test) { void format_value(fmt::BasicWriter<Char> &w, fmt::basic_formatter<Char> &f,
f.writer() << "test"; const Char *, Test) {
w << "test";
} }
template <typename Char, typename T> template <typename Char, typename T>
@ -568,7 +569,7 @@ TEST(ArgTest, MakeArg) {
fmt::MemoryWriter w; fmt::MemoryWriter w;
fmt::basic_formatter<char> formatter(fmt::format_args(), w); fmt::basic_formatter<char> formatter(fmt::format_args(), w);
const char *s = "}"; const char *s = "}";
arg.custom.format(&formatter, &t, &s); arg.custom.format(&formatter.writer(), &formatter, &t, &s);
EXPECT_EQ("test", w.str()); EXPECT_EQ("test", w.str());
} }
@ -581,7 +582,8 @@ struct CustomFormatter {
typedef char char_type; typedef char char_type;
}; };
void format_value(CustomFormatter &, const char *&s, const Test &) { void format_value(fmt::Writer &, CustomFormatter &, const char *&s,
const Test &) {
s = "custom_format"; s = "custom_format";
} }
@ -590,7 +592,8 @@ TEST(UtilTest, MakeValueWithCustomFormatter) {
Arg arg = fmt::internal::MakeValue<CustomFormatter>(t); Arg arg = fmt::internal::MakeValue<CustomFormatter>(t);
CustomFormatter formatter; CustomFormatter formatter;
const char *s = ""; const char *s = "";
arg.custom.format(&formatter, &t, &s); fmt::MemoryWriter w;
arg.custom.format(&w, &formatter, &t, &s);
EXPECT_STREQ("custom_format", s); EXPECT_STREQ("custom_format", s);
} }