A custom FormatSpec type can be passed as a template argument to the ArgFormatter chain (#439)

This commit is contained in:
Jean-Charles Lefebvre 2016-12-29 17:24:08 +01:00
parent db780cb119
commit 0cce094c89
6 changed files with 67 additions and 48 deletions

View File

@ -120,17 +120,19 @@ 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> { public fmt::BasicArgFormatter<CustomArgFormatter, char, fmt::FormatSpec> {
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>(f, s, fmt) {} : fmt::BasicArgFormatter<CustomArgFormatter, char,
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>::visit_int(value); fmt::BasicArgFormatter<CustomArgFormatter, char,
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> template <typename Impl, typename Char, typename Spec>
class BasicPrintfArgFormatter; class BasicPrintfArgFormatter;
template <typename CharType, template <typename CharType,
@ -1697,6 +1697,7 @@ struct TypeSpec : EmptySpec {
int precision() const { return -1; } int precision() const { return -1; }
bool flag(unsigned) const { return false; } bool flag(unsigned) const { return false; }
char type() const { return TYPE; } char type() const { return TYPE; }
char type_prefix() const { return TYPE; }
char fill() const { return ' '; } char fill() const { return ' '; }
}; };
@ -1732,6 +1733,7 @@ struct AlignTypeSpec : AlignSpec {
bool flag(unsigned) const { return false; } bool flag(unsigned) const { return false; }
char type() const { return TYPE; } char type() const { return TYPE; }
char type_prefix() const { return TYPE; }
}; };
// A full format specifier. // A full format specifier.
@ -1747,6 +1749,7 @@ struct FormatSpec : AlignSpec {
bool flag(unsigned f) const { return (flags_ & f) != 0; } bool flag(unsigned f) const { return (flags_ & f) != 0; }
int precision() const { return precision_; } int precision() const { return precision_; }
char type() const { return type_; } char type() const { return type_; }
char type_prefix() const { return type_; }
}; };
// An integer format specifier. // An integer format specifier.
@ -1922,11 +1925,11 @@ class ArgMap {
} }
}; };
template <typename Impl, typename Char> template <typename Impl, typename Char, typename Spec>
class ArgFormatterBase : public ArgVisitor<Impl, void> { class ArgFormatterBase : public ArgVisitor<Impl, void> {
private: private:
BasicWriter<Char> &writer_; BasicWriter<Char> &writer_;
FormatSpec &spec_; Spec &spec_;
FMT_DISALLOW_COPY_AND_ASSIGN(ArgFormatterBase); FMT_DISALLOW_COPY_AND_ASSIGN(ArgFormatterBase);
@ -1938,7 +1941,7 @@ class ArgFormatterBase : public ArgVisitor<Impl, void> {
protected: protected:
BasicWriter<Char> &writer() { return writer_; } BasicWriter<Char> &writer() { return writer_; }
FormatSpec &spec() { return spec_; } Spec &spec() { return spec_; }
void write(bool value) { void write(bool value) {
const char *str_value = value ? "true" : "false"; const char *str_value = value ? "true" : "false";
@ -1952,7 +1955,9 @@ class ArgFormatterBase : public ArgVisitor<Impl, void> {
} }
public: public:
ArgFormatterBase(BasicWriter<Char> &w, FormatSpec &s) typedef typename Spec SpecType;
ArgFormatterBase(BasicWriter<Char> &w, Spec &s)
: writer_(w), spec_(s) {} : writer_(w), spec_(s) {}
template <typename T> template <typename T>
@ -2086,8 +2091,8 @@ class FormatterBase {
will be called. will be called.
\endrst \endrst
*/ */
template <typename Impl, typename Char> template <typename Impl, typename Char, typename Spec>
class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> { class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char, Spec> {
private: private:
BasicFormatter<Char, Impl> &formatter_; BasicFormatter<Char, Impl> &formatter_;
const Char *format_; const Char *format_;
@ -2102,8 +2107,8 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
\endrst \endrst
*/ */
BasicArgFormatter(BasicFormatter<Char, Impl> &formatter, BasicArgFormatter(BasicFormatter<Char, Impl> &formatter,
FormatSpec &spec, const Char *fmt) Spec &spec, const Char *fmt)
: internal::ArgFormatterBase<Impl, Char>(formatter.writer(), spec), : internal::ArgFormatterBase<Impl, Char, Spec>(formatter.writer(), spec),
formatter_(formatter), format_(fmt) {} formatter_(formatter), format_(fmt) {}
/** Formats an argument of a custom (user-defined) type. */ /** Formats an argument of a custom (user-defined) type. */
@ -2114,12 +2119,14 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
/** The default argument formatter. */ /** The default argument formatter. */
template <typename Char> template <typename Char>
class ArgFormatter : public BasicArgFormatter<ArgFormatter<Char>, Char> { class ArgFormatter :
public BasicArgFormatter<ArgFormatter<Char>, Char, FormatSpec> {
public: public:
/** Constructs an argument formatter object. */ /** Constructs an argument formatter object. */
ArgFormatter(BasicFormatter<Char> &formatter, ArgFormatter(BasicFormatter<Char> &formatter,
FormatSpec &spec, const Char *fmt) FormatSpec &spec, const Char *fmt)
: BasicArgFormatter<ArgFormatter<Char>, Char>(formatter, spec, fmt) {} : BasicArgFormatter<ArgFormatter<Char>,
Char, FormatSpec>(formatter, spec, fmt) {}
}; };
/** This template formats data and writes the output to a writer. */ /** This template formats data and writes the output to a writer. */
@ -2501,16 +2508,16 @@ class BasicWriter {
void write_int(T value, Spec spec); void write_int(T value, Spec spec);
// Formats a floating-point number (double or long double). // Formats a floating-point number (double or long double).
template <typename T> template <typename T, typename Spec>
void write_double(T value, const FormatSpec &spec); void write_double(T value, const Spec &spec);
// Writes a formatted string. // Writes a formatted string.
template <typename StrChar> template <typename StrChar>
CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec); CharPtr write_str(const StrChar *s, std::size_t size, const AlignSpec &spec);
template <typename StrChar> template <typename StrChar, typename Spec>
void write_str(const internal::Arg::StringValue<StrChar> &str, void write_str(const internal::Arg::StringValue<StrChar> &str,
const FormatSpec &spec); const Spec &spec);
// This following methods are private to disallow writing wide characters // This following methods are private to disallow writing wide characters
// and strings to a char stream. If you want to print a wide string as a // and strings to a char stream. If you want to print a wide string as a
@ -2529,10 +2536,10 @@ 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 Impl, typename Char_, typename Spec_>
friend class internal::ArgFormatterBase; friend class internal::ArgFormatterBase;
template <typename Impl, typename Char_> template <typename Impl, typename Char_, typename Spec_>
friend class BasicPrintfArgFormatter; friend class BasicPrintfArgFormatter;
protected: protected:
@ -2729,9 +2736,9 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
} }
template <typename Char> template <typename Char>
template <typename StrChar> template <typename StrChar, typename Spec>
void BasicWriter<Char>::write_str( void BasicWriter<Char>::write_str(
const internal::Arg::StringValue<StrChar> &s, const FormatSpec &spec) { const internal::Arg::StringValue<StrChar> &s, const Spec &spec) {
// Check if StrChar is convertible to Char. // Check if StrChar is convertible to Char.
internal::CharTraits<Char>::convert(StrChar()); internal::CharTraits<Char>::convert(StrChar());
if (spec.type_ && spec.type_ != 's') if (spec.type_ && spec.type_ != 's')
@ -2855,7 +2862,7 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
UnsignedType n = abs_value; UnsignedType n = abs_value;
if (spec.flag(HASH_FLAG)) { if (spec.flag(HASH_FLAG)) {
prefix[prefix_size++] = '0'; prefix[prefix_size++] = '0';
prefix[prefix_size++] = spec.type(); prefix[prefix_size++] = spec.type_prefix();
} }
unsigned num_digits = 0; unsigned num_digits = 0;
do { do {
@ -2875,7 +2882,7 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
UnsignedType n = abs_value; UnsignedType n = abs_value;
if (spec.flag(HASH_FLAG)) { if (spec.flag(HASH_FLAG)) {
prefix[prefix_size++] = '0'; prefix[prefix_size++] = '0';
prefix[prefix_size++] = spec.type(); prefix[prefix_size++] = spec.type_prefix();
} }
unsigned num_digits = 0; unsigned num_digits = 0;
do { do {
@ -2923,8 +2930,8 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
} }
template <typename Char> template <typename Char>
template <typename T> template <typename T, typename Spec>
void BasicWriter<Char>::write_double(T value, const FormatSpec &spec) { void BasicWriter<Char>::write_double(T value, const Spec &spec) {
// Check type. // Check type.
char type = spec.type(); char type = spec.type();
bool upper = false; bool upper = false;
@ -3661,7 +3668,7 @@ const Char *BasicFormatter<Char, ArgFormatter>::format(
const Char *&format_str, const internal::Arg &arg) { const Char *&format_str, const internal::Arg &arg) {
using internal::Arg; using internal::Arg;
const Char *s = format_str; const Char *s = format_str;
FormatSpec spec; typename ArgFormatter::SpecType 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(this, arg.custom.value, &s);

View File

@ -189,15 +189,16 @@ class WidthHandler : public ArgVisitor<WidthHandler, unsigned> {
superclass will be called. superclass will be called.
\endrst \endrst
*/ */
template <typename Impl, typename Char> template <typename Impl, typename Char, typename Spec>
class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> { class BasicPrintfArgFormatter :
public internal::ArgFormatterBase<Impl, Char, Spec> {
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<Impl, Char> Base; typedef internal::ArgFormatterBase<Impl, Char, Spec> Base;
public: public:
/** /**
@ -207,12 +208,12 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
specifier information for standard argument types. specifier information for standard argument types.
\endrst \endrst
*/ */
BasicPrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s) BasicPrintfArgFormatter(BasicWriter<Char> &w, Spec &s)
: internal::ArgFormatterBase<Impl, Char>(w, s) {} : internal::ArgFormatterBase<Impl, Char, Spec>(w, s) {}
/** Formats an argument of type ``bool``. */ /** Formats an argument of type ``bool``. */
void visit_bool(bool value) { void visit_bool(bool value) {
FormatSpec &fmt_spec = this->spec(); Spec &fmt_spec = this->spec();
if (fmt_spec.type_ != 's') if (fmt_spec.type_ != 's')
return this->visit_any_int(value); return this->visit_any_int(value);
fmt_spec.type_ = 0; fmt_spec.type_ = 0;
@ -221,7 +222,7 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
/** Formats a character. */ /** Formats a character. */
void visit_char(int value) { void visit_char(int value) {
const FormatSpec &fmt_spec = this->spec(); const Spec &fmt_spec = this->spec();
BasicWriter<Char> &w = this->writer(); BasicWriter<Char> &w = this->writer();
if (fmt_spec.type_ && fmt_spec.type_ != 'c') if (fmt_spec.type_ && fmt_spec.type_ != 'c')
w.write_int(value, fmt_spec); w.write_int(value, fmt_spec);
@ -271,12 +272,12 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
/** The default printf argument formatter. */ /** The default printf argument formatter. */
template <typename Char> template <typename Char>
class PrintfArgFormatter class PrintfArgFormatter :
: public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char> { public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char, FormatSpec> {
public: public:
/** Constructs an argument formatter object. */ /** Constructs an argument formatter object. */
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s) PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
: BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {} : BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char, FormatSpec>(w, s) {}
}; };
/** This template formats data and writes the output to a writer. */ /** This template formats data and writes the output to a writer. */

View File

@ -14,26 +14,30 @@ 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> { public fmt::BasicArgFormatter<CustomArgFormatter, char, fmt::FormatSpec> {
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>(f, s, fmt) {} : fmt::BasicArgFormatter<CustomArgFormatter, char,
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>::visit_double(value); fmt::BasicArgFormatter<CustomArgFormatter, char,
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> Base; typedef BasicPrintfArgFormatter<CustomPrintfArgFormatter, char,
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,13 +1630,16 @@ 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> Base; typedef fmt::internal::ArgFormatterBase<MockArgFormatter, char,
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, char>(f.writer(), s) { : fmt::internal::ArgFormatterBase<MockArgFormatter,
char, fmt::FormatSpec>(f.writer(), s) {
EXPECT_CALL(*this, visit_int(42)); EXPECT_CALL(*this, visit_int(42));
} }

View File

@ -58,10 +58,12 @@ TEST(OStreamTest, Enum) {
EXPECT_EQ("0", fmt::format("{}", A)); EXPECT_EQ("0", fmt::format("{}", A));
} }
struct TestArgFormatter : fmt::BasicArgFormatter<TestArgFormatter, char> { struct TestArgFormatter :
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>(f, s, fmt) {} : fmt::BasicArgFormatter<TestArgFormatter, char,
fmt::FormatSpec>(f, s, fmt) {}
}; };
TEST(OStreamTest, CustomArg) { TEST(OStreamTest, CustomArg) {