Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add formatters for container adapters #3279

Merged
merged 6 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions include/fmt/ranges.h
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,43 @@ struct formatter<tuple_join_view<Char, T...>, Char> {
}
};

namespace detail {
// Check if T has an interface like container adapter (e.g. std::stack,
// std::queue, std::priority_queue).
template <typename T> class is_container_adaptor_like {
template <typename U> static auto check(U* p) -> typename U::container_type;
template <typename> static void check(...);

public:
static constexpr const bool value =
!std::is_void<decltype(check<T>(nullptr))>::value;
};
} // namespace detail

template <typename T, typename Char>
struct formatter<T, Char,
enable_if_t<detail::is_container_adaptor_like<T>::value>> {
private:
struct formatter<typename T::container_type, Char> container_formatter;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct not needed here and I suggest using inheritance instead of composition to avoid redefining parse.

Also please apply clang-format, indent is a bit off.


public:
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
return container_formatter.parse(ctx);
}

template <typename FormatContext>
auto format(T& t, FormatContext& ctx) const ->
typename FormatContext::iterator {
struct getter : T {
static auto get(T& t) -> typename T::container_type& {
return t.*(&getter::c); // Access c through the derived class.
}
};
return container_formatter.format(getter::get(t), ctx);
}
};

FMT_MODULE_EXPORT_BEGIN

/**
Expand Down
62 changes: 62 additions & 0 deletions test/ranges-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <map>
#include <string>
#include <vector>
#include <stack>
#include <queue>

#include "gtest/gtest.h"

Expand Down Expand Up @@ -406,3 +408,63 @@ TEST(ranges_test, range_of_range_of_mixed_const) {
TEST(ranges_test, vector_char) {
EXPECT_EQ(fmt::format("{}", std::vector<char>{'a', 'b'}), "['a', 'b']");
}

TEST(ranges_test, container_adaptor) {
{
using fmt::detail::is_container_adaptor_like;
using T = std::nullptr_t;
static_assert(is_container_adaptor_like<std::stack<T>>::value, "");
static_assert(is_container_adaptor_like<std::queue<T>>::value, "");
static_assert(is_container_adaptor_like<std::priority_queue<T>>::value, "");
static_assert(!is_container_adaptor_like<std::vector<T>>::value, "");
}

{
std::stack<int> s;
s.push(1);
s.push(2);
EXPECT_EQ(fmt::format("{}", s), "[1, 2]");
}

{
std::queue<int> q;
q.push(1);
q.push(2);
EXPECT_EQ(fmt::format("{}", q), "[1, 2]");
}

{
std::priority_queue<int> q;
q.push(3);
q.push(1);
q.push(2);
q.push(4);
EXPECT_EQ(fmt::format("{}", q), "[4, 3, 2, 1]");
}

{
std::stack<char, std::string> s;
s.push('a');
s.push('b');
// Note: The output is formatted as a string because the underlying
// container is a string. This behavior is conforming to the standard
// [container.adaptors.format].
Comment on lines +450 to +452
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inconsistent with formatting containers and looks like an oversight. I think we should open an LWG issue.

EXPECT_EQ(fmt::format("{}", s), "ab");
}

{
struct my_container_adaptor {
using value_type = int;
using container_type = std::vector<value_type>;
void push(const value_type& v) { c.push_back(v); }

protected:
container_type c;
};

my_container_adaptor m;
m.push(1);
m.push(2);
EXPECT_EQ(fmt::format("{}", m), "[1, 2]");
}
}