From 15eac8c4cd258c8356e22640d5e23116ea8bbb1d Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 29 Mar 2023 10:55:38 +0800 Subject: [PATCH 1/6] compound_compat.hh: specialize fmt::formatter this is a part of a series to migrating from `operator<<(ostream&, ..)` based formatting to fmtlib based formatting. the goal here is to enable fmtlib to print `composite::component_view` with the help of fmt::ostream. in this change, '#' is used to add 0x prefix. as fmtlib allows us to add '0x' prefix using '#' format specifier when printing numbers using 'x' as its type specifier. see https://fmt.dev/latest/syntax.html Refs #13245 Signed-off-by: Kefu Chai --- compound_compat.hh | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/compound_compat.hh b/compound_compat.hh index cbe97575c9..eab5168bfb 100644 --- a/compound_compat.hh +++ b/compound_compat.hh @@ -516,7 +516,8 @@ public: template friend inline std::ostream& operator<<(std::ostream& os, const std::pair& c) { - return os << "{value=" << c.first << "; eoc=" << format("0x{:02x}", eoc_type(c.second) & 0xff) << "}"; + fmt::print(os, "{}", c); + return os; } friend std::ostream& operator<<(std::ostream& os, const composite& v); @@ -529,6 +530,20 @@ public: }; }; +template +struct fmt::formatter> : fmt::formatter { + template + auto format(const std::pair& c, FormatContext& ctx) const { + if constexpr (std::same_as) { + return fmt::format_to(ctx.out(), "{{value={}; eoc={:#02x}}}", + fmt_hex(c.first), composite::eoc_type(c.second) & 0xff); + } else { + return fmt::format_to(ctx.out(), "{{value={}; eoc={:#02x}}}", + c.first, composite::eoc_type(c.second) & 0xff); + } + } +}; + class composite_view final { friend class composite; bytes_view _bytes; From 28cabd0a1fa7beaf49e4842dc5364907fd5e89c6 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 29 Mar 2023 11:06:51 +0800 Subject: [PATCH 2/6] compound_compat.hh: specialize fmt::formatter this is a part of a series to migrating from `operator<<(ostream&, ..)` based formatting to fmtlib based formatting. the goal here is to enable fmtlib to print `composite::composite_view` with the help of fmt::ostream. Refs #13245 Signed-off-by: Kefu Chai --- compound_compat.hh | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/compound_compat.hh b/compound_compat.hh index eab5168bfb..9dba8b4055 100644 --- a/compound_compat.hh +++ b/compound_compat.hh @@ -640,12 +640,23 @@ public: bool operator==(const composite_view& k) const { return k._bytes == _bytes && k._is_compound == _is_compound; } bool operator!=(const composite_view& k) const { return !(k == *this); } - friend inline std::ostream& operator<<(std::ostream& os, composite_view v) { - fmt::print(os, "{{{}, compound={}, static={}}}", fmt::join(v.components(), ", "), v._is_compound, v.is_static()); - return os; + friend fmt::formatter; +}; + +template <> +struct fmt::formatter : fmt::formatter { + template + auto format(const composite_view& v, FormatContext& ctx) const { + return fmt::format_to(ctx.out(), "{{{}, compound={}, static={}}}", + fmt::join(v.components(), ", "), v._is_compound, v.is_static()); } }; +inline std::ostream& operator<<(std::ostream& os, composite_view v) { + fmt::print(os, "{}", v); + return os; +} + inline composite::composite(const composite_view& v) : composite(bytes(v._bytes), v._is_compound) From 1ef8f63b4ef55907b49eaf3a63228cddbbb3b0ed Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 29 Mar 2023 11:11:45 +0800 Subject: [PATCH 3/6] compound_compat.hh: specialize fmt::formatter this is a part of a series to migrating from `operator<<(ostream&, ..)` based formatting to fmtlib based formatting. the goal here is to enable fmtlib to print `composite` with the help of fmt::ostream. Refs #13245 Signed-off-by: Kefu Chai --- compound_compat.hh | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/compound_compat.hh b/compound_compat.hh index 9dba8b4055..c2c4d501e5 100644 --- a/compound_compat.hh +++ b/compound_compat.hh @@ -662,9 +662,18 @@ composite::composite(const composite_view& v) : composite(bytes(v._bytes), v._is_compound) { } +template <> +struct fmt::formatter : fmt::formatter { + template + auto format(const composite& v, FormatContext& ctx) const { + return fmt::format_to(ctx.out(), "{}", composite_view(v)); + } +}; + inline std::ostream& operator<<(std::ostream& os, const composite& v) { - return os << composite_view(v); + fmt::print(os, "{}", v); + return os; } inline From cdb972222e9533b52ed432c4c55a1c110fdf5528 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 29 Mar 2023 15:42:55 +0800 Subject: [PATCH 4/6] sstables: do not use operator<< to print composite_view this change removes the last two callers of `operator<<(ostream&, const composite_view&)`, it paves the road to remove this operator. Refs #13245 Signed-off-by: Kefu Chai --- sstables/index_entry.hh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sstables/index_entry.hh b/sstables/index_entry.hh index e07699c736..75bd8e2d74 100644 --- a/sstables/index_entry.hh +++ b/sstables/index_entry.hh @@ -310,11 +310,11 @@ using index_list = partition_index_page; } inline std::ostream& operator<<(std::ostream& out, const sstables::promoted_index_block_position_view& pos) { - std::visit([&out] (const auto& pos) mutable { out << pos; }, pos); + std::visit([&out] (const auto& pos) mutable { fmt::print(out, "{}", pos); }, pos); return out; } inline std::ostream& operator<<(std::ostream& out, const sstables::promoted_index_block_position& pos) { - std::visit([&out] (const auto& pos) mutable { out << pos; }, pos); + std::visit([&out] (const auto& pos) mutable { fmt::print(out, "{}", pos); }, pos); return out; } From 212641abda6fa384959740e0ba488aab8ce6eb30 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 29 Mar 2023 15:43:44 +0800 Subject: [PATCH 5/6] compound_compat: remove operator<<(ostream, composite_view) since we don't have any callers of this operator, let's drop it. Signed-off-by: Kefu Chai --- compound_compat.hh | 5 ----- 1 file changed, 5 deletions(-) diff --git a/compound_compat.hh b/compound_compat.hh index c2c4d501e5..b40b428ff3 100644 --- a/compound_compat.hh +++ b/compound_compat.hh @@ -652,11 +652,6 @@ struct fmt::formatter : fmt::formatter { } }; -inline std::ostream& operator<<(std::ostream& os, composite_view v) { - fmt::print(os, "{}", v); - return os; -} - inline composite::composite(const composite_view& v) : composite(bytes(v._bytes), v._is_compound) From 57f51603dc2b642673d04b690e5cc3734da624f4 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Wed, 29 Mar 2023 15:42:04 +0800 Subject: [PATCH 6/6] compound_compat: remove operator<<(ostream, composite) since we don't have any callers of this operator, let's drop it. Signed-off-by: Kefu Chai --- compound_compat.hh | 8 -------- 1 file changed, 8 deletions(-) diff --git a/compound_compat.hh b/compound_compat.hh index b40b428ff3..10666c446a 100644 --- a/compound_compat.hh +++ b/compound_compat.hh @@ -520,8 +520,6 @@ public: return os; } - friend std::ostream& operator<<(std::ostream& os, const composite& v); - struct tri_compare { const std::vector& _types; tri_compare(const std::vector& types) : _types(types) {} @@ -665,12 +663,6 @@ struct fmt::formatter : fmt::formatter { } }; -inline -std::ostream& operator<<(std::ostream& os, const composite& v) { - fmt::print(os, "{}", v); - return os; -} - inline std::strong_ordering composite::tri_compare::operator()(const composite& v1, const composite& v2) const { return (*this)(composite_view(v1), composite_view(v2));