mirror of
https://github.com/fmtlib/fmt.git
synced 2025-03-01 01:13:32 +00:00
Merge BasicPrintfArgFormatter and PrintfArgFormatter
This commit is contained in:
parent
e2dfd39c75
commit
d58cc8a4a8
@ -374,8 +374,8 @@ typedef BasicWriter<wchar_t> WWriter;
|
|||||||
template <typename Char>
|
template <typename Char>
|
||||||
class ArgFormatter;
|
class ArgFormatter;
|
||||||
|
|
||||||
template <typename Impl, typename Char>
|
template <typename Char>
|
||||||
class BasicPrintfArgFormatter;
|
class PrintfArgFormatter;
|
||||||
|
|
||||||
template <typename Char>
|
template <typename Char>
|
||||||
class basic_format_context;
|
class basic_format_context;
|
||||||
@ -2489,8 +2489,8 @@ class BasicWriter {
|
|||||||
template <typename Impl, typename Char_>
|
template <typename Impl, typename Char_>
|
||||||
friend class internal::ArgFormatterBase;
|
friend class internal::ArgFormatterBase;
|
||||||
|
|
||||||
template <typename Impl, typename Char_>
|
template <typename Char_>
|
||||||
friend class BasicPrintfArgFormatter;
|
friend class PrintfArgFormatter;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
/**
|
/**
|
||||||
|
37
fmt/printf.h
37
fmt/printf.h
@ -204,30 +204,19 @@ class WidthHandler {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
\rst
|
\rst
|
||||||
A ``printf`` argument formatter based on the `curiously recurring template
|
The ``printf`` argument formatter.
|
||||||
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
|
\endrst
|
||||||
*/
|
*/
|
||||||
template <typename Impl, typename Char>
|
template <typename Char>
|
||||||
class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
class PrintfArgFormatter :
|
||||||
|
public internal::ArgFormatterBase<PrintfArgFormatter<Char>, Char> {
|
||||||
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<PrintfArgFormatter<Char>, Char> Base;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
@ -237,8 +226,8 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
|||||||
specifier information for standard argument types.
|
specifier information for standard argument types.
|
||||||
\endrst
|
\endrst
|
||||||
*/
|
*/
|
||||||
BasicPrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec)
|
PrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec)
|
||||||
: internal::ArgFormatterBase<Impl, Char>(writer, spec) {}
|
: internal::ArgFormatterBase<PrintfArgFormatter<Char>, Char>(writer, spec) {}
|
||||||
|
|
||||||
using Base::operator();
|
using Base::operator();
|
||||||
|
|
||||||
@ -246,7 +235,7 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
|||||||
void operator()(bool value) {
|
void operator()(bool value) {
|
||||||
FormatSpec &fmt_spec = this->spec();
|
FormatSpec &fmt_spec = this->spec();
|
||||||
if (fmt_spec.type_ != 's')
|
if (fmt_spec.type_ != 's')
|
||||||
return this->visit_any_int(value);
|
return (*this)(value ? 1 : 0);
|
||||||
fmt_spec.type_ = 0;
|
fmt_spec.type_ = 0;
|
||||||
this->write(value);
|
this->write(value);
|
||||||
}
|
}
|
||||||
@ -301,16 +290,6 @@ class BasicPrintfArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** The default printf argument formatter. */
|
|
||||||
template <typename Char>
|
|
||||||
class PrintfArgFormatter
|
|
||||||
: public BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char> {
|
|
||||||
public:
|
|
||||||
/** Constructs an argument formatter object. */
|
|
||||||
PrintfArgFormatter(BasicWriter<Char> &w, FormatSpec &s)
|
|
||||||
: BasicPrintfArgFormatter<PrintfArgFormatter<Char>, Char>(w, s) {}
|
|
||||||
};
|
|
||||||
|
|
||||||
/** This template formats data and writes the output to a writer. */
|
/** This template formats data and writes the output to a writer. */
|
||||||
template <typename Char,
|
template <typename Char,
|
||||||
typename ArgFormatter = PrintfArgFormatter<Char> >
|
typename ArgFormatter = PrintfArgFormatter<Char> >
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
#include "fmt/printf.h"
|
#include "fmt/printf.h"
|
||||||
#include "gtest-extra.h"
|
#include "gtest-extra.h"
|
||||||
|
|
||||||
using fmt::BasicPrintfArgFormatter;
|
using fmt::PrintfArgFormatter;
|
||||||
|
|
||||||
// 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.
|
||||||
@ -30,18 +30,17 @@ class CustomArgFormatter
|
|||||||
|
|
||||||
// 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 PrintfArgFormatter<char> {
|
||||||
public BasicPrintfArgFormatter<CustomPrintfArgFormatter, char> {
|
|
||||||
public:
|
public:
|
||||||
typedef BasicPrintfArgFormatter<CustomPrintfArgFormatter, char> Base;
|
|
||||||
|
|
||||||
CustomPrintfArgFormatter(fmt::BasicWriter<char> &w, fmt::FormatSpec &spec)
|
CustomPrintfArgFormatter(fmt::BasicWriter<char> &w, fmt::FormatSpec &spec)
|
||||||
: Base(w, spec) {}
|
: PrintfArgFormatter<char>(w, spec) {}
|
||||||
|
|
||||||
void visit_double(double value) {
|
using PrintfArgFormatter<char>::operator();
|
||||||
|
|
||||||
|
void operator()(double value) {
|
||||||
if (round(value * pow(10, spec().precision())) == 0)
|
if (round(value * pow(10, spec().precision())) == 0)
|
||||||
value = 0;
|
value = 0;
|
||||||
Base::visit_double(value);
|
PrintfArgFormatter<char>::operator()(value);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user