Skip to content

Commit

Permalink
Make printf argument formatters public (#335, #360)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jul 21, 2016
1 parent 631ffef commit 6ee9f2e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
6 changes: 6 additions & 0 deletions doc/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ argument type doesn't match its format specification.
.. doxygenclass:: fmt::PrintfFormatter
:members:

.. doxygenclass:: fmt::BasicPrintfArgFormatter
:members:

.. doxygenclass:: fmt::PrintfArgFormatter
:members:

Write API
=========

Expand Down
10 changes: 5 additions & 5 deletions fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,9 @@ typedef BasicWriter<wchar_t> WWriter;
template <typename Char>
class ArgFormatter;

template <typename Impl, typename Char>
class BasicPrintfArgFormatter;

template <typename CharType,
typename ArgFormatter = fmt::ArgFormatter<CharType> >
class BasicFormatter;
Expand Down Expand Up @@ -1319,9 +1322,6 @@ class RuntimeError : public std::runtime_error {
~RuntimeError() throw();
};

template <typename Impl, typename Char>
class BasicPrintfArgFormatter;

template <typename Char>
class ArgMap;
} // namespace internal
Expand Down Expand Up @@ -1979,7 +1979,7 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
: internal::ArgFormatterBase<Impl, Char>(formatter.writer(), spec),
formatter_(formatter), format_(fmt) {}

/** Formats argument of a custom (user-defined) type. */
/** Formats an argument of a custom (user-defined) type. */
void visit_custom(internal::Arg::CustomValue c) {
c.format(&formatter_, c.value, &format_);
}
Expand Down Expand Up @@ -2406,7 +2406,7 @@ class BasicWriter {
friend class internal::ArgFormatterBase;

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

protected:
/**
Expand Down
44 changes: 36 additions & 8 deletions fmt/printf.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,47 @@ class WidthHandler : public ArgVisitor<WidthHandler, unsigned> {
return static_cast<unsigned>(width);
}
};
} // namespace internal

/**
\rst
A ``printf`` argument formatter based on the `curiously recurring template
pattern <http://en.wikipedia.org/wiki/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.
\endrst
*/
template <typename Impl, typename Char>
class BasicPrintfArgFormatter : public ArgFormatterBase<Impl, Char> {
class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
private:
void write_null_pointer() {
this->spec().type_ = 0;
this->write("(nil)");
}

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

public:
BasicPrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
: ArgFormatterBase<Impl, Char>(w, s) {}
/**
\rst
Constructs an argument formatter object.
*writer* is a reference to the output writer and *spec* contains format
specifier information for standard argument types.
\endrst
*/
BasicPrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec)
: internal::ArgFormatterBase<Impl, Char>(writer, spec) {}

/** Formats an argument of type ``bool``. */
void visit_bool(bool value) {
FormatSpec &fmt_spec = this->spec();
if (fmt_spec.type_ != 's')
Expand All @@ -193,6 +219,7 @@ class BasicPrintfArgFormatter : public ArgFormatterBase<Impl, Char> {
this->write(value);
}

/** Formats a character. */
void visit_char(int value) {
const FormatSpec &fmt_spec = this->spec();
BasicWriter<Char> &w = this->writer();
Expand All @@ -215,6 +242,7 @@ class BasicPrintfArgFormatter : public ArgFormatterBase<Impl, Char> {
*out = static_cast<Char>(value);
}

/** Formats a null-terminated C string. */
void visit_cstring(const char *value) {
if (value)
Base::visit_cstring(value);
Expand All @@ -224,14 +252,16 @@ class BasicPrintfArgFormatter : public ArgFormatterBase<Impl, Char> {
this->write("(null)");
}

/** Formats a pointer. */
void visit_pointer(const void *value) {
if (value)
return Base::visit_pointer(value);
this->spec().type_ = 0;
write_null_pointer();
}

void visit_custom(Arg::CustomValue c) {
/** Formats an argument of a custom (user-defined) type. */
void visit_custom(internal::Arg::CustomValue c) {
BasicFormatter<Char> formatter(ArgList(), this->writer());
const Char format_str[] = {'}', 0};
const Char *format = format_str;
Expand All @@ -248,11 +278,9 @@ class PrintfArgFormatter
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
: BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
};
} // namespace internal

/** This template formats data and writes the output to a writer. */
template <typename Char,
typename ArgFormatter = internal::PrintfArgFormatter<Char> >
template <typename Char, typename ArgFormatter = PrintfArgFormatter<Char> >
class PrintfFormatter : private internal::FormatterBase {
private:
BasicWriter<Char> &writer_;
Expand Down
15 changes: 9 additions & 6 deletions test/custom-formatter-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "fmt/printf.h"
#include "gtest-extra.h"

using fmt::internal::BasicPrintfArgFormatter;
using fmt::BasicPrintfArgFormatter;

// A custom argument formatter that doesn't print `-` for floating-point values
// rounded to 0.
Expand All @@ -30,15 +30,18 @@ class CustomArgFormatter

// A custom argument formatter that doesn't print `-` for floating-point values
// rounded to 0.
class CustomPAF : public BasicPrintfArgFormatter<CustomPAF, char> {
class CustomPrintfArgFormatter :
public BasicPrintfArgFormatter<CustomPrintfArgFormatter, char> {
public:
CustomPAF(fmt::BasicWriter<char> &writer, fmt::FormatSpec &spec)
: BasicPrintfArgFormatter<CustomPAF, char>(writer, spec) {}
typedef BasicPrintfArgFormatter<CustomPrintfArgFormatter, char> Base;

CustomPrintfArgFormatter(fmt::BasicWriter<char> &w, fmt::FormatSpec &spec)
: Base(w, spec) {}

void visit_double(double value) {
if (round(value * pow(10, spec().precision())) == 0)
value = 0;
BasicPrintfArgFormatter<CustomPAF, char>::visit_double(value);
Base::visit_double(value);
}
};

Expand All @@ -53,7 +56,7 @@ FMT_VARIADIC(std::string, custom_format, const char *)

std::string custom_sprintf(const char* format_str, fmt::ArgList args){
fmt::MemoryWriter writer;
fmt::PrintfFormatter<char, CustomPAF> formatter(args, writer);
fmt::PrintfFormatter<char, CustomPrintfArgFormatter> formatter(args, writer);
formatter.format(format_str);
return writer.str();
}
Expand Down

0 comments on commit 6ee9f2e

Please sign in to comment.