mirror of
https://github.com/fmtlib/fmt.git
synced 2024-11-19 11:14:41 +00:00
Merge BasicArgFormatter and ArgFormatter
This commit is contained in:
parent
d4084ac5b1
commit
284297019f
42
fmt/format.h
42
fmt/format.h
@ -1873,7 +1873,7 @@ void ArgMap<Char>::init(const basic_format_args<Formatter> &args) {
|
||||
}
|
||||
}
|
||||
|
||||
template <typename Impl, typename Char>
|
||||
template <typename Char>
|
||||
class ArgFormatterBase {
|
||||
private:
|
||||
BasicWriter<Char> &writer_;
|
||||
@ -2055,9 +2055,9 @@ class format_context_base {
|
||||
};
|
||||
} // namespace internal
|
||||
|
||||
/** An argument formatter. */
|
||||
template <typename Impl, typename Char>
|
||||
class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
||||
/** The default argument formatter. */
|
||||
template <typename Char>
|
||||
class ArgFormatter : public internal::ArgFormatterBase<Char> {
|
||||
private:
|
||||
basic_format_context<Char> &ctx_;
|
||||
|
||||
@ -2065,31 +2065,21 @@ class BasicArgFormatter : public internal::ArgFormatterBase<Impl, Char> {
|
||||
/**
|
||||
\rst
|
||||
Constructs an argument formatter object.
|
||||
*formatter* is a reference to the main formatter object, *spec* contains
|
||||
format specifier information for standard argument types, and *fmt* points
|
||||
to the part of the format string being parsed for custom argument types.
|
||||
*writer* is a reference to the writer to be used for output,
|
||||
*ctx* is a reference to the formatting context, *spec* contains
|
||||
format specifier information for standard argument types.
|
||||
\endrst
|
||||
*/
|
||||
BasicArgFormatter(BasicWriter<Char> &writer, basic_format_context<Char> &ctx,
|
||||
FormatSpec &spec)
|
||||
: internal::ArgFormatterBase<Impl, Char>(writer, spec), ctx_(ctx) {}
|
||||
|
||||
using internal::ArgFormatterBase<Impl, Char>::operator();
|
||||
|
||||
/** Formats an argument of a custom (user-defined) type. */
|
||||
void operator()(internal::Arg::CustomValue c) {
|
||||
c.format(&this->writer(), c.value, &ctx_);
|
||||
}
|
||||
};
|
||||
|
||||
/** The default argument formatter. */
|
||||
template <typename Char>
|
||||
class ArgFormatter : public BasicArgFormatter<ArgFormatter<Char>, Char> {
|
||||
public:
|
||||
/** Constructs an argument formatter object. */
|
||||
ArgFormatter(BasicWriter<Char> &writer, basic_format_context<Char> &ctx,
|
||||
FormatSpec &spec)
|
||||
: BasicArgFormatter<ArgFormatter<Char>, Char>(writer, ctx, spec) {}
|
||||
: internal::ArgFormatterBase<Char>(writer, spec), ctx_(ctx) {}
|
||||
|
||||
using internal::ArgFormatterBase<Char>::operator();
|
||||
|
||||
/** Formats an argument of a custom (user-defined) type. */
|
||||
void operator()(format_arg::CustomValue c) {
|
||||
c.format(&this->writer(), c.value, &ctx_);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename Char>
|
||||
@ -2305,7 +2295,7 @@ class BasicWriter {
|
||||
template<typename T>
|
||||
void append_float_length(Char *&, T) {}
|
||||
|
||||
template <typename Impl, typename Char_>
|
||||
template <typename Char_>
|
||||
friend class internal::ArgFormatterBase;
|
||||
|
||||
template <typename Char_>
|
||||
|
@ -208,15 +208,14 @@ class WidthHandler {
|
||||
\endrst
|
||||
*/
|
||||
template <typename Char>
|
||||
class PrintfArgFormatter :
|
||||
public internal::ArgFormatterBase<PrintfArgFormatter<Char>, Char> {
|
||||
class PrintfArgFormatter : public internal::ArgFormatterBase<Char> {
|
||||
private:
|
||||
void write_null_pointer() {
|
||||
this->spec().type_ = 0;
|
||||
this->write("(nil)");
|
||||
}
|
||||
|
||||
typedef internal::ArgFormatterBase<PrintfArgFormatter<Char>, Char> Base;
|
||||
typedef internal::ArgFormatterBase<Char> Base;
|
||||
|
||||
public:
|
||||
/**
|
||||
@ -227,7 +226,7 @@ class PrintfArgFormatter :
|
||||
\endrst
|
||||
*/
|
||||
PrintfArgFormatter(BasicWriter<Char> &writer, FormatSpec &spec)
|
||||
: internal::ArgFormatterBase<PrintfArgFormatter<Char>, Char>(writer, spec) {}
|
||||
: internal::ArgFormatterBase<Char>(writer, spec) {}
|
||||
|
||||
using Base::operator();
|
||||
|
||||
|
@ -14,19 +14,18 @@ using fmt::PrintfArgFormatter;
|
||||
|
||||
// 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::ArgFormatter<char> {
|
||||
public:
|
||||
CustomArgFormatter(fmt::Writer &w, fmt::basic_format_context<char> &ctx,
|
||||
fmt::FormatSpec &s)
|
||||
: fmt::BasicArgFormatter<CustomArgFormatter, char>(w, ctx, s) {}
|
||||
: fmt::ArgFormatter<char>(w, ctx, s) {}
|
||||
|
||||
using fmt::BasicArgFormatter<CustomArgFormatter, char>::operator();
|
||||
using fmt::ArgFormatter<char>::operator();
|
||||
|
||||
void operator()(double value) {
|
||||
if (round(value * pow(10, spec().precision())) == 0)
|
||||
value = 0;
|
||||
fmt::BasicArgFormatter<CustomArgFormatter, char>::operator()(value);
|
||||
fmt::ArgFormatter<char>::operator()(value);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1619,17 +1619,16 @@ TEST(FormatTest, Enum) {
|
||||
EXPECT_EQ("0", fmt::format("{}", A));
|
||||
}
|
||||
|
||||
class MockArgFormatter :
|
||||
public fmt::internal::ArgFormatterBase<MockArgFormatter, char> {
|
||||
class MockArgFormatter : public fmt::internal::ArgFormatterBase<char> {
|
||||
private:
|
||||
MOCK_METHOD1(call, void (int value));
|
||||
|
||||
public:
|
||||
typedef fmt::internal::ArgFormatterBase<MockArgFormatter, char> Base;
|
||||
typedef fmt::internal::ArgFormatterBase<char> Base;
|
||||
|
||||
MockArgFormatter(fmt::Writer &w, fmt::format_context &ctx,
|
||||
fmt::FormatSpec &s)
|
||||
: fmt::internal::ArgFormatterBase<MockArgFormatter, char>(w, s) {
|
||||
: fmt::internal::ArgFormatterBase<char>(w, s) {
|
||||
EXPECT_CALL(*this, call(42));
|
||||
}
|
||||
|
||||
@ -1637,7 +1636,7 @@ class MockArgFormatter :
|
||||
|
||||
void operator()(int value) { call(value); }
|
||||
|
||||
void operator()(fmt::internal::Arg::CustomValue) {}
|
||||
void operator()(fmt::format_arg::CustomValue) {}
|
||||
};
|
||||
|
||||
void custom_vformat(fmt::CStringRef format_str, fmt::format_args args) {
|
||||
|
@ -58,10 +58,10 @@ TEST(OStreamTest, Enum) {
|
||||
EXPECT_EQ("0", fmt::format("{}", A));
|
||||
}
|
||||
|
||||
struct TestArgFormatter : fmt::BasicArgFormatter<TestArgFormatter, char> {
|
||||
struct TestArgFormatter : fmt::ArgFormatter<char> {
|
||||
TestArgFormatter(fmt::Writer &w, fmt::format_context &ctx,
|
||||
fmt::FormatSpec &s)
|
||||
: fmt::BasicArgFormatter<TestArgFormatter, char>(w, ctx, s) {}
|
||||
: fmt::ArgFormatter<char>(w, ctx, s) {}
|
||||
};
|
||||
|
||||
TEST(OStreamTest, CustomArg) {
|
||||
|
Loading…
Reference in New Issue
Block a user