Spec template argument defaulted to FormatSpec

This commit is contained in:
Jean-Charles Lefebvre 2016-12-30 08:40:02 +01:00
parent cf78fb2745
commit 1e6edbc9b5
5 changed files with 16 additions and 28 deletions

View File

@ -120,19 +120,17 @@ custom argument formatter class::
// A custom argument formatter that formats negative integers as unsigned // A custom argument formatter that formats negative integers as unsigned
// with the ``x`` format specifier. // with the ``x`` format specifier.
class CustomArgFormatter : class CustomArgFormatter :
public fmt::BasicArgFormatter<CustomArgFormatter, char, fmt::FormatSpec> { public fmt::BasicArgFormatter<CustomArgFormatter, char> {
public: public:
CustomArgFormatter(fmt::BasicFormatter<char, CustomArgFormatter> &f, CustomArgFormatter(fmt::BasicFormatter<char, CustomArgFormatter> &f,
fmt::FormatSpec &s, const char *fmt) fmt::FormatSpec &s, const char *fmt)
: fmt::BasicArgFormatter<CustomArgFormatter, char, : fmt::BasicArgFormatter<CustomArgFormatter, char>(f, s, fmt) {}
fmt::FormatSpec>(f, s, fmt) {}
void visit_int(int value) { void visit_int(int value) {
if (spec().type() == 'x') if (spec().type() == 'x')
visit_uint(value); // convert to unsigned and format visit_uint(value); // convert to unsigned and format
else else
fmt::BasicArgFormatter<CustomArgFormatter, char, fmt::BasicArgFormatter<CustomArgFormatter, char>::visit_int(value);
fmt::FormatSpec>::visit_int(value);
} }
}; };

View File

@ -431,7 +431,7 @@ typedef BasicWriter<wchar_t> WWriter;
template <typename Char> template <typename Char>
class ArgFormatter; class ArgFormatter;
template <typename Impl, typename Char, typename Spec> template <typename Impl, typename Char, typename Spec = fmt::FormatSpec>
class BasicPrintfArgFormatter; class BasicPrintfArgFormatter;
template <typename CharType, template <typename CharType,
@ -1925,7 +1925,7 @@ class ArgMap {
} }
}; };
template <typename Impl, typename Char, typename Spec> template <typename Impl, typename Char, typename Spec = fmt::FormatSpec>
class ArgFormatterBase : public ArgVisitor<Impl, void> { class ArgFormatterBase : public ArgVisitor<Impl, void> {
private: private:
BasicWriter<Char> &writer_; BasicWriter<Char> &writer_;
@ -2091,7 +2091,7 @@ class FormatterBase {
will be called. will be called.
\endrst \endrst
*/ */
template <typename Impl, typename Char, typename Spec> template <typename Impl, typename Char, typename Spec = fmt::FormatSpec>
class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char, Spec> { class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char, Spec> {
private: private:
BasicFormatter<Char, Impl> &formatter_; BasicFormatter<Char, Impl> &formatter_;

View File

@ -15,29 +15,25 @@ using fmt::BasicPrintfArgFormatter;
// 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::BasicArgFormatter<CustomArgFormatter, char, fmt::FormatSpec> { public fmt::BasicArgFormatter<CustomArgFormatter, char> {
public: public:
CustomArgFormatter(fmt::BasicFormatter<char, CustomArgFormatter> &f, CustomArgFormatter(fmt::BasicFormatter<char, CustomArgFormatter> &f,
fmt::FormatSpec &s, const char *fmt) fmt::FormatSpec &s, const char *fmt)
: fmt::BasicArgFormatter<CustomArgFormatter, char, : fmt::BasicArgFormatter<CustomArgFormatter, char>(f, s, fmt) {}
fmt::FormatSpec>(f, s, fmt) {}
void visit_double(double value) { void visit_double(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, fmt::BasicArgFormatter<CustomArgFormatter, char>::visit_double(value);
fmt::FormatSpec>::visit_double(value);
} }
}; };
// 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 CustomPrintfArgFormatter : class CustomPrintfArgFormatter :
public BasicPrintfArgFormatter<CustomPrintfArgFormatter, char, public BasicPrintfArgFormatter<CustomPrintfArgFormatter, char> {
fmt::FormatSpec> {
public: public:
typedef BasicPrintfArgFormatter<CustomPrintfArgFormatter, char, typedef BasicPrintfArgFormatter<CustomPrintfArgFormatter, char> Base;
fmt::FormatSpec> Base;
CustomPrintfArgFormatter(fmt::BasicWriter<char> &w, fmt::FormatSpec &spec) CustomPrintfArgFormatter(fmt::BasicWriter<char> &w, fmt::FormatSpec &spec)
: Base(w, spec) {} : Base(w, spec) {}

View File

@ -1630,16 +1630,13 @@ TEST(FormatTest, Enum) {
} }
class MockArgFormatter : class MockArgFormatter :
public fmt::internal::ArgFormatterBase<MockArgFormatter, char, public fmt::internal::ArgFormatterBase<MockArgFormatter, char> {
fmt::FormatSpec> {
public: public:
typedef fmt::internal::ArgFormatterBase<MockArgFormatter, char, typedef fmt::internal::ArgFormatterBase<MockArgFormatter, char> Base;
fmt::FormatSpec> Base;
MockArgFormatter(fmt::BasicFormatter<char, MockArgFormatter> &f, MockArgFormatter(fmt::BasicFormatter<char, MockArgFormatter> &f,
fmt::FormatSpec &s, const char *) fmt::FormatSpec &s, const char *)
: fmt::internal::ArgFormatterBase<MockArgFormatter, : fmt::internal::ArgFormatterBase<MockArgFormatter, char>(f.writer(), s) {
char, fmt::FormatSpec>(f.writer(), s) {
EXPECT_CALL(*this, visit_int(42)); EXPECT_CALL(*this, visit_int(42));
} }
@ -1663,4 +1660,3 @@ void convert(int);
TEST(FormatTest, ConvertCollision) { TEST(FormatTest, ConvertCollision) {
fmt::format("{}", 42); fmt::format("{}", 42);
} }

View File

@ -58,12 +58,10 @@ TEST(OStreamTest, Enum) {
EXPECT_EQ("0", fmt::format("{}", A)); EXPECT_EQ("0", fmt::format("{}", A));
} }
struct TestArgFormatter : struct TestArgFormatter : fmt::BasicArgFormatter<TestArgFormatter, char> {
fmt::BasicArgFormatter<TestArgFormatter, char, fmt::FormatSpec> {
TestArgFormatter(fmt::BasicFormatter<char, TestArgFormatter> &f, TestArgFormatter(fmt::BasicFormatter<char, TestArgFormatter> &f,
fmt::FormatSpec &s, const char *fmt) fmt::FormatSpec &s, const char *fmt)
: fmt::BasicArgFormatter<TestArgFormatter, char, : fmt::BasicArgFormatter<TestArgFormatter, char>(f, s, fmt) {}
fmt::FormatSpec>(f, s, fmt) {}
}; };
TEST(OStreamTest, CustomArg) { TEST(OStreamTest, CustomArg) {