Skip to content

Commit

Permalink
A custom FormatSpec type can be passed as a template argument to the …
Browse files Browse the repository at this point in the history
…ArgFormatter chain (fmtlib#439)
  • Loading branch information
polyvertex committed Dec 29, 2016
1 parent db780cb commit 0cce094
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 48 deletions.
8 changes: 5 additions & 3 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,19 @@ custom argument formatter class::
// A custom argument formatter that formats negative integers as unsigned
// with the ``x`` format specifier.
class CustomArgFormatter :
public fmt::BasicArgFormatter<CustomArgFormatter, char> {
public fmt::BasicArgFormatter<CustomArgFormatter, char, fmt::FormatSpec> {
public:
CustomArgFormatter(fmt::BasicFormatter<char, CustomArgFormatter> &f,
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) {
if (spec().type() == 'x')
visit_uint(value); // convert to unsigned and format
else
fmt::BasicArgFormatter<CustomArgFormatter, char>::visit_int(value);
fmt::BasicArgFormatter<CustomArgFormatter, char,
fmt::FormatSpec>::visit_int(value);
}
};

Expand Down
55 changes: 31 additions & 24 deletions fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ typedef BasicWriter<wchar_t> WWriter;
template <typename Char>
class ArgFormatter;

template <typename Impl, typename Char>
template <typename Impl, typename Char, typename Spec>
class BasicPrintfArgFormatter;

template <typename CharType,
Expand Down Expand Up @@ -1697,6 +1697,7 @@ struct TypeSpec : EmptySpec {
int precision() const { return -1; }
bool flag(unsigned) const { return false; }
char type() const { return TYPE; }
char type_prefix() const { return TYPE; }
char fill() const { return ' '; }
};

Expand Down Expand Up @@ -1732,6 +1733,7 @@ struct AlignTypeSpec : AlignSpec {

bool flag(unsigned) const { return false; }
char type() const { return TYPE; }
char type_prefix() const { return TYPE; }
};

// A full format specifier.
Expand All @@ -1747,6 +1749,7 @@ struct FormatSpec : AlignSpec {
bool flag(unsigned f) const { return (flags_ & f) != 0; }
int precision() const { return precision_; }
char type() const { return type_; }
char type_prefix() const { return type_; }
};

// An integer format specifier.
Expand Down Expand Up @@ -1922,11 +1925,11 @@ class ArgMap {
}
};

template <typename Impl, typename Char>
template <typename Impl, typename Char, typename Spec>
class ArgFormatterBase : public ArgVisitor<Impl, void> {
private:
BasicWriter<Char> &writer_;
FormatSpec &spec_;
Spec &spec_;

FMT_DISALLOW_COPY_AND_ASSIGN(ArgFormatterBase);

Expand All @@ -1938,7 +1941,7 @@ class ArgFormatterBase : public ArgVisitor<Impl, void> {

protected:
BasicWriter<Char> &writer() { return writer_; }
FormatSpec &spec() { return spec_; }
Spec &spec() { return spec_; }

void write(bool value) {
const char *str_value = value ? "true" : "false";
Expand All @@ -1952,7 +1955,9 @@ class ArgFormatterBase : public ArgVisitor<Impl, void> {
}

public:
ArgFormatterBase(BasicWriter<Char> &w, FormatSpec &s)
typedef typename Spec SpecType;

ArgFormatterBase(BasicWriter<Char> &w, Spec &s)
: writer_(w), spec_(s) {}

template <typename T>
Expand Down Expand Up @@ -2086,8 +2091,8 @@ class FormatterBase {
will be called.
\endrst
*/
template <typename Impl, typename Char>
class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
template <typename Impl, typename Char, typename Spec>
class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char, Spec> {
private:
BasicFormatter<Char, Impl> &formatter_;
const Char *format_;
Expand All @@ -2102,8 +2107,8 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
\endrst
*/
BasicArgFormatter(BasicFormatter<Char, Impl> &formatter,
FormatSpec &spec, const Char *fmt)
: internal::ArgFormatterBase<Impl, Char>(formatter.writer(), spec),
Spec &spec, const Char *fmt)
: internal::ArgFormatterBase<Impl, Char, Spec>(formatter.writer(), spec),
formatter_(formatter), format_(fmt) {}

/** Formats an argument of a custom (user-defined) type. */
Expand All @@ -2114,12 +2119,14 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {

/** The default argument formatter. */
template <typename Char>
class ArgFormatter : public BasicArgFormatter<ArgFormatter<Char>, Char> {
class ArgFormatter :
public BasicArgFormatter<ArgFormatter<Char>, Char, FormatSpec> {
public:
/** Constructs an argument formatter object. */
ArgFormatter(BasicFormatter<Char> &formatter,
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. */
Expand Down Expand Up @@ -2501,16 +2508,16 @@ class BasicWriter {
void write_int(T value, Spec spec);

// Formats a floating-point number (double or long double).
template <typename T>
void write_double(T value, const FormatSpec &spec);
template <typename T, typename Spec>
void write_double(T value, const Spec &spec);

// Writes a formatted string.
template <typename StrChar>
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,
const FormatSpec &spec);
const Spec &spec);

// 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
Expand All @@ -2529,10 +2536,10 @@ class BasicWriter {
template<typename T>
void append_float_length(Char *&, T) {}

template <typename Impl, typename Char_>
template <typename Impl, typename Char_, typename Spec_>
friend class internal::ArgFormatterBase;

template <typename Impl, typename Char_>
template <typename Impl, typename Char_, typename Spec_>
friend class BasicPrintfArgFormatter;

protected:
Expand Down Expand Up @@ -2729,9 +2736,9 @@ typename BasicWriter<Char>::CharPtr BasicWriter<Char>::write_str(
}

template <typename Char>
template <typename StrChar>
template <typename StrChar, typename Spec>
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.
internal::CharTraits<Char>::convert(StrChar());
if (spec.type_ && spec.type_ != 's')
Expand Down Expand Up @@ -2855,7 +2862,7 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
UnsignedType n = abs_value;
if (spec.flag(HASH_FLAG)) {
prefix[prefix_size++] = '0';
prefix[prefix_size++] = spec.type();
prefix[prefix_size++] = spec.type_prefix();
}
unsigned num_digits = 0;
do {
Expand All @@ -2875,7 +2882,7 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
UnsignedType n = abs_value;
if (spec.flag(HASH_FLAG)) {
prefix[prefix_size++] = '0';
prefix[prefix_size++] = spec.type();
prefix[prefix_size++] = spec.type_prefix();
}
unsigned num_digits = 0;
do {
Expand Down Expand Up @@ -2923,8 +2930,8 @@ void BasicWriter<Char>::write_int(T value, Spec spec) {
}

template <typename Char>
template <typename T>
void BasicWriter<Char>::write_double(T value, const FormatSpec &spec) {
template <typename T, typename Spec>
void BasicWriter<Char>::write_double(T value, const Spec &spec) {
// Check type.
char type = spec.type();
bool upper = false;
Expand Down Expand Up @@ -3661,7 +3668,7 @@ const Char *BasicFormatter<Char, ArgFormatter>::format(
const Char *&format_str, const internal::Arg &arg) {
using internal::Arg;
const Char *s = format_str;
FormatSpec spec;
typename ArgFormatter::SpecType spec;
if (*s == ':') {
if (arg.type == Arg::CUSTOM) {
arg.custom.format(this, arg.custom.value, &s);
Expand Down
21 changes: 11 additions & 10 deletions fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,15 +189,16 @@ class WidthHandler : public ArgVisitor<WidthHandler, unsigned> {
superclass will be called.
\endrst
*/
template <typename Impl, typename Char>
class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
template <typename Impl, typename Char, typename Spec>
class BasicPrintfArgFormatter :
public internal::ArgFormatterBase<Impl, Char, Spec> {
private:
void write_null_pointer() {
this->spec().type_ = 0;
this->write("(nil)");
}

typedef internal::ArgFormatterBase<Impl, Char> Base;
typedef internal::ArgFormatterBase<Impl, Char, Spec> Base;

public:
/**
Expand All @@ -207,12 +208,12 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
specifier information for standard argument types.
\endrst
*/
BasicPrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
: internal::ArgFormatterBase<Impl, Char>(w, s) {}
BasicPrintfArgFormatter(BasicWriter<Char> &w, Spec &s)
: internal::ArgFormatterBase<Impl, Char, Spec>(w, s) {}

/** Formats an argument of type ``bool``. */
void visit_bool(bool value) {
FormatSpec &fmt_spec = this->spec();
Spec &fmt_spec = this->spec();
if (fmt_spec.type_ != 's')
return this->visit_any_int(value);
fmt_spec.type_ = 0;
Expand All @@ -221,7 +222,7 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {

/** Formats a character. */
void visit_char(int value) {
const FormatSpec &fmt_spec = this->spec();
const Spec &fmt_spec = this->spec();
BasicWriter<Char> &w = this->writer();
if (fmt_spec.type_ && fmt_spec.type_ != 'c')
w.write_int(value, fmt_spec);
Expand Down Expand Up @@ -271,12 +272,12 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {

/** The default printf argument formatter. */
template <typename Char>
class PrintfArgFormatter
: public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char> {
class PrintfArgFormatter :
public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char, FormatSpec> {
public:
/** Constructs an argument formatter object. */
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. */
Expand Down
16 changes: 10 additions & 6 deletions test/custom-formatter-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,30 @@ using fmt::BasicPrintfArgFormatter;

// A custom argument formatter that doesn't print `-` for floating-point values
// rounded to 0.
class CustomArgFormatter
: public fmt::BasicArgFormatter<CustomArgFormatter, char> {
class CustomArgFormatter :
public fmt::BasicArgFormatter<CustomArgFormatter, char, fmt::FormatSpec> {
public:
CustomArgFormatter(fmt::BasicFormatter<char, CustomArgFormatter> &f,
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) {
if (round(value * pow(10, spec().precision())) == 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
// rounded to 0.
class CustomPrintfArgFormatter :
public BasicPrintfArgFormatter<CustomPrintfArgFormatter, char> {
public BasicPrintfArgFormatter<CustomPrintfArgFormatter, char,
fmt::FormatSpec> {
public:
typedef BasicPrintfArgFormatter<CustomPrintfArgFormatter, char> Base;
typedef BasicPrintfArgFormatter<CustomPrintfArgFormatter, char,
fmt::FormatSpec> Base;

CustomPrintfArgFormatter(fmt::BasicWriter<char> &w, fmt::FormatSpec &spec)
: Base(w, spec) {}
Expand Down
9 changes: 6 additions & 3 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1630,13 +1630,16 @@ TEST(FormatTest, Enum) {
}

class MockArgFormatter :
public fmt::internal::ArgFormatterBase<MockArgFormatter, char> {
public fmt::internal::ArgFormatterBase<MockArgFormatter, char,
fmt::FormatSpec> {
public:
typedef fmt::internal::ArgFormatterBase<MockArgFormatter, char> Base;
typedef fmt::internal::ArgFormatterBase<MockArgFormatter, char,
fmt::FormatSpec> Base;

MockArgFormatter(fmt::BasicFormatter<char, MockArgFormatter> &f,
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));
}

Expand Down
6 changes: 4 additions & 2 deletions test/ostream-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ TEST(OStreamTest, Enum) {
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,
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) {
Expand Down

0 comments on commit 0cce094

Please sign in to comment.