Merge BasicArgFormatter and ArgFormatter

This commit is contained in:
Victor Zverovich 2016-11-20 09:36:27 -08:00
parent d4084ac5b1
commit 284297019f
5 changed files with 29 additions and 42 deletions

View File

@ -1873,7 +1873,7 @@ void ArgMap<Char>::init(const basic_format_args<Formatter> &args) {
} }
} }
template <typename Impl, typename Char> template <typename Char>
class ArgFormatterBase { class ArgFormatterBase {
private: private:
BasicWriter<Char> &writer_; BasicWriter<Char> &writer_;
@ -2055,9 +2055,9 @@ class format_context_base {
}; };
} // namespace internal } // namespace internal
/** An argument formatter. */ /** The default argument formatter. */
template <typename Impl, typename Char> template <typename Char>
class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> { class ArgFormatter : public internal::ArgFormatterBase<Char> {
private: private:
basic_format_context<Char> &ctx_; basic_format_context<Char> &ctx_;
@ -2065,31 +2065,21 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
/** /**
\rst \rst
Constructs an argument formatter object. Constructs an argument formatter object.
*formatter* is a reference to the main formatter object, *spec* contains *writer* is a reference to the writer to be used for output,
format specifier information for standard argument types, and *fmt* points *ctx* is a reference to the formatting context, *spec* contains
to the part of the format string being parsed for custom argument types. format specifier information for standard argument types.
\endrst \endrst
*/ */
BasicArgFormatter(BasicWriter<Char> &writer, basic_format_context<Char> &ctx,
FormatSpec &spec)
: internal::ArgFormatterBase<Impl, Char>(writer, spec), ctx_(ctx) {}
using internal::ArgFormatterBase<Impl, Char>::operator();
/** Formats an argument of a custom (user-defined) type. */
void operator()(internal::Arg::CustomValue c) {
c.format(&this->writer(), c.value, &ctx_);
}
};
/** The default argument formatter. */
template <typename Char>
class ArgFormatter : public BasicArgFormatter<ArgFormatter<Char>, Char> {
public:
/** Constructs an argument formatter object. */
ArgFormatter(BasicWriter<Char> &writer, basic_format_context<Char> &ctx, ArgFormatter(BasicWriter<Char> &writer, basic_format_context<Char> &ctx,
FormatSpec &spec) FormatSpec &spec)
: BasicArgFormatter<ArgFormatter<Char>, Char>(writer, ctx, spec) {} : internal::ArgFormatterBase<Char>(writer, spec), ctx_(ctx) {}
using internal::ArgFormatterBase<Char>::operator();
/** Formats an argument of a custom (user-defined) type. */
void operator()(format_arg::CustomValue c) {
c.format(&this->writer(), c.value, &ctx_);
}
}; };
template <typename Char> template <typename Char>
@ -2305,7 +2295,7 @@ class BasicWriter {
template<typename T> template<typename T>
void append_float_length(Char *&, T) {} void append_float_length(Char *&, T) {}
template <typename Impl, typename Char_> template <typename Char_>
friend class internal::ArgFormatterBase; friend class internal::ArgFormatterBase;
template <typename Char_> template <typename Char_>

View File

@ -208,15 +208,14 @@ class WidthHandler {
\endrst \endrst
*/ */
template <typename Char> template <typename Char>
class PrintfArgFormatter : class PrintfArgFormatter : public internal::ArgFormatterBase<Char> {
public internal::ArgFormatterBase<PrintfArgFormatter<Char>, Char> {
private: private:
void write_null_pointer() { void write_null_pointer() {
this->spec().type_ = 0; this->spec().type_ = 0;
this->write("(nil)"); this->write("(nil)");
} }
typedef internal::ArgFormatterBase<PrintfArgFormatter<Char>, Char> Base; typedef internal::ArgFormatterBase<Char> Base;
public: public:
/** /**
@ -227,7 +226,7 @@ class PrintfArgFormatter :
\endrst \endrst
*/ */
PrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec) PrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec)
: internal::ArgFormatterBase<PrintfArgFormatter<Char>, Char>(writer, spec) {} : internal::ArgFormatterBase<Char>(writer, spec) {}
using Base::operator(); using Base::operator();

View File

@ -14,19 +14,18 @@ using fmt::PrintfArgFormatter;
// A custom argument formatter that doesn't print `-` for floating-point values // A custom argument formatter that doesn't print `-` for floating-point values
// rounded to 0. // rounded to 0.
class CustomArgFormatter class CustomArgFormatter : public fmt::ArgFormatter<char> {
: public fmt::BasicArgFormatter<CustomArgFormatter, char> {
public: public:
CustomArgFormatter(fmt::Writer &w, fmt::basic_format_context<char> &ctx, CustomArgFormatter(fmt::Writer &w, fmt::basic_format_context<char> &ctx,
fmt::FormatSpec &s) fmt::FormatSpec &s)
: fmt::BasicArgFormatter<CustomArgFormatter, char>(w, ctx, s) {} : fmt::ArgFormatter<char>(w, ctx, s) {}
using fmt::BasicArgFormatter<CustomArgFormatter, char>::operator(); using fmt::ArgFormatter<char>::operator();
void operator()(double value) { void operator()(double value) {
if (round(value * pow(10, spec().precision())) == 0) if (round(value * pow(10, spec().precision())) == 0)
value = 0; value = 0;
fmt::BasicArgFormatter<CustomArgFormatter, char>::operator()(value); fmt::ArgFormatter<char>::operator()(value);
} }
}; };

View File

@ -1619,17 +1619,16 @@ TEST(FormatTest, Enum) {
EXPECT_EQ("0", fmt::format("{}", A)); EXPECT_EQ("0", fmt::format("{}", A));
} }
class MockArgFormatter : class MockArgFormatter : public fmt::internal::ArgFormatterBase<char> {
public fmt::internal::ArgFormatterBase<MockArgFormatter, char> {
private: private:
MOCK_METHOD1(call, void (int value)); MOCK_METHOD1(call, void (int value));
public: public:
typedef fmt::internal::ArgFormatterBase<MockArgFormatter, char> Base; typedef fmt::internal::ArgFormatterBase<char> Base;
MockArgFormatter(fmt::Writer &w, fmt::format_context &ctx, MockArgFormatter(fmt::Writer &w, fmt::format_context &ctx,
fmt::FormatSpec &s) fmt::FormatSpec &s)
: fmt::internal::ArgFormatterBase<MockArgFormatter, char>(w, s) { : fmt::internal::ArgFormatterBase<char>(w, s) {
EXPECT_CALL(*this, call(42)); EXPECT_CALL(*this, call(42));
} }
@ -1637,7 +1636,7 @@ class MockArgFormatter :
void operator()(int value) { call(value); } void operator()(int value) { call(value); }
void operator()(fmt::internal::Arg::CustomValue) {} void operator()(fmt::format_arg::CustomValue) {}
}; };
void custom_vformat(fmt::CStringRef format_str, fmt::format_args args) { void custom_vformat(fmt::CStringRef format_str, fmt::format_args args) {

View File

@ -58,10 +58,10 @@ TEST(OStreamTest, Enum) {
EXPECT_EQ("0", fmt::format("{}", A)); EXPECT_EQ("0", fmt::format("{}", A));
} }
struct TestArgFormatter : fmt::BasicArgFormatter<TestArgFormatter, char> { struct TestArgFormatter : fmt::ArgFormatter<char> {
TestArgFormatter(fmt::Writer &w, fmt::format_context &ctx, TestArgFormatter(fmt::Writer &w, fmt::format_context &ctx,
fmt::FormatSpec &s) fmt::FormatSpec &s)
: fmt::BasicArgFormatter<TestArgFormatter, char>(w, ctx, s) {} : fmt::ArgFormatter<char>(w, ctx, s) {}
}; };
TEST(OStreamTest, CustomArg) { TEST(OStreamTest, CustomArg) {