]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
include/types_fmt: use fmt::join() to format containers 47255/head
authorKefu Chai <tchaikov@gmail.com>
Sun, 24 Jul 2022 16:16:33 +0000 (00:16 +0800)
committerKefu Chai <tchaikov@gmail.com>
Mon, 25 Jul 2022 13:59:07 +0000 (21:59 +0800)
* add formatter for classes with stream insertion operator.
  as in fmtlib v6.1.2, which is the one shipped with ubuntu focal,
  fmt::join() requires the printed element to have formatter.
  see https://github.com/fmtlib/fmt/issues/2040 and
  https://github.com/fmtlib/fmt/issues/1462
* use template parameter pack to represent container template argument,
  simpler this way. also, this enables us to print specialized
  classes which uses non-default template parameters.
* use fmt::join() to print container elements. see also
  https://fmt.dev/latest/api.html#_CPPv4I0EN3fmt4joinE9join_viewIN6detail10iterator_tI5RangeEEN6detail10sentinel_tI5RangeEEERR5Range11string_view

Signed-off-by: Kefu Chai <tchaikov@gmail.com>
src/include/types_fmt.h

index 5f33248a9839bdf812d6069f64d8615a6b410261..a3ea24d99e703079e8fdd99ee20220af4dc2db41 100644 (file)
 
 #include "include/types.h"
 
+template <class Key, class T>
+struct fmt::formatter<std::pair<const Key, T>> {
+  constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
+
+  template <typename FormatContext>
+  auto format(const std::pair<const Key, T>& p, FormatContext& ctx)
+  {
+    return fmt::format_to(ctx.out(), "{}={}", p.first, p.second);
+  }
+};
+
 template <class A, class B, class Comp, class Alloc>
 struct fmt::formatter<std::map<A, B, Comp, Alloc>> {
   constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
@@ -18,59 +29,39 @@ struct fmt::formatter<std::map<A, B, Comp, Alloc>> {
   template <typename FormatContext>
   auto format(const std::map<A, B, Comp, Alloc>& m, FormatContext& ctx)
   {
-    std::string_view sep = "{";
-    for (const auto& [k, v] : m) {
-      fmt::format_to(ctx.out(), "{}{}={}", sep, k, v);
-      sep = ",";
-    }
-    return fmt::format_to(ctx.out(), "}}");
+    return fmt::format_to(ctx.out(), "{{{}}}", fmt::join(m, ","));
   }
 };
 
-template <class A>
-struct fmt::formatter<std::list<A>> {
+template <class... Args>
+struct fmt::formatter<std::list<Args...>> {
   constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
 
   template <typename FormatContext>
-  auto format(const std::list<A>& l, FormatContext& ctx)
+  auto format(const std::list<Args...>& l, FormatContext& ctx)
   {
-    std::string_view sep = "";
-    for (const auto& e : l) {
-      fmt::format_to(ctx.out(), "{}{}", sep, e);
-      sep = ",";
-    }
-    return ctx.out();
+    return fmt::format_to(ctx.out(), "{}", fmt::join(l, ","));
   }
 };
 
-template <class A>
-struct fmt::formatter<std::vector<A>> {
+template <class... Args>
+struct fmt::formatter<std::vector<Args...>> {
   constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
 
   template <typename FormatContext>
-  auto format(const std::vector<A>& l, FormatContext& ctx)
+  auto format(const std::vector<Args...>& v, FormatContext& ctx)
   {
-    std::string_view sep = "[";
-    for (const auto& e : l) {
-      fmt::format_to(ctx.out(), "{}{}", sep, e);
-      sep = ",";
-    }
-    return fmt::format_to(ctx.out(), "]");
+    return fmt::format_to(ctx.out(), "[{}]", fmt::join(v, ","));
   }
 };
 
-template <class A>
-struct fmt::formatter<std::set<A>> {
+template <class... Args>
+struct fmt::formatter<std::set<Args...>> {
   constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
 
   template <typename FormatContext>
-  auto format(const std::set<A>& l, FormatContext& ctx)
+  auto format(const std::set<Args...>& s, FormatContext& ctx)
   {
-    std::string_view sep = "";
-    for (const auto& e : l) {
-      fmt::format_to(ctx.out(), "{}{}", sep, e);
-      sep = ",";
-    }
-    return ctx.out();
+    return fmt::format_to(ctx.out(), "{}", fmt::join(s, ","));
   }
 };