Skip to content

Commit

Permalink
Make formatter<T> override ostream<< for templates (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jan 21, 2019
1 parent 1b11b00 commit 4e52abb
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 29 deletions.
52 changes: 38 additions & 14 deletions include/fmt/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,25 +498,32 @@ template <typename Context> class basic_format_args;
// A formatter for objects of type T.
template <typename T, typename Char = char, typename Enable = void>
struct formatter {
formatter() = delete;
};

template <typename T, typename Char, typename Enable = void>
struct convert_to_int
: std::integral_constant<bool, !std::is_arithmetic<T>::value &&
std::is_convertible<T, int>::value> {};

namespace internal {

template <typename T, typename Char = char, typename Enable = void>
struct fallback_formatter {
static_assert(
internal::no_formatter_error<T>::value,
"don't know how to format the type, include fmt/ostream.h if it provides "
"an operator<< that should be used");

fallback_formatter() = delete;

// The following functions are not defined intentionally.
template <typename ParseContext>
typename ParseContext::iterator parse(ParseContext&);
template <typename FormatContext>
auto format(const T& val, FormatContext& ctx) -> decltype(ctx.out());
};

template <typename T, typename Char, typename Enable = void>
struct convert_to_int
: std::integral_constant<bool, !std::is_arithmetic<T>::value &&
std::is_convertible<T, int>::value> {};

namespace internal {

struct dummy_string_view {
typedef void char_type;
};
Expand Down Expand Up @@ -623,9 +630,29 @@ template <typename Context> class value {
}
value(const void* val) { pointer = val; }

template <typename T> explicit value(const T& val) {
template <typename T,
typename std::enable_if<
std::is_default_constructible<
typename Context::template formatter_type<T>::type>{},
int>::type = 0>
explicit value(const T& val) {
custom.value = &val;
custom.format = &format_custom_arg<T>;
// Get the formatter type through the context to allow different contexts
// have different extension points, e.g. `formatter<T>` for `format` and
// `printf_formatter<T>` for `printf`.
typedef typename Context::template formatter_type<T>::type formatter;
custom.format = &format_custom_arg<T, formatter>;
}

template <typename T,
typename std::enable_if<
!std::is_default_constructible<
typename Context::template formatter_type<T>::type>{},
int>::type = 0>
explicit value(const T& val) {
custom.value = &val;
custom.format =
&format_custom_arg<T, internal::fallback_formatter<T, char_type>>;
}

const named_arg_base<char_type>& as_named_arg() {
Expand All @@ -634,12 +661,9 @@ template <typename Context> class value {

private:
// Formats an argument of a custom type, such as a user-defined class.
template <typename T>
template <typename T, typename Formatter>
static void format_custom_arg(const void* arg, Context& ctx) {
// Get the formatter type through the context to allow different contexts
// have different extension points, e.g. `formatter<T>` for `format` and
// `printf_formatter<T>` for `printf`.
typename Context::template formatter_type<T>::type f;
Formatter f;
auto&& parse_ctx = ctx.parse_context();
parse_ctx.advance_to(f.parse(parse_ctx));
ctx.advance_to(f.format(*static_cast<const T*>(arg), ctx));
Expand Down
3 changes: 1 addition & 2 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -1702,8 +1702,7 @@ class specs_handler : public specs_setter<typename Context::char_type> {

FMT_CONSTEXPR format_arg get_arg(auto_id) { return context_.next_arg(); }

template <typename Id>
FMT_CONSTEXPR format_arg get_arg(Id arg_id) {
template <typename Id> FMT_CONSTEXPR format_arg get_arg(Id arg_id) {
context_.parse_context().check_arg_id(arg_id);
return context_.arg(arg_id);
}
Expand Down
24 changes: 11 additions & 13 deletions include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,22 +93,12 @@ void format_value(basic_buffer<Char>& buffer, const T& value) {
output << value;
buffer.resize(buffer.size());
}
} // namespace internal

// Disable conversion to int if T has an overloaded operator<< which is a free
// function (not a member of std::ostream).
template <typename T, typename Char> struct convert_to_int<T, Char, void> {
static const bool value = convert_to_int<T, Char, int>::value &&
!internal::is_streamable<T, Char>::value;
};

// Formats an object of type T that has an overloaded ostream operator<<.
template <typename T, typename Char>
struct formatter<T, Char,
typename std::enable_if<
internal::is_streamable<T, Char>::value &&
!internal::format_type<typename buffer_context<Char>::type,
T>::value>::type>
struct fallback_formatter<
T, Char,
typename std::enable_if<internal::is_streamable<T, Char>::value>::type>
: formatter<basic_string_view<Char>, Char> {
template <typename Context>
auto format(const T& value, Context& ctx) -> decltype(ctx.out()) {
Expand All @@ -118,6 +108,14 @@ struct formatter<T, Char,
return formatter<basic_string_view<Char>, Char>::format(str, ctx);
}
};
} // namespace internal

// Disable conversion to int if T has an overloaded operator<< which is a free
// function (not a member of std::ostream).
template <typename T, typename Char> struct convert_to_int<T, Char, void> {
static const bool value = convert_to_int<T, Char, int>::value &&
!internal::is_streamable<T, Char>::value;
};

template <typename Char>
inline void vprint(
Expand Down
4 changes: 4 additions & 0 deletions test/format-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2144,6 +2144,10 @@ struct test_context {
typedef char char_type;
typedef fmt::basic_format_arg<test_context> format_arg;

template <typename T> struct formatter_type {
typedef fmt::formatter<T, char_type> type;
};

FMT_CONSTEXPR fmt::basic_format_arg<test_context> next_arg() {
return fmt::internal::make_arg<test_context>(11);
}
Expand Down
20 changes: 20 additions & 0 deletions test/ostream-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ TEST(OStreamTest, Enum) {
EXPECT_EQ(L"0", fmt::format(L"{}", A));
}

template <typename T>
struct TestTemplate {};

template <typename T>
std::ostream& operator<<(std::ostream& os, TestTemplate<T>) {
return os << 1;
}

template <typename T>
struct fmt::formatter<TestTemplate<T>> : fmt::formatter<int> {
template <typename FormatContext>
typename FormatContext::iterator format(TestTemplate<T>, FormatContext& ctx) {
return formatter<int>::format(2, ctx);
}
};

TEST(OStreamTest, Template) {
EXPECT_EQ("2", fmt::format("{}", TestTemplate<int>()));
}

typedef fmt::back_insert_range<fmt::internal::buffer> range;

struct test_arg_formatter : fmt::arg_formatter<range> {
Expand Down

0 comments on commit 4e52abb

Please sign in to comment.