From d58cc8a4a80079348cc1c6edbe220dec7568738c Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 20 Nov 2016 07:42:38 -0800 Subject: [PATCH] Merge BasicPrintfArgFormatter and PrintfArgFormatter --- fmt/format.h | 8 ++++---- fmt/printf.h | 37 ++++++++--------------------------- test/custom-formatter-test.cc | 15 +++++++------- 3 files changed, 19 insertions(+), 41 deletions(-) diff --git a/fmt/format.h b/fmt/format.h index e6f01725..4ce0c1b1 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -374,8 +374,8 @@ typedef BasicWriter WWriter; template class ArgFormatter; -template -class BasicPrintfArgFormatter; +template +class PrintfArgFormatter; template class basic_format_context; @@ -2489,8 +2489,8 @@ class BasicWriter { template friend class internal::ArgFormatterBase; - template - friend class BasicPrintfArgFormatter; + template + friend class PrintfArgFormatter; protected: /** diff --git a/fmt/printf.h b/fmt/printf.h index c8ae99b2..f0adf71c 100644 --- a/fmt/printf.h +++ b/fmt/printf.h @@ -204,30 +204,19 @@ class WidthHandler { /** \rst - A ``printf`` argument formatter based on the `curiously recurring template - pattern `_. - - To use `~fmt::BasicPrintfArgFormatter` define a subclass that implements some - or all of the visit methods with the same signatures as the methods in - `~fmt::ArgVisitor`, for example, `~fmt::ArgVisitor::visit_int()`. - Pass the subclass as the *Impl* template parameter. When a formatting - function processes an argument, it will dispatch to a visit method - specific to the argument type. For example, if the argument type is - ``double`` then the `~fmt::ArgVisitor::visit_double()` method of a subclass - will be called. If the subclass doesn't contain a method with this signature, - then a corresponding method of `~fmt::BasicPrintfArgFormatter` or its - superclass will be called. + The ``printf`` argument formatter. \endrst */ -template -class BasicPrintfArgFormatter : public internal::ArgFormatterBase { +template +class PrintfArgFormatter : + public internal::ArgFormatterBase, Char> { private: void write_null_pointer() { this->spec().type_ = 0; this->write("(nil)"); } - typedef internal::ArgFormatterBase Base; + typedef internal::ArgFormatterBase, Char> Base; public: /** @@ -237,8 +226,8 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase { specifier information for standard argument types. \endrst */ - BasicPrintfArgFormatter(BasicWriter &writer, FormatSpec &spec) - : internal::ArgFormatterBase(writer, spec) {} + PrintfArgFormatter(BasicWriter &writer, FormatSpec &spec) + : internal::ArgFormatterBase, Char>(writer, spec) {} using Base::operator(); @@ -246,7 +235,7 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase { void operator()(bool value) { FormatSpec &fmt_spec = this->spec(); if (fmt_spec.type_ != 's') - return this->visit_any_int(value); + return (*this)(value ? 1 : 0); fmt_spec.type_ = 0; this->write(value); } @@ -301,16 +290,6 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase { } }; -/** The default printf argument formatter. */ -template -class PrintfArgFormatter - : public BasicPrintfArgFormatter, Char> { - public: - /** Constructs an argument formatter object. */ - PrintfArgFormatter(BasicWriter &w, FormatSpec &s) - : BasicPrintfArgFormatter, Char>(w, s) {} -}; - /** This template formats data and writes the output to a writer. */ template > diff --git a/test/custom-formatter-test.cc b/test/custom-formatter-test.cc index 83648aba..779c1ea2 100644 --- a/test/custom-formatter-test.cc +++ b/test/custom-formatter-test.cc @@ -10,7 +10,7 @@ #include "fmt/printf.h" #include "gtest-extra.h" -using fmt::BasicPrintfArgFormatter; +using fmt::PrintfArgFormatter; // A custom argument formatter that doesn't print `-` for floating-point values // rounded to 0. @@ -30,18 +30,17 @@ class CustomArgFormatter // A custom argument formatter that doesn't print `-` for floating-point values // rounded to 0. -class CustomPrintfArgFormatter : - public BasicPrintfArgFormatter { +class CustomPrintfArgFormatter : public PrintfArgFormatter { public: - typedef BasicPrintfArgFormatter Base; - CustomPrintfArgFormatter(fmt::BasicWriter &w, fmt::FormatSpec &spec) - : Base(w, spec) {} + : PrintfArgFormatter(w, spec) {} - void visit_double(double value) { + using PrintfArgFormatter::operator(); + + void operator()(double value) { if (round(value * pow(10, spec().precision())) == 0) value = 0; - Base::visit_double(value); + PrintfArgFormatter::operator()(value); } };